statistis_qc_service.go 47KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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) ([]models.DialysisPrescription, error) {
  498. var Dialyzer []models.DialysisPrescription
  499. err := XTReadDB().Model(&models.DialysisPrescription{}).
  500. Select("dialysis_dialyszers").
  501. Where(" user_org_id = ? and status = 1 and dialysis_dialyszers <> ''", org_id).
  502. Group("dialysis_dialyszers").
  503. Find(&Dialyzer).Error
  504. if err != nil {
  505. return nil, err
  506. }
  507. return Dialyzer, nil
  508. }
  509. func GetIrrigationSummary(org_id int64) ([]models.DialysisPrescription, error) {
  510. var Dialyzer []models.DialysisPrescription
  511. err := XTReadDB().Model(&models.DialysisPrescription{}).
  512. Select("dialysis_irrigation").
  513. Where(" user_org_id = ? and status = 1 and dialysis_irrigation <> ''", org_id).
  514. Group("dialysis_irrigation").
  515. Find(&Dialyzer).Error
  516. if err != nil {
  517. return nil, err
  518. }
  519. return Dialyzer, nil
  520. }
  521. func GetDialysisStrainerSummary(org_id int64) ([]models.DialysisPrescription, error) {
  522. var Dialyzer []models.DialysisPrescription
  523. err := XTReadDB().Model(&models.DialysisPrescription{}).
  524. Select("dialysis_strainer").
  525. Where(" user_org_id = ? and status = 1 and dialysis_strainer <> ''", org_id).
  526. Group("dialysis_strainer").
  527. Find(&Dialyzer).Error
  528. if err != nil {
  529. return nil, err
  530. }
  531. return Dialyzer, nil
  532. }
  533. type DialyzerResult struct {
  534. Dialyzer string
  535. Count int
  536. }
  537. func GetDialyzerData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  538. 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").
  539. Select("xt_dialysis_prescription.dialysis_dialyszers as dialyzer, COUNT(*) as count").
  540. 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 and xt_dialysis_prescription.dialysis_dialyszers <> ''", start_time, end_time, org_id).
  541. Group("xt_dialysis_prescription.dialysis_dialyszers").
  542. Scan(&dr).Error
  543. return
  544. }
  545. func GetDialysisIrrigationData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  546. 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").
  547. Select("xt_dialysis_prescription.dialysis_irrigation as dialyzer, COUNT(*) as count").
  548. 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 and xt_dialysis_prescription.dialysis_irrigation <> '' ", start_time, end_time, org_id).
  549. Group("xt_dialysis_prescription.dialysis_irrigation").
  550. Scan(&dr).Error
  551. return
  552. }
  553. func GetDialysisStrainerData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  554. 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").
  555. Select("xt_dialysis_prescription.dialysis_strainer as dialyzer, COUNT(*) as count").
  556. 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 and xt_dialysis_prescription.dialysis_strainer <> ''", start_time, end_time, org_id).
  557. Group("xt_dialysis_prescription.dialysis_strainer").
  558. Scan(&dr).Error
  559. return
  560. }
  561. func GetIrrigationData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  562. err = XTReadDB().Model(&models.DialysisPrescription{}).
  563. Select("dialysis_irrigation as dialyzer, COUNT(*) as count").
  564. Where("record_date >= ? and record_date <= ? and user_org_id = ? and status = 1", start_time, end_time, org_id).
  565. Group("dialysis_irrigation").
  566. Scan(&dr).Error
  567. return
  568. }
  569. func GetDialyzerTotal(start_time int64, end_time int64, org_id int64) (total int64, err error) {
  570. 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").
  571. 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).
  572. Count(&total).Error
  573. return
  574. }
  575. func GetPrescriptionByAnticoagulant(page int64, limit int64, orgid int64, anticoagulant int64, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
  576. 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")
  577. if anticoagulant > 0 {
  578. db = db.Where("xt_dialysis_prescription.anticoagulant = ?", anticoagulant)
  579. }
  580. if orgid > 0 {
  581. db = db.Where("xt_dialysis_prescription.user_org_id = ?", orgid)
  582. }
  583. db = db.Where("xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ?", start_time, end_time)
  584. offset := (page - 1) * limit
  585. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Error
  586. return solution, total, err
  587. }
  588. func GetPrescriptionByDialyzer(page int64, limit int64, orgid int64, dialyzer string, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
  589. 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")
  590. if len(dialyzer) > 0 {
  591. db = db.Where("xt_dialysis_prescription.dialysis_dialyszers = ? or xt_dialysis_prescription.dialysis_irrigation = ? or xt_dialysis_prescription.dialysis_strainer = ?", dialyzer, dialyzer, dialyzer)
  592. }
  593. if orgid > 0 {
  594. db = db.Where("xt_dialysis_prescription.user_org_id = ?", orgid)
  595. }
  596. db = db.Where("xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ? and xt_dialysis_prescription.status = 1", start_time, end_time)
  597. offset := (page - 1) * limit
  598. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Error
  599. return solution, total, err
  600. }
  601. type DialysisData struct {
  602. DialysisStatus string `json:"dialysis_status"`
  603. Count int `json:"count"`
  604. }
  605. type CustomData struct {
  606. DialysisNo string
  607. PatientName string
  608. DialysisDate string
  609. DialysisDuration string
  610. ActualDuration string
  611. Diff string
  612. Doctor string
  613. Nurse string
  614. }
  615. func GetDialysisCompletionRate(org_id int64, start_time int64, end_time int64) (map[string]int, error) {
  616. var results []DialysisData
  617. query := `
  618. SELECT
  619. CASE
  620. 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 '达到透析处方时间'
  621. 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 '超出处方时间'
  622. ELSE '未到达处方时间'
  623. END AS dialysis_status,
  624. COUNT(*) AS count
  625. FROM
  626. xt_dialysis_order o
  627. JOIN
  628. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  629. JOIN
  630. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  631. WHERE
  632. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
  633. GROUP BY
  634. dialysis_status;
  635. `
  636. if err := readDb.Raw(query, org_id, org_id, org_id, start_time, end_time).Scan(&results).Error; err != nil {
  637. return nil, err
  638. }
  639. dialysisCompletionRate := make(map[string]int)
  640. for _, result := range results {
  641. dialysisCompletionRate[result.DialysisStatus] = result.Count
  642. }
  643. return dialysisCompletionRate, nil
  644. }
  645. func GetDialysisCompletionTotal(org_id int64, start_time int64, end_time int64) (int64, error) {
  646. var Count int64
  647. query := `
  648. SELECT
  649. COUNT(*) AS count
  650. FROM
  651. xt_dialysis_order o
  652. JOIN
  653. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  654. JOIN
  655. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  656. WHERE
  657. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
  658. `
  659. if err := readDb.Raw(query, org_id, org_id, org_id, start_time, end_time).Count(&Count).Error; err != nil {
  660. return 0, err
  661. }
  662. return Count, nil
  663. }
  664. func GetDialysisCompletionDetail(org_id int64, start_time int64, end_time int64, mode int64, limit int64, page int64) (results []interface{}, total int64, err error) {
  665. var query string
  666. if mode == 0 {
  667. query = `
  668. SELECT
  669. p.dialysis_no as dialysis_no,
  670. p.name as patient_name,
  671. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  672. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  673. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  674. 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,
  675. p.prescription_doctor as doctor,
  676. o.finish_nurse as nurse,
  677. pp.id_card_no as id_card_no
  678. FROM
  679. xt_dialysis_order o
  680. JOIN
  681. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  682. JOIN
  683. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  684. JOIN
  685. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  686. WHERE
  687. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
  688. `
  689. readDb.Table("xt_dialysis_order o").
  690. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  691. 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).
  692. 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).
  693. 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)
  694. } else if mode == 1 {
  695. query = `
  696. SELECT
  697. pp.dialysis_no as dialysis_no,
  698. pp.name as patient_name,
  699. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  700. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  701. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  702. 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,
  703. p.prescription_doctor as doctor,
  704. o.finish_nurse as nurse,
  705. pp.id_card_no as id_card_no
  706. FROM
  707. xt_dialysis_order o
  708. JOIN
  709. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  710. JOIN
  711. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  712. JOIN
  713. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  714. WHERE
  715. 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
  716. `
  717. readDb.Table("xt_dialysis_order o").
  718. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  719. 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).
  720. 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).
  721. 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)
  722. } else if mode == 2 {
  723. query = `
  724. SELECT
  725. pp.dialysis_no as dialysis_no,
  726. pp.name as patient_name,
  727. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  728. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  729. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  730. 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,
  731. p.prescription_doctor as doctor,
  732. o.finish_nurse as nurse,
  733. pp.id_card_no as id_card_no
  734. FROM
  735. xt_dialysis_order o
  736. JOIN
  737. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  738. JOIN
  739. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  740. JOIN
  741. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  742. WHERE
  743. 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
  744. `
  745. readDb.Table("xt_dialysis_order o").
  746. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  747. 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).
  748. 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).
  749. 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)
  750. } else if mode == 3 {
  751. query = `
  752. SELECT
  753. pp.dialysis_no as dialysis_no,
  754. pp.name as patient_name,
  755. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  756. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  757. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  758. 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,
  759. p.prescription_doctor as doctor,
  760. o.finish_nurse as nurse,
  761. pp.id_card_no as id_card_no
  762. FROM
  763. xt_dialysis_order o
  764. JOIN
  765. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  766. JOIN
  767. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  768. JOIN
  769. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  770. WHERE
  771. 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
  772. `
  773. readDb.Table("xt_dialysis_order o").
  774. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  775. 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).
  776. 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).
  777. 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)
  778. }
  779. offset := (page - 1) * limit
  780. rows, err := readDb.Raw(query, org_id, org_id, org_id, org_id, start_time, end_time).Offset(offset).Limit(limit).Rows()
  781. if err != nil {
  782. fmt.Println(err)
  783. }
  784. defer rows.Close()
  785. // Define a map to hold the dynamic fields
  786. fields := map[string]interface{}{
  787. "dialysis_no": "",
  788. "patient_name": "",
  789. "dialysis_date": "",
  790. "dialysis_duration": "",
  791. "actual_duration": "",
  792. "diff": 0,
  793. "doctor": "",
  794. "nurse": "",
  795. "id_card_no": "",
  796. }
  797. // Create the dynamic struct type
  798. dynamicStructType := createDynamicStruct(fields)
  799. // Slice to hold the results
  800. //var results []interface{}
  801. // Iterate over the rows and scan into the dynamic struct
  802. for rows.Next() {
  803. // Create a new instance of the dynamic struct
  804. result := reflect.New(dynamicStructType).Interface()
  805. // Create a slice of pointers to the fields in the struct
  806. fieldPtrs := []interface{}{
  807. reflect.ValueOf(result).Elem().FieldByName("Dialysis_no").Addr().Interface(),
  808. reflect.ValueOf(result).Elem().FieldByName("Patient_name").Addr().Interface(),
  809. reflect.ValueOf(result).Elem().FieldByName("Dialysis_date").Addr().Interface(),
  810. reflect.ValueOf(result).Elem().FieldByName("Dialysis_duration").Addr().Interface(),
  811. reflect.ValueOf(result).Elem().FieldByName("Actual_duration").Addr().Interface(),
  812. reflect.ValueOf(result).Elem().FieldByName("Diff").Addr().Interface(),
  813. reflect.ValueOf(result).Elem().FieldByName("Doctor").Addr().Interface(),
  814. reflect.ValueOf(result).Elem().FieldByName("Nurse").Addr().Interface(),
  815. reflect.ValueOf(result).Elem().FieldByName("Id_card_no").Addr().Interface(),
  816. }
  817. // Scan the row into the struct
  818. if err := rows.Scan(fieldPtrs...); err != nil {
  819. //log.Fatalf("failed to scan row: %v", err)
  820. fmt.Println(err)
  821. }
  822. // Append the result to the slice
  823. results = append(results, result)
  824. }
  825. return results, total, err
  826. }
  827. type QualityControlStandard struct {
  828. ItemName string `json:"item_name"`
  829. ProjectMame string `json:"project_name"`
  830. InspectionMajor int64 `json:"inspection_major"`
  831. InspectionMinor int64 `json:"inspection_minor"`
  832. }
  833. func getQualityControlStandards(org_id int64) ([]QualityControlStandard, error) {
  834. var standards []QualityControlStandard
  835. 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 {
  836. return nil, err
  837. }
  838. return standards, nil
  839. }
  840. type DialysisQualityStat struct {
  841. Month string `json:"月"`
  842. Name string `json:"姓名"`
  843. Counts map[string]float64 `json:"counts"`
  844. }
  845. //func GetLatestInspectionValues(org_id int64, yearMonth string) ([]map[string]interface{}, error) {
  846. // standards, err := getQualityControlStandards(org_id)
  847. // if err != nil {
  848. // return nil, err
  849. // }
  850. //
  851. // var selectFields []string
  852. // for _, standard := range standards {
  853. // field := fmt.Sprintf("IFNULL(MAX(CASE WHEN i.item_name = %s THEN i.inspect_value END),'') AS `%s`",
  854. // "'"+standard.ItemName+"'", standard.ItemName)
  855. // selectFields = append(selectFields, field)
  856. // }
  857. //
  858. // query := fmt.Sprintf(`
  859. // SELECT
  860. // p.name as '姓名',
  861. // %s
  862. // FROM xt_inspection i
  863. // JOIN xt_patients p On p.id = i.patient_id
  864. // WHERE DATE_FORMAT(FROM_UNIXTIME(i.inspect_date), '%%Y-%%m') = ? and org_id = ?
  865. //
  866. // GROUP BY i.patient_id
  867. // `, strings.Join(selectFields, ", "))
  868. //
  869. // var results []map[string]interface{}
  870. // rows, err := readDb.Raw(query, yearMonth, org_id).Rows()
  871. // if err != nil {
  872. // return nil, err
  873. // }
  874. // defer rows.Close()
  875. //
  876. // columns, err := rows.Columns()
  877. // if err != nil {
  878. // return nil, err
  879. // }
  880. //
  881. // for rows.Next() {
  882. // // 创建一个长度为列数的切片,用于存储每一行的值
  883. // columnValues := make([]interface{}, len(columns))
  884. // columnPointers := make([]interface{}, len(columns))
  885. // for i := range columnValues {
  886. // columnPointers[i] = &columnValues[i]
  887. // }
  888. //
  889. // // 扫描当前行的值
  890. // if err := rows.Scan(columnPointers...); err != nil {
  891. // return nil, err
  892. // }
  893. //
  894. // // 将扫描到的值放入结果 map 中
  895. // result := make(map[string]interface{})
  896. // for i, colName := range columns {
  897. // val := columnPointers[i].(*interface{})
  898. // result[colName] = *val
  899. // }
  900. //
  901. // results = append(results, result)
  902. // }
  903. //
  904. // return results, nil
  905. //}
  906. func GetLatestInspectionValues(org_id int64, yearMonth string) ([]map[string]interface{}, error) {
  907. standards, err := getQualityControlStandards(org_id)
  908. if err != nil {
  909. return nil, err
  910. }
  911. var selectFields []string
  912. for _, standard := range standards {
  913. field := fmt.Sprintf("IFNULL(MAX(CASE WHEN latest_inspections.item_name = '%s' THEN latest_inspections.inspect_value END),'') AS `%s`",
  914. standard.ItemName, standard.ItemName)
  915. selectFields = append(selectFields, field)
  916. }
  917. query := fmt.Sprintf(`
  918. SELECT
  919. p.name as '姓名',
  920. %s
  921. FROM xt_patients p
  922. JOIN (
  923. SELECT
  924. i1.patient_id,
  925. i1.item_name,
  926. i1.inspect_value
  927. FROM
  928. xt_inspection i1
  929. JOIN (
  930. SELECT
  931. patient_id,
  932. item_name,
  933. MAX(inspect_date) AS latest_inspect_date
  934. FROM
  935. xt_inspection
  936. WHERE
  937. DATE_FORMAT(FROM_UNIXTIME(inspect_date), '%%Y-%%m') = ?
  938. AND org_id = ?
  939. GROUP BY
  940. patient_id, item_name
  941. ) latest
  942. ON
  943. i1.patient_id = latest.patient_id
  944. AND i1.item_name = latest.item_name
  945. AND i1.inspect_date = latest.latest_inspect_date
  946. ) latest_inspections
  947. ON p.id = latest_inspections.patient_id
  948. WHERE p.user_org_id = ? and p.lapseto = 1
  949. GROUP BY p.id, p.name
  950. `, strings.Join(selectFields, ", "))
  951. var results []map[string]interface{}
  952. rows, err := readDb.Raw(query, yearMonth, org_id, org_id).Rows() // 注意这里多传一个org_id参数
  953. if err != nil {
  954. return nil, err
  955. }
  956. defer rows.Close()
  957. columns, err := rows.Columns()
  958. if err != nil {
  959. return nil, err
  960. }
  961. for rows.Next() {
  962. // 创建一个长度为列数的切片,用于存储每一行的值
  963. columnValues := make([]interface{}, len(columns))
  964. columnPointers := make([]interface{}, len(columns))
  965. for i := range columnValues {
  966. columnPointers[i] = &columnValues[i]
  967. }
  968. // 扫描当前行的值
  969. if err := rows.Scan(columnPointers...); err != nil {
  970. return nil, err
  971. }
  972. // 将扫描到的值放入结果 map 中
  973. result := make(map[string]interface{})
  974. for i, colName := range columns {
  975. val := columnPointers[i].(*interface{})
  976. result[colName] = *val
  977. }
  978. results = append(results, result)
  979. }
  980. return results, nil
  981. }
  982. //func GetLatestInspectionValues(org_id int64, yearMonth string) ([]map[string]interface{}, error) {
  983. // standards, err := getQualityControlStandards(org_id)
  984. // if err != nil {
  985. // return nil, err
  986. // }
  987. //
  988. // var selectFields []string
  989. // for _, standard := range standards {
  990. // field := fmt.Sprintf("IFNULL(MAX(CASE WHEN latest_inspections.item_name = '%s' THEN latest_inspections.inspect_value END),'') AS `%s`",
  991. // standard.ItemName, standard.ItemName)
  992. // selectFields = append(selectFields, field)
  993. // }
  994. //
  995. // query := fmt.Sprintf(`
  996. // SELECT
  997. // p.name AS '姓名',
  998. // %s
  999. // FROM xt_patients p
  1000. // LEFT JOIN (
  1001. // SELECT
  1002. // i1.patient_id,
  1003. // i1.item_name,
  1004. // i1.inspect_value
  1005. // FROM
  1006. // xt_inspection i1
  1007. // JOIN (
  1008. // SELECT
  1009. // patient_id,
  1010. // item_name,
  1011. // MAX(inspect_date) AS latest_inspect_date
  1012. // FROM
  1013. // xt_inspection
  1014. // WHERE
  1015. // DATE_FORMAT(FROM_UNIXTIME(inspect_date), '%%Y-%%m') = ?
  1016. // AND org_id = ?
  1017. // GROUP BY
  1018. // patient_id, item_name
  1019. // ) latest
  1020. // ON
  1021. // i1.patient_id = latest.patient_id
  1022. // AND i1.item_name = latest.item_name
  1023. // AND i1.inspect_date = latest.latest_inspect_date
  1024. // ) latest_inspections
  1025. // ON p.id = latest_inspections.patient_id
  1026. // WHERE p.user_org_id = ?
  1027. // GROUP BY p.id, p.name
  1028. // `, strings.Join(selectFields, ", "))
  1029. //
  1030. // var results []map[string]interface{}
  1031. // rows, err := readDb.Raw(query, yearMonth, org_id, org_id).Rows() // 注意这里多传一个org_id参数
  1032. // if err != nil {
  1033. // return nil, err
  1034. // }
  1035. // defer rows.Close()
  1036. //
  1037. // columns, err := rows.Columns()
  1038. // if err != nil {
  1039. // return nil, err
  1040. // }
  1041. //
  1042. // for rows.Next() {
  1043. // // 创建一个长度为列数的切片,用于存储每一行的值
  1044. // columnValues := make([]interface{}, len(columns))
  1045. // columnPointers := make([]interface{}, len(columns))
  1046. // for i := range columnValues {
  1047. // columnPointers[i] = &columnValues[i]
  1048. // }
  1049. //
  1050. // // 扫描当前行的值
  1051. // if err := rows.Scan(columnPointers...); err != nil {
  1052. // return nil, err
  1053. // }
  1054. //
  1055. // // 将扫描到的值放入结果 map 中
  1056. // result := make(map[string]interface{})
  1057. // for i, colName := range columns {
  1058. // val := columnPointers[i].(*interface{})
  1059. // result[colName] = *val
  1060. // }
  1061. //
  1062. // results = append(results, result)
  1063. // }
  1064. //
  1065. // return results, nil
  1066. //}
  1067. func createDynamicStruct(fields map[string]interface{}) reflect.Type {
  1068. var structFields []reflect.StructField
  1069. for name, value := range fields {
  1070. structFields = append(structFields, reflect.StructField{
  1071. Name: strings.Title(name),
  1072. Type: reflect.TypeOf(value),
  1073. Tag: reflect.StructTag(fmt.Sprintf(`json:"%s"`, name)),
  1074. })
  1075. }
  1076. return reflect.StructOf(structFields)
  1077. }
  1078. func GetDialysisPrescriptionInfo(start_date string, end_date string, mode int64, org_id int64, page int64, limit int64) (qcp []models.QCPrescription, total int64, err error) {
  1079. offset := (page - 1) * limit
  1080. err = readDb.Model(&models.QCPrescription{}).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1081. return readUserDb.Where("status = 1 and org_id=?", org_id)
  1082. }).Preload("QCPatients", "status = 1 and user_org_id = ?", org_id).Preload("QCDialysisOrder", func(db *gorm.DB) *gorm.DB {
  1083. return db.Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1084. return readUserDb.Where("status = 1 and org_id=?", org_id)
  1085. }).Preload("DeviceNumber", "status = 1 and org_id = ?", org_id).Where("status = 1 AND user_org_id = ?", org_id)
  1086. }).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
  1087. return
  1088. }
  1089. func GetScheduleInfo(start_date string, end_date string, mode int64, org_id int64, page int64, limit int64) (qcp []models.QCSchedule, total int64, err error) {
  1090. offset := (page - 1) * limit
  1091. err = readDb.Model(&models.QCSchedule{}).Preload("QCPatients", "status = 1 and user_org_id =?", org_id).Preload("QCSPrescription", func(db *gorm.DB) *gorm.DB {
  1092. return db.Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1093. return readUserDb.Where("status = 1 and org_id=?", org_id)
  1094. }).Where("status = 1 AND user_org_id = ?", org_id)
  1095. }).Preload("QCAssessmentAfterDislysis", "status = 1 and user_org_id = ?", org_id).Preload("QCDialysisOrder", func(db *gorm.DB) *gorm.DB {
  1096. return db.Preload("DeviceNumber", "status = 1").Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1097. return readUserDb.Where("status = 1 and org_id=?", org_id)
  1098. }).Where("status = 1 AND user_org_id = ?", org_id)
  1099. }).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
  1100. return
  1101. }