qcd_service.go 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "time"
  6. )
  7. type DialysisTotalDataStruct struct {
  8. Date string `json:"date"`
  9. Number string `json:"number"`
  10. ModeID string `json:"mode_id"`
  11. }
  12. func GetDialysisTotalData(orgID, page, limit, lapseto, statisticalMethod, modeID, startTime, endTime int64, isStartTime, isEndTime bool) (dtd []*DialysisTotalDataStruct, ttd []*DialysisTotalDataStruct, total int64, err error) {
  13. db := readDb
  14. sql := "s.mode_id, from_unixtime(s.schedule_date, '%Y%m%d') as date, count(s.schedule_date) as number"
  15. datesql := "s.schedule_date as date"
  16. group := "from_unixtime(s.schedule_date, '%Y%m%d')"
  17. db = db.Table("xt_schedule as s")
  18. if statisticalMethod == 1 {
  19. db = db.Joins("JOIN xt_dialysis_order as o ON o.user_org_id =? and o.patient_id=s.patient_id and o.dialysis_date=s.schedule_date and o.status=1", orgID)
  20. }
  21. db = db.Where("s.user_org_id=?", orgID)
  22. if modeID > 0 {
  23. db = db.Where("s.mode_id=?", modeID)
  24. }
  25. if isStartTime {
  26. db = db.Where("s.schedule_date>=?", startTime)
  27. }
  28. if isEndTime {
  29. db = db.Where("s.schedule_date<=?", endTime)
  30. }
  31. db = db.Where("s.status=1")
  32. if lapseto > 0 {
  33. joinString := "JOIN xt_patients as p ON o.patient_id=p.id and p.lapseto=? and p.status=1"
  34. joinParams := make([]interface{}, 0)
  35. joinParams = append(joinParams, lapseto)
  36. if orgID > 0 {
  37. joinString += " AND p.user_org_id=?"
  38. joinParams = append(joinParams, orgID)
  39. }
  40. db = db.Joins(joinString, joinParams...)
  41. }
  42. err = db.Select("s.mode_id, count(s.mode_id) as number").Group("s.mode_id").Order("s.mode_id asc").Find(&ttd).Error
  43. if err != nil {
  44. return
  45. }
  46. offset := (page - 1) * limit
  47. var ds []*DialysisTotalDataStruct
  48. err = db.Select(datesql).Group(group).Count(&total).Order("date asc").Offset(offset).Limit(limit).Find(&ds).Error
  49. if err != nil {
  50. return
  51. }
  52. dates := make([]string, 0)
  53. if len(ds) > 0 {
  54. for _, d := range ds {
  55. dates = append(dates, d.Date)
  56. }
  57. db = db.Where("s.schedule_date IN (?)", dates)
  58. }
  59. err = db.Select(sql).Group(group + ", s.mode_id").Order("date asc, mode_id").Find(&dtd).Error
  60. return
  61. }
  62. type PIInspectionPageStruct struct {
  63. PatientID int64 `json:"patient_id"`
  64. InspectDate int64 `json:"inspect_date"`
  65. }
  66. type PIInspectionProjectCountStruct struct {
  67. Count int64 `json:"count"`
  68. }
  69. func GetProcessIndicatorsData(orgID, page, limit, lapseto, dialysisAge, projectID, startTime, endTime int64, isStartTime, isEndTime bool) (inspections []*models.Inspection, references []*models.InspectionReference, patients []*models.Patients, counts []*PIInspectionProjectCountStruct, total int64, err error) {
  70. err = readDb.Table("xt_inspection_reference").Find(&references).Error
  71. if err != nil || len(references) == 0 {
  72. return
  73. }
  74. projects := make(map[int64]int64, 0)
  75. for _, reference := range references {
  76. if _, exist := projects[reference.ProjectId]; !exist {
  77. projects[reference.ProjectId] = reference.ProjectId
  78. }
  79. }
  80. projectLen := len(projects)
  81. countSQL := "SELECT count(distinct project_id) as projects FROM xt_inspection as i "
  82. countParams := make([]interface{}, 0)
  83. if dialysisAge > 0 || lapseto > 0 {
  84. countSQL += " JOIN xt_patients as p ON i.patient_id=p.id"
  85. if orgID > 0 {
  86. countSQL += " and p.user_org_id=?"
  87. countParams = append(countParams, orgID)
  88. }
  89. if lapseto > 0 {
  90. countSQL += " and p.lapseto=?"
  91. countParams = append(countParams, lapseto)
  92. }
  93. if dialysisAge > 0 {
  94. loc, _ := time.LoadLocation("Local")
  95. nowTime := time.Now()
  96. nowDay := nowTime.Format("2006-01-02")
  97. dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
  98. if err != nil {
  99. return inspections, references, patients, counts, total, err
  100. }
  101. timePoint := dayTime.AddDate(0, -3, 0).Unix()
  102. if dialysisAge == 1 {
  103. countSQL += " and p.first_dialysis_date<>0 AND p.first_dialysis_date <= ?"
  104. countParams = append(countParams, timePoint)
  105. } else {
  106. countSQL += " and p.first_dialysis_date<>0 AND p.first_dialysis_date > ?"
  107. countParams = append(countParams, timePoint)
  108. }
  109. }
  110. }
  111. countSQL += " WHERE i.status=1"
  112. if orgID > 0 {
  113. countSQL += " AND i.org_id=?"
  114. countParams = append(countParams, orgID)
  115. }
  116. if projectID > 0 {
  117. countSQL += " AND i.project_id=?"
  118. countParams = append(countParams, projectID)
  119. }
  120. if isStartTime {
  121. countSQL += " AND i.inspect_date>=?"
  122. countParams = append(countParams, startTime)
  123. }
  124. if isEndTime {
  125. countSQL += " AND i.inspect_date<=?"
  126. countParams = append(countParams, endTime)
  127. }
  128. countParams = append(countParams, projectLen)
  129. err = readDb.Raw("SELECT COUNT(*) as count from ("+countSQL+" GROUP BY inspect_date, patient_id) as t where t.projects<?", countParams...).Scan(&counts).Error
  130. if err != nil {
  131. return
  132. }
  133. db := readDb.Table("xt_inspection as i")
  134. if orgID > 0 {
  135. db = db.Where("i.org_id=?", orgID)
  136. }
  137. if projectID > 0 {
  138. db = db.Where("i.project_id=?", projectID)
  139. }
  140. if isStartTime {
  141. db = db.Where("i.inspect_date>=?", startTime)
  142. }
  143. if isEndTime {
  144. db = db.Where("i.inspect_date<=?", endTime)
  145. }
  146. db = db.Where("i.status=1")
  147. if dialysisAge > 0 || lapseto > 0 {
  148. joinString := "JOIN xt_patients as p ON i.patient_id=p.id"
  149. joinParams := make([]interface{}, 0)
  150. if orgID > 0 {
  151. joinString += " and p.user_org_id=?"
  152. joinParams = append(joinParams, orgID)
  153. }
  154. if lapseto > 0 {
  155. joinString += " and p.lapseto=?"
  156. joinParams = append(joinParams, lapseto)
  157. }
  158. if dialysisAge > 0 {
  159. loc, _ := time.LoadLocation("Local")
  160. nowTime := time.Now()
  161. nowDay := nowTime.Format("2006-01-02")
  162. dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
  163. if err != nil {
  164. return inspections, references, patients, counts, total, err
  165. }
  166. timePoint := dayTime.AddDate(0, -3, 0).Unix()
  167. if dialysisAge == 1 {
  168. joinString += " and p.first_dialysis_date<>0 AND p.first_dialysis_date <= ?"
  169. joinParams = append(joinParams, timePoint)
  170. } else {
  171. joinString += " and p.first_dialysis_date<>0 AND p.first_dialysis_date > ?"
  172. joinParams = append(joinParams, timePoint)
  173. }
  174. }
  175. db = db.Joins(joinString, joinParams...)
  176. }
  177. offset := (page - 1) * limit
  178. var ds []*PIInspectionPageStruct
  179. err = db.Select("i.patient_id, i.inspect_date").Group("inspect_date, patient_id").Count(&total).Order("inspect_date desc, i.created_time desc, i.id asc, patient_id asc").Offset(offset).Limit(limit).Find(&ds).Error
  180. if err != nil {
  181. return
  182. }
  183. dates := make([]int64, 0)
  184. ids := make([]int64, 0)
  185. if len(ds) > 0 {
  186. for _, d := range ds {
  187. dates = append(dates, d.InspectDate)
  188. ids = append(ids, d.PatientID)
  189. }
  190. db = db.Where("i.inspect_date IN (?)", dates).Where("i.patient_id IN (?)", ids)
  191. }
  192. err = db.Select("i.id, i.patient_id, org_id, i.project_id, i.item_id, i.item_name, i.project_name, i.inspect_type, i.inspect_value, i.inspect_date, i.status, i.created_time, i.updated_time").Order("inspect_date desc, created_time desc, id asc, patient_id asc").Find(&inspections).Error
  193. if err != nil {
  194. return
  195. }
  196. if len(ids) > 0 {
  197. err = readDb.Table("xt_patients").Where("id IN (?)", ids).Find(&patients).Error
  198. if err != nil {
  199. return
  200. }
  201. }
  202. return
  203. }
  204. func GetOICData(orgID, page, limit, lapseto, dialysisAge, projectID, itemID, startTime, endTime int64, isStartTime, isEndTime bool) (inspections []*models.Inspection, references []*models.InspectionReference, patients []*models.Patients, counts []*PIInspectionProjectCountStruct, total int64, err error) {
  205. err = readDb.Table("xt_inspection_reference").Order("project_id").Find(&references).Error
  206. if err != nil || len(references) == 0 {
  207. return
  208. }
  209. countSQL := "SELECT count(distinct i.project_id) as projects FROM xt_inspection as i "
  210. countParams := make([]interface{}, 0)
  211. countSQL += " JOIN xt_inspection_reference as ir ON ir.id=i.item_id and ir.project_id=i.project_id"
  212. if dialysisAge > 0 || lapseto > 0 {
  213. countSQL += " JOIN xt_patients as p ON i.patient_id=p.id"
  214. if orgID > 0 {
  215. countSQL += " and p.user_org_id=?"
  216. countParams = append(countParams, orgID)
  217. }
  218. if lapseto > 0 {
  219. countSQL += " and p.lapseto=?"
  220. countParams = append(countParams, lapseto)
  221. }
  222. if dialysisAge > 0 {
  223. loc, _ := time.LoadLocation("Local")
  224. nowTime := time.Now()
  225. nowDay := nowTime.Format("2006-01-02")
  226. dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
  227. if err != nil {
  228. return inspections, references, patients, counts, total, err
  229. }
  230. timePoint := dayTime.AddDate(0, -3, 0).Unix()
  231. if dialysisAge == 1 {
  232. countSQL += " and p.first_dialysis_date<>0 AND p.first_dialysis_date <= ?"
  233. countParams = append(countParams, timePoint)
  234. } else {
  235. countSQL += " and p.first_dialysis_date<>0 AND p.first_dialysis_date > ?"
  236. countParams = append(countParams, timePoint)
  237. }
  238. }
  239. }
  240. countSQL += " WHERE i.status=1"
  241. if orgID > 0 {
  242. countSQL += " AND i.org_id=?"
  243. countParams = append(countParams, orgID)
  244. }
  245. if projectID > 0 {
  246. countSQL += " AND i.project_id=?"
  247. countParams = append(countParams, projectID)
  248. }
  249. if isStartTime {
  250. countSQL += " AND i.inspect_date>=?"
  251. countParams = append(countParams, startTime)
  252. }
  253. if isEndTime {
  254. countSQL += " AND i.inspect_date<=?"
  255. countParams = append(countParams, endTime)
  256. }
  257. countSQL += " AND ((i.inspect_type=1 AND (CONVERT(i.inspect_value, DECIMAL)<CONVERT(ir.range_min, DECIMAL) OR CONVERT(i.inspect_value, DECIMAL)>CONVERT(ir.range_max, DECIMAL))) OR (i.inspect_type=2 AND i.inspect_value<>ir.range_value))"
  258. err = readDb.Raw("SELECT COUNT(*) as count from ("+countSQL+" GROUP BY inspect_date, patient_id) as t ", countParams...).Scan(&counts).Error
  259. if err != nil {
  260. return
  261. }
  262. db := readDb.Table("xt_inspection as i")
  263. if orgID > 0 {
  264. db = db.Where("i.org_id=?", orgID)
  265. }
  266. if projectID > 0 {
  267. db = db.Where("i.project_id=?", projectID)
  268. }
  269. if itemID > 0 {
  270. db = db.Where("i.item_id=?", itemID)
  271. }
  272. if isStartTime {
  273. db = db.Where("i.inspect_date>=?", startTime)
  274. }
  275. if isEndTime {
  276. db = db.Where("i.inspect_date<=?", endTime)
  277. }
  278. db = db.Where("i.status=1")
  279. if dialysisAge > 0 || lapseto > 0 {
  280. joinString := "JOIN xt_patients as p ON i.patient_id=p.id"
  281. joinParams := make([]interface{}, 0)
  282. if orgID > 0 {
  283. joinString += " and p.user_org_id=?"
  284. joinParams = append(joinParams, orgID)
  285. }
  286. if lapseto > 0 {
  287. joinString += " and p.lapseto=?"
  288. joinParams = append(joinParams, lapseto)
  289. }
  290. if dialysisAge > 0 {
  291. loc, _ := time.LoadLocation("Local")
  292. nowTime := time.Now()
  293. nowDay := nowTime.Format("2006-01-02")
  294. dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
  295. if err != nil {
  296. return inspections, references, patients, counts, total, err
  297. }
  298. timePoint := dayTime.AddDate(0, -3, 0).Unix()
  299. if dialysisAge == 1 {
  300. joinString += " and p.first_dialysis_date<>0 AND p.first_dialysis_date <= ?"
  301. joinParams = append(joinParams, timePoint)
  302. } else {
  303. joinString += " and p.first_dialysis_date<>0 AND p.first_dialysis_date > ?"
  304. joinParams = append(joinParams, timePoint)
  305. }
  306. }
  307. db = db.Joins(joinString, joinParams...)
  308. }
  309. offset := (page - 1) * limit
  310. var ds []*PIInspectionPageStruct
  311. err = db.Select("i.patient_id, i.inspect_date").Group("inspect_date, patient_id").Count(&total).Order("inspect_date desc, i.created_time desc, i.id asc, patient_id asc").Offset(offset).Limit(limit).Find(&ds).Error
  312. if err != nil {
  313. return
  314. }
  315. dates := make([]int64, 0)
  316. ids := make([]int64, 0)
  317. if len(ds) > 0 {
  318. for _, d := range ds {
  319. dates = append(dates, d.InspectDate)
  320. ids = append(ids, d.PatientID)
  321. }
  322. db = db.Where("i.inspect_date IN (?)", dates).Where("i.patient_id IN (?)", ids)
  323. }
  324. err = db.Select("i.id, i.patient_id, org_id, i.project_id, i.item_id, i.item_name, i.project_name, i.inspect_type, i.inspect_value, i.inspect_date, i.status, i.created_time, i.updated_time").Order("inspect_date desc, created_time desc, id asc, patient_id asc").Find(&inspections).Error
  325. if err != nil {
  326. return
  327. }
  328. if len(ids) > 0 {
  329. err = readDb.Table("xt_patients").Where("id IN (?)", ids).Find(&patients).Error
  330. if err != nil {
  331. return
  332. }
  333. }
  334. return
  335. }
  336. func GetOIQData(orgID, page, limit, projectID, startTime, endTime int64, isStartTime, isEndTime bool, searchKey string) (inspections []*models.Inspection, references []*models.InspectionReference, patients []*models.Patients, total int64, err error) {
  337. err = readDb.Table("xt_inspection_reference").Order("project_id").Find(&references).Error
  338. if err != nil || len(references) == 0 {
  339. return
  340. }
  341. if projectID <= 0 {
  342. projectID = references[0].ProjectId
  343. }
  344. db := readDb.Table("xt_inspection as i")
  345. if orgID > 0 {
  346. db = db.Where("i.org_id=?", orgID)
  347. }
  348. if projectID > 0 {
  349. db = db.Where("i.project_id=?", projectID)
  350. }
  351. if isStartTime {
  352. db = db.Where("i.inspect_date>=?", startTime)
  353. }
  354. if isEndTime {
  355. db = db.Where("i.inspect_date<=?", endTime)
  356. }
  357. if len(searchKey) > 0 {
  358. searchKey = "%" + searchKey + "%"
  359. joinString := "JOIN xt_patients as p ON i.patient_id=p.id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)"
  360. joinParams := make([]interface{}, 0)
  361. joinParams = append(joinParams, searchKey)
  362. joinParams = append(joinParams, searchKey)
  363. db = db.Joins(joinString, joinParams...)
  364. db = db.Where("p.name LIKE ? OR p.dialysis_no LIKE ?", searchKey, searchKey)
  365. }
  366. db = db.Where("i.status=1")
  367. offset := (page - 1) * limit
  368. var ds []*PIInspectionPageStruct
  369. err = db.Select("i.patient_id, i.inspect_date").Group("inspect_date, patient_id").Count(&total).Order("inspect_date desc, i.created_time desc, i.id asc, patient_id asc").Offset(offset).Limit(limit).Find(&ds).Error
  370. if err != nil {
  371. return
  372. }
  373. dates := make([]int64, 0)
  374. ids := make([]int64, 0)
  375. if len(ds) > 0 {
  376. for _, d := range ds {
  377. dates = append(dates, d.InspectDate)
  378. ids = append(ids, d.PatientID)
  379. }
  380. db = db.Where("i.inspect_date IN (?)", dates).Where("i.patient_id IN (?)", ids)
  381. }
  382. err = db.Select("i.id, i.patient_id, org_id, i.project_id, i.item_id, i.item_name, i.project_name, i.inspect_type, i.inspect_value, i.inspect_date, i.status, i.created_time, i.updated_time").Order("inspect_date desc, created_time desc, id asc, patient_id asc").Find(&inspections).Error
  383. if err != nil {
  384. return
  385. }
  386. if len(ids) > 0 {
  387. err = readDb.Table("xt_patients").Where("id IN (?)", ids).Find(&patients).Error
  388. if err != nil {
  389. return
  390. }
  391. }
  392. return
  393. }
  394. func GetPATotalData(orgID, page, limit, lapseto, source, age, startTime, endTime int64, isStartTime, isEndTime bool, searchKey string) (patients []*models.Patients, lapsetoOut, lapsetoIn, total int64, err error) {
  395. db := readDb.Table("xt_patients").Where("status=1")
  396. if orgID > 0 {
  397. db = db.Where("user_org_id=?", orgID)
  398. }
  399. if lapseto > 0 {
  400. db = db.Where("lapseto=?", lapseto)
  401. }
  402. if source > 0 {
  403. if source == 1 || source == 2 {
  404. db = db.Where("source=?", source)
  405. } else {
  406. db = db.Where("source NOT IN (1,2)")
  407. }
  408. }
  409. loc, _ := time.LoadLocation("Local")
  410. nowTime := time.Now()
  411. nowDay := nowTime.Format("2006-01-02")
  412. dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
  413. if err != nil {
  414. return
  415. }
  416. var timeStartPoint int64
  417. var timeEndPoint int64
  418. if age == 1 {
  419. timeStartPoint = dayTime.AddDate(-20, 0, 0).Unix()
  420. timeEndPoint = dayTime.AddDate(-30, 0, 0).Unix()
  421. db = db.Where("birthday>? and birthday<=?", timeEndPoint, timeStartPoint)
  422. } else if age == 2 {
  423. timeStartPoint = dayTime.AddDate(-30, 0, 0).Unix()
  424. timeEndPoint = dayTime.AddDate(-40, 0, 0).Unix()
  425. db = db.Where("birthday>? and birthday<=?", timeEndPoint, timeStartPoint)
  426. } else if age == 3 {
  427. timeStartPoint = dayTime.AddDate(-40, 0, 0).Unix()
  428. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  429. db = db.Where("birthday>? and birthday<=?", timeEndPoint, timeStartPoint)
  430. } else if age == 4 {
  431. timeStartPoint = dayTime.AddDate(-10, 0, 0).Unix()
  432. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  433. db = db.Where("birthday>? or birthday<=?", timeStartPoint, timeEndPoint)
  434. }
  435. if isStartTime {
  436. db = db.Where("created_time>=?", startTime)
  437. }
  438. if isEndTime {
  439. db = db.Where("created_time<=?", endTime)
  440. }
  441. if len(searchKey) > 0 {
  442. searchKey = "%" + searchKey + "%"
  443. db = db.Where("name LIKE ? OR dialysis_no LIKE ? ", searchKey, searchKey)
  444. }
  445. offset := (page - 1) * limit
  446. err = db.Count(&total).Order("id desc").Offset(offset).Limit(limit).Find(&patients).Error
  447. if err != nil {
  448. return
  449. }
  450. err = db.Where("lapseto=1").Count(&lapsetoIn).Error
  451. if err != nil {
  452. return
  453. }
  454. lapsetoOut = total - lapsetoIn
  455. return
  456. }
  457. type PADialysisAgePieDataStruct struct {
  458. Age int64 `json:"age"`
  459. Count int64 `json:"count"`
  460. }
  461. func GetPADialysisAgeData(orgID, page, limit, lapseto, age, dialysisAge, startTime, endTime int64, isStartTime, isEndTime bool) (patients []*models.Patients, counts []*PADialysisAgePieDataStruct, total int64, err error) {
  462. loc, _ := time.LoadLocation("Local")
  463. nowTime := time.Now()
  464. nowDay := nowTime.Format("2006-01-02")
  465. dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
  466. if err != nil {
  467. return
  468. }
  469. var timeStartPoint int64
  470. var timeEndPoint int64
  471. oneYearPoint := dayTime.AddDate(-1, 0, 0).Unix()
  472. fiveYearPoint := dayTime.AddDate(-5, 0, 0).Unix()
  473. tenYearPoint := dayTime.AddDate(-10, 0, 0).Unix()
  474. twentyYearPoint := dayTime.AddDate(-20, 0, 0).Unix()
  475. db := readDb.Table("xt_patients").Where("status=1")
  476. countSQL := "SELECT nnd AS 'age',COUNT(*) AS 'count' FROM(" +
  477. "SELECT " +
  478. "CASE " +
  479. " WHEN first_dialysis_date<>0 AND first_dialysis_date>? THEN '1'" +
  480. " WHEN first_dialysis_date<>0 AND first_dialysis_date>? && first_dialysis_date<=? THEN '2'" +
  481. " WHEN first_dialysis_date<>0 AND first_dialysis_date>? && first_dialysis_date<=? THEN '3'" +
  482. " WHEN first_dialysis_date<>0 AND first_dialysis_date>? && first_dialysis_date<=? THEN '4'" +
  483. " WHEN first_dialysis_date<>0 AND first_dialysis_date<=? THEN '5'" +
  484. " ELSE '0'" +
  485. "END AS nnd FROM xt_patients WHERE status=1 "
  486. countParams := make([]interface{}, 0)
  487. countParams = append(countParams, oneYearPoint)
  488. countParams = append(countParams, fiveYearPoint)
  489. countParams = append(countParams, oneYearPoint)
  490. countParams = append(countParams, tenYearPoint)
  491. countParams = append(countParams, fiveYearPoint)
  492. countParams = append(countParams, twentyYearPoint)
  493. countParams = append(countParams, tenYearPoint)
  494. countParams = append(countParams, twentyYearPoint)
  495. if orgID > 0 {
  496. db = db.Where("user_org_id=?", orgID)
  497. countSQL += " AND user_org_id=?"
  498. countParams = append(countParams, orgID)
  499. }
  500. if lapseto > 0 {
  501. db = db.Where("lapseto=?", lapseto)
  502. countSQL += " AND lapseto=?"
  503. countParams = append(countParams, lapseto)
  504. }
  505. if age == 1 {
  506. timeStartPoint = dayTime.AddDate(-20, 0, 0).Unix()
  507. timeEndPoint = dayTime.AddDate(-30, 0, 0).Unix()
  508. db = db.Where("birthday>? and birthday<=?", timeEndPoint, timeStartPoint)
  509. countSQL += " AND (birthday>? and birthday<=?)"
  510. countParams = append(countParams, timeEndPoint)
  511. countParams = append(countParams, timeStartPoint)
  512. } else if age == 2 {
  513. timeStartPoint = dayTime.AddDate(-30, 0, 0).Unix()
  514. timeEndPoint = dayTime.AddDate(-40, 0, 0).Unix()
  515. db = db.Where("birthday>? and birthday<=?", timeEndPoint, timeStartPoint)
  516. countSQL += " AND (birthday>? and birthday<=?)"
  517. countParams = append(countParams, timeEndPoint)
  518. countParams = append(countParams, timeStartPoint)
  519. } else if age == 3 {
  520. timeStartPoint = dayTime.AddDate(-40, 0, 0).Unix()
  521. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  522. db = db.Where("birthday>? and birthday<=?", timeEndPoint, timeStartPoint)
  523. countSQL += " AND (birthday>? and birthday<=?)"
  524. countParams = append(countParams, timeEndPoint)
  525. countParams = append(countParams, timeStartPoint)
  526. } else if age == 4 {
  527. timeStartPoint = dayTime.AddDate(-10, 0, 0).Unix()
  528. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  529. db = db.Where("birthday>? or birthday<=?", timeStartPoint, timeEndPoint)
  530. countSQL += " AND (birthday>? or birthday<=?)"
  531. countParams = append(countParams, timeStartPoint)
  532. countParams = append(countParams, timeEndPoint)
  533. }
  534. if dialysisAge == 1 {
  535. timeStartPoint = dayTime.AddDate(-1, 0, 0).Unix()
  536. db = db.Where("first_dialysis_date>?", timeStartPoint)
  537. db = db.Where("first_dialysis_date<>0")
  538. countSQL += " AND (first_dialysis_date<>0 AND first_dialysis_date>?)"
  539. countParams = append(countParams, timeStartPoint)
  540. } else if dialysisAge == 2 {
  541. timeStartPoint = dayTime.AddDate(-1, 0, 0).Unix()
  542. timeEndPoint = dayTime.AddDate(-5, 0, 0).Unix()
  543. db = db.Where("first_dialysis_date<>0")
  544. db = db.Where("first_dialysis_date>? and first_dialysis_date<=?", timeEndPoint, timeStartPoint)
  545. countSQL += " AND (first_dialysis_date<>0 AND first_dialysis_date>? and first_dialysis_date<=?)"
  546. countParams = append(countParams, timeEndPoint)
  547. countParams = append(countParams, timeStartPoint)
  548. } else if dialysisAge == 3 {
  549. timeStartPoint = dayTime.AddDate(-5, 0, 0).Unix()
  550. timeEndPoint = dayTime.AddDate(-10, 0, 0).Unix()
  551. db = db.Where("first_dialysis_date<>0")
  552. db = db.Where("first_dialysis_date>? and first_dialysis_date<=?", timeEndPoint, timeStartPoint)
  553. countSQL += " AND (first_dialysis_date<>0 AND first_dialysis_date>? and first_dialysis_date<=?)"
  554. countParams = append(countParams, timeEndPoint)
  555. countParams = append(countParams, timeStartPoint)
  556. } else if dialysisAge == 4 {
  557. timeStartPoint = dayTime.AddDate(-10, 0, 0).Unix()
  558. timeEndPoint = dayTime.AddDate(-20, 0, 0).Unix()
  559. db = db.Where("first_dialysis_date<>0")
  560. db = db.Where("first_dialysis_date>? and first_dialysis_date<=?", timeEndPoint, timeStartPoint)
  561. countSQL += " AND (first_dialysis_date<>0 AND first_dialysis_date>? and first_dialysis_date<=?)"
  562. countParams = append(countParams, timeEndPoint)
  563. countParams = append(countParams, timeStartPoint)
  564. } else if dialysisAge == 5 {
  565. timeStartPoint = dayTime.AddDate(-20, 0, 0).Unix()
  566. db = db.Where("first_dialysis_date<>0")
  567. db = db.Where("first_dialysis_date<=?", timeStartPoint)
  568. countSQL += " AND (first_dialysis_date<>0 AND first_dialysis_date<=?)"
  569. countParams = append(countParams, timeStartPoint)
  570. }
  571. if isStartTime {
  572. db = db.Where("created_time>=?", startTime)
  573. countSQL += " AND (created_time>=?)"
  574. countParams = append(countParams, startTime)
  575. }
  576. if isEndTime {
  577. db = db.Where("created_time<=?", endTime)
  578. countSQL += " AND (created_time<=?)"
  579. countParams = append(countParams, endTime)
  580. }
  581. countSQL += ")a GROUP BY nnd"
  582. err = readDb.Raw(countSQL, countParams...).Scan(&counts).Error
  583. if err != nil {
  584. return
  585. }
  586. offset := (page - 1) * limit
  587. err = db.Count(&total).Order("id desc").Offset(offset).Limit(limit).Find(&patients).Error
  588. if err != nil {
  589. return
  590. }
  591. return
  592. }
  593. type PAWeightDataStruct struct {
  594. models.Schedule
  595. Patient *models.Patients `json:"patient" gorm:"foreignkey:PatientId;"`
  596. AssessmentAfterDislysis *models.AssessmentAfterDislysis `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"assessment_after_dislysis"`
  597. AssessmentBeforeDislysis *models.PredialysisEvaluation `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"assessment_before_dislysis"`
  598. }
  599. type PAWeightPieDataStruct struct {
  600. Weight int64 `json:"weight"`
  601. Count int64 `json:"count"`
  602. }
  603. func (PAWeightDataStruct) TableName() string {
  604. return "xt_schedule"
  605. }
  606. func GetPAWeightData(orgID, page, limit, lapseto, age, startTime, endTime int64, isStartTime, isEndTime bool) (weight []*PAWeightDataStruct, dryweight []*PAWeightPieDataStruct, weightincrease []*PAWeightPieDataStruct, afterweight []*PAWeightPieDataStruct, total int64, err error) {
  607. loc, _ := time.LoadLocation("Local")
  608. nowTime := time.Now()
  609. nowDay := nowTime.Format("2006-01-02")
  610. dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
  611. if err != nil {
  612. return
  613. }
  614. var timeStartPoint int64
  615. var timeEndPoint int64
  616. dryCountSQL := "SELECT nnd AS 'weight',COUNT(*) AS 'count' FROM(" +
  617. "SELECT " +
  618. "CASE " +
  619. " WHEN abd.dry_weight<>0 and abd.dry_weight<40 THEN '1'" +
  620. " WHEN abd.dry_weight>=40 && abd.dry_weight<50 THEN '2'" +
  621. " WHEN abd.dry_weight>=50 && abd.dry_weight<60 THEN '3'" +
  622. " WHEN abd.dry_weight>=60 && abd.dry_weight<70 THEN '4'" +
  623. " WHEN abd.dry_weight>=70 THEN '5'" +
  624. " ELSE '0'" +
  625. "END AS nnd FROM xt_assessment_before_dislysis as abd JOIN xt_patients as p ON p.id=abd.patient_id and p.user_org_id=? WHERE p.status=1"
  626. dryCountParams := make([]interface{}, 0)
  627. dryCountParams = append(dryCountParams, orgID)
  628. increaseCountSQL := "SELECT nnd AS 'weight',COUNT(*) AS 'count' FROM(" +
  629. "SELECT " +
  630. "CASE " +
  631. " WHEN (abd.weight_before-abd.dry_weight)<1 THEN '1'" +
  632. " WHEN (abd.weight_before-abd.dry_weight)>=1 && (abd.weight_before-abd.dry_weight)<2 THEN '2'" +
  633. " WHEN (abd.weight_before-abd.dry_weight)>=2 && (abd.weight_before-abd.dry_weight)<3 THEN '3'" +
  634. " WHEN (abd.weight_before-abd.dry_weight)>=3 && (abd.weight_before-abd.dry_weight)<4 THEN '4'" +
  635. " WHEN (abd.weight_before-abd.dry_weight)>=4 THEN '5'" +
  636. " ELSE '0'" +
  637. "END AS nnd FROM xt_assessment_before_dislysis as abd JOIN xt_patients as p ON p.id=abd.patient_id and p.user_org_id=? WHERE p.status=1"
  638. increaseCountParams := make([]interface{}, 0)
  639. increaseCountParams = append(increaseCountParams, orgID)
  640. afterCountSQL := "SELECT nnd AS 'weight',COUNT(*) AS 'count' FROM(" +
  641. "SELECT " +
  642. "CASE " +
  643. " WHEN afd.weight_after<>0 and afd.weight_after<40 THEN '1'" +
  644. " WHEN afd.weight_after>=40 && afd.weight_after<50 THEN '2'" +
  645. " WHEN afd.weight_after>=50 && afd.weight_after<60 THEN '3'" +
  646. " WHEN afd.weight_after>=60 && afd.weight_after<70 THEN '4'" +
  647. " WHEN afd.weight_after>=70 THEN '5'" +
  648. " ELSE '0'" +
  649. "END AS nnd FROM xt_assessment_after_dislysis as afd JOIN xt_patients as p ON p.id=afd.patient_id and p.user_org_id=? WHERE p.status=1"
  650. afterCountParams := make([]interface{}, 0)
  651. afterCountParams = append(afterCountParams, orgID)
  652. db := readDb.Table("xt_schedule as c").Joins("JOIN xt_patients as p ON p.id=c.patient_id and p.user_org_id=?", orgID)
  653. if lapseto > 0 {
  654. db = db.Where("p.lapseto=?", lapseto)
  655. dryCountSQL += " AND p.lapseto=?"
  656. dryCountParams = append(dryCountParams, lapseto)
  657. increaseCountSQL += " AND p.lapseto=?"
  658. increaseCountParams = append(increaseCountParams, lapseto)
  659. afterCountSQL += " AND p.lapseto=?"
  660. afterCountParams = append(afterCountParams, lapseto)
  661. }
  662. if age == 1 {
  663. timeStartPoint = dayTime.AddDate(-20, 0, 0).Unix()
  664. timeEndPoint = dayTime.AddDate(-30, 0, 0).Unix()
  665. db = db.Where("p.birthday>? and p.birthday<=?", timeEndPoint, timeStartPoint)
  666. dryCountSQL += " AND p.birthday>? and p.birthday<=?"
  667. dryCountParams = append(dryCountParams, timeEndPoint, timeStartPoint)
  668. increaseCountSQL += " AND p.birthday>? and p.birthday<=?"
  669. increaseCountParams = append(increaseCountParams, timeEndPoint, timeStartPoint)
  670. afterCountSQL += " AND p.birthday>? and p.birthday<=?"
  671. afterCountParams = append(afterCountParams, timeEndPoint, timeStartPoint)
  672. } else if age == 2 {
  673. timeStartPoint = dayTime.AddDate(-30, 0, 0).Unix()
  674. timeEndPoint = dayTime.AddDate(-40, 0, 0).Unix()
  675. db = db.Where("p.birthday>? and p.birthday<=?", timeEndPoint, timeStartPoint)
  676. dryCountSQL += " AND p.birthday>? and p.birthday<=?"
  677. dryCountParams = append(dryCountParams, timeEndPoint, timeStartPoint)
  678. increaseCountSQL += " AND p.birthday>? and p.birthday<=?"
  679. increaseCountParams = append(increaseCountParams, timeEndPoint, timeStartPoint)
  680. afterCountSQL += " AND p.birthday>? and p.birthday<=?"
  681. afterCountParams = append(afterCountParams, timeEndPoint, timeStartPoint)
  682. } else if age == 3 {
  683. timeStartPoint = dayTime.AddDate(-40, 0, 0).Unix()
  684. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  685. db = db.Where("p.birthday>? and p.birthday<=?", timeEndPoint, timeStartPoint)
  686. dryCountSQL += " AND p.birthday>? and p.birthday<=?"
  687. dryCountParams = append(dryCountParams, timeEndPoint, timeStartPoint)
  688. increaseCountSQL += " AND p.birthday>? and p.birthday<=?"
  689. increaseCountParams = append(increaseCountParams, timeEndPoint, timeStartPoint)
  690. afterCountSQL += " AND p.birthday>? and p.birthday<=?"
  691. afterCountParams = append(afterCountParams, timeEndPoint, timeStartPoint)
  692. } else if age == 4 {
  693. timeStartPoint = dayTime.AddDate(-10, 0, 0).Unix()
  694. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  695. db = db.Where("p.birthday>? or p.birthday<=?", timeStartPoint, timeEndPoint)
  696. dryCountSQL += " AND (p.birthday>? or p.birthday<=?)"
  697. dryCountParams = append(dryCountParams, timeStartPoint, timeEndPoint)
  698. increaseCountSQL += " AND (p.birthday>? or p.birthday<=?)"
  699. increaseCountParams = append(increaseCountParams, timeStartPoint, timeEndPoint)
  700. afterCountSQL += " AND (p.birthday>? or p.birthday<=?)"
  701. afterCountParams = append(afterCountParams, timeStartPoint, timeEndPoint)
  702. }
  703. if isStartTime {
  704. db = db.Where("c.schedule_date>=?", startTime)
  705. dryCountSQL += " AND abd.assessment_date>=?"
  706. dryCountParams = append(dryCountParams, startTime)
  707. increaseCountSQL += " AND abd.assessment_date>=?"
  708. increaseCountParams = append(increaseCountParams, startTime)
  709. afterCountSQL += " AND afd.assessment_date>=?"
  710. afterCountParams = append(afterCountParams, startTime)
  711. }
  712. if isEndTime {
  713. db = db.Where("c.schedule_date<=?", endTime)
  714. dryCountSQL += " AND abd.assessment_date<=?"
  715. dryCountParams = append(dryCountParams, endTime)
  716. increaseCountSQL += " AND abd.assessment_date<=?"
  717. increaseCountParams = append(increaseCountParams, endTime)
  718. afterCountSQL += " AND afd.assessment_date<=?"
  719. afterCountParams = append(afterCountParams, endTime)
  720. }
  721. dryCountSQL += ")a GROUP BY nnd"
  722. err = readDb.Raw(dryCountSQL, dryCountParams...).Scan(&dryweight).Error
  723. if err != nil {
  724. return
  725. }
  726. increaseCountSQL += ")a GROUP BY nnd"
  727. err = readDb.Raw(increaseCountSQL, increaseCountParams...).Scan(&weightincrease).Error
  728. if err != nil {
  729. return
  730. }
  731. afterCountSQL += ")a GROUP BY nnd"
  732. err = readDb.Raw(afterCountSQL, afterCountParams...).Scan(&afterweight).Error
  733. if err != nil {
  734. return
  735. }
  736. db = db.Where("c.status=1")
  737. offset := (page - 1) * limit
  738. err = db.Select("c.id, c.user_org_id, c.partition_id, c.bed_id, c.patient_id, c.schedule_date, c.schedule_type, c.schedule_week, c.mode_id, c.status, c.created_time, c.updated_time").
  739. Preload("Patient", "user_org_id = ? AND status = 1", orgID).
  740. Preload("AssessmentAfterDislysis", "user_org_id = ? AND status = 1", orgID).
  741. Preload("AssessmentBeforeDislysis", "user_org_id = ? AND status = 1", orgID).
  742. Order("c.schedule_date desc, c.schedule_type desc, c.id desc").Count(&total).Offset(offset).Limit(limit).Find(&weight).Error
  743. return
  744. }
  745. type PABloodPressureDataStruct struct {
  746. models.Schedule
  747. Patient *models.Patients `json:"patient" gorm:"foreignkey:PatientId;"`
  748. AssessmentAfterDislysis *models.AssessmentAfterDislysis `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"assessment_after_dislysis"`
  749. AssessmentBeforeDislysis *models.PredialysisEvaluation `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"assessment_before_dislysis"`
  750. MonitoringRecord []*models.MonitoringRecord `gorm:"ForeignKey:MonitoringDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"monitor_records"`
  751. }
  752. func (PABloodPressureDataStruct) TableName() string {
  753. return "xt_schedule"
  754. }
  755. type PABloodPressurePieDataStruct struct {
  756. Blood int64 `json:"blood"`
  757. Count int64 `json:"count"`
  758. }
  759. func GetPABloodPressureData(orgID, page, limit, lapseto, dialysisStage, startTime, endTime int64, isStartTime, isEndTime bool) (bloodPressures []*PABloodPressureDataStruct, bps []*PABloodPressurePieDataStruct, total int64, err error) {
  760. db := readDb.Table("xt_schedule as c").Joins("JOIN xt_patients as p ON p.id=c.patient_id and p.user_org_id=?", orgID)
  761. countSQL := "SELECT lmr.* from xt_monitoring_record as lmr JOIN xt_patients as p ON p.id=lmr.patient_id and p.user_org_id=? "
  762. countParams := make([]interface{}, 0)
  763. countParams = append(countParams, orgID)
  764. if dialysisStage == 1 {
  765. db = db.Joins("JOIN xt_assessment_before_dislysis as abd ON abd.patient_id=c.patient_id and abd.assessment_date=c.schedule_date and abd.user_org_id=?", orgID)
  766. countSQL += "JOIN xt_assessment_before_dislysis as abd ON abd.patient_id=lmr.patient_id and abd.assessment_date=lmr.monitoring_date and abd.user_org_id=?"
  767. countParams = append(countParams, orgID)
  768. } else if dialysisStage == 2 {
  769. db = db.Joins("JOIN xt_assessment_after_dislysis as aad ON aad.patient_id=c.patient_id and aad.assessment_date=c.schedule_date and aad.user_org_id=?", orgID)
  770. countSQL += "JOIN xt_assessment_after_dislysis as aad ON aad.patient_id=lmr.patient_id and aad.assessment_date=lmr.monitoring_date and aad.user_org_id=?"
  771. countParams = append(countParams, orgID)
  772. }
  773. countSQL += " WHERE lmr.user_org_id=? AND lmr.status=1"
  774. countParams = append(countParams, orgID)
  775. if lapseto > 0 {
  776. db = db.Where("p.lapseto=?", lapseto)
  777. countSQL += " AND p.lapseto=?"
  778. countParams = append(countParams, lapseto)
  779. }
  780. if isStartTime {
  781. db = db.Where("c.schedule_date>=?", startTime)
  782. countSQL += " AND lmr.monitoring_date>=?"
  783. countParams = append(countParams, startTime)
  784. }
  785. if isEndTime {
  786. db = db.Where("c.schedule_date<=?", endTime)
  787. countSQL += " AND lmr.monitoring_date<=?"
  788. countParams = append(countParams, endTime)
  789. }
  790. countSQL += " order by lmr.systolic_blood_pressure desc, lmr.diastolic_blood_pressure desc"
  791. lastSQL := "SELECT nnd as 'blood' ,COUNT(*) AS 'count' FROM(" +
  792. "SELECT " +
  793. "CASE " +
  794. " WHEN systolic_blood_pressure<>0 && diastolic_blood_pressure <> 0 && systolic_blood_pressure<140 && diastolic_blood_pressure<90 THEN '1'" +
  795. " WHEN systolic_blood_pressure>=160 && diastolic_blood_pressure>=100 THEN '2'" +
  796. " WHEN systolic_blood_pressure<160 && systolic_blood_pressure>=140 && diastolic_blood_pressure<100 && diastolic_blood_pressure>=90 THEN '3'" +
  797. " ELSE '0' " +
  798. "END AS nnd FROM (" + countSQL + ") b group by monitoring_date, patient_id)a GROUP BY nnd"
  799. err = readDb.Raw(lastSQL, countParams...).Find(&bps).Error
  800. if err != nil {
  801. return
  802. }
  803. db = db.Where("c.status=1")
  804. offset := (page - 1) * limit
  805. err = db.Select("c.id, c.user_org_id, c.partition_id, c.bed_id, c.patient_id, c.schedule_date, c.schedule_type, c.schedule_week, c.mode_id, c.status, c.created_time, c.updated_time").
  806. Preload("Patient", "user_org_id = ? AND status = 1", orgID).
  807. Preload("AssessmentAfterDislysis", "user_org_id = ? AND status = 1", orgID).
  808. Preload("AssessmentBeforeDislysis", "user_org_id = ? AND status = 1", orgID).
  809. Preload("MonitoringRecord", "user_org_id = ? AND status = 1", orgID).
  810. Order("c.schedule_date desc, c.schedule_type desc, c.id desc").Count(&total).Offset(offset).Limit(limit).Find(&bloodPressures).Error
  811. return
  812. }
  813. type PAComplicationDataStruct struct {
  814. models.Schedule
  815. Patient *models.Patients `json:"patient" gorm:"foreignkey:PatientId;"`
  816. AssessmentAfterDislysis *models.AssessmentAfterDislysis `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"assessment_after_dislysis"`
  817. AssessmentBeforeDislysis *models.PredialysisEvaluation `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"assessment_before_dislysis"`
  818. }
  819. type PAComplicationPieDataStruct struct {
  820. Complication int64 `json:"complication"`
  821. Name string `json:"name"`
  822. Count int64 `json:"count"`
  823. }
  824. func GetPAComplicationData(orgID, page, limit, age, statisticalState, startTime, endTime int64, isStartTime, isEndTime bool) (complications []*PAComplicationDataStruct, pcps []*PAComplicationPieDataStruct, total int64, err error) {
  825. db := readDb.Table("xt_schedule as c").Joins("JOIN xt_patients as p ON p.id=c.patient_id and p.user_org_id=?", orgID)
  826. if statisticalState == 1 {
  827. db = db.Joins("JOIN xt_assessment_before_dislysis as abd ON abd.patient_id=c.patient_id and abd.assessment_date=c.schedule_date and abd.user_org_id=?", orgID)
  828. } else if statisticalState == 2 {
  829. db = db.Joins("JOIN xt_assessment_after_dislysis as aad ON aad.patient_id=c.patient_id and aad.assessment_date=c.schedule_date and aad.user_org_id=?", orgID)
  830. } else {
  831. return
  832. }
  833. var dc models.Dataconfig
  834. err = readDb.Model(&models.Dataconfig{}).Where("module='hemodialysis' AND field_name='complication'").First(&dc).Error
  835. if err != nil {
  836. return
  837. }
  838. ComplicationsMap := make([]map[string]interface{}, 0)
  839. if dc.ID > 0 {
  840. var dcs []*models.Dataconfig
  841. err = readDb.Model(&models.Dataconfig{}).Where("parent_id=? and org_id in (0,?) and status = 1 ", dc.ID, orgID).Order("orders desc, id asc").Find(&dcs).Error
  842. if err != nil {
  843. return
  844. }
  845. if len(dcs) > 0 {
  846. for _, item := range dcs {
  847. mapItem := map[string]interface{}{"id": item.ID, "name": item.Name}
  848. ComplicationsMap = append(ComplicationsMap, mapItem)
  849. }
  850. }
  851. }
  852. sqlMap := make(map[int64]string, 0)
  853. countParamsMap := make(map[int64][]interface{}, 0)
  854. for _, citem := range ComplicationsMap {
  855. mapID := citem["id"].(int64)
  856. mapName := citem["name"].(string)
  857. countParamsMap[mapID] = make([]interface{}, 0)
  858. sqlMap[mapID] = fmt.Sprintf(" SELECT COUNT(ad.id) as count, '%d' AS complication, '%s' AS `name` FROM ", mapID, mapName)
  859. if statisticalState == 1 {
  860. sqlMap[mapID] += "xt_assessment_before_dislysis AS ad"
  861. } else {
  862. sqlMap[mapID] += "xt_assessment_after_dislysis AS ad"
  863. }
  864. likeKeyMap := make([]interface{}, 0)
  865. likeKeyMap = append(likeKeyMap, mapName)
  866. likeKeyMap = append(likeKeyMap, mapName+",%")
  867. likeKeyMap = append(likeKeyMap, "%,"+mapName)
  868. likeKeyMap = append(likeKeyMap, "%,"+mapName+",%")
  869. sqlMap[mapID] += " JOIN xt_patients as p ON p.id=ad.patient_id and p.user_org_id=? WHERE ad.user_org_id=? AND (complication = ? OR complication LIKE ? OR complication LIKE ? OR complication LIKE ?)"
  870. countParamsMap[mapID] = append(countParamsMap[mapID], orgID)
  871. countParamsMap[mapID] = append(countParamsMap[mapID], orgID)
  872. countParamsMap[mapID] = append(countParamsMap[mapID], likeKeyMap...)
  873. }
  874. loc, _ := time.LoadLocation("Local")
  875. nowTime := time.Now()
  876. nowDay := nowTime.Format("2006-01-02")
  877. dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
  878. if err != nil {
  879. return
  880. }
  881. var timeStartPoint int64
  882. var timeEndPoint int64
  883. if age == 1 {
  884. timeStartPoint = dayTime.AddDate(-20, 0, 0).Unix()
  885. timeEndPoint = dayTime.AddDate(-30, 0, 0).Unix()
  886. db = db.Where("p.birthday>? and p.birthday<=?", timeEndPoint, timeStartPoint)
  887. for key := range sqlMap {
  888. sqlMap[key] += " AND p.birthday>? and p.birthday<=?"
  889. countParamsMap[key] = append(countParamsMap[key], timeEndPoint)
  890. countParamsMap[key] = append(countParamsMap[key], timeStartPoint)
  891. }
  892. } else if age == 2 {
  893. timeStartPoint = dayTime.AddDate(-30, 0, 0).Unix()
  894. timeEndPoint = dayTime.AddDate(-40, 0, 0).Unix()
  895. db = db.Where("p.birthday>? and p.birthday<=?", timeEndPoint, timeStartPoint)
  896. for key := range sqlMap {
  897. sqlMap[key] += " AND p.birthday>? and p.birthday<=?"
  898. countParamsMap[key] = append(countParamsMap[key], timeEndPoint)
  899. countParamsMap[key] = append(countParamsMap[key], timeStartPoint)
  900. }
  901. } else if age == 3 {
  902. timeStartPoint = dayTime.AddDate(-40, 0, 0).Unix()
  903. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  904. db = db.Where("p.birthday>? and p.birthday<=?", timeEndPoint, timeStartPoint)
  905. for key := range sqlMap {
  906. sqlMap[key] += " AND p.birthday>? and p.birthday<=?"
  907. countParamsMap[key] = append(countParamsMap[key], timeEndPoint)
  908. countParamsMap[key] = append(countParamsMap[key], timeStartPoint)
  909. }
  910. } else if age == 4 {
  911. timeStartPoint = dayTime.AddDate(-10, 0, 0).Unix()
  912. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  913. db = db.Where("p.birthday>? or p.birthday<=?", timeStartPoint, timeEndPoint)
  914. for key := range sqlMap {
  915. sqlMap[key] += " AND (p.birthday>? or p.birthday<=?)"
  916. countParamsMap[key] = append(countParamsMap[key], timeStartPoint)
  917. countParamsMap[key] = append(countParamsMap[key], timeEndPoint)
  918. }
  919. }
  920. if isStartTime {
  921. db = db.Where("c.schedule_date>=?", startTime)
  922. for key := range sqlMap {
  923. sqlMap[key] += " AND ad.assessment_date>=?"
  924. countParamsMap[key] = append(countParamsMap[key], startTime)
  925. }
  926. }
  927. if isEndTime {
  928. db = db.Where("c.schedule_date<=?", endTime)
  929. for key := range sqlMap {
  930. sqlMap[key] += " AND ad.assessment_date<=?"
  931. countParamsMap[key] = append(countParamsMap[key], endTime)
  932. }
  933. }
  934. db = db.Where("c.status=1")
  935. for key := range sqlMap {
  936. sqlMap[key] += " AND ad.status=1"
  937. var pcp []*PAComplicationPieDataStruct
  938. err = readDb.Raw(sqlMap[key], countParamsMap[key]...).Find(&pcp).Error
  939. if err != nil {
  940. return
  941. }
  942. for _, item := range pcp {
  943. pcps = append(pcps, item)
  944. }
  945. }
  946. offset := (page - 1) * limit
  947. err = db.Select("c.id, c.user_org_id, c.partition_id, c.bed_id, c.patient_id, c.schedule_date, c.schedule_type, c.schedule_week, c.mode_id, c.status, c.created_time, c.updated_time").
  948. Preload("Patient", "user_org_id = ? AND status = 1", orgID).
  949. Preload("AssessmentAfterDislysis", "user_org_id = ? AND status = 1", orgID).
  950. Preload("AssessmentBeforeDislysis", "user_org_id = ? AND status = 1", orgID).
  951. Order("c.schedule_date desc, c.schedule_type desc, c.id desc").Count(&total).Offset(offset).Limit(limit).Find(&complications).Error
  952. return
  953. }
  954. type PADiseasesCountPieStruct struct {
  955. Diseases int64 `json:"diseases"`
  956. Count int64 `json:"count"`
  957. }
  958. func GetPAInfectiousDiseasesData(orgID, page, limit, lapseto, age, diseasesID, startTime, endTime int64, isStartTime, isEndTime bool) (patients []*models.Patients, dsp []*PADiseasesCountPieStruct, total int64, err error) {
  959. loc, _ := time.LoadLocation("Local")
  960. nowTime := time.Now()
  961. nowDay := nowTime.Format("2006-01-02")
  962. dayTime, err := time.ParseInLocation("2006-01-02 15:04:05", nowDay+" 00:00:00", loc)
  963. if err != nil {
  964. return
  965. }
  966. var timeStartPoint int64
  967. var timeEndPoint int64
  968. db := readDb.Table("xt_patients as p")
  969. countSQL := "SELECT count(pid.id) as count, pid.disease_id as diseases FROM xt_patients as p JOIN xt_patients_infectious_diseases as pid ON pid.patient_id=p.id"
  970. countParams := make([]interface{}, 0)
  971. if diseasesID > 0 {
  972. db = db.Joins("JOIN xt_patients_infectious_diseases as pid ON pid.patient_id=p.id AND pid.disease_id=? AND pid.status=1", diseasesID)
  973. countSQL += " AND pid.disease_id=? "
  974. countParams = append(countParams, diseasesID)
  975. }
  976. countSQL += " AND pid.status=1 WHERE p.user_org_id = ? AND p.status=1"
  977. countParams = append(countParams, orgID)
  978. db = db.Where("p.user_org_id = ? AND p.status=1", orgID)
  979. if lapseto > 0 {
  980. db = db.Where("p.lapseto=?", lapseto)
  981. countSQL += " AND p.lapseto=?"
  982. countParams = append(countParams, lapseto)
  983. }
  984. if age == 1 {
  985. timeStartPoint = dayTime.AddDate(-20, 0, 0).Unix()
  986. timeEndPoint = dayTime.AddDate(-30, 0, 0).Unix()
  987. db = db.Where("p.birthday>? and p.birthday<=?", timeEndPoint, timeStartPoint)
  988. countSQL += " AND p.birthday>? and p.birthday<=?"
  989. countParams = append(countParams, timeEndPoint)
  990. countParams = append(countParams, timeStartPoint)
  991. } else if age == 2 {
  992. timeStartPoint = dayTime.AddDate(-30, 0, 0).Unix()
  993. timeEndPoint = dayTime.AddDate(-40, 0, 0).Unix()
  994. db = db.Where("p.birthday>? and p.birthday<=?", timeEndPoint, timeStartPoint)
  995. countSQL += " AND p.birthday>? and p.birthday<=?"
  996. countParams = append(countParams, timeEndPoint)
  997. countParams = append(countParams, timeStartPoint)
  998. } else if age == 3 {
  999. timeStartPoint = dayTime.AddDate(-40, 0, 0).Unix()
  1000. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  1001. db = db.Where("p.birthday>? and p.birthday<=?", timeEndPoint, timeStartPoint)
  1002. countSQL += " AND p.birthday>? and p.birthday<=?"
  1003. countParams = append(countParams, timeEndPoint)
  1004. countParams = append(countParams, timeStartPoint)
  1005. } else if age == 4 {
  1006. timeStartPoint = dayTime.AddDate(-10, 0, 0).Unix()
  1007. timeEndPoint = dayTime.AddDate(-50, 0, 0).Unix()
  1008. db = db.Where("p.birthday>? or p.birthday<=?", timeStartPoint, timeEndPoint)
  1009. countSQL += " AND (p.birthday>? or p.birthday<=?)"
  1010. countParams = append(countParams, timeStartPoint)
  1011. countParams = append(countParams, timeEndPoint)
  1012. }
  1013. if isStartTime {
  1014. db = db.Where("p.created_time>=?", startTime)
  1015. countSQL += " AND p.created_time>=?"
  1016. countParams = append(countParams, startTime)
  1017. }
  1018. if isEndTime {
  1019. db = db.Where("p.created_time<=?", endTime)
  1020. countSQL += " AND p.created_time<=?"
  1021. countParams = append(countParams, endTime)
  1022. }
  1023. countSQL += " GROUP BY pid.disease_id"
  1024. err = readDb.Raw(countSQL, countParams...).Find(&dsp).Error
  1025. if err != nil {
  1026. return
  1027. }
  1028. offset := (page - 1) * limit
  1029. err = db.Select("p.id, p.user_org_id, p.user_id, p.avatar, 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.nation, p.native_place, 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.predialysis_condition, p.pre_hospital_dialysis_frequency, p.pre_hospital_dialysis_times, p.hospital_first_dialysis_date, 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.patient_complains, p.present_history, p.past_history, p.temperature, p.pulse, p.respiratory, p.sbp, p.dbp, p.status, p.created_time, p.updated_time").
  1030. Preload("Contagions", "status = 1").
  1031. Count(&total).Order("p.id desc").Offset(offset).Limit(limit).Find(&patients).Error
  1032. if err != nil {
  1033. return
  1034. }
  1035. return
  1036. }
  1037. func GetPersonLapsetoData(orgID, patientID, page, limit int64) (*models.Patients, []*models.PatientLapseto, int64, error) {
  1038. var patient models.Patients
  1039. var pls []*models.PatientLapseto
  1040. var total int64
  1041. var err error
  1042. err = readDb.Model(&models.Patients{}).Where("user_org_id =? and id=?", orgID, patientID).First(&patient).Error
  1043. if patient.ID == 0 {
  1044. return nil, pls, total, err
  1045. }
  1046. offset := (page - 1) * limit
  1047. err = readDb.Model(&models.PatientLapseto{}).Where("patient_id=? and status=1", patientID).Count(&total).Offset(offset).Limit(limit).Order("lapseto_time desc, id desc").Find(&pls).Error
  1048. if err != nil {
  1049. return nil, pls, total, err
  1050. }
  1051. return &patient, pls, total, err
  1052. }
  1053. func GetPersonWeightData(orgID, patientID, page, limit, startTime, endTime int64, isStartTime, isEndTime bool) (*models.Patients, []*PAWeightDataStruct, []*PAWeightDataStruct, int64, error) {
  1054. var patient models.Patients
  1055. var weight []*PAWeightDataStruct
  1056. var pie []*PAWeightDataStruct
  1057. var total int64
  1058. var err error
  1059. err = readDb.Model(&models.Patients{}).Where("user_org_id =? and id=?", orgID, patientID).First(&patient).Error
  1060. if patient.ID == 0 {
  1061. return nil, weight, pie, total, err
  1062. }
  1063. db := readDb.Table("xt_schedule as c").Joins("JOIN xt_patients as p ON p.id=c.patient_id and p.user_org_id=? and p.id=?", orgID, patientID)
  1064. db = db.Where("c.patient_id=?", patientID)
  1065. if isStartTime {
  1066. db = db.Where("c.schedule_date>=?", startTime)
  1067. }
  1068. if isEndTime {
  1069. db = db.Where("c.schedule_date<=?", endTime)
  1070. }
  1071. db = db.Where("c.status=1")
  1072. offset := (page - 1) * limit
  1073. err = db.Select("c.id, c.user_org_id, c.partition_id, c.bed_id, c.patient_id, c.schedule_date, c.schedule_type, c.schedule_week, c.mode_id, c.status, c.created_time, c.updated_time").
  1074. Preload("AssessmentAfterDislysis", "user_org_id = ? AND status = 1", orgID).
  1075. Preload("AssessmentBeforeDislysis", "user_org_id = ? AND status = 1", orgID).
  1076. Order("c.schedule_date desc, c.schedule_type desc, c.id desc").Count(&total).Offset(offset).Limit(limit).Find(&weight).Error
  1077. if err != nil {
  1078. return nil, weight, pie, total, err
  1079. }
  1080. err = db.Select("c.id, c.user_org_id, c.partition_id, c.bed_id, c.patient_id, c.schedule_date, c.schedule_type, c.schedule_week, c.mode_id, c.status, c.created_time, c.updated_time").
  1081. Preload("AssessmentAfterDislysis", "user_org_id = ? AND status = 1", orgID).
  1082. Preload("AssessmentBeforeDislysis", "user_org_id = ? AND status = 1", orgID).
  1083. Order("c.schedule_date asc, c.schedule_type asc, c.id asc").Find(&pie).Error
  1084. if err != nil {
  1085. return nil, weight, pie, total, err
  1086. }
  1087. return &patient, weight, pie, total, err
  1088. }
  1089. func GetPersonBloodPressureData(orgID, patientID, page, limit, startTime, endTime int64, isStartTime, isEndTime bool) (*models.Patients, []*PABloodPressureDataStruct, []*PABloodPressureDataStruct, int64, error) {
  1090. var patient models.Patients
  1091. var bloodPressures []*PABloodPressureDataStruct
  1092. var lines []*PABloodPressureDataStruct
  1093. var total int64
  1094. var err error
  1095. err = readDb.Model(&models.Patients{}).Where("user_org_id =? and id=?", orgID, patientID).First(&patient).Error
  1096. if patient.ID == 0 {
  1097. return nil, bloodPressures, lines, total, err
  1098. }
  1099. db := readDb.Table("xt_schedule as c").Joins("JOIN xt_patients as p ON p.id=c.patient_id and p.user_org_id=? and c.patient_id=? and p.id = ?", orgID, patientID, patientID)
  1100. if isStartTime {
  1101. db = db.Where("c.schedule_date>=?", startTime)
  1102. }
  1103. if isEndTime {
  1104. db = db.Where("c.schedule_date<=?", endTime)
  1105. }
  1106. db = db.Where("c.patient_id=? AND c.status=1", patientID)
  1107. err = db.Select("c.id, c.user_org_id, c.partition_id, c.bed_id, c.patient_id, c.schedule_date, c.schedule_type, c.schedule_week, c.mode_id, c.status, c.created_time, c.updated_time").
  1108. Preload("AssessmentAfterDislysis", "user_org_id = ? AND status = 1", orgID).
  1109. Preload("AssessmentBeforeDislysis", "user_org_id = ? AND status = 1", orgID).
  1110. Preload("MonitoringRecord", "user_org_id = ? AND status = 1", orgID).
  1111. Order("c.schedule_date asc, c.schedule_type asc, c.id asc").Find(&lines).Error
  1112. if err != nil {
  1113. return &patient, bloodPressures, lines, total, err
  1114. }
  1115. offset := (page - 1) * limit
  1116. err = db.Select("c.id, c.user_org_id, c.partition_id, c.bed_id, c.patient_id, c.schedule_date, c.schedule_type, c.schedule_week, c.mode_id, c.status, c.created_time, c.updated_time").
  1117. Preload("Patient", "user_org_id = ? AND status = 1", orgID).
  1118. Preload("AssessmentAfterDislysis", "user_org_id = ? AND status = 1", orgID).
  1119. Preload("AssessmentBeforeDislysis", "user_org_id = ? AND status = 1", orgID).
  1120. Preload("MonitoringRecord", "user_org_id = ? AND status = 1", orgID).
  1121. Order("c.schedule_date desc, c.schedule_type desc, c.id desc").Count(&total).Offset(offset).Limit(limit).Find(&bloodPressures).Error
  1122. return &patient, bloodPressures, lines, total, err
  1123. }
  1124. func GetPersonICData(orgID, patientID, page, limit int64) (*models.Patients, []*models.Inspection, []*models.InspectionReference, int64, error) {
  1125. var patient models.Patients
  1126. var inspections []*models.Inspection
  1127. var references []*models.InspectionReference
  1128. var total int64
  1129. var err error
  1130. err = readDb.Model(&models.Patients{}).Where("user_org_id =? and id=?", orgID, patientID).First(&patient).Error
  1131. if patient.ID == 0 {
  1132. return nil, inspections, references, total, err
  1133. }
  1134. err = readDb.Table("xt_inspection_reference").Order("project_id").Find(&references).Error
  1135. if err != nil || len(references) == 0 {
  1136. return nil, inspections, references, total, err
  1137. }
  1138. db := readDb.Table("xt_inspection as i").Where("i.patient_id=?", patientID)
  1139. if orgID > 0 {
  1140. db = db.Where("i.org_id=?", orgID)
  1141. }
  1142. db = db.Where("i.status=1")
  1143. offset := (page - 1) * limit
  1144. var ds []*PIInspectionPageStruct
  1145. err = db.Select("i.inspect_date").Group("inspect_date").Count(&total).Order("inspect_date desc, i.created_time desc, i.id asc").Offset(offset).Limit(limit).Find(&ds).Error
  1146. if err != nil {
  1147. return nil, inspections, references, total, err
  1148. }
  1149. dates := make([]int64, 0)
  1150. if len(ds) > 0 {
  1151. for _, d := range ds {
  1152. dates = append(dates, d.InspectDate)
  1153. }
  1154. db = db.Where("i.inspect_date IN (?)", dates)
  1155. }
  1156. err = db.Select("i.id, i.patient_id, org_id, i.project_id, i.item_id, i.item_name, i.project_name, i.inspect_type, i.inspect_value, i.inspect_date, i.status, i.created_time, i.updated_time").Order("inspect_date desc, created_time desc, id asc, patient_id asc").Find(&inspections).Error
  1157. if err != nil {
  1158. return nil, inspections, references, total, err
  1159. }
  1160. return &patient, inspections, references, total, err
  1161. }
  1162. func GetPersonAdviceData(orgID, patientID, page, limit, adviceType, medicalType, startTime, endTime int64, isStartTime, isEndTime bool) (*models.Patients, []*models.DoctorAdvice, int64, error) {
  1163. var patient models.Patients
  1164. var advices []*models.DoctorAdvice
  1165. var total int64
  1166. var err error
  1167. err = readDb.Model(&models.Patients{}).Where("user_org_id =? and id=?", orgID, patientID).First(&patient).Error
  1168. if patient.ID == 0 {
  1169. return nil, advices, total, err
  1170. }
  1171. db := readDb.Table("xt_doctor_advice as da").Where("da.patient_id=? and da.delivery_way='口服'", patientID)
  1172. if orgID > 0 {
  1173. db = db.Where("da.user_org_id=?", orgID)
  1174. }
  1175. db = db.Where("da.status=1")
  1176. if adviceType > 0 {
  1177. db = db.Where("da.advice_type=?", adviceType)
  1178. } else {
  1179. db = db.Where("da.advice_type IN (1,2)")
  1180. }
  1181. if isStartTime {
  1182. db = db.Where("da.advice_date>=?", startTime)
  1183. }
  1184. if isEndTime {
  1185. db = db.Where("da.advice_date<=?", endTime)
  1186. }
  1187. offset := (page - 1) * limit
  1188. var groups []*models.DoctorAdvice
  1189. err = db.Select("da.id, da.user_org_id, da.patient_id, da.advice_type, da.advice_date, da.record_date, da.start_time, da.advice_name, da.advice_desc, da.reminder_date, da.drug_spec, da.drug_spec_unit, da.single_dose, da.single_dose_unit, da.prescribing_number, da.prescribing_number_unit, da.delivery_way, da.execution_frequency, da.advice_doctor, da.status, da.created_time,da.updated_time, da.advice_affirm, da.remark, da.stop_time, da.stop_reason, da.stop_doctor, da.stop_state, da.parent_id, da.execution_time, da.execution_staff, da.execution_state, da.checker, da.check_state, da.check_time, da.groupno, IF(da.parent_id > 0, da.parent_id, da.id) as advice_order").Group("da.groupno").Count(&total).Order("da.start_time desc, da.groupno desc, advice_order desc, da.id asc").Offset(offset).Limit(limit).Find(&groups).Error
  1190. if err != nil {
  1191. return nil, advices, total, err
  1192. }
  1193. if len(groups) > 0 {
  1194. gs := make([]int64, 0)
  1195. for _, g := range groups {
  1196. gs = append(gs, g.GroupNo)
  1197. }
  1198. db = db.Where("da.groupno IN (?)", gs)
  1199. }
  1200. err = db.Select("da.id, da.user_org_id, da.patient_id, da.advice_type, da.advice_date, da.record_date, da.start_time, da.advice_name, da.advice_desc, da.reminder_date, da.drug_spec, da.drug_spec_unit, da.single_dose, da.single_dose_unit, da.prescribing_number, da.prescribing_number_unit, da.delivery_way, da.execution_frequency, da.advice_doctor, da.status, da.created_time,da.updated_time, da.advice_affirm, da.remark, da.stop_time, da.stop_reason, da.stop_doctor, da.stop_state, da.parent_id, da.execution_time, da.execution_staff, da.execution_state, da.checker, da.check_state, da.check_time, da.groupno, IF(da.parent_id > 0, da.parent_id, da.id) as advice_order").Order("da.start_time desc, da.groupno desc, advice_order desc, da.id asc").Find(&advices).Error
  1201. if err != nil {
  1202. return nil, advices, total, err
  1203. }
  1204. return &patient, advices, total, err
  1205. }