patient_service.go 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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.Order("p.id desc").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,p.user_sys_before_count").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 FindPatientByIdTwo(orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  103. err = readDb.Model(&models.XtPatientsNew{}).Where("blood_id = ? and user_org_id = ? and status =1", id, orgID).First(&patient).Error
  104. return
  105. }
  106. func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  107. 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
  108. return
  109. }
  110. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  111. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  112. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  113. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  114. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  115. }).Where("user_org_id=? and dialysis_no = ? and status=1", orgID, no).First(&patient).Error
  116. return
  117. }
  118. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  119. user, _ := GetSgjUserByMobild(patient.Phone)
  120. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  121. utx := writeDb.Begin()
  122. btx := writeUserDb.Begin()
  123. if user.ID == 0 {
  124. user.Mobile = patient.Phone
  125. user.Avatar = patient.Avatar
  126. user.AvatarThumb = patient.Avatar
  127. user.Birthday = patient.Birthday
  128. user.Username = patient.Name
  129. user.Gender = patient.Gender
  130. user.Sources = 11
  131. user.Introduce = patient.Remark
  132. user.Status = 1
  133. user.UpdatedTime = patient.UpdatedTime
  134. user.CreatedTime = patient.CreatedTime
  135. err = btx.Create(&user).Error
  136. if err != nil {
  137. utx.Rollback()
  138. btx.Rollback()
  139. return
  140. }
  141. }
  142. patient.UserId = user.ID
  143. if customer == nil {
  144. err = btx.Create(&models.SgjCustomer{
  145. UserOrgId: patient.UserOrgId,
  146. UserId: user.ID,
  147. Mobile: patient.Phone,
  148. Name: patient.Name,
  149. Gender: patient.Gender,
  150. Birthday: patient.Birthday,
  151. Sources: 11,
  152. Status: 1,
  153. CreatedTime: patient.CreatedTime,
  154. UpdatedTime: patient.UpdatedTime,
  155. Avatar: patient.Avatar,
  156. Remark: patient.Remark,
  157. }).Error
  158. if err != nil {
  159. utx.Rollback()
  160. btx.Rollback()
  161. return
  162. }
  163. }
  164. err = utx.Create(patient).Error
  165. if err != nil {
  166. utx.Rollback()
  167. btx.Rollback()
  168. return
  169. }
  170. var lapseto models.PatientLapseto
  171. lapseto.PatientId = patient.ID
  172. lapseto.LapsetoType = patient.Lapseto
  173. lapseto.CreatedTime = patient.CreatedTime
  174. lapseto.UpdatedTime = patient.CreatedTime
  175. lapseto.Status = 1
  176. lapseto.LapsetoTime = patient.CreatedTime
  177. err = utx.Create(&lapseto).Error
  178. if err != nil {
  179. utx.Rollback()
  180. btx.Rollback()
  181. return
  182. }
  183. if len(contagions) > 0 {
  184. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  185. insertParams := make([]string, 0)
  186. insertData := make([]interface{}, 0)
  187. for _, contagion := range contagions {
  188. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  189. insertData = append(insertData, patient.ID)
  190. insertData = append(insertData, contagion)
  191. insertData = append(insertData, 1)
  192. insertData = append(insertData, patient.CreatedTime)
  193. insertData = append(insertData, patient.UpdatedTime)
  194. }
  195. thisSQL += strings.Join(insertParams, ", ")
  196. err = utx.Exec(thisSQL, insertData...).Error
  197. if err != nil {
  198. utx.Rollback()
  199. btx.Rollback()
  200. return
  201. }
  202. }
  203. if len(diseases) > 0 {
  204. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  205. insertParams := make([]string, 0)
  206. insertData := make([]interface{}, 0)
  207. for _, disease := range diseases {
  208. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  209. insertData = append(insertData, patient.ID)
  210. insertData = append(insertData, disease)
  211. insertData = append(insertData, 1)
  212. insertData = append(insertData, patient.CreatedTime)
  213. insertData = append(insertData, patient.UpdatedTime)
  214. }
  215. thisSQL += strings.Join(insertParams, ", ")
  216. err = utx.Exec(thisSQL, insertData...).Error
  217. if err != nil {
  218. utx.Rollback()
  219. btx.Rollback()
  220. return
  221. }
  222. }
  223. utx.Commit()
  224. btx.Commit()
  225. return
  226. }
  227. func GetLastPatientData(orgid int64) (models.Patients, error) {
  228. patients := models.Patients{}
  229. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  230. return patients, err
  231. }
  232. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  233. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  234. return err
  235. }
  236. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  237. utx := writeDb.Begin()
  238. err = utx.Model(&models.Patients{}).Where("id=?", patient.ID).Update(map[string]interface{}{"Lapseto": patient.Lapseto}).Error
  239. //err = utx.Model(&models.PatientLapseto{}).Where("id=?", patient.ID).Update(map[string]interface{}{"lapseto_type": patient.Lapseto, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  240. if err != nil {
  241. utx.Rollback()
  242. return
  243. }
  244. err = utx.Create(lapseto).Error
  245. if err != nil {
  246. utx.Rollback()
  247. return
  248. }
  249. // 删除排班和排班模板信息
  250. if lapseto.LapsetoType == 2 {
  251. now := time.Now()
  252. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  253. "status": 0,
  254. "updated_time": now.Unix(),
  255. }).Error
  256. if deleteScheduleErr != nil {
  257. utx.Rollback()
  258. err = deleteScheduleErr
  259. return
  260. }
  261. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  262. "status": 0,
  263. "mtime": now.Unix(),
  264. }).Error
  265. if deleteSchTempItemErr != nil {
  266. utx.Rollback()
  267. err = deleteSchTempItemErr
  268. return
  269. }
  270. }
  271. utx.Commit()
  272. return
  273. }
  274. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  275. // if len(contagions) > 0 || len(diseases) > 0 {
  276. utx := writeDb.Begin()
  277. err = utx.Save(patient).Error
  278. if err != nil {
  279. utx.Rollback()
  280. return
  281. }
  282. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  283. fmt.Println("err", err)
  284. if err != nil {
  285. utx.Rollback()
  286. return
  287. }
  288. if len(contagions) > 0 {
  289. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  290. insertParams := make([]string, 0)
  291. insertData := make([]interface{}, 0)
  292. for _, contagion := range contagions {
  293. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  294. insertData = append(insertData, patient.ID)
  295. insertData = append(insertData, contagion)
  296. insertData = append(insertData, 1)
  297. insertData = append(insertData, patient.CreatedTime)
  298. insertData = append(insertData, patient.UpdatedTime)
  299. }
  300. thisSQL += strings.Join(insertParams, ", ")
  301. err = utx.Exec(thisSQL, insertData...).Error
  302. if err != nil {
  303. utx.Rollback()
  304. return
  305. }
  306. }
  307. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  308. if err != nil {
  309. utx.Rollback()
  310. return
  311. }
  312. if len(diseases) > 0 {
  313. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  314. insertParams := make([]string, 0)
  315. insertData := make([]interface{}, 0)
  316. for _, disease := range diseases {
  317. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  318. insertData = append(insertData, patient.ID)
  319. insertData = append(insertData, disease)
  320. insertData = append(insertData, 1)
  321. insertData = append(insertData, patient.CreatedTime)
  322. insertData = append(insertData, patient.UpdatedTime)
  323. }
  324. thisSQL += strings.Join(insertParams, ", ")
  325. err = utx.Exec(thisSQL, insertData...).Error
  326. if err != nil {
  327. utx.Rollback()
  328. return
  329. }
  330. }
  331. utx.Commit()
  332. // } else {
  333. // err = writeDb.Save(patient).Error
  334. // }
  335. return
  336. }
  337. func UpdatePatientLapseto(patientid int64, lapseto models.PatientLapseto) error {
  338. err := XTWriteDB().Model(&lapseto).Where("patient_id = ?", patientid).Updates(map[string]interface{}{"lapseto_type": lapseto.LapsetoType, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  339. return err
  340. }
  341. func UpdatepatientTwo(patientsNew *models.XtPatientsNew, id int64) error {
  342. err := XTWriteDB().Model(&patientsNew).Where("blood_id = ?", id).Update(map[string]interface{}{"user_org_id": patientsNew.UserOrgId, "user_id": patientsNew.UserId, "avatar": patientsNew.Avatar, "patient_type": patientsNew.Avatar, "dialysis_no": patientsNew.DialysisNo, "admission_number": patientsNew.AdmissionNumber, "source": patientsNew.Source, "lapseto": patientsNew.Lapseto, "partition_id": patientsNew.PartitionId, "bed_id": patientsNew.BedId, "name": patientsNew.Name, "alias": patientsNew.Alias, "gender": patientsNew.Gender, "marital_status": patientsNew.MaritalStatus, "id_card_no": patientsNew.IdCardNo, "birthday": patientsNew.Birthday, "reimbursement_way_id": patientsNew.ReimbursementWayId, "health_care_type": patientsNew.HealthCareType, "health_care_no": patientsNew.HealthCareType, "health_care_due_date": patientsNew.HealthCareType, "height": patientsNew.Height, "blood_type": patientsNew.BloodType, "rh": patientsNew.Rh, "health_care_due_alert_date": patientsNew.HealthCareDueAlertDate, "education_level": patientsNew.EducationLevel, "profession": patientsNew.Profession, "phone": patientsNew.Phone, "home_telephone": patientsNew.HomeTelephone, "relative_phone": patientsNew.RelativePhone, "relative_relations": patientsNew.RelativeRelations, "home_address": patientsNew.HomeAddress, "work_unit": patientsNew.WorkUnit, "unit_address": patientsNew.UnitAddress, "children": patientsNew.Children, "receiving_date": patientsNew.ReceivingDate, "is_hospital_first_dialysis": patientsNew.IsHospitalFirstDialysis, "first_dialysis_date": patientsNew.FirstDialysisDate, "first_dialysis_hospital": patientsNew.FirstDialysisHospital, "predialysis_condition": patientsNew.PredialysisCondition, "pre_hospital_dialysis_frequency": patientsNew.PreHospitalDialysisFrequency, "pre_hospital_dialysis_times": patientsNew.PreHospitalDialysisFrequency, "hospital_first_dialysis_date": patientsNew.HospitalFirstDialysisDate, "induction_period": patientsNew.InductionPeriod, "initial_dialysis": patientsNew.InitialDialysis, "total_dialysis": patientsNew.TotalDialysis, "attending_doctor_id": patientsNew.AttendingDoctorId, "head_nurse_id": patientsNew.HeadNurseId, "evaluate": patientsNew.Evaluate, "diagnose": patientsNew.Diagnose, "remark": patientsNew.Remark, "registrars_id": patientsNew.RegistrarsId, "registrars": patientsNew.Registrars, "qr_code": patientsNew.QrCode, "binding_state": patientsNew.BindingState, "patient_complains": patientsNew.PatientComplains, "present_history": patientsNew.PresentHistory, "past_history": patientsNew.PastHistory, "temperature": patientsNew.Temperature,
  343. "pulse": patientsNew.Pulse, "respiratory": patientsNew.Respiratory, "sbp": patientsNew.Sbp, "dbp": patientsNew.Dbp, "nation": patientsNew.Nation, "native_place": patientsNew.NativePlace, "age": patientsNew.Age, "infectious_next_record_time": patientsNew.InfectiousNextRecordTime, "is_infectious": patientsNew.IsInfectious, "remind_cycle": patientsNew.RemindCycle, "response_result": patientsNew.ResponseResult, "is_open_remind": patientsNew.IsOpenRemind, "first_treatment_date": patientsNew.FirstTreatmentDate, "dialysis_age": patientsNew.DialysisAge, "expense_kind": patientsNew.ExpenseKind, "tell_phone": patientsNew.ExpenseKind, "contact_name": patientsNew.ContactName, "blood_patients": patientsNew.BloodPatients, "slow_patients": patientsNew.SlowPatients, "member_patients": patientsNew.MemberPatients, "ecommer_patients": patientsNew.EcommerPatients}).Error
  344. return err
  345. }
  346. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  347. 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
  348. return
  349. }
  350. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  351. 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
  352. return
  353. }
  354. func GetPatientDiseases(id int64) []int64 {
  355. var dis []models.ChronicDiseases
  356. ids := make([]int64, 0)
  357. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  358. if err != nil || len(dis) == 0 {
  359. return ids
  360. }
  361. for _, item := range dis {
  362. ids = append(ids, item.DiseaseId)
  363. }
  364. return ids
  365. }
  366. func GetPatientContagions(id int64) []int64 {
  367. var cis []models.InfectiousDiseases
  368. ids := make([]int64, 0)
  369. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  370. if err != nil || len(cis) == 0 {
  371. return ids
  372. }
  373. for _, item := range cis {
  374. ids = append(ids, item.DiseaseId)
  375. }
  376. return ids
  377. }
  378. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  379. 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
  380. return
  381. }
  382. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  383. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  384. return
  385. }
  386. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  387. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  388. return
  389. }
  390. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  391. err = writeDb.Create(solution).Error
  392. return
  393. }
  394. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  395. err = writeDb.Save(solution).Error
  396. return
  397. }
  398. //GetPatientDialysisSolutionList 返回患者透析方案的列表
  399. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  400. offset := (page - 1) * limit
  401. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  402. if orgID > 0 {
  403. db = db.Where("ds.user_org_id=?", orgID)
  404. }
  405. if patientID > 0 {
  406. db = db.Where("ds.patient_id=?", patientID)
  407. }
  408. db = db.Count(&total).Offset(offset).Limit(limit)
  409. err = db.Order("id desc").Find(&solutions).Error
  410. if err != nil {
  411. return
  412. }
  413. if len(solutions) > 0 {
  414. nilNameIds := make([]int64, 0)
  415. for _, solution := range solutions {
  416. if len(solution.ModeName) == 0 {
  417. nilNameIds = append(nilNameIds, solution.ModeId)
  418. }
  419. }
  420. if len(nilNameIds) > 0 {
  421. var modes []*models.TreatmentMode
  422. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  423. if err != nil {
  424. return
  425. }
  426. modesMap := make(map[int64]models.TreatmentMode, 0)
  427. for _, mode := range modes {
  428. modesMap[mode.ID] = *mode
  429. }
  430. for index, solution := range solutions {
  431. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  432. solutions[index].ModeName = modesMap[solution.ModeId].Name
  433. }
  434. }
  435. }
  436. }
  437. return
  438. }
  439. //GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  440. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  441. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  442. if orgID > 0 {
  443. db = db.Where("dwa.user_org_id=?", orgID)
  444. }
  445. if patientID > 0 {
  446. db = db.Where("dwa.patient_id=?", patientID)
  447. }
  448. 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")
  449. if page > 0 && limit > 0 {
  450. offset := (page - 1) * limit
  451. db = db.Offset(offset).Limit(limit)
  452. }
  453. err = db.Find(&weights).Error
  454. return
  455. }
  456. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  457. err = writeDb.Create(m).Error
  458. return
  459. }
  460. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  461. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  462. return
  463. }
  464. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  465. return writeDb.Create(m).Error
  466. }
  467. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  468. var advice models.DoctorAdvice
  469. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  470. if err != nil {
  471. fmt.Println(err)
  472. group = 0
  473. }
  474. group = advice.GroupNo
  475. return
  476. }
  477. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  478. if group == 0 {
  479. group = GetMaxAdviceGroupID(orgId) + 1
  480. }
  481. tx := writeDb.Begin()
  482. defer func() {
  483. if r := recover(); r != nil {
  484. tx.Rollback()
  485. }
  486. }()
  487. for _, advice := range advices {
  488. advice.GroupNo = group
  489. if err = tx.Create(advice).Error; err != nil {
  490. tx.Rollback()
  491. return
  492. }
  493. }
  494. tx.Commit()
  495. return
  496. }
  497. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  498. if groupNo <= 0 {
  499. group := GetMaxAdviceGroupID(orgId)
  500. groupNo = group + 1
  501. }
  502. tx := writeDb.Begin()
  503. defer func() {
  504. if r := recover(); r != nil {
  505. tx.Rollback()
  506. }
  507. }()
  508. for _, advice := range advices {
  509. advice.GroupNo = groupNo
  510. if err = tx.Create(advice).Error; err != nil {
  511. tx.Rollback()
  512. return
  513. }
  514. list = append(list, advice)
  515. if len(advice.Children) > 0 {
  516. for _, child := range advice.Children {
  517. child.GroupNo = groupNo
  518. child.ParentId = advice.ID
  519. fmt.Println(child)
  520. if err = tx.Create(&child).Error; err != nil {
  521. tx.Rollback()
  522. return
  523. }
  524. list = append(list, child)
  525. }
  526. }
  527. }
  528. tx.Commit()
  529. return
  530. }
  531. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  532. return writeDb.Save(m).Error
  533. }
  534. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  535. 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
  536. if err != nil {
  537. return
  538. }
  539. return
  540. }
  541. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  542. ut := writeDb.Begin()
  543. err = ut.Save(m).Error
  544. if err != nil {
  545. ut.Rollback()
  546. return
  547. }
  548. 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
  549. if err != nil {
  550. ut.Rollback()
  551. return
  552. }
  553. ut.Commit()
  554. return
  555. }
  556. func DeleteSolution(m *models.DialysisSolution) (err error) {
  557. if m.ParentId > 0 {
  558. return writeDb.Save(m).Error
  559. } else {
  560. ut := writeDb.Begin()
  561. err = ut.Save(m).Error
  562. if err != nil {
  563. ut.Rollback()
  564. return
  565. }
  566. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  567. if err != nil {
  568. ut.Rollback()
  569. return
  570. }
  571. ut.Commit()
  572. }
  573. return
  574. }
  575. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  576. if m.ParentId > 0 {
  577. return writeDb.Save(m).Error
  578. } else {
  579. ut := writeDb.Begin()
  580. err = ut.Save(m).Error
  581. if err != nil {
  582. ut.Rollback()
  583. return
  584. }
  585. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "Modifier": m.Modifier}).Error
  586. if err != nil {
  587. ut.Rollback()
  588. return
  589. }
  590. ut.Commit()
  591. }
  592. return
  593. }
  594. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  595. 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
  596. if err != nil {
  597. return
  598. }
  599. return
  600. }
  601. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  602. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  603. return
  604. }
  605. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  606. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  607. return
  608. }
  609. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  610. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  611. table := UserReadDB().Table("sgj_user_admin_role as r")
  612. fmt.Print("table", table)
  613. if orgID > 0 {
  614. db = db.Where("x.user_org_id=?", orgID)
  615. }
  616. if patientID > 0 {
  617. db = db.Where("x.patient_id = ?", patientID)
  618. }
  619. if advice_type > 0 {
  620. db = db.Where("x.advice_type = ?", advice_type)
  621. } else if advice_type == 0 {
  622. db = db.Where("x.advice_type in (?)", []int{1, 3})
  623. }
  624. if stop == 1 {
  625. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  626. } else if stop == 2 {
  627. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  628. }
  629. if start != 0 {
  630. db = db.Where("x.start_time>=?", start)
  631. }
  632. if end != 0 {
  633. db = db.Where("start_time<=?", end)
  634. }
  635. if len(keywords) > 0 {
  636. likeKey := "%" + keywords + "%"
  637. db = db.Where("x.advice_name LIKE ?", likeKey)
  638. }
  639. 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
  640. fmt.Print("err", err)
  641. return
  642. }
  643. func GetDoctorAdviceListOne(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  644. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  645. table := UserReadDB().Table("sgj_user_admin_role as r")
  646. fmt.Print("table", table)
  647. if orgID > 0 {
  648. db = db.Where("x.user_org_id=?", orgID)
  649. }
  650. if patientID > 0 {
  651. db = db.Where("x.patient_id = ?", patientID)
  652. }
  653. if advice_type == 1 && advice_type > 0 {
  654. db = db.Where("x.advice_type = ?", advice_type)
  655. }
  656. if advice_type == 3 && advice_type > 0 {
  657. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  658. }
  659. if stop == 1 {
  660. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  661. } else if stop == 2 {
  662. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  663. }
  664. if start != 0 {
  665. db = db.Where("x.start_time>=?", start)
  666. }
  667. if end != 0 {
  668. db = db.Where("start_time<=?", end)
  669. }
  670. if len(keywords) > 0 {
  671. likeKey := "%" + keywords + "%"
  672. db = db.Where("x.advice_name LIKE ?", likeKey)
  673. }
  674. offset := (page - 1) * limit
  675. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).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
  676. fmt.Print("err", err)
  677. return
  678. }
  679. func GetDoctorAdviceListTwo(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64) (advices []*models.DoctorAdvices, total int64, err error) {
  680. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  681. table := UserReadDB().Table("sgj_user_admin_role as r")
  682. fmt.Println(table)
  683. if orgID > 0 {
  684. db = db.Where("x.user_org_id=?", orgID)
  685. }
  686. if patientID > 0 {
  687. db = db.Where("x.patient_id = ?", patientID)
  688. }
  689. if advice_type == 1 {
  690. db = db.Where("x.advice_type = ?", advice_type)
  691. }
  692. if advice_type == 3 {
  693. db = db.Where("x.advice_type <> 1")
  694. }
  695. if stop == 1 {
  696. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  697. } else if stop == 2 {
  698. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  699. }
  700. if start != 0 {
  701. db = db.Where("x.start_time>=?", start)
  702. }
  703. if end != 0 {
  704. db = db.Where("start_time<=?", end)
  705. }
  706. if len(keywords) > 0 {
  707. likeKey := "%" + keywords + "%"
  708. db = db.Where("x.advice_name LIKE ?", likeKey)
  709. }
  710. offset := (page - 1) * limit
  711. err = db.Order("x.start_time desc").Group("x.start_time").Count(&total).Offset(offset).Limit(limit).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").Scan(&advices).Error
  712. fmt.Print("错误是什么", err)
  713. return
  714. }
  715. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  716. offset := (page - 1) * limit
  717. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  718. if orgID > 0 {
  719. db = db.Where("user_org_id=?", orgID)
  720. }
  721. if patientID > 0 {
  722. db = db.Where("patient_id = ?", patientID)
  723. }
  724. if advice_type > 0 {
  725. db = db.Where("advice_type = ?", advice_type)
  726. }
  727. if stop == 1 {
  728. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  729. } else if stop == 2 {
  730. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  731. }
  732. if start != 0 {
  733. db = db.Where("start_time>=?", start)
  734. }
  735. if end != 0 {
  736. db = db.Where("start_time<=?", end)
  737. }
  738. if len(keywords) > 0 {
  739. likeKey := "%" + keywords + "%"
  740. db = db.Where("advice_name LIKE ?", likeKey)
  741. }
  742. 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
  743. return
  744. }
  745. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  746. if len(advices) > 0 {
  747. utx := writeDb.Begin()
  748. if len(advices) > 0 {
  749. 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 "
  750. insertParams := make([]string, 0)
  751. insertData := make([]interface{}, 0)
  752. for _, advice := range advices {
  753. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  754. insertData = append(insertData, advice.SingleDoseUnit)
  755. insertData = append(insertData, advice.PrescribingNumber)
  756. insertData = append(insertData, advice.PrescribingNumberUnit)
  757. insertData = append(insertData, advice.AdviceName)
  758. insertData = append(insertData, advice.AdviceDesc)
  759. insertData = append(insertData, advice.SingleDose)
  760. insertData = append(insertData, advice.CreatedTime)
  761. insertData = append(insertData, advice.UpdatedTime)
  762. insertData = append(insertData, advice.PatientId)
  763. insertData = append(insertData, advice.ParentId)
  764. insertData = append(insertData, advice.UserOrgId)
  765. insertData = append(insertData, advice.RecordDate)
  766. insertData = append(insertData, 2)
  767. insertData = append(insertData, advice.DrugSpecUnit)
  768. }
  769. thisSQL += strings.Join(insertParams, ", ")
  770. err = utx.Exec(thisSQL, insertData...).Error
  771. if err != nil {
  772. utx.Rollback()
  773. return
  774. }
  775. }
  776. utx.Commit()
  777. }
  778. return
  779. }
  780. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  781. offset := (page - 1) * limit
  782. var total int64
  783. var err error
  784. var orders []*models.PatientDialysisRecord
  785. // err = readDb.Table("xt_dialysis_order as do").
  786. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  787. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  788. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  789. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  790. // 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).
  791. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  792. // 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
  793. db := readDb.Table("xt_dialysis_order as do").
  794. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  795. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  796. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  797. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  798. return readUserDb.Where("status = 1")
  799. })
  800. }).
  801. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  802. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  803. Preload("Device", "org_id=? and status=1", orgID).
  804. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  805. return readUserDb.Where("org_id=? and status = 1", orgID)
  806. }).
  807. 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).
  808. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  809. Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Group("s.schedule_date")
  810. if start != 0 {
  811. db = db.Where("do.dialysis_date>=?", start)
  812. }
  813. if end != 0 {
  814. db = db.Where("do.dialysis_date<=?", end)
  815. }
  816. if mode_id > 0 {
  817. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.record_id=do.id")
  818. db = db.Where("dp.mode_id=?", mode_id)
  819. }
  820. 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
  821. if len(orders) > 0 {
  822. ids := make([]int64, 0)
  823. for _, order := range orders {
  824. dialyzer := order.DialysisPrescription.Dialyzer
  825. ids = append(ids, dialyzer)
  826. }
  827. if len(ids) > 0 {
  828. var dialyzers []*models.DeviceNumber
  829. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  830. if err != nil {
  831. return nil, 0, err
  832. }
  833. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  834. for _, item := range dialyzers {
  835. dialyzerMap[item.ID] = *item
  836. }
  837. for orderIndex, order := range orders {
  838. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  839. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  840. }
  841. }
  842. }
  843. }
  844. return orders, total, err
  845. }
  846. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  847. offset := (page - 1) * limit
  848. fmt.Println(offset)
  849. fmt.Println(limit)
  850. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  851. if start != 0 {
  852. db = db.Where("assessment_date >= ?", start)
  853. }
  854. if end != 0 {
  855. db = db.Where("assessment_date <= ?", end)
  856. }
  857. db = db.Where("status=1")
  858. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  859. return
  860. }
  861. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  862. offset := (page - 1) * limit
  863. err = readDb.Table("xt_schedule as s").
  864. Preload("DeviceZone", "org_id=? and status=1", orgID).
  865. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  866. Preload("TreatmentMode", "status=1").
  867. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  868. Limit(limit).Offset(offset).Find(&schedules).Error
  869. return
  870. }
  871. func GetMonitorRecord(orgID int64, date int64, partition int64) ([]*models.MonitorDialysisSchedule, error) {
  872. var mds []*models.MonitorDialysisSchedule
  873. db := readDb.
  874. Model(&models.MonitorDialysisSchedule{}).
  875. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  876. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  877. Preload("TreatmentMode", "status = 1").
  878. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  879. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  880. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  881. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  882. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  883. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  884. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  885. Where("status = 1 AND user_org_id = ?", orgID)
  886. if date != 0 {
  887. db = db.Where("schedule_date = ? ", date)
  888. }
  889. if partition != 0 {
  890. db = db.Where("partition_id = ? ", partition)
  891. }
  892. err := db.Find(&mds).Error
  893. return mds, err
  894. }
  895. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  896. var patients []*models.Patients
  897. 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
  898. if getPatientErr != nil {
  899. return nil, getPatientErr
  900. }
  901. patientIDs := make([]int64, len(patients))
  902. for index, patient := range patients {
  903. patientIDs[index] = patient.ID
  904. }
  905. db := readDb.
  906. Model(&models.MonitorDialysisSchedule{}).
  907. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  908. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  909. Preload("TreatmentMode", "status = 1").
  910. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  911. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  912. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  913. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  914. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  915. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  916. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  917. var schedules []*models.MonitorDialysisSchedule
  918. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  919. return schedules, err
  920. }
  921. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  922. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID)
  923. if len(keywords) > 0 {
  924. likekey := "%" + keywords + "%"
  925. err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
  926. } else {
  927. err = db.Find(&patient).Error
  928. }
  929. return
  930. }
  931. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  932. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  933. return
  934. }
  935. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  936. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  937. return
  938. }
  939. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  940. now := time.Now().Unix()
  941. 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{}{
  942. "start_time": startTime,
  943. "updated_time": now,
  944. "modifier": admin_user_id,
  945. }).Error
  946. return err
  947. }
  948. func QueryPatientById(id int64) *models.Patients {
  949. var pat models.Patients
  950. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  951. if err != nil {
  952. }
  953. return &pat
  954. }
  955. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  956. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  957. return
  958. }
  959. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  960. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  961. return
  962. }
  963. func BatchDeleteDoctorAdvice(ids []string, user_id int64) (err error) {
  964. ut := writeDb.Begin()
  965. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix(), "modifier": user_id}).Error
  966. if err != nil {
  967. ut.Rollback()
  968. return
  969. }
  970. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix(), "modifier": user_id}).Error
  971. if err != nil {
  972. ut.Rollback()
  973. return
  974. }
  975. ut.Commit()
  976. return err
  977. }
  978. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  979. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  980. return
  981. }
  982. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  983. ut := writeDb.Begin()
  984. err = ut.Save(m).Error
  985. if err != nil {
  986. ut.Rollback()
  987. return
  988. }
  989. 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
  990. if err != nil {
  991. ut.Rollback()
  992. return
  993. }
  994. ut.Commit()
  995. return err
  996. }
  997. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  998. type Total struct {
  999. Count int64
  1000. }
  1001. var totals Total
  1002. offset := (page - 1) * limit
  1003. 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
  1004. 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)
  1005. return totals.Count, patient, err
  1006. }
  1007. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  1008. 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
  1009. return
  1010. }
  1011. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  1012. err = writeDb.Create(m).Error
  1013. return
  1014. }
  1015. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  1016. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  1017. return
  1018. }
  1019. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  1020. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1021. if len(keywords) > 0 {
  1022. likekey := "%" + keywords + "%"
  1023. 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
  1024. } else {
  1025. err = db.Find(&patient).Error
  1026. }
  1027. return
  1028. }
  1029. func GetPatientScheduleOne(patientid int64, nowdate int64, orgid int64) (models.XtSchedule, error) {
  1030. schedule := models.XtSchedule{}
  1031. err := XTReadDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patientid, nowdate, orgid).Find(&schedule).Error
  1032. return schedule, err
  1033. }
  1034. func CreateExportPatient(patient *models.Patients, contagions []int64, org_creator int64) (err error) {
  1035. user, _ := GetSgjUserByMobild(patient.Phone)
  1036. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  1037. utx := writeDb.Begin()
  1038. btx := writeUserDb.Begin()
  1039. if user.ID == 0 {
  1040. user.Mobile = patient.Phone
  1041. user.Avatar = patient.Avatar
  1042. user.AvatarThumb = patient.Avatar
  1043. user.Birthday = patient.Birthday
  1044. user.Username = patient.Name
  1045. user.Gender = patient.Gender
  1046. user.Sources = 11
  1047. user.Introduce = patient.Remark
  1048. user.Status = 1
  1049. user.UpdatedTime = patient.UpdatedTime
  1050. user.CreatedTime = patient.CreatedTime
  1051. err = btx.Create(&user).Error
  1052. if err != nil {
  1053. utx.Rollback()
  1054. btx.Rollback()
  1055. return
  1056. }
  1057. }
  1058. patient.UserId = user.ID
  1059. if customer == nil {
  1060. err = btx.Create(&models.SgjCustomer{
  1061. UserOrgId: patient.UserOrgId,
  1062. UserId: user.ID,
  1063. Mobile: patient.Phone,
  1064. Name: patient.Name,
  1065. Gender: patient.Gender,
  1066. Birthday: patient.Birthday,
  1067. Sources: 11,
  1068. Status: 1,
  1069. CreatedTime: patient.CreatedTime,
  1070. UpdatedTime: patient.UpdatedTime,
  1071. Avatar: patient.Avatar,
  1072. Remark: patient.Remark,
  1073. }).Error
  1074. if err != nil {
  1075. utx.Rollback()
  1076. btx.Rollback()
  1077. return
  1078. }
  1079. }
  1080. err = utx.Create(patient).Error
  1081. if err != nil {
  1082. utx.Rollback()
  1083. btx.Rollback()
  1084. return
  1085. }
  1086. if patient.DryWeight > 0 {
  1087. var dryWeight models.SgjPatientDryweight
  1088. dryWeight.PatientId = patient.ID
  1089. dryWeight.UserOrgId = patient.UserOrgId
  1090. dryWeight.Status = 1
  1091. dryWeight.AdjustedValue = "/"
  1092. dryWeight.Creator = org_creator
  1093. dryWeight.UserId = org_creator
  1094. dryWeight.Ctime = time.Now().Unix()
  1095. dryWeight.Mtime = time.Now().Unix()
  1096. dryWeight.DryWeight = patient.DryWeight
  1097. dryWeight.Remakes = ""
  1098. err = utx.Create(&dryWeight).Error
  1099. }
  1100. var lapseto models.PatientLapseto
  1101. lapseto.PatientId = patient.ID
  1102. lapseto.LapsetoType = patient.Lapseto
  1103. lapseto.CreatedTime = patient.CreatedTime
  1104. lapseto.UpdatedTime = patient.CreatedTime
  1105. lapseto.Status = 1
  1106. lapseto.LapsetoTime = patient.CreatedTime
  1107. err = utx.Create(&lapseto).Error
  1108. if err != nil {
  1109. utx.Rollback()
  1110. btx.Rollback()
  1111. return
  1112. }
  1113. if len(contagions) > 0 {
  1114. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  1115. insertParams := make([]string, 0)
  1116. insertData := make([]interface{}, 0)
  1117. for _, contagion := range contagions {
  1118. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  1119. insertData = append(insertData, patient.ID)
  1120. insertData = append(insertData, contagion)
  1121. insertData = append(insertData, 1)
  1122. insertData = append(insertData, patient.CreatedTime)
  1123. insertData = append(insertData, patient.UpdatedTime)
  1124. }
  1125. thisSQL += strings.Join(insertParams, ", ")
  1126. err = utx.Exec(thisSQL, insertData...).Error
  1127. if err != nil {
  1128. utx.Rollback()
  1129. btx.Rollback()
  1130. return
  1131. }
  1132. }
  1133. patientsNew := models.XtPatientsNew{
  1134. Name: patient.Name,
  1135. Gender: patient.Gender,
  1136. Phone: patient.Phone,
  1137. IdCardNo: patient.IdCardNo,
  1138. FirstDialysisDate: patient.FirstDialysisDate,
  1139. Source: patient.Source,
  1140. Lapseto: patient.Lapseto,
  1141. IsInfectious: patient.IsInfectious,
  1142. DialysisNo: patient.DialysisNo,
  1143. Height: patient.Height,
  1144. HomeAddress: patient.HomeAddress,
  1145. IsExcelExport: 1,
  1146. BloodPatients: 1,
  1147. Status: 1,
  1148. CreatedTime: time.Now().Unix(),
  1149. UserOrgId: patient.UserOrgId,
  1150. BloodId: patient.ID,
  1151. Avatar: "https://images.shengws.com/201809182128111.png",
  1152. }
  1153. err = utx.Create(&patientsNew).Error
  1154. if err != nil {
  1155. utx.Rollback()
  1156. btx.Rollback()
  1157. return
  1158. }
  1159. utx.Commit()
  1160. btx.Commit()
  1161. return
  1162. }
  1163. func FindPatientPhoneIsExist(phone string, org_id int64) (count int64) {
  1164. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND phone = ? AND status = 1", org_id, phone).Count(&count)
  1165. return
  1166. }
  1167. func FindPatientIdCardNoIsExist(id_card_no string, org_id int64) (count int64) {
  1168. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND id_card_no = ? AND status = 1", org_id, id_card_no).Count(&count)
  1169. return
  1170. }
  1171. func CreateExportErrLog(log *models.ExportErrLog) {
  1172. writeDb.Create(&log)
  1173. return
  1174. }
  1175. func FindPatientExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1176. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 1", org_id, export_time).Find(&errLogs).Error
  1177. return
  1178. }
  1179. func CreateExportLog(log *models.ExportLog) {
  1180. writeDb.Create(&log)
  1181. }
  1182. func UpdateDoctorEditAdvice(advice models.XtDoctorAdvice, orgid int64, groupno int64, date int64, patientid int64) error {
  1183. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and groupno = ? and advice_date = ? and patient_id = ?", orgid, groupno, date, patientid).Update(map[string]interface{}{"start_time": advice.StartTime, "updated_time": advice.UpdatedTime}).Error
  1184. return err
  1185. }
  1186. func GetPatientsByKey(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1187. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1188. if len(keywords) > 0 {
  1189. likekey := "%" + keywords + "%"
  1190. err = db.Where("(name LIKE ? OR dialysis_no LIKE ?)", likekey, likekey).Find(&patient).Error
  1191. } else {
  1192. err = db.Find(&patient).Error
  1193. }
  1194. return
  1195. }