qcd_service.go 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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. }