patient_service.go 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 GetLastPatientData(orgid int64) (models.Patients, error) {
  224. patients := models.Patients{}
  225. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  226. return patients, err
  227. }
  228. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  229. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  230. return err
  231. }
  232. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  233. utx := writeDb.Begin()
  234. err = utx.Model(&models.Patients{}).Where("id=?", patient.ID).Update(map[string]interface{}{"Lapseto": patient.Lapseto}).Error
  235. if err != nil {
  236. utx.Rollback()
  237. return
  238. }
  239. err = utx.Create(lapseto).Error
  240. if err != nil {
  241. utx.Rollback()
  242. return
  243. }
  244. // 删除排班和排班模板信息
  245. if lapseto.LapsetoType == 2 {
  246. now := time.Now()
  247. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  248. "status": 0,
  249. "updated_time": now.Unix(),
  250. }).Error
  251. if deleteScheduleErr != nil {
  252. utx.Rollback()
  253. err = deleteScheduleErr
  254. return
  255. }
  256. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  257. "status": 0,
  258. "mtime": now.Unix(),
  259. }).Error
  260. if deleteSchTempItemErr != nil {
  261. utx.Rollback()
  262. err = deleteSchTempItemErr
  263. return
  264. }
  265. }
  266. utx.Commit()
  267. return
  268. }
  269. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  270. // if len(contagions) > 0 || len(diseases) > 0 {
  271. utx := writeDb.Begin()
  272. err = utx.Save(patient).Error
  273. if err != nil {
  274. utx.Rollback()
  275. return
  276. }
  277. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  278. fmt.Println("错误是什么。。。。。。。。。。。。。。。。。。。。。。。。。。。", err)
  279. if err != nil {
  280. utx.Rollback()
  281. return
  282. }
  283. if len(contagions) > 0 {
  284. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  285. insertParams := make([]string, 0)
  286. insertData := make([]interface{}, 0)
  287. for _, contagion := range contagions {
  288. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  289. insertData = append(insertData, patient.ID)
  290. insertData = append(insertData, contagion)
  291. insertData = append(insertData, 1)
  292. insertData = append(insertData, patient.CreatedTime)
  293. insertData = append(insertData, patient.UpdatedTime)
  294. }
  295. thisSQL += strings.Join(insertParams, ", ")
  296. err = utx.Exec(thisSQL, insertData...).Error
  297. if err != nil {
  298. utx.Rollback()
  299. return
  300. }
  301. }
  302. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  303. if err != nil {
  304. utx.Rollback()
  305. return
  306. }
  307. if len(diseases) > 0 {
  308. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  309. insertParams := make([]string, 0)
  310. insertData := make([]interface{}, 0)
  311. for _, disease := range diseases {
  312. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  313. insertData = append(insertData, patient.ID)
  314. insertData = append(insertData, disease)
  315. insertData = append(insertData, 1)
  316. insertData = append(insertData, patient.CreatedTime)
  317. insertData = append(insertData, patient.UpdatedTime)
  318. }
  319. thisSQL += strings.Join(insertParams, ", ")
  320. err = utx.Exec(thisSQL, insertData...).Error
  321. if err != nil {
  322. utx.Rollback()
  323. return
  324. }
  325. }
  326. utx.Commit()
  327. // } else {
  328. // err = writeDb.Save(patient).Error
  329. // }
  330. return
  331. }
  332. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  333. 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
  334. return
  335. }
  336. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  337. 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
  338. return
  339. }
  340. func GetPatientDiseases(id int64) []int64 {
  341. var dis []models.ChronicDiseases
  342. ids := make([]int64, 0)
  343. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  344. if err != nil || len(dis) == 0 {
  345. return ids
  346. }
  347. for _, item := range dis {
  348. ids = append(ids, item.DiseaseId)
  349. }
  350. return ids
  351. }
  352. func GetPatientContagions(id int64) []int64 {
  353. var cis []models.InfectiousDiseases
  354. ids := make([]int64, 0)
  355. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  356. if err != nil || len(cis) == 0 {
  357. return ids
  358. }
  359. for _, item := range cis {
  360. ids = append(ids, item.DiseaseId)
  361. }
  362. return ids
  363. }
  364. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  365. 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
  366. return
  367. }
  368. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  369. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  370. return
  371. }
  372. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  373. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  374. return
  375. }
  376. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  377. err = writeDb.Create(solution).Error
  378. return
  379. }
  380. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  381. err = writeDb.Save(solution).Error
  382. return
  383. }
  384. //GetPatientDialysisSolutionList 返回患者透析方案的列表
  385. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  386. offset := (page - 1) * limit
  387. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  388. if orgID > 0 {
  389. db = db.Where("ds.user_org_id=?", orgID)
  390. }
  391. if patientID > 0 {
  392. db = db.Where("ds.patient_id=?", patientID)
  393. }
  394. db = db.Count(&total).Offset(offset).Limit(limit)
  395. err = db.Order("id desc").Find(&solutions).Error
  396. if err != nil {
  397. return
  398. }
  399. if len(solutions) > 0 {
  400. nilNameIds := make([]int64, 0)
  401. for _, solution := range solutions {
  402. if len(solution.ModeName) == 0 {
  403. nilNameIds = append(nilNameIds, solution.ModeId)
  404. }
  405. }
  406. if len(nilNameIds) > 0 {
  407. var modes []*models.TreatmentMode
  408. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  409. if err != nil {
  410. return
  411. }
  412. modesMap := make(map[int64]models.TreatmentMode, 0)
  413. for _, mode := range modes {
  414. modesMap[mode.ID] = *mode
  415. }
  416. for index, solution := range solutions {
  417. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  418. solutions[index].ModeName = modesMap[solution.ModeId].Name
  419. }
  420. }
  421. }
  422. }
  423. return
  424. }
  425. //GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  426. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  427. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  428. if orgID > 0 {
  429. db = db.Where("dwa.user_org_id=?", orgID)
  430. }
  431. if patientID > 0 {
  432. db = db.Where("dwa.patient_id=?", patientID)
  433. }
  434. 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")
  435. if page > 0 && limit > 0 {
  436. offset := (page - 1) * limit
  437. db = db.Offset(offset).Limit(limit)
  438. }
  439. err = db.Find(&weights).Error
  440. return
  441. }
  442. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  443. err = writeDb.Create(m).Error
  444. return
  445. }
  446. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  447. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  448. return
  449. }
  450. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  451. return writeDb.Create(m).Error
  452. }
  453. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  454. var advice models.DoctorAdvice
  455. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  456. if err != nil {
  457. fmt.Println(err)
  458. group = 0
  459. }
  460. group = advice.GroupNo
  461. return
  462. }
  463. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  464. if group == 0 {
  465. group = GetMaxAdviceGroupID(orgId) + 1
  466. }
  467. tx := writeDb.Begin()
  468. defer func() {
  469. if r := recover(); r != nil {
  470. tx.Rollback()
  471. }
  472. }()
  473. for _, advice := range advices {
  474. advice.GroupNo = group
  475. if err = tx.Create(advice).Error; err != nil {
  476. tx.Rollback()
  477. return
  478. }
  479. }
  480. tx.Commit()
  481. return
  482. }
  483. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  484. if groupNo <= 0 {
  485. group := GetMaxAdviceGroupID(orgId)
  486. groupNo = group + 1
  487. }
  488. tx := writeDb.Begin()
  489. defer func() {
  490. if r := recover(); r != nil {
  491. tx.Rollback()
  492. }
  493. }()
  494. for _, advice := range advices {
  495. advice.GroupNo = groupNo
  496. if err = tx.Create(advice).Error; err != nil {
  497. tx.Rollback()
  498. return
  499. }
  500. list = append(list, advice)
  501. if len(advice.Children) > 0 {
  502. for _, child := range advice.Children {
  503. child.GroupNo = groupNo
  504. child.ParentId = advice.ID
  505. fmt.Println(child)
  506. if err = tx.Create(&child).Error; err != nil {
  507. tx.Rollback()
  508. return
  509. }
  510. list = append(list, child)
  511. }
  512. }
  513. }
  514. tx.Commit()
  515. return
  516. }
  517. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  518. return writeDb.Save(m).Error
  519. }
  520. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  521. 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
  522. if err != nil {
  523. return
  524. }
  525. return
  526. }
  527. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  528. ut := writeDb.Begin()
  529. err = ut.Save(m).Error
  530. if err != nil {
  531. ut.Rollback()
  532. return
  533. }
  534. 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
  535. if err != nil {
  536. ut.Rollback()
  537. return
  538. }
  539. ut.Commit()
  540. return
  541. }
  542. func DeleteSolution(m *models.DialysisSolution) (err error) {
  543. if m.ParentId > 0 {
  544. return writeDb.Save(m).Error
  545. } else {
  546. ut := writeDb.Begin()
  547. err = ut.Save(m).Error
  548. if err != nil {
  549. ut.Rollback()
  550. return
  551. }
  552. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  553. if err != nil {
  554. ut.Rollback()
  555. return
  556. }
  557. ut.Commit()
  558. }
  559. return
  560. }
  561. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  562. if m.ParentId > 0 {
  563. return writeDb.Save(m).Error
  564. } else {
  565. ut := writeDb.Begin()
  566. err = ut.Save(m).Error
  567. if err != nil {
  568. ut.Rollback()
  569. return
  570. }
  571. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "Modifier": m.Modifier}).Error
  572. if err != nil {
  573. ut.Rollback()
  574. return
  575. }
  576. ut.Commit()
  577. }
  578. return
  579. }
  580. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  581. 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
  582. if err != nil {
  583. return
  584. }
  585. return
  586. }
  587. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  588. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  589. return
  590. }
  591. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  592. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  593. return
  594. }
  595. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  596. //db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  597. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  598. table := UserReadDB().Table("sgj_user_admin_role as r")
  599. fmt.Print("table", table)
  600. if orgID > 0 {
  601. db = db.Where("x.user_org_id=?", orgID)
  602. }
  603. if patientID > 0 {
  604. db = db.Where("x.patient_id = ?", patientID)
  605. }
  606. if advice_type > 0 {
  607. db = db.Where("x.advice_type = ?", advice_type)
  608. } else if advice_type == 0 {
  609. db = db.Where("x.advice_type in (?)", []int{1, 3})
  610. }
  611. if stop == 1 {
  612. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  613. } else if stop == 2 {
  614. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  615. }
  616. if start != 0 {
  617. db = db.Where("x.start_time>=?", start)
  618. }
  619. if end != 0 {
  620. db = db.Where("start_time<=?", end)
  621. }
  622. if len(keywords) > 0 {
  623. likeKey := "%" + keywords + "%"
  624. db = db.Where("x.advice_name LIKE ?", likeKey)
  625. }
  626. 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
  627. fmt.Print("err", err)
  628. return
  629. }
  630. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  631. offset := (page - 1) * limit
  632. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  633. if orgID > 0 {
  634. db = db.Where("user_org_id=?", orgID)
  635. }
  636. if patientID > 0 {
  637. db = db.Where("patient_id = ?", patientID)
  638. }
  639. if advice_type > 0 {
  640. db = db.Where("advice_type = ?", advice_type)
  641. }
  642. if stop == 1 {
  643. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  644. } else if stop == 2 {
  645. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  646. }
  647. if start != 0 {
  648. db = db.Where("start_time>=?", start)
  649. }
  650. if end != 0 {
  651. db = db.Where("start_time<=?", end)
  652. }
  653. if len(keywords) > 0 {
  654. likeKey := "%" + keywords + "%"
  655. db = db.Where("advice_name LIKE ?", likeKey)
  656. }
  657. 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
  658. return
  659. }
  660. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  661. if len(advices) > 0 {
  662. utx := writeDb.Begin()
  663. if len(advices) > 0 {
  664. 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 "
  665. insertParams := make([]string, 0)
  666. insertData := make([]interface{}, 0)
  667. for _, advice := range advices {
  668. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  669. insertData = append(insertData, advice.SingleDoseUnit)
  670. insertData = append(insertData, advice.PrescribingNumber)
  671. insertData = append(insertData, advice.PrescribingNumberUnit)
  672. insertData = append(insertData, advice.AdviceName)
  673. insertData = append(insertData, advice.AdviceDesc)
  674. insertData = append(insertData, advice.SingleDose)
  675. insertData = append(insertData, advice.CreatedTime)
  676. insertData = append(insertData, advice.UpdatedTime)
  677. insertData = append(insertData, advice.PatientId)
  678. insertData = append(insertData, advice.ParentId)
  679. insertData = append(insertData, advice.UserOrgId)
  680. insertData = append(insertData, advice.RecordDate)
  681. insertData = append(insertData, 2)
  682. insertData = append(insertData, advice.DrugSpecUnit)
  683. }
  684. thisSQL += strings.Join(insertParams, ", ")
  685. err = utx.Exec(thisSQL, insertData...).Error
  686. if err != nil {
  687. utx.Rollback()
  688. return
  689. }
  690. }
  691. utx.Commit()
  692. }
  693. return
  694. }
  695. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  696. offset := (page - 1) * limit
  697. var total int64
  698. var err error
  699. var orders []*models.PatientDialysisRecord
  700. // err = readDb.Table("xt_dialysis_order as do").
  701. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  702. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  703. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  704. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  705. // 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).
  706. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  707. // 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
  708. db := readDb.Table("xt_dialysis_order as do").
  709. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  710. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  711. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  712. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  713. return readUserDb.Where("status = 1")
  714. })
  715. }).
  716. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  717. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  718. Preload("Device", "org_id=? and status=1", orgID).
  719. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  720. return readUserDb.Where("org_id=? and status = 1", orgID)
  721. }).
  722. 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).
  723. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  724. Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Group("s.schedule_date")
  725. if start != 0 {
  726. db = db.Where("do.dialysis_date>=?", start)
  727. }
  728. if end != 0 {
  729. db = db.Where("do.dialysis_date<=?", end)
  730. }
  731. if mode_id > 0 {
  732. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.record_id=do.id")
  733. db = db.Where("dp.mode_id=?", mode_id)
  734. }
  735. 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
  736. if len(orders) > 0 {
  737. ids := make([]int64, 0)
  738. for _, order := range orders {
  739. dialyzer := order.DialysisPrescription.Dialyzer
  740. ids = append(ids, dialyzer)
  741. }
  742. if len(ids) > 0 {
  743. var dialyzers []*models.DeviceNumber
  744. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  745. if err != nil {
  746. return nil, 0, err
  747. }
  748. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  749. for _, item := range dialyzers {
  750. dialyzerMap[item.ID] = *item
  751. }
  752. for orderIndex, order := range orders {
  753. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  754. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  755. }
  756. }
  757. }
  758. }
  759. return orders, total, err
  760. }
  761. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  762. offset := (page - 1) * limit
  763. fmt.Println(offset)
  764. fmt.Println(limit)
  765. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  766. if start != 0 {
  767. db = db.Where("assessment_date >= ?", start)
  768. }
  769. if end != 0 {
  770. db = db.Where("assessment_date <= ?", end)
  771. }
  772. db = db.Where("status=1")
  773. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  774. return
  775. }
  776. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  777. offset := (page - 1) * limit
  778. err = readDb.Table("xt_schedule as s").
  779. Preload("DeviceZone", "org_id=? and status=1", orgID).
  780. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  781. Preload("TreatmentMode", "status=1").
  782. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  783. Limit(limit).Offset(offset).Find(&schedules).Error
  784. return
  785. }
  786. func GetMonitorRecord(orgID int64, date int64, partition int64) ([]*models.MonitorDialysisSchedule, error) {
  787. var mds []*models.MonitorDialysisSchedule
  788. db := readDb.
  789. Model(&models.MonitorDialysisSchedule{}).
  790. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  791. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  792. Preload("TreatmentMode", "status = 1").
  793. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  794. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  795. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  796. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  797. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  798. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  799. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  800. Where("status = 1 AND user_org_id = ?", orgID)
  801. if date != 0 {
  802. db = db.Where("schedule_date = ? ", date)
  803. }
  804. if partition != 0 {
  805. db = db.Where("partition_id = ? ", partition)
  806. }
  807. err := db.Find(&mds).Error
  808. return mds, err
  809. }
  810. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  811. var patients []*models.Patients
  812. 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
  813. if getPatientErr != nil {
  814. return nil, getPatientErr
  815. }
  816. patientIDs := make([]int64, len(patients))
  817. for index, patient := range patients {
  818. patientIDs[index] = patient.ID
  819. }
  820. db := readDb.
  821. Model(&models.MonitorDialysisSchedule{}).
  822. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  823. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  824. Preload("TreatmentMode", "status = 1").
  825. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  826. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  827. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  828. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  829. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  830. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  831. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  832. var schedules []*models.MonitorDialysisSchedule
  833. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  834. return schedules, err
  835. }
  836. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  837. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID)
  838. if len(keywords) > 0 {
  839. likekey := "%" + keywords + "%"
  840. err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
  841. } else {
  842. err = db.Find(&patient).Error
  843. }
  844. return
  845. }
  846. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  847. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  848. return
  849. }
  850. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  851. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  852. return
  853. }
  854. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  855. now := time.Now().Unix()
  856. 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{}{
  857. "start_time": startTime,
  858. "updated_time": now,
  859. }).Error
  860. return err
  861. }
  862. func QueryPatientById(id int64) *models.Patients {
  863. var pat models.Patients
  864. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  865. if err != nil {
  866. }
  867. return &pat
  868. }
  869. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  870. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  871. return
  872. }
  873. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  874. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  875. return
  876. }
  877. func BatchDeleteDoctorAdvice(ids []string) (err error) {
  878. ut := writeDb.Begin()
  879. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  880. if err != nil {
  881. ut.Rollback()
  882. return
  883. }
  884. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  885. if err != nil {
  886. ut.Rollback()
  887. return
  888. }
  889. ut.Commit()
  890. return err
  891. }
  892. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  893. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  894. return
  895. }
  896. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  897. ut := writeDb.Begin()
  898. err = ut.Save(m).Error
  899. if err != nil {
  900. ut.Rollback()
  901. return
  902. }
  903. 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
  904. if err != nil {
  905. ut.Rollback()
  906. return
  907. }
  908. ut.Commit()
  909. return err
  910. }
  911. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  912. type Total struct {
  913. Count int64
  914. }
  915. var totals Total
  916. offset := (page - 1) * limit
  917. 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
  918. 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)
  919. return totals.Count, patient, err
  920. }
  921. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  922. 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
  923. return
  924. }
  925. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  926. err = writeDb.Create(m).Error
  927. return
  928. }
  929. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  930. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  931. return
  932. }
  933. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  934. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  935. if len(keywords) > 0 {
  936. likekey := "%" + keywords + "%"
  937. 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
  938. } else {
  939. err = db.Find(&patient).Error
  940. }
  941. return
  942. }