statistics_api_controller.go 72KB

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