patient_service.go 57KB

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