statistis_qc_service.go 45KB

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