patient_service.go 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. package service
  2. import (
  3. "XT_New/models"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "fmt"
  8. "github.com/jinzhu/gorm"
  9. )
  10. //GetPatientList 返回患者的列表
  11. func GetPatientList(orgID int64, keywords string, page, limit, schedulType, bindingState, lapseto, source, startTime, endTime, contagion, reimbursementWay, isscheduling, isprescription int64, isStartTime, isEndTime bool) (patients []*models.Patients, total int64, err error) {
  12. db := readDb.Table("xt_patients as p").Where("p.status=1")
  13. if orgID > 0 {
  14. db = db.Where("p.user_org_id=?", orgID)
  15. }
  16. if len(keywords) > 0 {
  17. likeKey := "%" + keywords + "%"
  18. db = db.Where("p.name LIKE ? OR p.dialysis_no LIKE ?", likeKey, likeKey)
  19. }
  20. if schedulType > 0 {
  21. db = db.Joins("JOIN xt_schedule as s ON s.patient_id=p.id")
  22. db = db.Where("s.status=1 and s.schedule_type=?", schedulType)
  23. }
  24. if contagion > 0 {
  25. db = db.Joins("JOIN xt_patients_infectious_diseases as xpid ON xpid.patient_id=p.id")
  26. db = db.Where("xpid.disease_id=? and xpid.status=1", contagion)
  27. }
  28. if isscheduling == 1 {
  29. db = db.Where("EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  30. } else if isscheduling == 2 {
  31. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  32. }
  33. if isprescription == 1 {
  34. db = db.Where("EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  35. } else if isprescription == 2 {
  36. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  37. }
  38. if bindingState > 0 {
  39. db = db.Where("p.binding_state=?", bindingState)
  40. }
  41. if lapseto > 0 {
  42. db = db.Where("p.lapseto=?", lapseto)
  43. }
  44. if source > 0 {
  45. db = db.Where("p.source=?", source)
  46. }
  47. if reimbursementWay > 0 {
  48. db = db.Where("p.reimbursement_way_id = ?", reimbursementWay)
  49. }
  50. if isStartTime {
  51. db = db.Where("p.created_time>=?", startTime)
  52. }
  53. if isEndTime {
  54. db = db.Where("p.created_time<=?", endTime)
  55. }
  56. offset := (page - 1) * limit
  57. err = db.Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  58. return
  59. }
  60. //GetAllPatientList 返回全部患者的列表
  61. func GetAllPatientList(orgID int64) (patients []*models.Patients, total int64, err error) {
  62. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1", orgID)
  63. err = db.Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").Count(&total).Find(&patients).Error
  64. return
  65. }
  66. func GetPatientCount(orgID int64) (total int64) {
  67. readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID).Count(&total)
  68. return
  69. }
  70. func GetLapsetoPatientCount(orgID int64, lapseto int64) (total int64) {
  71. readDb.Model(&models.Patients{}).Where("user_org_id=? and lapseto=? and status=1", orgID, lapseto).Count(&total)
  72. return
  73. }
  74. func ChechLastDialysisNo(orgID int64) (dialysisNo int64) {
  75. var patient models.Patients
  76. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("dialysis_no desc").First(&patient).Error
  77. if err != nil {
  78. return
  79. }
  80. if patient.ID == 0 {
  81. return
  82. }
  83. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  84. return
  85. }
  86. func FindPatientByDialysisNo(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  87. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=?", orgID, dialysisNo).First(&patient).Error
  88. return
  89. }
  90. func FindPatientByIdCardNo(orgID int64, idCardNo string) (patient models.Patients, err error) {
  91. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  92. return
  93. }
  94. func FindPatientByMobile(orgID int64, mobile string) (patient models.Patients, err error) {
  95. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  96. return
  97. }
  98. func FindPatientById(orgID int64, id int64) (patient models.Patients, err error) {
  99. err = readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  100. return
  101. }
  102. func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  103. err = readDb.Model(&models.Patients{}).Preload("Contagions", "status = 1").Preload("Diseases", "status = 1").Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  104. return
  105. }
  106. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  107. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  108. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  109. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  110. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  111. }).Where("user_org_id=? and dialysis_no = ? and status=1", orgID, no).First(&patient).Error
  112. return
  113. }
  114. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  115. user, _ := GetSgjUserByMobild(patient.Phone)
  116. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  117. utx := writeDb.Begin()
  118. btx := writeUserDb.Begin()
  119. if user.ID == 0 {
  120. user.Mobile = patient.Phone
  121. user.Avatar = patient.Avatar
  122. user.AvatarThumb = patient.Avatar
  123. user.Birthday = patient.Birthday
  124. user.Username = patient.Name
  125. user.Gender = patient.Gender
  126. user.Sources = 11
  127. user.Introduce = patient.Remark
  128. user.Status = 1
  129. user.UpdatedTime = patient.UpdatedTime
  130. user.CreatedTime = patient.CreatedTime
  131. err = btx.Create(&user).Error
  132. if err != nil {
  133. utx.Rollback()
  134. btx.Rollback()
  135. return
  136. }
  137. }
  138. patient.UserId = user.ID
  139. if customer == nil {
  140. err = btx.Create(&models.SgjCustomer{
  141. UserOrgId: patient.UserOrgId,
  142. UserId: user.ID,
  143. Mobile: patient.Phone,
  144. Name: patient.Name,
  145. Gender: patient.Gender,
  146. Birthday: patient.Birthday,
  147. Sources: 11,
  148. Status: 1,
  149. CreatedTime: patient.CreatedTime,
  150. UpdatedTime: patient.UpdatedTime,
  151. Avatar: patient.Avatar,
  152. Remark: patient.Remark,
  153. }).Error
  154. if err != nil {
  155. utx.Rollback()
  156. btx.Rollback()
  157. return
  158. }
  159. }
  160. err = utx.Create(patient).Error
  161. if err != nil {
  162. utx.Rollback()
  163. btx.Rollback()
  164. return
  165. }
  166. var lapseto models.PatientLapseto
  167. lapseto.PatientId = patient.ID
  168. lapseto.LapsetoType = patient.Lapseto
  169. lapseto.CreatedTime = patient.CreatedTime
  170. lapseto.UpdatedTime = patient.CreatedTime
  171. lapseto.Status = 1
  172. lapseto.LapsetoTime = patient.CreatedTime
  173. err = utx.Create(&lapseto).Error
  174. if err != nil {
  175. utx.Rollback()
  176. btx.Rollback()
  177. return
  178. }
  179. if len(contagions) > 0 {
  180. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  181. insertParams := make([]string, 0)
  182. insertData := make([]interface{}, 0)
  183. for _, contagion := range contagions {
  184. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  185. insertData = append(insertData, patient.ID)
  186. insertData = append(insertData, contagion)
  187. insertData = append(insertData, 1)
  188. insertData = append(insertData, patient.CreatedTime)
  189. insertData = append(insertData, patient.UpdatedTime)
  190. }
  191. thisSQL += strings.Join(insertParams, ", ")
  192. err = utx.Exec(thisSQL, insertData...).Error
  193. if err != nil {
  194. utx.Rollback()
  195. btx.Rollback()
  196. return
  197. }
  198. }
  199. if len(diseases) > 0 {
  200. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  201. insertParams := make([]string, 0)
  202. insertData := make([]interface{}, 0)
  203. for _, disease := range diseases {
  204. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  205. insertData = append(insertData, patient.ID)
  206. insertData = append(insertData, disease)
  207. insertData = append(insertData, 1)
  208. insertData = append(insertData, patient.CreatedTime)
  209. insertData = append(insertData, patient.UpdatedTime)
  210. }
  211. thisSQL += strings.Join(insertParams, ", ")
  212. err = utx.Exec(thisSQL, insertData...).Error
  213. if err != nil {
  214. utx.Rollback()
  215. btx.Rollback()
  216. return
  217. }
  218. }
  219. utx.Commit()
  220. btx.Commit()
  221. return
  222. }
  223. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  224. utx := writeDb.Begin()
  225. err = utx.Model(&models.Patients{}).Where("id=?", patient.ID).Update(map[string]interface{}{"Lapseto": patient.Lapseto}).Error
  226. if err != nil {
  227. utx.Rollback()
  228. return
  229. }
  230. err = utx.Create(lapseto).Error
  231. if err != nil {
  232. utx.Rollback()
  233. return
  234. }
  235. // 删除排班和排班模板信息
  236. if lapseto.LapsetoType == 2 {
  237. now := time.Now()
  238. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  239. "status": 0,
  240. "updated_time": now.Unix(),
  241. }).Error
  242. if deleteScheduleErr != nil {
  243. utx.Rollback()
  244. err = deleteScheduleErr
  245. return
  246. }
  247. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  248. "status": 0,
  249. "mtime": now.Unix(),
  250. }).Error
  251. if deleteSchTempItemErr != nil {
  252. utx.Rollback()
  253. err = deleteSchTempItemErr
  254. return
  255. }
  256. }
  257. utx.Commit()
  258. return
  259. }
  260. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  261. // if len(contagions) > 0 || len(diseases) > 0 {
  262. utx := writeDb.Begin()
  263. err = utx.Save(patient).Error
  264. if err != nil {
  265. utx.Rollback()
  266. return
  267. }
  268. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  269. fmt.Println("错误是什么。。。。。。。。。。。。。。。。。。。。。。。。。。。", err)
  270. if err != nil {
  271. utx.Rollback()
  272. return
  273. }
  274. if len(contagions) > 0 {
  275. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  276. insertParams := make([]string, 0)
  277. insertData := make([]interface{}, 0)
  278. for _, contagion := range contagions {
  279. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  280. insertData = append(insertData, patient.ID)
  281. insertData = append(insertData, contagion)
  282. insertData = append(insertData, 1)
  283. insertData = append(insertData, patient.CreatedTime)
  284. insertData = append(insertData, patient.UpdatedTime)
  285. }
  286. thisSQL += strings.Join(insertParams, ", ")
  287. err = utx.Exec(thisSQL, insertData...).Error
  288. if err != nil {
  289. utx.Rollback()
  290. return
  291. }
  292. }
  293. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  294. if err != nil {
  295. utx.Rollback()
  296. return
  297. }
  298. if len(diseases) > 0 {
  299. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  300. insertParams := make([]string, 0)
  301. insertData := make([]interface{}, 0)
  302. for _, disease := range diseases {
  303. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  304. insertData = append(insertData, patient.ID)
  305. insertData = append(insertData, disease)
  306. insertData = append(insertData, 1)
  307. insertData = append(insertData, patient.CreatedTime)
  308. insertData = append(insertData, patient.UpdatedTime)
  309. }
  310. thisSQL += strings.Join(insertParams, ", ")
  311. err = utx.Exec(thisSQL, insertData...).Error
  312. if err != nil {
  313. utx.Rollback()
  314. return
  315. }
  316. }
  317. utx.Commit()
  318. // } else {
  319. // err = writeDb.Save(patient).Error
  320. // }
  321. return
  322. }
  323. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  324. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? AND inspect_date = ? ", id, org_id, project_id, date).Last(&inspection).Error
  325. return
  326. }
  327. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  328. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? and inspect_date = ?", patient_id, org_id, project_id, date).Find(&inspection).Error
  329. return
  330. }
  331. func GetPatientDiseases(id int64) []int64 {
  332. var dis []models.ChronicDiseases
  333. ids := make([]int64, 0)
  334. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  335. if err != nil || len(dis) == 0 {
  336. return ids
  337. }
  338. for _, item := range dis {
  339. ids = append(ids, item.DiseaseId)
  340. }
  341. return ids
  342. }
  343. func GetPatientContagions(id int64) []int64 {
  344. var cis []models.InfectiousDiseases
  345. ids := make([]int64, 0)
  346. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  347. if err != nil || len(cis) == 0 {
  348. return ids
  349. }
  350. for _, item := range cis {
  351. ids = append(ids, item.DiseaseId)
  352. }
  353. return ids
  354. }
  355. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  356. err = readDb.Model(&models.DialysisSolution{}).Where("user_org_id=? and patient_id=? and mode_id=? and parent_id=0 and status=1", orgID, patientID, modeId).First(&solution).Error
  357. return
  358. }
  359. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  360. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  361. return
  362. }
  363. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  364. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  365. return
  366. }
  367. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  368. err = writeDb.Create(solution).Error
  369. return
  370. }
  371. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  372. err = writeDb.Save(solution).Error
  373. return
  374. }
  375. //GetPatientDialysisSolutionList 返回患者透析方案的列表
  376. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  377. offset := (page - 1) * limit
  378. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  379. if orgID > 0 {
  380. db = db.Where("ds.user_org_id=?", orgID)
  381. }
  382. if patientID > 0 {
  383. db = db.Where("ds.patient_id=?", patientID)
  384. }
  385. db = db.Count(&total).Offset(offset).Limit(limit)
  386. err = db.Order("id desc").Find(&solutions).Error
  387. if err != nil {
  388. return
  389. }
  390. if len(solutions) > 0 {
  391. nilNameIds := make([]int64, 0)
  392. for _, solution := range solutions {
  393. if len(solution.ModeName) == 0 {
  394. nilNameIds = append(nilNameIds, solution.ModeId)
  395. }
  396. }
  397. if len(nilNameIds) > 0 {
  398. var modes []*models.TreatmentMode
  399. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  400. if err != nil {
  401. return
  402. }
  403. modesMap := make(map[int64]models.TreatmentMode, 0)
  404. for _, mode := range modes {
  405. modesMap[mode.ID] = *mode
  406. }
  407. for index, solution := range solutions {
  408. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  409. solutions[index].ModeName = modesMap[solution.ModeId].Name
  410. }
  411. }
  412. }
  413. }
  414. return
  415. }
  416. //GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  417. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  418. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  419. if orgID > 0 {
  420. db = db.Where("dwa.user_org_id=?", orgID)
  421. }
  422. if patientID > 0 {
  423. db = db.Where("dwa.patient_id=?", patientID)
  424. }
  425. db = db.Select("dwa.id, dwa.user_org_id, dwa.patient_id, dwa.weight, dwa.adjusted_value, dwa.doctor, dwa.registrars_id, dwa.remark, dwa.status, dwa.created_time, dwa.updated_time").Count(&total).Order("id desc")
  426. if page > 0 && limit > 0 {
  427. offset := (page - 1) * limit
  428. db = db.Offset(offset).Limit(limit)
  429. }
  430. err = db.Find(&weights).Error
  431. return
  432. }
  433. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  434. err = writeDb.Create(m).Error
  435. return
  436. }
  437. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  438. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  439. return
  440. }
  441. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  442. return writeDb.Create(m).Error
  443. }
  444. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  445. var advice models.DoctorAdvice
  446. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  447. if err != nil {
  448. fmt.Println(err)
  449. group = 0
  450. }
  451. group = advice.GroupNo
  452. return
  453. }
  454. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  455. if group == 0 {
  456. group = GetMaxAdviceGroupID(orgId) + 1
  457. }
  458. tx := writeDb.Begin()
  459. defer func() {
  460. if r := recover(); r != nil {
  461. tx.Rollback()
  462. }
  463. }()
  464. for _, advice := range advices {
  465. advice.GroupNo = group
  466. if err = tx.Create(advice).Error; err != nil {
  467. tx.Rollback()
  468. return
  469. }
  470. }
  471. tx.Commit()
  472. return
  473. }
  474. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  475. if groupNo <= 0 {
  476. group := GetMaxAdviceGroupID(orgId)
  477. groupNo = group + 1
  478. }
  479. tx := writeDb.Begin()
  480. defer func() {
  481. if r := recover(); r != nil {
  482. tx.Rollback()
  483. }
  484. }()
  485. for _, advice := range advices {
  486. advice.GroupNo = groupNo
  487. if err = tx.Create(advice).Error; err != nil {
  488. tx.Rollback()
  489. return
  490. }
  491. list = append(list, advice)
  492. if len(advice.Children) > 0 {
  493. for _, child := range advice.Children {
  494. child.GroupNo = groupNo
  495. child.ParentId = advice.ID
  496. fmt.Println(child)
  497. if err = tx.Create(&child).Error; err != nil {
  498. tx.Rollback()
  499. return
  500. }
  501. list = append(list, child)
  502. }
  503. }
  504. }
  505. tx.Commit()
  506. return
  507. }
  508. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  509. return writeDb.Save(m).Error
  510. }
  511. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  512. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? and groupno=?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime}).Error
  513. if err != nil {
  514. return
  515. }
  516. return
  517. }
  518. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  519. ut := writeDb.Begin()
  520. err = ut.Save(m).Error
  521. if err != nil {
  522. ut.Rollback()
  523. return
  524. }
  525. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime, "Modifier": m.Modifier}).Error
  526. if err != nil {
  527. ut.Rollback()
  528. return
  529. }
  530. ut.Commit()
  531. return
  532. }
  533. func DeleteSolution(m *models.DialysisSolution) (err error) {
  534. if m.ParentId > 0 {
  535. return writeDb.Save(m).Error
  536. } else {
  537. ut := writeDb.Begin()
  538. err = ut.Save(m).Error
  539. if err != nil {
  540. ut.Rollback()
  541. return
  542. }
  543. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  544. if err != nil {
  545. ut.Rollback()
  546. return
  547. }
  548. ut.Commit()
  549. }
  550. return
  551. }
  552. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  553. if m.ParentId > 0 {
  554. return writeDb.Save(m).Error
  555. } else {
  556. ut := writeDb.Begin()
  557. err = ut.Save(m).Error
  558. if err != nil {
  559. ut.Rollback()
  560. return
  561. }
  562. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "Modifier": m.Modifier}).Error
  563. if err != nil {
  564. ut.Rollback()
  565. return
  566. }
  567. ut.Commit()
  568. }
  569. return
  570. }
  571. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  572. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? and groupno=?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": time.Now().Unix(), "Status": 0, "Modifier": admin_user_id}).Error
  573. if err != nil {
  574. return
  575. }
  576. return
  577. }
  578. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  579. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  580. return
  581. }
  582. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  583. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  584. return
  585. }
  586. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  587. //db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  588. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  589. table := UserReadDB().Table("sgj_user_admin_role as r")
  590. fmt.Print("table", table)
  591. if orgID > 0 {
  592. db = db.Where("x.user_org_id=?", orgID)
  593. }
  594. if patientID > 0 {
  595. db = db.Where("x.patient_id = ?", patientID)
  596. }
  597. if advice_type > 0 {
  598. db = db.Where("x.advice_type = ?", advice_type)
  599. } else if advice_type == 0 {
  600. db = db.Where("x.advice_type in (?)", []int{1, 3})
  601. }
  602. if stop == 1 {
  603. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  604. } else if stop == 2 {
  605. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  606. }
  607. if start != 0 {
  608. db = db.Where("x.start_time>=?", start)
  609. }
  610. if end != 0 {
  611. db = db.Where("start_time<=?", end)
  612. }
  613. if len(keywords) > 0 {
  614. likeKey := "%" + keywords + "%"
  615. db = db.Where("x.advice_name LIKE ?", likeKey)
  616. }
  617. err = db.Group("x.id").Count(&total).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  618. fmt.Print("err", err)
  619. return
  620. }
  621. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  622. offset := (page - 1) * limit
  623. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  624. if orgID > 0 {
  625. db = db.Where("user_org_id=?", orgID)
  626. }
  627. if patientID > 0 {
  628. db = db.Where("patient_id = ?", patientID)
  629. }
  630. if advice_type > 0 {
  631. db = db.Where("advice_type = ?", advice_type)
  632. }
  633. if stop == 1 {
  634. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  635. } else if stop == 2 {
  636. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  637. }
  638. if start != 0 {
  639. db = db.Where("start_time>=?", start)
  640. }
  641. if end != 0 {
  642. db = db.Where("start_time<=?", end)
  643. }
  644. if len(keywords) > 0 {
  645. likeKey := "%" + keywords + "%"
  646. db = db.Where("advice_name LIKE ?", likeKey)
  647. }
  648. err = db.Count(&total).Select("id,user_org_id,patient_id,advice_type,advice_date,start_time,advice_name,advice_desc,reminder_date,single_dose,single_dose_unit,prescribing_number,prescribing_number_unit,delivery_way,execution_frequency,advice_doctor,status,created_time,updated_time,advice_affirm,remark,stop_time,stop_reason,stop_doctor,stop_state,parent_id,execution_time,execution_staff,execution_state,checker IF(parent_id>0, parent_id, id) as advice_order").Order("advice_order desc, id").Offset(offset).Limit(limit).Scan(&advices).Error
  649. return
  650. }
  651. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  652. if len(advices) > 0 {
  653. utx := writeDb.Begin()
  654. if len(advices) > 0 {
  655. thisSQL := "INSERT INTO xt_doctor_advice (single_dose_unit, prescribing_number, prescribing_number_unit, advice_name, advice_desc,single_dose,created_time,updated_time,patient_id,parent_id,user_org_id,record_date,advice_type,drug_spec_unit) VALUES "
  656. insertParams := make([]string, 0)
  657. insertData := make([]interface{}, 0)
  658. for _, advice := range advices {
  659. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  660. insertData = append(insertData, advice.SingleDoseUnit)
  661. insertData = append(insertData, advice.PrescribingNumber)
  662. insertData = append(insertData, advice.PrescribingNumberUnit)
  663. insertData = append(insertData, advice.AdviceName)
  664. insertData = append(insertData, advice.AdviceDesc)
  665. insertData = append(insertData, advice.SingleDose)
  666. insertData = append(insertData, advice.CreatedTime)
  667. insertData = append(insertData, advice.UpdatedTime)
  668. insertData = append(insertData, advice.PatientId)
  669. insertData = append(insertData, advice.ParentId)
  670. insertData = append(insertData, advice.UserOrgId)
  671. insertData = append(insertData, advice.RecordDate)
  672. insertData = append(insertData, 2)
  673. insertData = append(insertData, advice.DrugSpecUnit)
  674. }
  675. thisSQL += strings.Join(insertParams, ", ")
  676. err = utx.Exec(thisSQL, insertData...).Error
  677. if err != nil {
  678. utx.Rollback()
  679. return
  680. }
  681. }
  682. utx.Commit()
  683. }
  684. return
  685. }
  686. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  687. offset := (page - 1) * limit
  688. var total int64
  689. var err error
  690. var orders []*models.PatientDialysisRecord
  691. // err = readDb.Table("xt_dialysis_order as do").
  692. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  693. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  694. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  695. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  696. // Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  697. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  698. // Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select(" do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time, s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  699. db := readDb.Table("xt_dialysis_order as do").
  700. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  701. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  702. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  703. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  704. return readUserDb.Where("status = 1")
  705. })
  706. }).
  707. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  708. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  709. Preload("Device", "org_id=? and status=1", orgID).
  710. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  711. return readUserDb.Where("org_id=? and status = 1", orgID)
  712. }).
  713. Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  714. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  715. Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Group("s.schedule_date")
  716. if start != 0 {
  717. db = db.Where("do.dialysis_date>=?", start)
  718. }
  719. if end != 0 {
  720. db = db.Where("do.dialysis_date<=?", end)
  721. }
  722. if mode_id > 0 {
  723. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.record_id=do.id")
  724. db = db.Where("dp.mode_id=?", mode_id)
  725. }
  726. err = db.Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select("do.bed_id, do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time,do.start_nurse,do.finish_nurse ,s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  727. if len(orders) > 0 {
  728. ids := make([]int64, 0)
  729. for _, order := range orders {
  730. dialyzer := order.DialysisPrescription.Dialyzer
  731. ids = append(ids, dialyzer)
  732. }
  733. if len(ids) > 0 {
  734. var dialyzers []*models.DeviceNumber
  735. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  736. if err != nil {
  737. return nil, 0, err
  738. }
  739. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  740. for _, item := range dialyzers {
  741. dialyzerMap[item.ID] = *item
  742. }
  743. for orderIndex, order := range orders {
  744. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  745. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  746. }
  747. }
  748. }
  749. }
  750. return orders, total, err
  751. }
  752. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  753. offset := (page - 1) * limit
  754. fmt.Println(offset)
  755. fmt.Println(limit)
  756. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  757. if start != 0 {
  758. db = db.Where("assessment_date >= ?", start)
  759. }
  760. if end != 0 {
  761. db = db.Where("assessment_date <= ?", end)
  762. }
  763. db = db.Where("status=1")
  764. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  765. return
  766. }
  767. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  768. offset := (page - 1) * limit
  769. err = readDb.Table("xt_schedule as s").
  770. Preload("DeviceZone", "org_id=? and status=1", orgID).
  771. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  772. Preload("TreatmentMode", "status=1").
  773. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  774. Limit(limit).Offset(offset).Find(&schedules).Error
  775. return
  776. }
  777. func GetMonitorRecord(orgID int64, date int64, partition int64) ([]*models.MonitorDialysisSchedule, error) {
  778. var mds []*models.MonitorDialysisSchedule
  779. db := readDb.
  780. Model(&models.MonitorDialysisSchedule{}).
  781. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  782. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  783. Preload("TreatmentMode", "status = 1").
  784. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  785. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  786. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  787. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  788. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  789. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  790. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  791. Where("status = 1 AND user_org_id = ?", orgID)
  792. if date != 0 {
  793. db = db.Where("schedule_date = ? ", date)
  794. }
  795. if partition != 0 {
  796. db = db.Where("partition_id = ? ", partition)
  797. }
  798. err := db.Find(&mds).Error
  799. return mds, err
  800. }
  801. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  802. var patients []*models.Patients
  803. getPatientErr := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND (name like ? OR dialysis_no like ?)", orgID, "%"+keyword+"%", "%"+keyword+"%").Find(&patients).Error
  804. if getPatientErr != nil {
  805. return nil, getPatientErr
  806. }
  807. patientIDs := make([]int64, len(patients))
  808. for index, patient := range patients {
  809. patientIDs[index] = patient.ID
  810. }
  811. db := readDb.
  812. Model(&models.MonitorDialysisSchedule{}).
  813. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  814. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  815. Preload("TreatmentMode", "status = 1").
  816. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  817. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  818. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  819. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  820. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  821. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  822. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  823. var schedules []*models.MonitorDialysisSchedule
  824. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  825. return schedules, err
  826. }
  827. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  828. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID)
  829. if len(keywords) > 0 {
  830. likekey := "%" + keywords + "%"
  831. err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
  832. } else {
  833. err = db.Find(&patient).Error
  834. }
  835. return
  836. }
  837. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  838. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  839. return
  840. }
  841. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  842. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  843. return
  844. }
  845. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  846. now := time.Now().Unix()
  847. err := writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND status = 1 AND execution_state <> 1 AND check_state <> 1 AND groupno = ? AND admin_user_id = ?", orgID, groupNO, admin_user_id).Updates(map[string]interface{}{
  848. "start_time": startTime,
  849. "updated_time": now,
  850. }).Error
  851. return err
  852. }
  853. func QueryPatientById(id int64) *models.Patients {
  854. var pat models.Patients
  855. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  856. if err != nil {
  857. }
  858. return &pat
  859. }
  860. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  861. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  862. return
  863. }
  864. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  865. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  866. return
  867. }
  868. func BatchDeleteDoctorAdvice(ids []string) (err error) {
  869. ut := writeDb.Begin()
  870. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  871. if err != nil {
  872. ut.Rollback()
  873. return
  874. }
  875. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  876. if err != nil {
  877. ut.Rollback()
  878. return
  879. }
  880. ut.Commit()
  881. return err
  882. }
  883. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  884. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  885. return
  886. }
  887. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  888. ut := writeDb.Begin()
  889. err = ut.Save(m).Error
  890. if err != nil {
  891. ut.Rollback()
  892. return
  893. }
  894. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", m.ID).Updates(map[string]interface{}{"start_time": m.StartTime, "groupno": m.GroupNo, "mtime": time.Now().Unix()}).Error
  895. if err != nil {
  896. ut.Rollback()
  897. return
  898. }
  899. ut.Commit()
  900. return err
  901. }
  902. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  903. type Total struct {
  904. Count int64
  905. }
  906. var totals Total
  907. offset := (page - 1) * limit
  908. err = readDb.Raw("select * from xt_patients where user_org_id = ? AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Offset(offset).Limit(limit).Scan(&patient).Error
  909. readDb.Raw("select Count(id) as count from xt_patients where user_org_id = ? AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Scan(&totals)
  910. return totals.Count, patient, err
  911. }
  912. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  913. err = writeDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).Updates(map[string]interface{}{"is_open_remind": remind}).Error
  914. return
  915. }
  916. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  917. err = writeDb.Create(m).Error
  918. return
  919. }
  920. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  921. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  922. return
  923. }
  924. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  925. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  926. if len(keywords) > 0 {
  927. likekey := "%" + keywords + "%"
  928. err = db.Where("(name LIKE ? OR dialysis_no LIKE ?) AND NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`dialysis_date` = ? AND d.`status` = 1 AND d.`patient_id` = xt_patients.id AND d.user_org_id = xt_patients.user_org_id)", likekey, likekey, date).Find(&patient).Error
  929. } else {
  930. err = db.Find(&patient).Error
  931. }
  932. return
  933. }