statistis_qc_service.go 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. package service
  2. import (
  3. "XT_New/models"
  4. "database/sql"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "reflect"
  8. "strings"
  9. "time"
  10. )
  11. func GetDialysisTotalDatas() {
  12. }
  13. func GetNewDialysiTotal(startime int64, endtime int64, orgid int64, origin int64) (int64, error) {
  14. counts := models.PatientPrescriptionCountStruct{}
  15. var err error
  16. if origin == 1 { //透析记录,上机为准
  17. db := XTReadDB().Table("xt_dialysis_order as x").Joins("join xt_dialysis_prescription p on p.patient_id = x.patient_id and p.record_date = x.dialysis_date")
  18. err = db.Select("count(x.id) as count").Where("x.dialysis_date >= ? and x.dialysis_date<=? and x.user_org_id = ? AND x.status = 1", startime, endtime, orgid).Scan(&counts).Error
  19. return counts.Count, err
  20. } else {
  21. var count int64
  22. var schs []models.Schedule
  23. db := XTReadDB().Model(&models.Schedule{})
  24. err := db.
  25. Group("patient_id, schedule_date").
  26. Where("schedule_date >= ? AND schedule_date <= ? and user_org_id = ? and status = 1", startime, endtime, orgid).
  27. Find(&schs).
  28. Error
  29. count = int64(len(schs))
  30. return count, err
  31. }
  32. }
  33. type TreatmentMode struct {
  34. ModeID int `json:"mode_id"`
  35. ModeName string `json:"mode_name"`
  36. }
  37. type DialysisStat struct {
  38. Date string `json:"日期"`
  39. Counts map[string]int `json:"counts"`
  40. Total int `json:"合计"`
  41. }
  42. func GetTreatmentModes() ([]models.TreatmentMode, error) {
  43. var modes []models.TreatmentMode
  44. if err := XTReadDB().Model(&models.TreatmentMode{}).Find(&modes).Error; err != nil {
  45. return nil, err
  46. }
  47. return modes, nil
  48. }
  49. func GetDialysisStats(start int64, end int64, mode int64, org_id int64, time_way int64) ([]map[string]interface{}, error) {
  50. // 将时间戳转换为time.Time类型
  51. t := time.Unix(start, 0)
  52. // 使用布局定义格式化时间
  53. layout := "2006-01-02"
  54. // 将时间格式化为字符串
  55. startDate := t.Format(layout) + " 00:00:00"
  56. t2 := time.Unix(end, 0)
  57. // 使用布局定义格式化时间
  58. //layout := "2006-01-02"
  59. // 将时间格式化为字符串
  60. endDate := t2.Format(layout) + " 00:00:00"
  61. //var stats []DialysisStat
  62. var modes []models.TreatmentMode
  63. fmt.Println(mode)
  64. if mode == 0 {
  65. modes, _ = GetTreatmentModes()
  66. } else {
  67. modes_two, _ := GetTreatmentModes()
  68. for _, item := range modes_two {
  69. if item.ModeID == mode {
  70. modes = append(modes, item)
  71. }
  72. }
  73. }
  74. // 获取透析模式
  75. if err != nil {
  76. return nil, err
  77. }
  78. var selectClauses []string
  79. // 构建动态查询语句
  80. if time_way == 1 {
  81. //selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0])}
  82. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
  83. } else if time_way == 3 {
  84. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
  85. //selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(strings.Split(startDate, " ")[0], "-")[0]+"-"+strings.Split(strings.Split(startDate, " ")[0], "-")[1])}
  86. } else if time_way == 4 {
  87. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
  88. //selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(strings.Split(startDate, " ")[0], "-")[0])}
  89. } else {
  90. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
  91. }
  92. for _, mode := range modes {
  93. fmt.Println(mode)
  94. selectClauses = append(selectClauses, fmt.Sprintf("COALESCE(SUM(CASE WHEN t.name = '%s' THEN 1 ELSE 0 END), 0) AS `%s`", mode.Name, mode.Name))
  95. }
  96. selectClauses = append(selectClauses, "COUNT(*) AS `合计`")
  97. var rows *sql.Rows
  98. var query string
  99. if mode > 0 {
  100. query = fmt.Sprintf(`
  101. SELECT
  102. %s
  103. FROM
  104. xt_dialysis_prescription p
  105. JOIN
  106. xt_treatment_mode t ON p.mode_id = t.mode_id
  107. WHERE
  108. FROM_UNIXTIME(p.record_date) >= ? AND FROM_UNIXTIME(p.record_date) <= ? AND p.user_org_id = ? and p.mode_id = ?
  109. GROUP BY
  110. 日期
  111. `, strings.Join(selectClauses, ", "))
  112. rows, _ = readDb.Raw(query, startDate, endDate, org_id, mode).Rows()
  113. } else {
  114. query = fmt.Sprintf(`
  115. SELECT
  116. %s
  117. FROM
  118. xt_dialysis_prescription p
  119. JOIN
  120. xt_treatment_mode t ON p.mode_id = t.mode_id
  121. WHERE
  122. FROM_UNIXTIME(p.record_date) >= ? AND FROM_UNIXTIME(p.record_date) <= ? AND p.user_org_id = ?
  123. GROUP BY
  124. 日期
  125. `, strings.Join(selectClauses, ", "))
  126. rows, _ = readDb.Raw(query, startDate, endDate, org_id).Rows()
  127. }
  128. if err != nil {
  129. return nil, err
  130. }
  131. defer rows.Close()
  132. columns, err := rows.Columns()
  133. if err != nil {
  134. return nil, err
  135. }
  136. var results []map[string]interface{}
  137. for rows.Next() {
  138. // 创建一个长度为列数的切片,用于存储每一行的值
  139. columnValues := make([]interface{}, len(columns))
  140. columnPointers := make([]interface{}, len(columns))
  141. for i := range columnValues {
  142. columnPointers[i] = &columnValues[i]
  143. }
  144. // 扫描当前行的值
  145. if err := rows.Scan(columnPointers...); err != nil {
  146. return nil, err
  147. }
  148. // 将扫描到的值放入结果 map 中
  149. result := make(map[string]interface{})
  150. for i, colName := range columns {
  151. val := columnPointers[i].(*interface{})
  152. result[colName] = *val
  153. }
  154. results = append(results, result)
  155. }
  156. //// 解析查询结果
  157. //for rows.Next() {
  158. // var stat DialysisStat
  159. // stat.Counts = make(map[string]int)
  160. // cols := []interface{}{&stat.Date}
  161. // for _, mode := range modes {
  162. // var count int
  163. // cols = append(cols, &count)
  164. // stat.Counts[mode.Name] = count
  165. // }
  166. // var total int
  167. // cols = append(cols, &total)
  168. // stat.Total = total
  169. // if err := rows.Scan(cols...); err != nil {
  170. // return nil, err
  171. // }
  172. // stats = append(stats, stat)
  173. //}
  174. return results, nil
  175. }
  176. func GetScheduleStats(start int64, end int64, mode int64, org_id int64, time_way int64) ([]map[string]interface{}, error) {
  177. // 将时间戳转换为time.Time类型
  178. t := time.Unix(start, 0)
  179. // 使用布局定义格式化时间
  180. layout := "2006-01-02"
  181. // 将时间格式化为字符串
  182. startDate := t.Format(layout) + " 00:00:00"
  183. t2 := time.Unix(end, 0)
  184. // 使用布局定义格式化时间
  185. //layout := "2006-01-02"
  186. // 将时间格式化为字符串
  187. endDate := t2.Format(layout) + " 00:00:00"
  188. //var stats []DialysisStat
  189. var modes []models.TreatmentMode
  190. fmt.Println(mode)
  191. if mode == 0 {
  192. modes, _ = GetTreatmentModes()
  193. } else {
  194. modes_two, _ := GetTreatmentModes()
  195. for _, item := range modes_two {
  196. if item.ModeID == mode {
  197. modes = append(modes, item)
  198. }
  199. }
  200. }
  201. // 获取透析模式
  202. if err != nil {
  203. return nil, err
  204. }
  205. var selectClauses []string
  206. // 构建动态查询语句
  207. if time_way == 1 {
  208. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0])}
  209. } else if time_way == 3 {
  210. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(strings.Split(startDate, " ")[0], "-")[0]+strings.Split(strings.Split(startDate, " ")[0], "-")[1])}
  211. } else if time_way == 4 {
  212. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(strings.Split(startDate, " ")[0], "-")[0])}
  213. } else {
  214. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
  215. }
  216. for _, mode := range modes {
  217. fmt.Println(mode)
  218. selectClauses = append(selectClauses, fmt.Sprintf("COALESCE(SUM(CASE WHEN t.name = '%s' THEN 1 ELSE 0 END), 0) AS `%s`", mode.Name, mode.Name))
  219. }
  220. selectClauses = append(selectClauses, "COUNT(*) AS `合计`")
  221. var rows *sql.Rows
  222. var query string
  223. if mode > 0 {
  224. query = fmt.Sprintf(`
  225. SELECT
  226. %s
  227. FROM
  228. xt_schedule p
  229. JOIN
  230. xt_treatment_mode t ON p.mode_id = t.mode_id
  231. WHERE
  232. FROM_UNIXTIME(p.schedule_date) >= ? AND FROM_UNIXTIME(p.schedule_date) <= ? AND p.user_org_id = ? and p.mode_id = ?
  233. GROUP BY
  234. 日期
  235. `, strings.Join(selectClauses, ", "))
  236. rows, _ = readDb.Raw(query, startDate, endDate, org_id, mode).Rows()
  237. } else {
  238. query = fmt.Sprintf(`
  239. SELECT
  240. %s
  241. FROM
  242. xt_schedule p
  243. JOIN
  244. xt_treatment_mode t ON p.mode_id = t.mode_id
  245. WHERE
  246. FROM_UNIXTIME(p.schedule_date) >= ? AND FROM_UNIXTIME(p.schedule_date) <= ? AND p.user_org_id = ?
  247. GROUP BY
  248. 日期
  249. `, strings.Join(selectClauses, ", "))
  250. rows, _ = readDb.Raw(query, startDate, endDate, org_id).Rows()
  251. }
  252. if err != nil {
  253. return nil, err
  254. }
  255. defer rows.Close()
  256. columns, err := rows.Columns()
  257. if err != nil {
  258. return nil, err
  259. }
  260. var results []map[string]interface{}
  261. for rows.Next() {
  262. // 创建一个长度为列数的切片,用于存储每一行的值
  263. columnValues := make([]interface{}, len(columns))
  264. columnPointers := make([]interface{}, len(columns))
  265. for i := range columnValues {
  266. columnPointers[i] = &columnValues[i]
  267. }
  268. // 扫描当前行的值
  269. if err := rows.Scan(columnPointers...); err != nil {
  270. return nil, err
  271. }
  272. // 将扫描到的值放入结果 map 中
  273. result := make(map[string]interface{})
  274. for i, colName := range columns {
  275. val := columnPointers[i].(*interface{})
  276. result[colName] = *val
  277. }
  278. results = append(results, result)
  279. }
  280. //// 解析查询结果
  281. //for rows.Next() {
  282. // var stat DialysisStat
  283. // stat.Counts = make(map[string]int)
  284. // cols := []interface{}{&stat.Date}
  285. // for _, mode := range modes {
  286. // var count int
  287. // cols = append(cols, &count)
  288. // stat.Counts[mode.Name] = count
  289. // }
  290. // var total int
  291. // cols = append(cols, &total)
  292. // stat.Total = total
  293. // if err := rows.Scan(cols...); err != nil {
  294. // return nil, err
  295. // }
  296. // stats = append(stats, stat)
  297. //}
  298. return results, nil
  299. }
  300. //func GetScheduleStats(startDate int64, endDate int64, groupBy int64, mode int64, org_id int64) ([]DialysisStat, error) {
  301. // var stats []DialysisStat
  302. // var modes []models.TreatmentMode
  303. // if mode > 0 {
  304. // modes, _ = GetTreatmentModes()
  305. // } else {
  306. // modes_two, _ := GetTreatmentModes()
  307. // for _, item := range modes_two {
  308. // if item.ModeID == mode {
  309. // modes = append(modes, item)
  310. // }
  311. // }
  312. // }
  313. // // 获取透析模式
  314. // if err != nil {
  315. // return nil, err
  316. // }
  317. //
  318. // // 构建日期格式
  319. // dateFormat := "%Y-%m-%d"
  320. // switch groupBy {
  321. // case 2:
  322. // dateFormat = "%Y-%m-%d"
  323. // case 1:
  324. // dateFormat = "%Y-%u"
  325. // case 3:
  326. // dateFormat = "%Y-%m"
  327. // case 4:
  328. // dateFormat = "%Y"
  329. // default:
  330. // return nil, fmt.Errorf("invalid group by option")
  331. // }
  332. // // 构建动态查询语句
  333. // selectClauses := []string{fmt.Sprintf("DATE_FORMAT(p.schudle_date, '%s') AS `日期`", dateFormat)}
  334. // for _, mode := range modes {
  335. // selectClauses = append(selectClauses, fmt.Sprintf("SUM(CASE WHEN t.mode_name = '%s' THEN 1 ELSE 0 END) AS `%s`", mode.Name, mode.Name))
  336. // }
  337. // selectClauses = append(selectClauses, "COUNT(*) AS `合计`")
  338. //
  339. // query := fmt.Sprintf(`
  340. // SELECT
  341. // %s
  342. // FROM
  343. // xt_schedule p
  344. // JOIN
  345. // xt_treatment_mode t ON p.mode_id = t.mode_id
  346. // WHERE
  347. // p.schedule_date >= ? AND p.schedule_date <= ?
  348. // GROUP BY
  349. // DATE_FORMAT(p.schedule_date, '%s')
  350. // `, dateFormat, dateFormat, selectClauses)
  351. //
  352. // rows, err := readDb.Raw(query, startDate, endDate).Rows()
  353. // if err != nil {
  354. // return nil, err
  355. // }
  356. // defer rows.Close()
  357. //
  358. // // 解析查询结果
  359. // for rows.Next() {
  360. // var stat DialysisStat
  361. // stat.Counts = make(map[string]int)
  362. // cols := []interface{}{&stat.Date}
  363. // for _, mode := range modes {
  364. // var count int
  365. // cols = append(cols, &count)
  366. // stat.Counts[mode.Name] = count
  367. // }
  368. // var total int
  369. // cols = append(cols, &total)
  370. // stat.Total = total
  371. // if err := rows.Scan(cols...); err != nil {
  372. // return nil, err
  373. // }
  374. // stats = append(stats, stat)
  375. // }
  376. //
  377. // return stats, nil
  378. //}
  379. func GetNewDialysisCountMode(starttime int64, endtime int64, orgid int64, origin int64, mode_id int64) (counts []*models.PatientPrescriptionCountStruct, err error) {
  380. if origin == 1 {
  381. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  382. if starttime > 0 {
  383. db = db.Where("o.dialysis_date >=?", starttime)
  384. }
  385. if endtime > 0 {
  386. db = db.Where("o.dialysis_date<=?", endtime)
  387. }
  388. if orgid > 0 {
  389. db = db.Where("o.user_org_id = ?", orgid)
  390. }
  391. if mode_id == 0 {
  392. err = db.Select("s.mode_id,count(s.mode_id) as count").Joins("join xt_dialysis_prescription as s on s.patient_id = o.patient_id and s.record_date = o.dialysis_date and s.status= 1 AND s.record_date >= ? AND s.record_date <= ? AND s.mode_id > 0 ", starttime, endtime).Group("s.mode_id").Scan(&counts).Error
  393. } else {
  394. err = db.Select("s.mode_id,count(s.mode_id) as count").Joins("join xt_dialysis_prescription as s on s.patient_id = o.patient_id and s.record_date = o.dialysis_date and s.status= 1 AND s.record_date >= ? AND s.record_date <= ? AND s.mode_id = ? ", starttime, endtime, mode_id).Group("s.mode_id").Scan(&counts).Error
  395. }
  396. return counts, err
  397. } else {
  398. err = readDb.Raw("select mode_id, count(aa.`mode_id`) as count from xt_schedule aa join (SELECT DISTINCT o.patient_id, o.`schedule_date` FROM xt_schedule AS o WHERE o.status = 1 AND o.schedule_date >= ? AND o.schedule_date <= ? AND o.user_org_id = ?) as b on aa.`patient_id` = b.`patient_id` and aa.`schedule_date` = b.`schedule_date` where aa.`status` = 1 and aa.schedule_date >= ? AND aa.schedule_date <= ? AND aa.user_org_id = ? Group by aa.`mode_id`", starttime, endtime, orgid, starttime, endtime, orgid).Find(&counts).Error
  399. return counts, err
  400. }
  401. }
  402. func GetNewDialysisCountModeTwo(starttime int64, endtime int64, orgid int64, origin int64, mode_id int64) (counts models.CustomDialysisData, err error) {
  403. // 将时间戳转换为time.Time类型
  404. t := time.Unix(starttime, 0)
  405. // 使用布局定义格式化时间
  406. layout := "2006-01-02"
  407. // 将时间格式化为字符串
  408. startDate := t.Format(layout) + " 00:00:00"
  409. t2 := time.Unix(endtime, 0)
  410. // 使用布局定义格式化时间
  411. //layout := "2006-01-02"
  412. // 将时间格式化为字符串
  413. endDate := t2.Format(layout) + " 00:00:00"
  414. //fmt.Println(formattedDate)
  415. // 将日期字符串解析为time.Time类型
  416. //start, _ := time.Parse("2006-01-02 15:04:05", formattedDate)
  417. //starttime = start.Unix()
  418. //fmt.Println(starttime)
  419. if origin == 1 {
  420. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  421. if starttime > 0 {
  422. db = db.Where("DATE_FORMAT(FROM_UNIXTIME(o.dialysis_date), '%Y-%m-%d %H:%i:%s') >= ?", startDate)
  423. }
  424. if endtime > 0 {
  425. db = db.Where("DATE_FORMAT(FROM_UNIXTIME(o.dialysis_date), '%Y-%m-%d %H:%i:%s') <= ?", endDate)
  426. }
  427. if orgid > 0 {
  428. db = db.Where("o.user_org_id = ?", orgid)
  429. }
  430. err = db.Select("s.mode_id,count(s.mode_id) as count").Joins("join xt_dialysis_prescription as s on s.patient_id = o.patient_id and s.record_date = o.dialysis_date and s.status= 1 AND DATE_FORMAT(FROM_UNIXTIME(s.record_date), '%Y-%m-%d %H:%i:%s') >= ? AND DATE_FORMAT(FROM_UNIXTIME(s.record_date), '%Y-%m-%d %H:%i:%s') <= ? AND s.mode_id = ? ", startDate, endDate, mode_id).Group("s.mode_id").Scan(&counts).Error
  431. return counts, err
  432. } else {
  433. db := readDb.Table("xt_schedule as o").Where("o.status = 1").Select("patient_id, schedule_date").Group("mode_id")
  434. if starttime > 0 {
  435. db = db.Where("DATE_FORMAT(FROM_UNIXTIME(o.schedule_date),'%Y-%m-%d %H:%i:%s') >= ?", startDate)
  436. }
  437. if endtime > 0 {
  438. db = db.Where("DATE_FORMAT(FROM_UNIXTIME(o.schedule_date),'%Y-%m-%d %H:%i:%s') <= ?", endDate)
  439. }
  440. if orgid > 0 {
  441. db = db.Where("o.user_org_id = ?", orgid)
  442. }
  443. if mode_id > 0 {
  444. db = db.Where("o.mode_id = ?", mode_id)
  445. }
  446. err = db.Select("o.mode_id,count(o.mode_id) as count").Scan(&counts).Error
  447. return counts, err
  448. }
  449. }
  450. var anticoagulantMap = map[int]string{
  451. 1: "无肝素",
  452. 2: "普通肝素",
  453. 3: "低分子肝素",
  454. 4: "阿加曲班",
  455. 5: "枸橼酸钠",
  456. 6: "低分子肝素钙",
  457. 7: "低分子肝素钠",
  458. 8: "依诺肝素",
  459. 9: "达肝素",
  460. 10: "体外抗凝",
  461. 11: "那屈肝素",
  462. 12: "无抗凝剂",
  463. 13: "那屈肝素钙",
  464. 14: "肝素钙注射液",
  465. 15: "甲磺酸萘莫司他",
  466. 16: "低分子量肝素钙",
  467. 17: "肝素钠",
  468. }
  469. func GetAnticoagulantData(start_time int64, end_time int64, org_id int64) (map[string]int, error) {
  470. var results []struct {
  471. Anticoagulant int
  472. Count int
  473. }
  474. err := XTReadDB().Model(&models.DialysisPrescription{}).
  475. Select("anticoagulant, COUNT(*) as count").
  476. Where("status = 1 and record_date >= ? and record_date <= ? and user_org_id = ?", start_time, end_time, org_id).
  477. Group("anticoagulant").
  478. Scan(&results).Error
  479. if err != nil {
  480. return nil, err
  481. }
  482. anticoagulantData := make(map[string]int)
  483. for _, result := range results {
  484. if name, ok := anticoagulantMap[result.Anticoagulant]; ok {
  485. anticoagulantData[name] = result.Count
  486. } else {
  487. anticoagulantData[fmt.Sprintf("Unknown (%d)", result.Anticoagulant)] = result.Count
  488. }
  489. }
  490. return anticoagulantData, nil
  491. }
  492. func GetAnticoagulantTotal(start_time int64, end_time int64, org_id int64) (total int64, err error) {
  493. err = XTReadDB().Model(&models.DialysisPrescription{}).
  494. Where("record_date >= ? and record_date <= ? and user_org_id = ? and status = 1", start_time, end_time, org_id).
  495. Count(&total).Error
  496. return
  497. }
  498. func GetDialyzerSummary(org_id int64) ([]string, error) {
  499. var Dialyzer []string
  500. err := XTReadDB().Model(&models.DialysisPrescription{}).
  501. Select("dialysis_dialyszers").
  502. Where(" user_org_id = ? and status = 1", org_id).
  503. Group("dialysis_dialyszers").
  504. Scan(&Dialyzer).Error
  505. if err != nil {
  506. return nil, err
  507. }
  508. return Dialyzer, nil
  509. }
  510. func GetIrrigationSummary(org_id int64) ([]string, error) {
  511. var Irrigation []string
  512. err := XTReadDB().Model(&models.DialysisPrescription{}).
  513. Select("dialysis_irrigation").
  514. Where(" user_org_id = ? and status = 1", org_id).
  515. Group("dialysis_irrigation").
  516. Scan(&Irrigation).Error
  517. if err != nil {
  518. return nil, err
  519. }
  520. return Irrigation, nil
  521. }
  522. type DialyzerResult struct {
  523. Dialyzer string
  524. Count int
  525. }
  526. func GetDialyzerData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  527. err = XTReadDB().Model(&models.DialysisPrescription{}).
  528. Select("dialysis_dialyszers as dialyzer, COUNT(*) as count").
  529. Where("record_date >= ? and record_date <= ? and user_org_id = ? and status = 1", start_time, end_time, org_id).
  530. Group("dialysis_dialyszers").
  531. Scan(&dr).Error
  532. return
  533. }
  534. func GetIrrigationData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  535. err = XTReadDB().Model(&models.DialysisPrescription{}).
  536. Select("dialysis_irrigation as dialyzer, COUNT(*) as count").
  537. Where("record_date >= ? and record_date <= ? and user_org_id = ? and status = 1", start_time, end_time, org_id).
  538. Group("dialysis_irrigation").
  539. Scan(&dr).Error
  540. return
  541. }
  542. func GetDialyzerTotal(start_time int64, end_time int64, org_id int64) (total int64, err error) {
  543. err = XTReadDB().Model(&models.DialysisPrescription{}).
  544. Where("status = 1 and record_date >= ? and record_date <= ? and user_org_id = ? and dialysis_dialyszers <> ''", start_time, end_time, org_id).
  545. Count(&total).Error
  546. return
  547. }
  548. func GetPrescriptionByAnticoagulant(page int64, limit int64, orgid int64, anticoagulant int64, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
  549. db := XTReadDB().Model(&models.DialysisPrescription{}).Preload("QCPatients", "status = 1 and user_org_id = ?", orgid).Where("status = 1")
  550. if anticoagulant > 0 {
  551. db = db.Where("anticoagulant = ?", anticoagulant)
  552. }
  553. if orgid > 0 {
  554. db = db.Where("user_org_id = ?", orgid)
  555. }
  556. db = db.Where("record_date >= ? and record_date <= ?", start_time, end_time)
  557. offset := (page - 1) * limit
  558. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Error
  559. return solution, total, err
  560. }
  561. func GetPrescriptionByDialyzer(page int64, limit int64, orgid int64, dialyzer string, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
  562. db := XTReadDB().Model(&models.DialysisPrescription{}).Preload("QCPatients", "status = 1 and user_org_id = ?", orgid).Where("status = 1")
  563. if len(dialyzer) > 0 {
  564. db = db.Where("dialysis_dialyszers = ?", dialyzer)
  565. }
  566. if orgid > 0 {
  567. db = db.Where("user_org_id = ?", orgid)
  568. }
  569. db = db.Where("record_date >= ? and record_date <= ? and status = 1", start_time, end_time)
  570. offset := (page - 1) * limit
  571. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Error
  572. return solution, total, err
  573. }
  574. type DialysisData struct {
  575. DialysisStatus string `json:"dialysis_status"`
  576. Count int `json:"count"`
  577. }
  578. type CustomData struct {
  579. DialysisNo string
  580. PatientName string
  581. DialysisDate string
  582. DialysisDuration string
  583. ActualDuration string
  584. Diff string
  585. Doctor string
  586. Nurse string
  587. }
  588. func GetDialysisCompletionRate(org_id int64, start_time int64, end_time int64) (map[string]int, error) {
  589. var results []DialysisData
  590. query := `
  591. SELECT
  592. CASE
  593. WHEN ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) <= 15 THEN '达标'
  594. WHEN TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) < 0 THEN '未达标'
  595. ELSE '超时'
  596. END AS dialysis_status,
  597. COUNT(*) AS count
  598. FROM
  599. xt_dialysis_order o
  600. JOIN
  601. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  602. JOIN
  603. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  604. WHERE
  605. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
  606. GROUP BY
  607. dialysis_status;
  608. `
  609. if err := readDb.Raw(query, org_id, org_id, org_id, start_time, end_time).Scan(&results).Error; err != nil {
  610. return nil, err
  611. }
  612. dialysisCompletionRate := make(map[string]int)
  613. for _, result := range results {
  614. dialysisCompletionRate[result.DialysisStatus] = result.Count
  615. }
  616. return dialysisCompletionRate, nil
  617. }
  618. func GetDialysisCompletionTotal(org_id int64, start_time int64, end_time int64) (int64, error) {
  619. var Count int64
  620. query := `
  621. SELECT
  622. COUNT(*) AS count
  623. FROM
  624. xt_dialysis_order o
  625. JOIN
  626. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  627. JOIN
  628. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  629. WHERE
  630. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
  631. `
  632. if err := readDb.Raw(query, org_id, org_id, org_id, start_time, end_time).Count(&Count).Error; err != nil {
  633. return 0, err
  634. }
  635. return Count, nil
  636. }
  637. func GetDialysisCompletionDetail(org_id int64, start_time int64, end_time int64, mode int64, limit int64, page int64) (results []interface{}, total int64, err error) {
  638. var query string
  639. if mode == 0 {
  640. query = `
  641. SELECT
  642. p.dialysis_no as dialysis_no,
  643. p.name as patient_name,
  644. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  645. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  646. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  647. ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) as diff,
  648. p.prescription_doctor as doctor,
  649. o.finish_nurse as nurse,
  650. pp.id_card_no as id_card_no
  651. FROM
  652. xt_dialysis_order o
  653. JOIN
  654. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  655. JOIN
  656. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  657. JOIN
  658. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  659. WHERE
  660. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
  661. `
  662. readDb.Table("xt_dialysis_order o").
  663. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  664. Joins("JOIN xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?", org_id).
  665. Joins("JOIN xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?", org_id).
  666. Where("o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?", org_id, start_time, end_time).Count(&total)
  667. } else if mode == 1 {
  668. query = `
  669. SELECT
  670. pp.dialysis_no as dialysis_no,
  671. pp.name as patient_name,
  672. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  673. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  674. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  675. ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) as diff,
  676. p.prescription_doctor as doctor,
  677. o.finish_nurse as nurse,
  678. pp.id_card_no as id_card_no
  679. FROM
  680. xt_dialysis_order o
  681. JOIN
  682. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  683. JOIN
  684. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  685. JOIN
  686. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  687. WHERE
  688. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? AND ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) <= 15
  689. `
  690. readDb.Table("xt_dialysis_order o").
  691. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  692. Joins("JOIN xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?", org_id).
  693. Joins("JOIN xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?", org_id).
  694. Where("o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? and ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) <= 15", org_id, start_time, end_time).Count(&total)
  695. } else if mode == 2 {
  696. query = `
  697. SELECT
  698. pp.dialysis_no as dialysis_no,
  699. pp.name as patient_name,
  700. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  701. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  702. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  703. ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) as diff,
  704. p.prescription_doctor as doctor,
  705. o.finish_nurse as nurse,
  706. pp.id_card_no as id_card_no
  707. FROM
  708. xt_dialysis_order o
  709. JOIN
  710. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  711. JOIN
  712. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  713. JOIN
  714. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  715. WHERE
  716. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? AND TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) < 0
  717. `
  718. readDb.Table("xt_dialysis_order o").
  719. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  720. Joins("JOIN xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?", org_id).
  721. Joins("JOIN xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?", org_id).
  722. Where("o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? and TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) < 0", org_id, start_time, end_time).Count(&total)
  723. } else if mode == 3 {
  724. query = `
  725. SELECT
  726. pp.dialysis_no as dialysis_no,
  727. pp.name as patient_name,
  728. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  729. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  730. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  731. ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) as diff,
  732. p.prescription_doctor as doctor,
  733. o.finish_nurse as nurse,
  734. pp.id_card_no as id_card_no
  735. FROM
  736. xt_dialysis_order o
  737. JOIN
  738. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  739. JOIN
  740. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  741. JOIN
  742. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  743. WHERE
  744. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? AND TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) > 15
  745. `
  746. readDb.Table("xt_dialysis_order o").
  747. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  748. Joins("JOIN xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?", org_id).
  749. Joins("JOIN xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?", org_id).
  750. Where("o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? and TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) > 15", org_id, start_time, end_time).Count(&total)
  751. }
  752. offset := (page - 1) * limit
  753. rows, err := readDb.Raw(query, org_id, org_id, org_id, org_id, start_time, end_time).Offset(offset).Limit(limit).Rows()
  754. if err != nil {
  755. fmt.Println(err)
  756. }
  757. defer rows.Close()
  758. // Define a map to hold the dynamic fields
  759. fields := map[string]interface{}{
  760. "dialysis_no": "",
  761. "patient_name": "",
  762. "dialysis_date": "",
  763. "dialysis_duration": "",
  764. "actual_duration": "",
  765. "diff": 0,
  766. "doctor": "",
  767. "nurse": "",
  768. "id_card_no": "",
  769. }
  770. // Create the dynamic struct type
  771. dynamicStructType := createDynamicStruct(fields)
  772. // Slice to hold the results
  773. //var results []interface{}
  774. // Iterate over the rows and scan into the dynamic struct
  775. for rows.Next() {
  776. // Create a new instance of the dynamic struct
  777. result := reflect.New(dynamicStructType).Interface()
  778. // Create a slice of pointers to the fields in the struct
  779. fieldPtrs := []interface{}{
  780. reflect.ValueOf(result).Elem().FieldByName("Dialysis_no").Addr().Interface(),
  781. reflect.ValueOf(result).Elem().FieldByName("Patient_name").Addr().Interface(),
  782. reflect.ValueOf(result).Elem().FieldByName("Dialysis_date").Addr().Interface(),
  783. reflect.ValueOf(result).Elem().FieldByName("Dialysis_duration").Addr().Interface(),
  784. reflect.ValueOf(result).Elem().FieldByName("Actual_duration").Addr().Interface(),
  785. reflect.ValueOf(result).Elem().FieldByName("Diff").Addr().Interface(),
  786. reflect.ValueOf(result).Elem().FieldByName("Doctor").Addr().Interface(),
  787. reflect.ValueOf(result).Elem().FieldByName("Nurse").Addr().Interface(),
  788. reflect.ValueOf(result).Elem().FieldByName("Id_card_no").Addr().Interface(),
  789. }
  790. // Scan the row into the struct
  791. if err := rows.Scan(fieldPtrs...); err != nil {
  792. //log.Fatalf("failed to scan row: %v", err)
  793. fmt.Println(err)
  794. }
  795. // Append the result to the slice
  796. results = append(results, result)
  797. }
  798. return results, total, err
  799. }
  800. type QualityControlStandard struct {
  801. ItemName string `json:"item_name"`
  802. ProjectMame string `json:"project_name"`
  803. InspectionMajor int64 `json:"inspection_major"`
  804. InspectionMinor int64 `json:"inspection_minor"`
  805. }
  806. func getQualityControlStandards(org_id int64) ([]QualityControlStandard, error) {
  807. var standards []QualityControlStandard
  808. if err := readDb.Model(&models.XtQualityControlStandard{}).Select("re.project_name as project_name,re.item_name as item_name,xt_quality_control_standard.inspection_major as inspection_major,xt_quality_control_standard.inspection_minor as inspection_minor").Joins("join xt_inspection_reference re on re.project_id = xt_quality_control_standard.inspection_major and re.item_id = xt_quality_control_standard.inspection_minor and re.org_id = ?", org_id).Where("xt_quality_control_standard.user_org_id = ? and xt_quality_control_standard.status = 1", org_id).Scan(&standards).Error; err != nil {
  809. return nil, err
  810. }
  811. return standards, nil
  812. }
  813. type DialysisQualityStat struct {
  814. Month string `json:"月"`
  815. Name string `json:"姓名"`
  816. Counts map[string]float64 `json:"counts"`
  817. }
  818. func GetLatestInspectionValues(org_id int64, yearMonth string) ([]map[string]interface{}, error) {
  819. standards, err := getQualityControlStandards(org_id)
  820. if err != nil {
  821. return nil, err
  822. }
  823. var selectFields []string
  824. for _, standard := range standards {
  825. field := fmt.Sprintf("IFNULL(MAX(CASE WHEN i.item_name = %s THEN i.inspect_value END),'') AS `%s`",
  826. "'"+standard.ItemName+"'", standard.ItemName)
  827. selectFields = append(selectFields, field)
  828. }
  829. query := fmt.Sprintf(`
  830. SELECT
  831. p.name as '姓名',
  832. %s
  833. FROM xt_inspection i
  834. JOIN xt_patients p On p.id = i.patient_id
  835. WHERE DATE_FORMAT(FROM_UNIXTIME(i.inspect_date), '%%Y-%%m') = ? and org_id = ?
  836. GROUP BY i.patient_id
  837. `, strings.Join(selectFields, ", "))
  838. var results []map[string]interface{}
  839. rows, err := readDb.Raw(query, yearMonth, org_id).Rows()
  840. if err != nil {
  841. return nil, err
  842. }
  843. defer rows.Close()
  844. columns, err := rows.Columns()
  845. if err != nil {
  846. return nil, err
  847. }
  848. for rows.Next() {
  849. // 创建一个长度为列数的切片,用于存储每一行的值
  850. columnValues := make([]interface{}, len(columns))
  851. columnPointers := make([]interface{}, len(columns))
  852. for i := range columnValues {
  853. columnPointers[i] = &columnValues[i]
  854. }
  855. // 扫描当前行的值
  856. if err := rows.Scan(columnPointers...); err != nil {
  857. return nil, err
  858. }
  859. // 将扫描到的值放入结果 map 中
  860. result := make(map[string]interface{})
  861. for i, colName := range columns {
  862. val := columnPointers[i].(*interface{})
  863. result[colName] = *val
  864. }
  865. results = append(results, result)
  866. }
  867. return results, nil
  868. }
  869. func createDynamicStruct(fields map[string]interface{}) reflect.Type {
  870. var structFields []reflect.StructField
  871. for name, value := range fields {
  872. structFields = append(structFields, reflect.StructField{
  873. Name: strings.Title(name),
  874. Type: reflect.TypeOf(value),
  875. Tag: reflect.StructTag(fmt.Sprintf(`json:"%s"`, name)),
  876. })
  877. }
  878. return reflect.StructOf(structFields)
  879. }
  880. func GetDialysisPrescriptionInfo(start_date string, end_date string, mode int64, org_id int64, page int64, limit int64) (qcp []models.QCPrescription, total int64, err error) {
  881. offset := (page - 1) * limit
  882. err = readDb.Model(&models.QCPrescription{}).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  883. return readUserDb.Where("status = 1 and org_id=?", org_id)
  884. }).Preload("QCPatients", "status = 1 and user_org_id = ?", org_id).Preload("QCDialysisOrder", func(db *gorm.DB) *gorm.DB {
  885. return db.Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  886. return readUserDb.Where("status = 1 and org_id=?", org_id)
  887. }).Preload("DeviceNumber", "status = 1 and org_id = ?", org_id).Where("status = 1 AND user_org_id = ?", org_id)
  888. }).Preload("QCAssessmentAfterDislysis", "status = 1 and user_org_id = ?", org_id).Where("FROM_UNIXTIME(record_date, '%Y-%m-%d') >= ? and FROM_UNIXTIME(record_date, '%Y-%m-%d') <= ? and user_org_id = ? and mode_id = ? and status = 1", start_date, end_date, org_id, mode).Count(&total).Offset(offset).Limit(limit).Find(&qcp).Error
  889. return
  890. }
  891. func GetScheduleInfo(start_date string, end_date string, mode int64, org_id int64, page int64, limit int64) (qcp []models.QCSchedule, total int64, err error) {
  892. offset := (page - 1) * limit
  893. err = readDb.Model(&models.QCSchedule{}).Preload("QCPatients", "status = 1 and user_org_id =?", org_id).Preload("QCSPrescription", func(db *gorm.DB) *gorm.DB {
  894. return db.Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  895. return readUserDb.Where("status = 1 and org_id=?", org_id)
  896. }).Where("status = 1 AND user_org_id = ?", org_id)
  897. }).Preload("QCAssessmentAfterDislysis", "status = 1 and user_org_id = ?", org_id).Preload("QCDialysisOrder", func(db *gorm.DB) *gorm.DB {
  898. return db.Preload("DeviceNumber", "status = 1").Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  899. return readUserDb.Where("status = 1 and org_id=?", org_id)
  900. }).Where("status = 1 AND user_org_id = ?", org_id)
  901. }).Where("FROM_UNIXTIME(schedule_date, '%Y-%m-%d') >= ? and FROM_UNIXTIME(schedule_date, '%Y-%m-%d') <= ? and user_org_id = ? and mode_id = ? and status = 1", start_date, end_date, org_id, mode).Count(&total).Offset(offset).Limit(limit).Find(&qcp).Error
  902. return
  903. }