statistics_api_controller.go 73KB

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