statistics_api_controller.go 72KB

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