statistics_api_controller.go 67KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "fmt"
  8. "github.com/astaxie/beego"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type StatisticsApiController struct {
  15. BaseAuthAPIController
  16. }
  17. func StatisticsApiRegistRouters() {
  18. beego.Router("/api/statistisc/index", &StatisticsApiController{}, "get:GetStatistics")
  19. beego.Router("/api/qc/statistiscall/get", &StatisticsApiController{}, "get:GetAllInspectionStatistisc")
  20. beego.Router("/api/qc/statistiscperson/get", &StatisticsApiController{}, "get:GetPersonInspectionStatistisc")
  21. beego.Router("/api/qc/statistiscperson/list", &StatisticsApiController{}, "get:GetInsepctionList")
  22. beego.Router("/api/qc/patientstatistiscall/get", &StatisticsApiController{}, "get:GetPatientInspectionStatistisc")
  23. beego.Router("/api/qc/patientinspectionstatistis/get", &StatisticsApiController{}, "get:GetFivePatientInspectionStatistisc")
  24. beego.Router("/api/commonqc/statistiscall/get", &StatisticsApiController{}, "get:GetAllCommonInspectionStatistisc")
  25. beego.Router("/api/commonqc/statistiscperson/get", &StatisticsApiController{}, "get:GetPersonCommonInspectionStatistisc")
  26. beego.Router("/api/commonqc/patientstatistiscall/get", &StatisticsApiController{}, "get:GetPatientCommonInspectionStatistisc")
  27. //透析总量统计
  28. beego.Router("/api/commonqc/dialysis/total", &StatisticsApiController{}, "get:GetDialysisTotal")
  29. beego.Router("/api/commonqc/dialysis/detail", &StatisticsApiController{}, "get:GetDialysisTotalDetail")
  30. beego.Router("/api/commonqc/dialysis/detailsinfo", &StatisticsApiController{}, "get:GetDialysisTotalDetailInfo")
  31. beego.Router("/api/commonqc/anticoagulant", &StatisticsApiController{}, "get:GetAnticoagulant")
  32. beego.Router("/api/commonqc/anticoagulant/detail", &StatisticsApiController{}, "get:GetAnticoagulantDetail")
  33. beego.Router("/api/commonqc/dialyzer", &StatisticsApiController{}, "get:GetDialyzer")
  34. beego.Router("/api/commonqc/dialyzer/config", &StatisticsApiController{}, "get:GetDialyzerConfig")
  35. beego.Router("/api/commonqc/dialyzer/detail", &StatisticsApiController{}, "get:GetDialyzerDetail")
  36. beego.Router("/api/commonqc/dialysistreat/finish", &StatisticsApiController{}, "get:GetDialysisTreatFinsh")
  37. beego.Router("/api/commonqc/dialysistreat/detail", &StatisticsApiController{}, "get:GetDialysisTreatDetail")
  38. }
  39. func (c *StatisticsApiController) GetDialysisTreatFinsh() {
  40. start_date := c.GetString("start_date")
  41. end_date := c.GetString("end_date")
  42. timeLayout := "2006-01-02"
  43. loc, _ := time.LoadLocation("Local")
  44. var startTime int64
  45. if len(start_date) > 0 {
  46. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  47. fmt.Println("err-----------", err)
  48. if err != nil {
  49. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  50. return
  51. }
  52. startTime = theTime.Unix()
  53. }
  54. var endTime int64
  55. if len(end_date) > 0 {
  56. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  57. if err != nil {
  58. utils.ErrorLog(err.Error())
  59. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  60. return
  61. }
  62. endTime = theTime.Unix()
  63. }
  64. data, _ := service.GetDialysisCompletionRate(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime)
  65. total, _ := service.GetDialysisCompletionTotal(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime)
  66. c.ServeSuccessJSON(map[string]interface{}{
  67. "data": data,
  68. "total": total,
  69. })
  70. }
  71. func (c *StatisticsApiController) GetDialysisTreatDetail() {
  72. start_date := c.GetString("start_date")
  73. end_date := c.GetString("end_date")
  74. mode, _ := c.GetInt64("mode")
  75. page, _ := c.GetInt64("page", 0)
  76. limit, _ := c.GetInt64("limit", 0)
  77. if page <= 0 {
  78. page = 1
  79. }
  80. if limit <= 0 {
  81. limit = 10
  82. }
  83. timeLayout := "2006-01-02"
  84. loc, _ := time.LoadLocation("Local")
  85. var startTime int64
  86. if len(start_date) > 0 {
  87. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  88. fmt.Println("err-----------", err)
  89. if err != nil {
  90. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  91. return
  92. }
  93. startTime = theTime.Unix()
  94. }
  95. var endTime int64
  96. if len(end_date) > 0 {
  97. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  98. if err != nil {
  99. utils.ErrorLog(err.Error())
  100. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  101. return
  102. }
  103. endTime = theTime.Unix()
  104. }
  105. data, total, _ := service.GetDialysisCompletionDetail(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime, mode, limit, page)
  106. c.ServeSuccessJSON(map[string]interface{}{
  107. "list": data,
  108. "total": total,
  109. })
  110. }
  111. func (c *StatisticsApiController) GetDialysisTotal() {
  112. start_date := c.GetString("start_date")
  113. end_date := c.GetString("end_date")
  114. mode, _ := c.GetInt64("mode")
  115. origin, _ := c.GetInt64("origin")
  116. time_way, _ := c.GetInt64("time_way")
  117. timeLayout := "2006-01-02"
  118. loc, _ := time.LoadLocation("Local")
  119. var startTime int64
  120. if len(start_date) > 0 {
  121. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  122. fmt.Println("err-----------", err)
  123. if err != nil {
  124. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  125. return
  126. }
  127. startTime = theTime.Unix()
  128. }
  129. var endTime int64
  130. if len(end_date) > 0 {
  131. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  132. if err != nil {
  133. utils.ErrorLog(err.Error())
  134. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  135. return
  136. }
  137. endTime = theTime.Unix()
  138. }
  139. if mode == 0 { //统计透析模式不限
  140. //获取总人数
  141. p_total, _ := service.GetNewDialysiTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, origin)
  142. count_struct, _ := service.GetNewDialysisCountMode(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, origin, 0)
  143. c.ServeSuccessJSON(map[string]interface{}{
  144. "patient_count": p_total,
  145. "list": count_struct,
  146. })
  147. } else { //固定某个模式
  148. // 拆分日期范围
  149. dates, err := splitDateRange(start_date, end_date)
  150. if err != nil {
  151. fmt.Println("Error:", err)
  152. return
  153. }
  154. var cuss []models.CustomDialysisData
  155. p_total, _ := service.GetNewDialysiTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, origin)
  156. switch time_way {
  157. case 1:
  158. // 按周统计
  159. weeks := groupByWeek(dates)
  160. fmt.Println(weeks)
  161. for _, week := range weeks {
  162. // 解析字符串时显式指定时区为 UTC
  163. t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", week[0].String())
  164. if err != nil {
  165. fmt.Println("解析时间出错:", err)
  166. return
  167. }
  168. // 格式化时间时将时区设置为 UTC
  169. //fmt.Println(t.UTC().Format("2006-01-02 15:04:05"))
  170. var cus models.CustomDialysisData
  171. //week_date := week[0].Format("2006-01-02") + " 00:00:00"
  172. //date, err := time.Parse("2006-01-02 15:04:05", week_date)
  173. //if err != nil {
  174. // fmt.Println("日期解析错误:", err)
  175. // return
  176. //}
  177. // 将time.Time类型转换为时间戳(秒)
  178. //week_start := date.Unix()
  179. // 解析字符串时显式指定时区为 UTC
  180. t2, err := time.Parse("2006-01-02 15:04:05 -0700 MST", week[len(week)-1].String())
  181. if err != nil {
  182. fmt.Println("解析时间出错:", err)
  183. return
  184. }
  185. //week_end_date := week[len(week)-1].Format("2006-01-02") + " 00:00:00"
  186. //end_date, err := time.Parse("2006-01-02 15:04:05", week_end_date)
  187. //if err != nil {
  188. // fmt.Println("日期解析错误:", err)
  189. // return
  190. //}
  191. //
  192. //// 将time.Time类型转换为时间戳(秒)
  193. //week_end := end_date.Unix()
  194. counts, _ := service.GetNewDialysisCountModeTwo(t.Unix(), t2.Unix(), c.GetAdminUserInfo().CurrentOrgId, origin, mode)
  195. cus.Date = strings.Split(week[0].String(), " ")[0] + "~" + strings.Split(week[len(week)-1].String(), " ")[0]
  196. cus.Count = counts.Count
  197. cus.Total = p_total
  198. cuss = append(cuss, cus)
  199. }
  200. c.ServeSuccessJSON(map[string]interface{}{
  201. "list": cuss,
  202. })
  203. break
  204. case 2:
  205. startDate, err := time.Parse("2006-01-02", start_date)
  206. if err != nil {
  207. fmt.Println("Error parsing start date:", err)
  208. return
  209. }
  210. endDate, err := time.Parse("2006-01-02", end_date)
  211. if err != nil {
  212. fmt.Println("Error parsing end date:", err)
  213. return
  214. }
  215. dailyDates := splitByDay(startDate, endDate)
  216. for _, date := range dailyDates {
  217. //fmt.Println(date)
  218. var cus models.CustomDialysisData
  219. counts, _ := service.GetNewDialysisCountModeTwo(date.Unix(), date.Unix(), c.GetAdminUserInfo().CurrentOrgId, origin, mode)
  220. cus.Date = date.Format("2006-01-02")
  221. cus.Count = counts.Count
  222. cus.Total = p_total
  223. cuss = append(cuss, cus)
  224. }
  225. c.ServeSuccessJSON(map[string]interface{}{
  226. "list": cuss,
  227. })
  228. break
  229. case 3:
  230. // 按月统计
  231. months := groupByMonth(dates)
  232. for _, month := range months {
  233. var cus models.CustomDialysisData
  234. counts, _ := service.GetNewDialysisCountModeTwo(month[0].Unix(), month[len(month)-1].Unix(), c.GetAdminUserInfo().CurrentOrgId, origin, mode)
  235. cus.Date = strings.Split(month[0].String(), " ")[0] + "~" + strings.Split(month[len(month)-1].String(), " ")[0]
  236. cus.Count = counts.Count
  237. cus.Total = p_total
  238. cuss = append(cuss, cus)
  239. //fmt.Println("Month:", month[0], "~", month[len(month)-1])
  240. }
  241. c.ServeSuccessJSON(map[string]interface{}{
  242. "list": cuss,
  243. })
  244. break
  245. case 4:
  246. // 按年统计
  247. years := groupByYear(dates)
  248. for _, year := range years {
  249. var cus models.CustomDialysisData
  250. counts, _ := service.GetNewDialysisCountModeTwo(year[0].Unix(), year[len(year)-1].Unix(), c.GetAdminUserInfo().CurrentOrgId, origin, mode)
  251. cus.Date = strings.Split(strings.Split(year[0].String(), " ")[0], "-")[0]
  252. cus.Count = counts.Count
  253. cus.Total = p_total
  254. cuss = append(cuss, cus)
  255. }
  256. c.ServeSuccessJSON(map[string]interface{}{
  257. "list": cuss,
  258. })
  259. break
  260. }
  261. }
  262. }
  263. func (c *StatisticsApiController) GetDialysisTotalDetail() {
  264. start_date := c.GetString("start_date")
  265. end_date := c.GetString("end_date")
  266. mode, _ := c.GetInt64("mode")
  267. origin, _ := c.GetInt64("origin")
  268. time_way, _ := c.GetInt64("time_way")
  269. //timeLayout := "2006-01-02"
  270. //loc, _ := time.LoadLocation("Local")
  271. //var startTime int64
  272. //if len(start_date) > 0 {
  273. // theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  274. // fmt.Println("err-----------", err)
  275. // if err != nil {
  276. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  277. // return
  278. // }
  279. // //startTime = theTime.Unix()
  280. //}
  281. //var endTime int64
  282. //if len(end_date) > 0 {
  283. // theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  284. // if err != nil {
  285. // utils.ErrorLog(err.Error())
  286. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  287. // return
  288. // }
  289. // //endTime = theTime.Unix()
  290. //}
  291. dates, err := splitDateRange(start_date, end_date)
  292. if err != nil {
  293. fmt.Println("Error:", err)
  294. return
  295. }
  296. if origin == 1 {
  297. switch time_way {
  298. case 2:
  299. weeks := groupByWeek(dates)
  300. //var uniqueFields []map[string]interface{}
  301. var uniqueFields []map[string]interface{}
  302. for _, week := range weeks {
  303. // 解析字符串时显式指定时区为 UTC
  304. t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", week[0].String())
  305. if err != nil {
  306. fmt.Println("解析时间出错:", err)
  307. return
  308. }
  309. // 解析字符串时显式指定时区为 UTC
  310. t2, err := time.Parse("2006-01-02 15:04:05 -0700 MST", week[len(week)-1].String())
  311. if err != nil {
  312. fmt.Println("解析时间出错:", err)
  313. return
  314. }
  315. //dynamicFields := make([]map[string]interface{}, 0)
  316. list, _ := service.GetDialysisStats(t.Unix(), t2.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  317. //fmt.Println(aa)
  318. var dynamicFields []map[string]interface{}
  319. //var uniqueFields []map[string]interface{}
  320. for _, result := range list {
  321. nb := result["日期"].([]byte)
  322. name := string(nb)
  323. dynamicField := make(map[string]interface{})
  324. dynamicField["日期"] = name
  325. numericColumns := make(map[string]interface{})
  326. nonNumericColumns := make(map[string]interface{})
  327. // 分类处理列
  328. for columnName, columnValue := range result {
  329. if columnName == "日期" || columnName == "合计" {
  330. continue
  331. }
  332. if columnValue == nil {
  333. nonNumericColumns[columnName] = ""
  334. continue
  335. }
  336. byteValue, ok := columnValue.([]byte)
  337. if !ok {
  338. fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  339. continue
  340. }
  341. strValue := string(byteValue)
  342. floatValue, err := strconv.ParseFloat(strValue, 64)
  343. if err != nil {
  344. fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  345. nonNumericColumns[columnName] = strValue
  346. } else {
  347. if floatValue == 0 {
  348. nonNumericColumns[columnName] = ""
  349. } else {
  350. numericColumns[columnName] = floatValue
  351. }
  352. }
  353. }
  354. // 添加数值列
  355. for columnName, columnValue := range numericColumns {
  356. dynamicField[columnName] = columnValue
  357. }
  358. // 添加非数值列
  359. for columnName, columnValue := range nonNumericColumns {
  360. dynamicField[columnName] = columnValue
  361. }
  362. hj := result["合计"].(int64)
  363. dynamicField["合计"] = hj
  364. dynamicFields = append(dynamicFields, dynamicField)
  365. }
  366. seen := make(map[interface{}]struct{})
  367. for _, field := range dynamicFields {
  368. value := field["日期"]
  369. if _, ok := seen[value]; !ok {
  370. seen[value] = struct{}{}
  371. uniqueFields = append(uniqueFields, field)
  372. // 对字段进行排序,有值的排在前面
  373. sortedField := sortFieldsByValue(field)
  374. uniqueFields = append(uniqueFields, sortedField)
  375. }
  376. }
  377. }
  378. c.ServeSuccessJSON(map[string]interface{}{
  379. "list": uniqueFields,
  380. })
  381. break
  382. case 1:
  383. startDate, err := time.Parse("2006-01-02", start_date)
  384. if err != nil {
  385. fmt.Println("Error parsing start date:", err)
  386. return
  387. }
  388. endDate, err := time.Parse("2006-01-02", end_date)
  389. if err != nil {
  390. fmt.Println("Error parsing end date:", err)
  391. return
  392. }
  393. dailyDates := splitByDay(startDate, endDate)
  394. var uniqueFields []map[string]interface{}
  395. for _, date := range dailyDates {
  396. list, _ := service.GetDialysisStats(date.Unix(), date.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  397. var dynamicFields []map[string]interface{}
  398. for _, result := range list {
  399. nb := result["日期"].([]byte)
  400. name := string(nb)
  401. dynamicField := make(map[string]interface{})
  402. dynamicField["日期"] = name
  403. numericColumns := make(map[string]interface{})
  404. nonNumericColumns := make(map[string]interface{})
  405. // 分类处理列
  406. for columnName, columnValue := range result {
  407. if columnName == "日期" || columnName == "合计" {
  408. continue
  409. }
  410. if columnValue == nil {
  411. nonNumericColumns[columnName] = ""
  412. continue
  413. }
  414. byteValue, ok := columnValue.([]byte)
  415. if !ok {
  416. fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  417. continue
  418. }
  419. strValue := string(byteValue)
  420. floatValue, err := strconv.ParseFloat(strValue, 64)
  421. if err != nil {
  422. fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  423. nonNumericColumns[columnName] = strValue
  424. } else {
  425. if floatValue == 0 {
  426. nonNumericColumns[columnName] = ""
  427. } else {
  428. numericColumns[columnName] = floatValue
  429. }
  430. }
  431. }
  432. // 添加数值列
  433. for columnName, columnValue := range numericColumns {
  434. dynamicField[columnName] = columnValue
  435. }
  436. // 添加非数值列
  437. for columnName, columnValue := range nonNumericColumns {
  438. dynamicField[columnName] = columnValue
  439. }
  440. hj := result["合计"].(int64)
  441. dynamicField["合计"] = hj
  442. dynamicFields = append(dynamicFields, dynamicField)
  443. }
  444. seen := make(map[interface{}]struct{})
  445. for _, field := range dynamicFields {
  446. value := field["日期"]
  447. if _, ok := seen[value]; !ok {
  448. seen[value] = struct{}{}
  449. uniqueFields = append(uniqueFields, field)
  450. }
  451. }
  452. }
  453. //for _, date := range dailyDates {
  454. // list, _ := service.GetDialysisStats(date.Unix(), date.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  455. // //fmt.Println(aa)
  456. // var dynamicFields []map[string]interface{}
  457. //
  458. // for _, result := range list {
  459. // // 打印患者ID
  460. // nb := result["日期"].([]byte)
  461. // name := string(nb)
  462. //
  463. // // 创建动态字段的map
  464. // dynamicField := make(map[string]interface{})
  465. // // 将日期放在首位
  466. // dynamicField["日期"] = name
  467. //
  468. // // 临时存储无数值的列
  469. // nonNumericColumns := make(map[string]interface{})
  470. //
  471. // // 先处理包含数值的列
  472. // for columnName, columnValue := range result {
  473. // if columnName == "日期" || columnName == "合计" {
  474. // continue
  475. // }
  476. // if columnValue == nil {
  477. // nonNumericColumns[columnName] = ""
  478. // continue
  479. // }
  480. //
  481. // byteValue, ok := columnValue.([]byte)
  482. // if !ok {
  483. // fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  484. // continue
  485. // }
  486. //
  487. // strValue := string(byteValue)
  488. //
  489. // floatValue, err := strconv.ParseFloat(strValue, 64)
  490. // if err != nil {
  491. // fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  492. // nonNumericColumns[columnName] = strValue
  493. // } else {
  494. // if floatValue == 0 {
  495. // nonNumericColumns[columnName] = ""
  496. // } else {
  497. // dynamicField[columnName] = floatValue
  498. // }
  499. // }
  500. // }
  501. //
  502. // // 添加无数值的列
  503. // for columnName, columnValue := range nonNumericColumns {
  504. // dynamicField[columnName] = columnValue
  505. // }
  506. //
  507. // // 将合计放在末尾
  508. // hj := result["合计"].(int64)
  509. // dynamicField["合计"] = hj
  510. //
  511. // dynamicFields = append(dynamicFields, dynamicField)
  512. // }
  513. //
  514. // // 将日期放在首位
  515. // seen := make(map[interface{}]struct{})
  516. // for _, field := range dynamicFields {
  517. // value := field["日期"]
  518. // if _, ok := seen[value]; !ok {
  519. // seen[value] = struct{}{}
  520. // uniqueFields = append(uniqueFields, field)
  521. // }
  522. // }
  523. //}
  524. //for _, date := range dailyDates {
  525. // list, _ := service.GetDialysisStats(date.Unix(), date.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  526. // //fmt.Println(aa)
  527. // var dynamicFields []map[string]interface{}
  528. //
  529. // for _, result := range list {
  530. // // 打印患者ID
  531. // nb := result["日期"].([]byte)
  532. // name := string(nb)
  533. //
  534. // // 创建动态字段的map
  535. // dynamicField := make(map[string]interface{})
  536. // // 将日期放在首位
  537. // dynamicField["日期"] = name
  538. //
  539. // // 打印其他列的值,并添加到动态字段中
  540. // for columnName, columnValue := range result {
  541. // if columnName == "日期" {
  542. // continue
  543. // }
  544. // if columnName == "合计" {
  545. // continue
  546. // }
  547. // if columnValue == nil {
  548. // dynamicField[columnName] = ""
  549. // continue
  550. // }
  551. //
  552. // byteValue, ok := columnValue.([]byte)
  553. // if !ok {
  554. // fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  555. // continue
  556. // }
  557. //
  558. // strValue := string(byteValue)
  559. //
  560. // floatValue, err := strconv.ParseFloat(strValue, 64)
  561. // if err != nil {
  562. // fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  563. // } else {
  564. // if floatValue == 0 {
  565. // dynamicField[columnName] = ""
  566. // } else {
  567. // dynamicField[columnName] = floatValue
  568. // }
  569. // }
  570. // }
  571. //
  572. // // 将合计放在末尾
  573. // hj := result["合计"].(int64)
  574. // dynamicField["合计"] = hj
  575. //
  576. // dynamicFields = append(dynamicFields, dynamicField)
  577. // }
  578. //
  579. // // 将日期放在首位
  580. // seen := make(map[interface{}]struct{})
  581. // for _, field := range dynamicFields {
  582. // value := field["日期"]
  583. // if _, ok := seen[value]; !ok {
  584. // seen[value] = struct{}{}
  585. // uniqueFields = append(uniqueFields, field)
  586. // }
  587. // }
  588. //}
  589. c.ServeSuccessJSON(map[string]interface{}{
  590. "list": uniqueFields,
  591. })
  592. break
  593. case 3:
  594. months := groupByMonth(dates)
  595. var uniqueFields []map[string]interface{}
  596. for _, month := range months {
  597. // 解析字符串时显式指定时区为 UTC
  598. t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", month[0].String())
  599. if err != nil {
  600. fmt.Println("解析时间出错:", err)
  601. return
  602. }
  603. // 解析字符串时显式指定时区为 UTC
  604. t2, err := time.Parse("2006-01-02 15:04:05 -0700 MST", month[len(month)-1].String())
  605. if err != nil {
  606. fmt.Println("解析时间出错:", err)
  607. return
  608. }
  609. //dynamicFields := make([]map[string]interface{}, 0)
  610. list, _ := service.GetDialysisStats(t.Unix(), t2.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  611. //fmt.Println(aa)
  612. var dynamicFields []map[string]interface{}
  613. //var uniqueFields []map[string]interface{}
  614. for _, result := range list {
  615. // 打印患者ID
  616. nb := result["日期"].([]byte)
  617. name := string(nb)
  618. // 创建动态字段的map
  619. dynamicField := make(map[string]interface{})
  620. // 将日期放在首位
  621. dynamicField["日期"] = name
  622. // 打印其他列的值,并添加到动态字段中
  623. for columnName, columnValue := range result {
  624. if columnName == "日期" {
  625. continue
  626. }
  627. if columnName == "合计" {
  628. continue
  629. }
  630. if columnValue == nil {
  631. dynamicField[columnName] = ""
  632. continue
  633. }
  634. byteValue, ok := columnValue.([]byte)
  635. if !ok {
  636. fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  637. continue
  638. }
  639. strValue := string(byteValue)
  640. floatValue, err := strconv.ParseFloat(strValue, 64)
  641. if err != nil {
  642. fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  643. } else {
  644. if floatValue == 0 {
  645. dynamicField[columnName] = ""
  646. } else {
  647. dynamicField[columnName] = floatValue
  648. }
  649. }
  650. }
  651. // 将合计放在末尾
  652. hj := result["合计"].(int64)
  653. dynamicField["合计"] = hj
  654. dynamicFields = append(dynamicFields, dynamicField)
  655. }
  656. // 将日期放在首位
  657. seen := make(map[interface{}]struct{})
  658. for _, field := range dynamicFields {
  659. value := field["日期"]
  660. if _, ok := seen[value]; !ok {
  661. seen[value] = struct{}{}
  662. uniqueFields = append(uniqueFields, field)
  663. }
  664. }
  665. }
  666. c.ServeSuccessJSON(map[string]interface{}{
  667. "list": uniqueFields,
  668. })
  669. break
  670. case 4:
  671. years := groupByYear(dates)
  672. var uniqueFields []map[string]interface{}
  673. for _, year := range years {
  674. // 解析字符串时显式指定时区为 UTC
  675. t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", year[0].String())
  676. if err != nil {
  677. fmt.Println("解析时间出错:", err)
  678. return
  679. }
  680. // 解析字符串时显式指定时区为 UTC
  681. t2, err := time.Parse("2006-01-02 15:04:05 -0700 MST", year[len(year)-1].String())
  682. if err != nil {
  683. fmt.Println("解析时间出错:", err)
  684. return
  685. }
  686. //dynamicFields := make([]map[string]interface{}, 0)
  687. list, _ := service.GetDialysisStats(t.Unix(), t2.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  688. //fmt.Println(aa)
  689. var dynamicFields []map[string]interface{}
  690. //var uniqueFields []map[string]interface{}
  691. for _, result := range list {
  692. // 打印患者ID
  693. nb := result["日期"].([]byte)
  694. name := string(nb)
  695. // 创建动态字段的map
  696. dynamicField := make(map[string]interface{})
  697. // 将日期放在首位
  698. dynamicField["日期"] = name
  699. // 打印其他列的值,并添加到动态字段中
  700. for columnName, columnValue := range result {
  701. if columnName == "日期" {
  702. continue
  703. }
  704. if columnName == "合计" {
  705. continue
  706. }
  707. if columnValue == nil {
  708. dynamicField[columnName] = ""
  709. continue
  710. }
  711. byteValue, ok := columnValue.([]byte)
  712. if !ok {
  713. fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  714. continue
  715. }
  716. strValue := string(byteValue)
  717. floatValue, err := strconv.ParseFloat(strValue, 64)
  718. if err != nil {
  719. fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  720. } else {
  721. if floatValue == 0 {
  722. dynamicField[columnName] = ""
  723. } else {
  724. dynamicField[columnName] = floatValue
  725. }
  726. }
  727. }
  728. // 将合计放在末尾
  729. hj := result["合计"].(int64)
  730. dynamicField["合计"] = hj
  731. dynamicFields = append(dynamicFields, dynamicField)
  732. }
  733. // 将日期放在首位
  734. seen := make(map[interface{}]struct{})
  735. for _, field := range dynamicFields {
  736. value := field["日期"]
  737. if _, ok := seen[value]; !ok {
  738. seen[value] = struct{}{}
  739. uniqueFields = append(uniqueFields, field)
  740. }
  741. }
  742. }
  743. c.ServeSuccessJSON(map[string]interface{}{
  744. "list": uniqueFields,
  745. })
  746. break
  747. }
  748. } else {
  749. switch time_way {
  750. case 2:
  751. weeks := groupByWeek(dates)
  752. //var uniqueFields []map[string]interface{}
  753. var uniqueFields []map[string]interface{}
  754. for _, week := range weeks {
  755. // 解析字符串时显式指定时区为 UTC
  756. t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", week[0].String())
  757. if err != nil {
  758. fmt.Println("解析时间出错:", err)
  759. return
  760. }
  761. // 解析字符串时显式指定时区为 UTC
  762. t2, err := time.Parse("2006-01-02 15:04:05 -0700 MST", week[len(week)-1].String())
  763. if err != nil {
  764. fmt.Println("解析时间出错:", err)
  765. return
  766. }
  767. //dynamicFields := make([]map[string]interface{}, 0)
  768. list, _ := service.GetScheduleStats(t.Unix(), t2.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  769. //fmt.Println(aa)
  770. var dynamicFields []map[string]interface{}
  771. //var uniqueFields []map[string]interface{}
  772. for _, result := range list {
  773. // 打印患者ID
  774. nb := result["日期"].([]byte)
  775. name := string(nb)
  776. // 创建动态字段的map
  777. dynamicField := make(map[string]interface{})
  778. // 将日期放在首位
  779. dynamicField["日期"] = name
  780. // 打印其他列的值,并添加到动态字段中
  781. for columnName, columnValue := range result {
  782. if columnName == "日期" {
  783. continue
  784. }
  785. if columnName == "合计" {
  786. continue
  787. }
  788. if columnValue == nil {
  789. dynamicField[columnName] = ""
  790. continue
  791. }
  792. byteValue, ok := columnValue.([]byte)
  793. if !ok {
  794. fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  795. continue
  796. }
  797. strValue := string(byteValue)
  798. floatValue, err := strconv.ParseFloat(strValue, 64)
  799. if err != nil {
  800. fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  801. } else {
  802. if floatValue == 0 {
  803. dynamicField[columnName] = ""
  804. } else {
  805. dynamicField[columnName] = floatValue
  806. }
  807. }
  808. }
  809. // 将合计放在末尾
  810. hj := result["合计"].(int64)
  811. dynamicField["合计"] = hj
  812. dynamicFields = append(dynamicFields, dynamicField)
  813. }
  814. // 将日期放在首位
  815. seen := make(map[interface{}]struct{})
  816. for _, field := range dynamicFields {
  817. value := field["日期"]
  818. if _, ok := seen[value]; !ok {
  819. seen[value] = struct{}{}
  820. uniqueFields = append(uniqueFields, field)
  821. }
  822. }
  823. }
  824. c.ServeSuccessJSON(map[string]interface{}{
  825. "list": uniqueFields,
  826. })
  827. break
  828. case 1:
  829. startDate, err := time.Parse("2006-01-02", start_date)
  830. if err != nil {
  831. fmt.Println("Error parsing start date:", err)
  832. return
  833. }
  834. endDate, err := time.Parse("2006-01-02", end_date)
  835. if err != nil {
  836. fmt.Println("Error parsing end date:", err)
  837. return
  838. }
  839. dailyDates := splitByDay(startDate, endDate)
  840. var uniqueFields []map[string]interface{}
  841. for _, date := range dailyDates {
  842. list, _ := service.GetScheduleStats(date.Unix(), date.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  843. //fmt.Println(aa)
  844. var dynamicFields []map[string]interface{}
  845. for _, result := range list {
  846. // 打印患者ID
  847. nb := result["日期"].([]byte)
  848. name := string(nb)
  849. // 创建动态字段的map
  850. dynamicField := make(map[string]interface{})
  851. // 将日期放在首位
  852. dynamicField["日期"] = name
  853. // 打印其他列的值,并添加到动态字段中
  854. for columnName, columnValue := range result {
  855. if columnName == "日期" {
  856. continue
  857. }
  858. if columnName == "合计" {
  859. continue
  860. }
  861. if columnValue == nil {
  862. dynamicField[columnName] = ""
  863. continue
  864. }
  865. byteValue, ok := columnValue.([]byte)
  866. if !ok {
  867. fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  868. continue
  869. }
  870. strValue := string(byteValue)
  871. floatValue, err := strconv.ParseFloat(strValue, 64)
  872. if err != nil {
  873. fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  874. } else {
  875. if floatValue == 0 {
  876. dynamicField[columnName] = ""
  877. } else {
  878. dynamicField[columnName] = floatValue
  879. }
  880. }
  881. }
  882. // 将合计放在末尾
  883. hj := result["合计"].(int64)
  884. dynamicField["合计"] = hj
  885. dynamicFields = append(dynamicFields, dynamicField)
  886. }
  887. // 将日期放在首位
  888. seen := make(map[interface{}]struct{})
  889. for _, field := range dynamicFields {
  890. value := field["日期"]
  891. if _, ok := seen[value]; !ok {
  892. seen[value] = struct{}{}
  893. uniqueFields = append(uniqueFields, field)
  894. }
  895. }
  896. }
  897. c.ServeSuccessJSON(map[string]interface{}{
  898. "list": uniqueFields,
  899. })
  900. break
  901. case 3:
  902. months := groupByMonth(dates)
  903. var uniqueFields []map[string]interface{}
  904. for _, month := range months {
  905. // 解析字符串时显式指定时区为 UTC
  906. t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", month[0].String())
  907. if err != nil {
  908. fmt.Println("解析时间出错:", err)
  909. return
  910. }
  911. // 解析字符串时显式指定时区为 UTC
  912. t2, err := time.Parse("2006-01-02 15:04:05 -0700 MST", month[len(month)-1].String())
  913. if err != nil {
  914. fmt.Println("解析时间出错:", err)
  915. return
  916. }
  917. //dynamicFields := make([]map[string]interface{}, 0)
  918. list, _ := service.GetScheduleStats(t.Unix(), t2.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  919. //fmt.Println(aa)
  920. var dynamicFields []map[string]interface{}
  921. //var uniqueFields []map[string]interface{}
  922. for _, result := range list {
  923. // 打印患者ID
  924. nb := result["日期"].([]byte)
  925. name := string(nb)
  926. // 创建动态字段的map
  927. dynamicField := make(map[string]interface{})
  928. // 将日期放在首位
  929. dynamicField["日期"] = name
  930. // 打印其他列的值,并添加到动态字段中
  931. for columnName, columnValue := range result {
  932. if columnName == "日期" {
  933. continue
  934. }
  935. if columnName == "合计" {
  936. continue
  937. }
  938. if columnValue == nil {
  939. dynamicField[columnName] = ""
  940. continue
  941. }
  942. byteValue, ok := columnValue.([]byte)
  943. if !ok {
  944. fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  945. continue
  946. }
  947. strValue := string(byteValue)
  948. floatValue, err := strconv.ParseFloat(strValue, 64)
  949. if err != nil {
  950. fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  951. } else {
  952. if floatValue == 0 {
  953. dynamicField[columnName] = ""
  954. } else {
  955. dynamicField[columnName] = floatValue
  956. }
  957. }
  958. }
  959. // 将合计放在末尾
  960. hj := result["合计"].(int64)
  961. dynamicField["合计"] = hj
  962. dynamicFields = append(dynamicFields, dynamicField)
  963. }
  964. // 将日期放在首位
  965. seen := make(map[interface{}]struct{})
  966. for _, field := range dynamicFields {
  967. value := field["日期"]
  968. if _, ok := seen[value]; !ok {
  969. seen[value] = struct{}{}
  970. uniqueFields = append(uniqueFields, field)
  971. }
  972. }
  973. }
  974. c.ServeSuccessJSON(map[string]interface{}{
  975. "list": uniqueFields,
  976. })
  977. break
  978. case 4:
  979. years := groupByYear(dates)
  980. var uniqueFields []map[string]interface{}
  981. for _, year := range years {
  982. // 解析字符串时显式指定时区为 UTC
  983. t, err := time.Parse("2006-01-02 15:04:05 -0700 MST", year[0].String())
  984. if err != nil {
  985. fmt.Println("解析时间出错:", err)
  986. return
  987. }
  988. // 解析字符串时显式指定时区为 UTC
  989. t2, err := time.Parse("2006-01-02 15:04:05 -0700 MST", year[len(year)-1].String())
  990. if err != nil {
  991. fmt.Println("解析时间出错:", err)
  992. return
  993. }
  994. //dynamicFields := make([]map[string]interface{}, 0)
  995. list, _ := service.GetScheduleStats(t.Unix(), t2.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
  996. //fmt.Println(aa)
  997. var dynamicFields []map[string]interface{}
  998. //var uniqueFields []map[string]interface{}
  999. for _, result := range list {
  1000. // 打印患者ID
  1001. nb := result["日期"].([]byte)
  1002. name := string(nb)
  1003. // 创建动态字段的map
  1004. dynamicField := make(map[string]interface{})
  1005. // 将日期放在首位
  1006. dynamicField["日期"] = name
  1007. // 打印其他列的值,并添加到动态字段中
  1008. for columnName, columnValue := range result {
  1009. if columnName == "日期" {
  1010. continue
  1011. }
  1012. if columnName == "合计" {
  1013. continue
  1014. }
  1015. if columnValue == nil {
  1016. dynamicField[columnName] = ""
  1017. continue
  1018. }
  1019. byteValue, ok := columnValue.([]byte)
  1020. if !ok {
  1021. fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  1022. continue
  1023. }
  1024. strValue := string(byteValue)
  1025. floatValue, err := strconv.ParseFloat(strValue, 64)
  1026. if err != nil {
  1027. fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  1028. } else {
  1029. if floatValue == 0 {
  1030. dynamicField[columnName] = ""
  1031. } else {
  1032. dynamicField[columnName] = floatValue
  1033. }
  1034. }
  1035. }
  1036. // 将合计放在末尾
  1037. hj := result["合计"].(int64)
  1038. dynamicField["合计"] = hj
  1039. dynamicFields = append(dynamicFields, dynamicField)
  1040. }
  1041. // 将日期放在首位
  1042. seen := make(map[interface{}]struct{})
  1043. for _, field := range dynamicFields {
  1044. value := field["日期"]
  1045. if _, ok := seen[value]; !ok {
  1046. seen[value] = struct{}{}
  1047. uniqueFields = append(uniqueFields, field)
  1048. }
  1049. }
  1050. }
  1051. c.ServeSuccessJSON(map[string]interface{}{
  1052. "list": uniqueFields,
  1053. })
  1054. break
  1055. }
  1056. }
  1057. }
  1058. func (c *StatisticsApiController) GetDialysisTotalDetailInfo() {
  1059. date_str := c.GetString("date")
  1060. mode, _ := c.GetInt64("mode")
  1061. origin, _ := c.GetInt64("origin")
  1062. page, _ := c.GetInt64("page")
  1063. limit, _ := c.GetInt64("limit")
  1064. start_date := strings.Split(date_str, "~")[0]
  1065. end_date := strings.Split(date_str, "~")[1]
  1066. if origin == 1 {
  1067. qcp, total, _ := service.GetDialysisPrescriptionInfo(start_date, end_date, mode, c.GetAdminUserInfo().CurrentOrgId, page, limit)
  1068. c.ServeSuccessJSON(map[string]interface{}{
  1069. "list": qcp,
  1070. "total": total,
  1071. })
  1072. } else {
  1073. qcp, total, _ := service.GetScheduleInfo(start_date, end_date, mode, c.GetAdminUserInfo().CurrentOrgId, page, limit)
  1074. c.ServeSuccessJSON(map[string]interface{}{
  1075. "list": qcp,
  1076. "total": total,
  1077. })
  1078. }
  1079. }
  1080. func (c *StatisticsApiController) GetAnticoagulant() {
  1081. start_date := c.GetString("start_date")
  1082. end_date := c.GetString("end_date")
  1083. timeLayout := "2006-01-02"
  1084. loc, _ := time.LoadLocation("Local")
  1085. var startTime int64
  1086. if len(start_date) > 0 {
  1087. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1088. fmt.Println("err-----------", err)
  1089. if err != nil {
  1090. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1091. return
  1092. }
  1093. startTime = theTime.Unix()
  1094. }
  1095. var endTime int64
  1096. if len(end_date) > 0 {
  1097. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1098. if err != nil {
  1099. utils.ErrorLog(err.Error())
  1100. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1101. return
  1102. }
  1103. endTime = theTime.Unix()
  1104. }
  1105. total, err2 := service.GetAnticoagulantTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
  1106. if err2 != nil {
  1107. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1108. return
  1109. }
  1110. anticoagulantData, err := service.GetAnticoagulantData(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
  1111. if err != nil {
  1112. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1113. return
  1114. }
  1115. type respData struct {
  1116. Name string `json:"name"`
  1117. Count int `json:"count"`
  1118. Percentage float64 `json:"percentage"`
  1119. }
  1120. var respDatas []respData
  1121. for anticoagulant, count := range anticoagulantData {
  1122. var respData respData
  1123. respData.Name = anticoagulant
  1124. respData.Count = count
  1125. respData.Percentage = float64(count) / float64(total) * 100
  1126. respDatas = append(respDatas, respData)
  1127. }
  1128. c.ServeSuccessJSON(map[string]interface{}{
  1129. "data": respDatas,
  1130. })
  1131. }
  1132. func (c *StatisticsApiController) GetAnticoagulantDetail() {
  1133. anticoagulant, _ := c.GetInt64("anticoagulant")
  1134. start_date := c.GetString("start_date")
  1135. end_date := c.GetString("end_date")
  1136. timeLayout := "2006-01-02"
  1137. page, _ := c.GetInt64("page", 0)
  1138. limit, _ := c.GetInt64("limit", 0)
  1139. if page <= 0 {
  1140. page = 1
  1141. }
  1142. if limit <= 0 {
  1143. limit = 10
  1144. }
  1145. loc, _ := time.LoadLocation("Local")
  1146. var startTime int64
  1147. if len(start_date) > 0 {
  1148. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1149. fmt.Println("err-----------", err)
  1150. if err != nil {
  1151. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1152. return
  1153. }
  1154. startTime = theTime.Unix()
  1155. }
  1156. var endTime int64
  1157. if len(end_date) > 0 {
  1158. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1159. if err != nil {
  1160. utils.ErrorLog(err.Error())
  1161. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1162. return
  1163. }
  1164. endTime = theTime.Unix()
  1165. }
  1166. prescriptions, total, _ := service.GetPrescriptionByAnticoagulant(page, limit, c.GetAdminUserInfo().CurrentOrgId, anticoagulant, startTime, endTime)
  1167. c.ServeSuccessJSON(map[string]interface{}{
  1168. "prescriptions": prescriptions,
  1169. "total": total,
  1170. })
  1171. }
  1172. func (c *StatisticsApiController) GetDialyzer() {
  1173. start_date := c.GetString("start_date")
  1174. end_date := c.GetString("end_date")
  1175. timeLayout := "2006-01-02"
  1176. loc, _ := time.LoadLocation("Local")
  1177. var startTime int64
  1178. if len(start_date) > 0 {
  1179. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1180. fmt.Println("err-----------", err)
  1181. if err != nil {
  1182. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1183. return
  1184. }
  1185. startTime = theTime.Unix()
  1186. }
  1187. var endTime int64
  1188. if len(end_date) > 0 {
  1189. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1190. if err != nil {
  1191. utils.ErrorLog(err.Error())
  1192. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1193. return
  1194. }
  1195. endTime = theTime.Unix()
  1196. }
  1197. total, err2 := service.GetDialyzerTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
  1198. if err2 != nil {
  1199. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1200. return
  1201. }
  1202. dialyzers, err := service.GetDialyzerData(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
  1203. DialysisIrrigation, err := service.GetDialysisIrrigationData(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
  1204. if err != nil {
  1205. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1206. return
  1207. }
  1208. type respData struct {
  1209. Name string `json:"name"`
  1210. Count int `json:"count"`
  1211. Percentage float64 `json:"percentage"`
  1212. }
  1213. var respDatas []respData
  1214. for _, item := range dialyzers {
  1215. var respData respData
  1216. respData.Name = item.Dialyzer
  1217. respData.Count = item.Count
  1218. respData.Percentage = float64(item.Count) / float64(total) * 100
  1219. respDatas = append(respDatas, respData)
  1220. }
  1221. for _, item := range DialysisIrrigation {
  1222. var respData respData
  1223. respData.Name = item.Dialyzer
  1224. respData.Count = item.Count
  1225. respData.Percentage = float64(item.Count) / float64(total) * 100
  1226. respDatas = append(respDatas, respData)
  1227. }
  1228. c.ServeSuccessJSON(map[string]interface{}{
  1229. "data": respDatas,
  1230. })
  1231. }
  1232. func (c *StatisticsApiController) GetDialyzerDetail() {
  1233. dialyzer := c.GetString("dialyzer")
  1234. start_date := c.GetString("start_date")
  1235. end_date := c.GetString("end_date")
  1236. timeLayout := "2006-01-02"
  1237. page, _ := c.GetInt64("page", 0)
  1238. limit, _ := c.GetInt64("limit", 0)
  1239. if page <= 0 {
  1240. page = 1
  1241. }
  1242. if limit <= 0 {
  1243. limit = 10
  1244. }
  1245. loc, _ := time.LoadLocation("Local")
  1246. var startTime int64
  1247. if len(start_date) > 0 {
  1248. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1249. fmt.Println("err-----------", err)
  1250. if err != nil {
  1251. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1252. return
  1253. }
  1254. startTime = theTime.Unix()
  1255. }
  1256. var endTime int64
  1257. if len(end_date) > 0 {
  1258. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1259. if err != nil {
  1260. utils.ErrorLog(err.Error())
  1261. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1262. return
  1263. }
  1264. endTime = theTime.Unix()
  1265. }
  1266. prescriptions, total, _ := service.GetPrescriptionByDialyzer(page, limit, c.GetAdminUserInfo().CurrentOrgId, dialyzer, startTime, endTime)
  1267. c.ServeSuccessJSON(map[string]interface{}{
  1268. "prescriptions": prescriptions,
  1269. "total": total,
  1270. })
  1271. }
  1272. func (c *StatisticsApiController) GetDialyzerConfig() {
  1273. dialyzers, _ := service.GetDialyzerSummary(c.GetAdminUserInfo().CurrentOrgId)
  1274. Irrigation, _ := service.GetIrrigationSummary(c.GetAdminUserInfo().CurrentOrgId)
  1275. dialyzers = append(dialyzers, Irrigation...)
  1276. c.ServeSuccessJSON(map[string]interface{}{
  1277. "dialyzers": dialyzers,
  1278. })
  1279. }
  1280. // 配置检验数据查询
  1281. func (this *StatisticsApiController) GetInsepctionList() {
  1282. adminUser := this.GetAdminUserInfo()
  1283. orgid := adminUser.CurrentOrgId
  1284. configurationlist, err := service.GetInsepctionConfigurationList(orgid)
  1285. if err != nil {
  1286. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  1287. return
  1288. }
  1289. this.ServeSuccessJSON(map[string]interface{}{
  1290. "configurationlist": configurationlist,
  1291. })
  1292. }
  1293. func (c *StatisticsApiController) GetAllCommonInspectionStatistisc() {
  1294. start_date := c.GetString("start_date")
  1295. end_date := c.GetString("end_date")
  1296. project_id, _ := c.GetInt64("project_id")
  1297. item_id, _ := c.GetInt64("item_id")
  1298. timeLayout := "2006-01-02"
  1299. loc, _ := time.LoadLocation("Local")
  1300. var startTime int64
  1301. if len(start_date) > 0 {
  1302. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1303. fmt.Println("err-----------", err)
  1304. if err != nil {
  1305. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1306. return
  1307. }
  1308. startTime = theTime.Unix()
  1309. }
  1310. var endTime int64
  1311. if len(end_date) > 0 {
  1312. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1313. if err != nil {
  1314. utils.ErrorLog(err.Error())
  1315. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1316. return
  1317. }
  1318. endTime = theTime.Unix()
  1319. }
  1320. //患者总数
  1321. patientCount := service.GetPatientCount(c.GetAdminUserInfo().CurrentOrgId)
  1322. //获取配置
  1323. reference, _ := service.GetInspectionReferenceFour(project_id, item_id, c.GetAdminUserInfo().CurrentOrgId)
  1324. //获取数值在正常范围内的总数
  1325. config, _ := service.GetConfigurationById(project_id, item_id, c.GetAdminUserInfo().CurrentOrgId)
  1326. max, _ := strconv.ParseFloat(config.LargeRange, 64)
  1327. min, _ := strconv.ParseFloat(config.MinRange, 64)
  1328. normalTotal, _ := service.GetNormalInspectionTotalByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, max, min)
  1329. //获取数值异常的总数
  1330. unusualTotal, _ := service.GetUnusualInspectionTotalByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, max, min)
  1331. //获取没有检查的患者总数
  1332. noCheckTotal, _ := service.GetPatientNotInspectionTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName)
  1333. c.ServeSuccessJSON(map[string]interface{}{
  1334. "patient_count": patientCount,
  1335. "normal_total": normalTotal,
  1336. "unusual_total": unusualTotal,
  1337. "no_check_total": noCheckTotal,
  1338. "reference": reference,
  1339. "config": config,
  1340. })
  1341. }
  1342. func (c *StatisticsApiController) GetPersonCommonInspectionStatistisc() {
  1343. start_date := c.GetString("start_date")
  1344. end_date := c.GetString("end_date")
  1345. project_id, _ := c.GetInt64("project_id")
  1346. item_id, _ := c.GetInt64("item_id")
  1347. patient_id, _ := c.GetInt64("patient_id")
  1348. timeLayout := "2006-01-02"
  1349. loc, _ := time.LoadLocation("Local")
  1350. var startTime int64
  1351. if len(start_date) > 0 {
  1352. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1353. fmt.Println("err-----------", err)
  1354. if err != nil {
  1355. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1356. return
  1357. }
  1358. startTime = theTime.Unix()
  1359. }
  1360. var endTime int64
  1361. if len(end_date) > 0 {
  1362. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1363. if err != nil {
  1364. utils.ErrorLog(err.Error())
  1365. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1366. return
  1367. }
  1368. endTime = theTime.Unix()
  1369. }
  1370. patient, _ := service.GetFaPiaoPatientByID(c.GetAdminUserInfo().CurrentOrgId, patient_id)
  1371. config, _ := service.GetConfigurationById(project_id, item_id, c.GetAdminUserInfo().CurrentOrgId)
  1372. //获取配置
  1373. reference, _ := service.GetInspectionReferenceFour(config.InspectionMajor, config.InspectionMinor, c.GetAdminUserInfo().CurrentOrgId)
  1374. //获取数值在正常范围内的总数
  1375. inspections, _ := service.GetPatientInspectionByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, patient_id, reference.ItemName)
  1376. c.ServeSuccessJSON(map[string]interface{}{
  1377. "patient": patient,
  1378. "reference": reference,
  1379. "inspections": inspections,
  1380. })
  1381. }
  1382. func (c *StatisticsApiController) GetPatientCommonInspectionStatistisc() {
  1383. start_date := c.GetString("start_date")
  1384. end_date := c.GetString("end_date")
  1385. project_id, _ := c.GetInt64("project_id")
  1386. item_id, _ := c.GetInt64("item_id")
  1387. item_type, _ := c.GetInt64("item_type")
  1388. keyword := c.GetString("keyword")
  1389. timeLayout := "2006-01-02"
  1390. loc, _ := time.LoadLocation("Local")
  1391. var startTime int64
  1392. if len(start_date) > 0 {
  1393. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1394. fmt.Println("err-----------", err)
  1395. if err != nil {
  1396. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1397. return
  1398. }
  1399. startTime = theTime.Unix()
  1400. }
  1401. var endTime int64
  1402. if len(end_date) > 0 {
  1403. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1404. if err != nil {
  1405. utils.ErrorLog(err.Error())
  1406. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1407. return
  1408. }
  1409. endTime = theTime.Unix()
  1410. }
  1411. //获取配置
  1412. reference, _ := service.GetInspectionReferenceFour(project_id, item_id, c.GetAdminUserInfo().CurrentOrgId)
  1413. //获取数值在正常范围内的总数
  1414. config, _ := service.GetConfigurationById(project_id, item_id, c.GetAdminUserInfo().CurrentOrgId)
  1415. max, _ := strconv.ParseFloat(config.LargeRange, 64)
  1416. min, _ := strconv.ParseFloat(config.MinRange, 64)
  1417. //max, _ := strconv.ParseFloat(reference.RangeMax, 64)
  1418. //min, _ := strconv.ParseFloat(reference.RangeMin, 64)
  1419. //config, _ := service.GetConfigurationById(project_id, item_id, c.GetAdminUserInfo().CurrentOrgId)
  1420. switch item_type {
  1421. case 1:
  1422. list, _ := service.GetUnusualInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, max, min, keyword)
  1423. c.ServeSuccessJSON(map[string]interface{}{
  1424. "list": list,
  1425. "reference": reference,
  1426. })
  1427. break
  1428. case 2:
  1429. list, _ := service.GetPatientNotInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, keyword)
  1430. c.ServeSuccessJSON(map[string]interface{}{
  1431. "list": list,
  1432. "reference": reference,
  1433. })
  1434. break
  1435. case 3:
  1436. list, _ := service.GetNormalInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, max, min, keyword)
  1437. c.ServeSuccessJSON(map[string]interface{}{
  1438. "list": list,
  1439. "reference": reference,
  1440. })
  1441. break
  1442. }
  1443. }
  1444. func (c *StatisticsApiController) GetFivePatientInspectionStatistisc() {
  1445. //start_date := c.GetString("start_date")
  1446. end_date := c.GetString("end_date")
  1447. //keyword := c.GetString("keyword")
  1448. //timeLayout := "2006-01-02"
  1449. //loc, _ := time.LoadLocation("Local")
  1450. dynamicFields := make([]map[string]interface{}, 0)
  1451. list, _ := service.GetLatestInspectionValues(c.GetAdminUserInfo().CurrentOrgId, strings.Split(end_date, "-")[0]+"-"+strings.Split(end_date, "-")[1])
  1452. //fmt.Println(list)
  1453. for _, result := range list {
  1454. //fmt.Println(result)
  1455. // 打印患者ID
  1456. nb := result["姓名"].([]byte)
  1457. name := string(nb)
  1458. //ynamicFields := make([]map[string]interface{})
  1459. //var dynamicFields DynamicData
  1460. ynamicField := make(map[string]interface{})
  1461. ynamicField["姓名"] = name
  1462. // 打印其他列的值
  1463. for columnName, columnValue := range result {
  1464. // 跳过患者ID列
  1465. if columnName == "姓名" {
  1466. continue
  1467. }
  1468. // 检查值是否为nil
  1469. if columnValue == nil {
  1470. fmt.Printf("Column: %s, Value: <nil>\n", columnName)
  1471. continue
  1472. }
  1473. // 将字节切片转换为字符串
  1474. byteValue, ok := columnValue.([]byte)
  1475. if !ok {
  1476. // 如果无法转换为[]byte,输出错误信息
  1477. fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
  1478. continue
  1479. }
  1480. // 将字节切片转换为字符串
  1481. strValue := string(byteValue)
  1482. // 尝试将字符串转换为浮点数
  1483. floatValue, _ := strconv.ParseFloat(strValue, 64)
  1484. //if err != nil {
  1485. // // 如果转换失败,输出错误信息
  1486. // fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
  1487. //} else {
  1488. // // 如果转换成功,输出列名和浮点数值
  1489. // fmt.Printf("Column: %s, Value: %f\n", columnName, floatValue)
  1490. //}
  1491. ynamicField["年月"] = strings.Split(end_date, "-")[0] + "-" + strings.Split(end_date, "-")[1]
  1492. if floatValue == 0 {
  1493. ynamicField[columnName] = ""
  1494. } else {
  1495. ynamicField[columnName] = floatValue
  1496. }
  1497. fmt.Println(ynamicField)
  1498. dynamicFields = append(dynamicFields, ynamicField)
  1499. }
  1500. }
  1501. seen := make(map[interface{}]struct{})
  1502. var uniqueFields []map[string]interface{}
  1503. for _, field := range dynamicFields {
  1504. value := field["姓名"]
  1505. // 如果值未在map中出现过,则将其添加到新的切片中,并将其添加到map中
  1506. if _, ok := seen[value]; !ok {
  1507. seen[value] = struct{}{}
  1508. uniqueFields = append(uniqueFields, field)
  1509. }
  1510. }
  1511. c.ServeSuccessJSON(map[string]interface{}{
  1512. "list": uniqueFields,
  1513. })
  1514. //list, _ := service.GetPatientFiveInspectionByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, keyword)
  1515. //
  1516. //var names []string
  1517. //
  1518. //for _, item := range list {
  1519. // names = append(names, item.ItemName)
  1520. //}
  1521. //
  1522. //names = RemoveRepeatedNameElement(names)
  1523. //fmt.Println(names)
  1524. //
  1525. //var tempList []models.InspectionTen
  1526. //list_two := RemoveRepeatedInspectPatientElement(list)
  1527. //for _, item := range list_two {
  1528. // var temp models.InspectionTen
  1529. // var temptwos []models.InspectionValue
  1530. // temp.PatientId = item.PatientId
  1531. // temp.Name = item.Name
  1532. // for _, subitem := range list {
  1533. // if item.PatientId == subitem.PatientId {
  1534. // var temptwo models.InspectionValue
  1535. // temptwo.Name = subitem.ItemName
  1536. // temptwo.Value = subitem.InspectValue
  1537. // temptwos = append(temptwos, temptwo)
  1538. // }
  1539. // }
  1540. // temp.Values = temptwos
  1541. // tempList = append(tempList, temp)
  1542. //}
  1543. //
  1544. //var filtered []string
  1545. //for _, item := range names {
  1546. // if item != "a" {
  1547. // filtered = append(filtered, item)
  1548. // }
  1549. //}
  1550. //
  1551. //for _, item := range tempList {
  1552. // for _, subitem := range item.Values {
  1553. // for _, name := range filtered {
  1554. // if subitem.Name != name {
  1555. // filtered = append(filtered, name)
  1556. // }
  1557. // }
  1558. // }
  1559. //}
  1560. //
  1561. ////// 将剩余字符串填充到 Inspection 数组中
  1562. ////for _, name := range filtered {
  1563. //// var temptwo models.InspectionValue
  1564. //// temptwo.Name = subitem.ItemName
  1565. //// temptwo.Value = subitem.InspectValue
  1566. //// temptwos = append(temptwos, temptwo)
  1567. ////}
  1568. //
  1569. //for _, item := range tempList {
  1570. // //for _, subitem := range item.Values {
  1571. // var temptwo models.InspectionValue
  1572. // for _, name := range filtered {
  1573. // temptwo.Name = name
  1574. // temptwo.Value = ""
  1575. // }
  1576. // item.Values = append(item.Values, temptwo)
  1577. // //}
  1578. // fmt.Println(item.Values)
  1579. //}
  1580. }
  1581. func (c *StatisticsApiController) GetPatientInspectionStatistisc() {
  1582. start_date := c.GetString("start_date")
  1583. end_date := c.GetString("end_date")
  1584. project_id, _ := c.GetInt64("project_id")
  1585. item_id, _ := c.GetInt64("item_id")
  1586. item_type, _ := c.GetInt64("item_type")
  1587. //order_type, _ := c.GetInt64("order_type")
  1588. keyword := c.GetString("keyword")
  1589. s_type, _ := c.GetInt64("type")
  1590. timeLayout := "2006-01-02"
  1591. loc, _ := time.LoadLocation("Local")
  1592. var startTime int64
  1593. if len(start_date) > 0 {
  1594. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1595. fmt.Println("err-----------", err)
  1596. if err != nil {
  1597. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1598. return
  1599. }
  1600. startTime = theTime.Unix()
  1601. }
  1602. var endTime int64
  1603. if len(end_date) > 0 {
  1604. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1605. if err != nil {
  1606. utils.ErrorLog(err.Error())
  1607. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1608. return
  1609. }
  1610. endTime = theTime.Unix()
  1611. }
  1612. //获取配置
  1613. reference, _ := service.GetInspectionReferenceThree(project_id, item_id)
  1614. //获取数值在正常范围内的总数
  1615. max, _ := strconv.ParseFloat(reference.RangeMax, 64)
  1616. min, _ := strconv.ParseFloat(reference.RangeMin, 64)
  1617. switch item_type {
  1618. case 0:
  1619. break
  1620. case 1:
  1621. if s_type == 2 { //KTV
  1622. list, _ := service.GetUnusualKTVORURRInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10950, 0, 1.2, keyword)
  1623. c.ServeSuccessJSON(map[string]interface{}{
  1624. "list": list,
  1625. })
  1626. } else if s_type == 3 { //URR
  1627. list, _ := service.GetUnusualKTVORURRInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10951, 0, 65, keyword)
  1628. c.ServeSuccessJSON(map[string]interface{}{
  1629. "list": list,
  1630. })
  1631. } else {
  1632. list, _ := service.GetUnusualInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, max, min, keyword)
  1633. c.ServeSuccessJSON(map[string]interface{}{
  1634. "list": list,
  1635. "reference": reference,
  1636. })
  1637. }
  1638. break
  1639. case 2:
  1640. if s_type == 2 { //KTV
  1641. list, _ := service.GetPatientNotKTVORURRInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10950, keyword)
  1642. c.ServeSuccessJSON(map[string]interface{}{
  1643. "list": list,
  1644. })
  1645. } else if s_type == 3 { //URR
  1646. list, _ := service.GetPatientNotKTVORURRInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10951, keyword)
  1647. c.ServeSuccessJSON(map[string]interface{}{
  1648. "list": list,
  1649. })
  1650. } else {
  1651. list, _ := service.GetPatientNotInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, keyword)
  1652. c.ServeSuccessJSON(map[string]interface{}{
  1653. "list": list,
  1654. "reference": reference,
  1655. })
  1656. }
  1657. break
  1658. case 3:
  1659. if s_type == 2 { //KTV
  1660. list, _ := service.GetNormalKTVORURRInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10950, 0, 1.2, keyword)
  1661. c.ServeSuccessJSON(map[string]interface{}{
  1662. "list": list,
  1663. })
  1664. } else if s_type == 3 { //URR
  1665. list, _ := service.GetNormalKTVORURRInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10951, 0, 65, keyword)
  1666. c.ServeSuccessJSON(map[string]interface{}{
  1667. "list": list,
  1668. })
  1669. } else {
  1670. list, _ := service.GetNormalInspectionPatientList(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, max, min, keyword)
  1671. c.ServeSuccessJSON(map[string]interface{}{
  1672. "list": list,
  1673. "reference": reference,
  1674. })
  1675. }
  1676. break
  1677. }
  1678. }
  1679. func (c *StatisticsApiController) GetAllInspectionStatistisc() {
  1680. start_date := c.GetString("start_date")
  1681. end_date := c.GetString("end_date")
  1682. project_id, _ := c.GetInt64("project_id")
  1683. item_id, _ := c.GetInt64("item_id")
  1684. s_type, _ := c.GetInt64("type")
  1685. timeLayout := "2006-01-02"
  1686. loc, _ := time.LoadLocation("Local")
  1687. var startTime int64
  1688. if len(start_date) > 0 {
  1689. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1690. fmt.Println("err-----------", err)
  1691. if err != nil {
  1692. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1693. return
  1694. }
  1695. startTime = theTime.Unix()
  1696. }
  1697. var endTime int64
  1698. if len(end_date) > 0 {
  1699. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1700. if err != nil {
  1701. utils.ErrorLog(err.Error())
  1702. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1703. return
  1704. }
  1705. endTime = theTime.Unix()
  1706. }
  1707. //患者总数
  1708. patientCount := service.GetPatientCount(c.GetAdminUserInfo().CurrentOrgId)
  1709. //获取配置
  1710. reference, _ := service.GetInspectionReferenceThree(project_id, item_id)
  1711. //获取数值在正常范围内的总数
  1712. max, _ := strconv.ParseFloat(reference.RangeMax, 64)
  1713. min, _ := strconv.ParseFloat(reference.RangeMin, 64)
  1714. if s_type == 2 {
  1715. normalTotal, _ := service.GetNormalKTVORURRInspectionTotalByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10950, 1.2)
  1716. //获取数值异常的总数
  1717. unusualTotal, _ := service.GetUnusualKTVORURRInspectionTotalByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10950, 1.2)
  1718. //获取没有检查的患者总数
  1719. noCheckTotal, _ := service.GetPatientNotKTVORURRInspectionTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10950)
  1720. c.ServeSuccessJSON(map[string]interface{}{
  1721. "patient_count": patientCount,
  1722. "normal_total": normalTotal,
  1723. "unusual_total": unusualTotal,
  1724. "no_check_total": noCheckTotal,
  1725. })
  1726. } else if s_type == 3 {
  1727. normalTotal, _ := service.GetNormalKTVORURRInspectionTotalByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10951, 65)
  1728. //获取数值异常的总数
  1729. //unusualTotal, _ := service.GetUnusualKTVORURRInspectionTotalByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1.2)
  1730. unusualTotal, _ := service.GetUnusualKTVORURRInspectionTotalByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10951, 65)
  1731. //获取没有检查的患者总数
  1732. noCheckTotal, _ := service.GetPatientNotKTVORURRInspectionTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10951)
  1733. c.ServeSuccessJSON(map[string]interface{}{
  1734. "patient_count": patientCount,
  1735. "normal_total": normalTotal,
  1736. "unusual_total": unusualTotal,
  1737. "no_check_total": noCheckTotal,
  1738. })
  1739. } else {
  1740. normalTotal, _ := service.GetNormalInspectionTotalByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, max, min)
  1741. //获取数值异常的总数
  1742. unusualTotal, _ := service.GetUnusualInspectionTotalByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName, max, min)
  1743. //获取没有检查的患者总数
  1744. noCheckTotal, _ := service.GetPatientNotInspectionTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, reference.ItemName)
  1745. c.ServeSuccessJSON(map[string]interface{}{
  1746. "patient_count": patientCount,
  1747. "normal_total": normalTotal,
  1748. "unusual_total": unusualTotal,
  1749. "no_check_total": noCheckTotal,
  1750. "reference": reference,
  1751. })
  1752. }
  1753. }
  1754. func (c *StatisticsApiController) GetPersonInspectionStatistisc() {
  1755. start_date := c.GetString("start_time")
  1756. end_date := c.GetString("end_time")
  1757. project_id, _ := c.GetInt64("project_id")
  1758. item_id, _ := c.GetInt64("item_id")
  1759. patient_id, _ := c.GetInt64("patient_id")
  1760. s_type, _ := c.GetInt64("type")
  1761. timeLayout := "2006-01-02"
  1762. loc, _ := time.LoadLocation("Local")
  1763. var startTime int64
  1764. if len(start_date) > 0 {
  1765. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
  1766. fmt.Println("err-----------", err)
  1767. if err != nil {
  1768. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1769. return
  1770. }
  1771. startTime = theTime.Unix()
  1772. }
  1773. var endTime int64
  1774. if len(end_date) > 0 {
  1775. theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
  1776. if err != nil {
  1777. utils.ErrorLog(err.Error())
  1778. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  1779. return
  1780. }
  1781. endTime = theTime.Unix()
  1782. }
  1783. if s_type == 2 {
  1784. patient, _ := service.GetFaPiaoPatientByID(c.GetAdminUserInfo().CurrentOrgId, patient_id)
  1785. //获取配置
  1786. reference, _ := service.GetInspectionReferenceThree(1014, 10950)
  1787. //获取数值在正常范围内的总数
  1788. inspections, _ := service.GetPatientKTVORURRInspectionByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10950, patient_id)
  1789. c.ServeSuccessJSON(map[string]interface{}{
  1790. "patient": patient,
  1791. "reference": reference,
  1792. "inspections": inspections,
  1793. })
  1794. } else if s_type == 3 {
  1795. patient, _ := service.GetFaPiaoPatientByID(c.GetAdminUserInfo().CurrentOrgId, patient_id)
  1796. //获取配置
  1797. reference, _ := service.GetInspectionReferenceThree(1014, 10951)
  1798. //获取数值在正常范围内的总数
  1799. inspections, _ := service.GetPatientKTVORURRInspectionByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, 1014, 10951, patient_id)
  1800. c.ServeSuccessJSON(map[string]interface{}{
  1801. "patient": patient,
  1802. "reference": reference,
  1803. "inspections": inspections,
  1804. })
  1805. } else {
  1806. patient, _ := service.GetFaPiaoPatientByID(c.GetAdminUserInfo().CurrentOrgId, patient_id)
  1807. //获取配置
  1808. reference, _ := service.GetInspectionReferenceThree(project_id, item_id)
  1809. //获取数值在正常范围内的总数
  1810. inspections, _ := service.GetPatientInspectionByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, patient_id, reference.ItemName)
  1811. c.ServeSuccessJSON(map[string]interface{}{
  1812. "patient": patient,
  1813. "reference": reference,
  1814. "inspections": inspections,
  1815. })
  1816. }
  1817. }
  1818. func (c *StatisticsApiController) GetStatistics() {
  1819. adminUserInfo := c.GetAdminUserInfo()
  1820. // thisTime := time.Now()
  1821. year, month, day := time.Now().Date()
  1822. todayTime := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
  1823. startYearTime := time.Date(year, 1, 1, 0, 0, 0, 0, time.Local)
  1824. endYearTime := time.Date(year+1, 1, 1, 0, 0, 0, 0, time.Local)
  1825. todayWeek := int(todayTime.Weekday())
  1826. if todayWeek == 0 {
  1827. todayWeek = 7
  1828. }
  1829. weekEnd := 7 - todayWeek
  1830. weekStart := weekEnd - 6
  1831. // endDay := todayTime.AddDate(0, 0, weekEnd)
  1832. startDay := todayTime.AddDate(0, 0, weekStart)
  1833. //患者总数
  1834. patientCount := service.GetPatientCount(adminUserInfo.CurrentOrgId)
  1835. //今日透析
  1836. todayDialysisCount := service.GetDayDialysisCount(adminUserInfo.CurrentOrgId, todayTime.Unix())
  1837. //本周透析
  1838. weekDaylysisCount := service.GetTimebetweenDialysisCount(adminUserInfo.CurrentOrgId, startDay.Unix(), todayTime.Unix())
  1839. //传染病
  1840. diseaseCounts := service.GetPatientContagionCounts(adminUserInfo.CurrentOrgId)
  1841. //性别分布
  1842. genderCounts := service.GetPatientGenderCounts(adminUserInfo.CurrentOrgId)
  1843. //年龄分布
  1844. ageCounts := service.GetPatiendAgeBetweenCount(adminUserInfo.CurrentOrgId)
  1845. //透析模式
  1846. modeCounts := service.GetPatientDialysisModeBetweenCount(adminUserInfo.CurrentOrgId, startYearTime.Unix(), endYearTime.Unix())
  1847. c.ServeSuccessJSON(map[string]interface{}{
  1848. "patient_count": patientCount,
  1849. "today_dialysis_count": todayDialysisCount,
  1850. "week_daylysis_count": weekDaylysisCount,
  1851. "disease_counts": diseaseCounts,
  1852. "gender_counts": genderCounts,
  1853. "age_counts": ageCounts,
  1854. "mode_counts": modeCounts,
  1855. })
  1856. }
  1857. func RemoveRepeatedInspectPatientElement(arr []models.InspectionTenOne) (newArr []models.InspectionTenOne) {
  1858. newArr = make([]models.InspectionTenOne, 0)
  1859. for i := 0; i < len(arr); i++ {
  1860. repeat := false
  1861. for j := i + 1; j < len(arr); j++ {
  1862. if arr[i].PatientId == arr[j].PatientId {
  1863. repeat = true
  1864. break
  1865. }
  1866. }
  1867. if !repeat {
  1868. newArr = append(newArr, arr[i])
  1869. }
  1870. }
  1871. return
  1872. }
  1873. func RemoveRepeatedNameElement(arr []string) (newArr []string) {
  1874. newArr = make([]string, 0)
  1875. for i := 0; i < len(arr); i++ {
  1876. repeat := false
  1877. for j := i + 1; j < len(arr); j++ {
  1878. if arr[i] == arr[j] {
  1879. repeat = true
  1880. break
  1881. }
  1882. }
  1883. if !repeat {
  1884. newArr = append(newArr, arr[i])
  1885. }
  1886. }
  1887. return
  1888. }
  1889. // 根据指定的日期范围拆分成周、月、年
  1890. func splitDateRange(startDate, endDate string) ([]time.Time, error) {
  1891. startTime, err := time.Parse("2006-01-02", startDate)
  1892. if err != nil {
  1893. return nil, err
  1894. }
  1895. endTime, err := time.Parse("2006-01-02", endDate)
  1896. if err != nil {
  1897. return nil, err
  1898. }
  1899. var result []time.Time
  1900. for date := startTime; date.Before(endTime) || date.Equal(endTime); date = date.AddDate(0, 0, 1) {
  1901. result = append(result, date)
  1902. }
  1903. return result, nil
  1904. }
  1905. // 按周统计日期
  1906. func groupByWeek(dates []time.Time) [][]time.Time {
  1907. var result [][]time.Time
  1908. var currentWeek []time.Time
  1909. for _, date := range dates {
  1910. currentWeek = append(currentWeek, date)
  1911. if date.Weekday() == time.Sunday {
  1912. result = append(result, currentWeek)
  1913. currentWeek = nil
  1914. }
  1915. }
  1916. if len(currentWeek) > 0 {
  1917. result = append(result, currentWeek)
  1918. }
  1919. return result
  1920. }
  1921. // 按月统计日期
  1922. func groupByMonth(dates []time.Time) [][]time.Time {
  1923. var result [][]time.Time
  1924. var currentMonth []time.Time
  1925. lastMonth := dates[0].Month()
  1926. for _, date := range dates {
  1927. if date.Month() != lastMonth {
  1928. result = append(result, currentMonth)
  1929. currentMonth = nil
  1930. lastMonth = date.Month()
  1931. }
  1932. currentMonth = append(currentMonth, date)
  1933. }
  1934. if len(currentMonth) > 0 {
  1935. result = append(result, currentMonth)
  1936. }
  1937. return result
  1938. }
  1939. // 按年统计日期
  1940. func groupByYear(dates []time.Time) [][]time.Time {
  1941. var result [][]time.Time
  1942. var currentYear []time.Time
  1943. lastYear := dates[0].Year()
  1944. for _, date := range dates {
  1945. if date.Year() != lastYear {
  1946. result = append(result, currentYear)
  1947. currentYear = nil
  1948. lastYear = date.Year()
  1949. }
  1950. currentYear = append(currentYear, date)
  1951. }
  1952. if len(currentYear) > 0 {
  1953. result = append(result, currentYear)
  1954. }
  1955. return result
  1956. }
  1957. // 将日期拆分为按天统计
  1958. func splitByDay(startDate, endDate time.Time) []time.Time {
  1959. var dates []time.Time
  1960. for d := startDate; !d.After(endDate); d = d.AddDate(0, 0, 1) {
  1961. dates = append(dates, d)
  1962. }
  1963. return dates
  1964. }
  1965. // 排序函数
  1966. func sortFieldsByValue(field map[string]interface{}) map[string]interface{} {
  1967. // 将键值对分成两个部分,一个有值,一个无值
  1968. var withValue, withoutValue []string
  1969. for key, value := range field {
  1970. if key == "日期" || key == "合计" {
  1971. continue
  1972. }
  1973. if value == "" {
  1974. withoutValue = append(withoutValue, key)
  1975. } else {
  1976. withValue = append(withValue, key)
  1977. }
  1978. }
  1979. // 对有值和无值部分分别排序
  1980. sort.Strings(withValue)
  1981. sort.Strings(withoutValue)
  1982. // 创建一个新的有序map
  1983. sortedField := make(map[string]interface{})
  1984. sortedField["日期"] = field["日期"]
  1985. for _, key := range withValue {
  1986. sortedField[key] = field[key]
  1987. }
  1988. for _, key := range withoutValue {
  1989. sortedField[key] = field[key]
  1990. }
  1991. sortedField["合计"] = field["合计"]
  1992. return sortedField
  1993. }