analysis_api_controller.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. package admin_api_controllers
  2. import (
  3. "XT_New/models"
  4. "XT_New/service"
  5. "XT_New/utils"
  6. "time"
  7. )
  8. type AnalysisAPIController struct {
  9. AdminBaseAPIAuthController
  10. // AdminBaseAPIController
  11. }
  12. // /admin/api/org/regist [get] RegistOrg
  13. func (this *AnalysisAPIController) RegistOrg() {
  14. totalOrg, getTotalOrgErr := service.GetTotalOrgCount()
  15. if getTotalOrgErr != nil {
  16. this.ErrorLog("获取总机构数失败:%v", getTotalOrgErr)
  17. }
  18. now := time.Now()
  19. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  20. registOrgCountToday, getRegistOrgCountTodayErr := service.GetRegistedOrgCountFromDayToDay(zeroHourTimeOfToday, now)
  21. if getRegistOrgCountTodayErr != nil {
  22. this.ErrorLog("获取今日注册机构数失败:%v", getRegistOrgCountTodayErr)
  23. }
  24. monday, sunday := utils.GetMondayAndSundayOfWeekDate(&now)
  25. registOrgCountThisWeek, getRegistOrgCountThisWeekErr := service.GetRegistedOrgCountFromDayToDay(monday, sunday)
  26. if getRegistOrgCountThisWeekErr != nil {
  27. this.ErrorLog("获取本周注册机构数失败:%v", getRegistOrgCountThisWeekErr)
  28. }
  29. thisYear := now.Year()
  30. thisMonth := int(now.Month())
  31. beginningOfMonth, endOfMonth := utils.MonthBeginningToEnd(thisYear, thisMonth)
  32. registOrgCountThisMonth, getRegistOrgCountThisMonthErr := service.GetRegistedOrgCountFromDayToDay(beginningOfMonth, endOfMonth)
  33. if getRegistOrgCountThisMonthErr != nil {
  34. this.ErrorLog("获取本月注册机构数失败:%v", getRegistOrgCountThisMonthErr)
  35. }
  36. monthRegistOrgCounts := make([]map[string]interface{}, 0, 12)
  37. for m := 1; m < 12; m++ {
  38. if m > thisMonth {
  39. break
  40. }
  41. if m == thisMonth {
  42. monthRegistOrgCounts = append(monthRegistOrgCounts, map[string]interface{}{
  43. "month": m,
  44. "count": registOrgCountThisMonth,
  45. })
  46. } else {
  47. BOM, EOM := utils.MonthBeginningToEnd(thisYear, m)
  48. registOrgCount, getRegistOrgCountErr := service.GetRegistedOrgCountFromDayToDay(BOM, EOM)
  49. if getRegistOrgCountErr != nil {
  50. this.ErrorLog("获取%v月注册机构数失败:%v", m, getRegistOrgCountErr)
  51. }
  52. monthRegistOrgCounts = append(monthRegistOrgCounts, map[string]interface{}{
  53. "month": m,
  54. "count": registOrgCount,
  55. })
  56. }
  57. }
  58. registOrgsThisWeek, getRegistOrgThisWeekErr := service.GetRegistedOrgsFromDayToDay(monday, sunday)
  59. if getRegistOrgThisWeekErr != nil {
  60. this.ErrorLog("获取本周注册机构失败:%v", getRegistOrgThisWeekErr)
  61. registOrgsThisWeek = make([]*models.Org, 0)
  62. }
  63. registOrgsThisMonth, getRegistOrgThisMonthErr := service.GetRegistedOrgsFromDayToDay(beginningOfMonth, endOfMonth)
  64. if getRegistOrgThisMonthErr != nil {
  65. this.ErrorLog("获取本月注册机构失败:%v", getRegistOrgThisMonthErr)
  66. registOrgsThisMonth = make([]*models.Org, 0)
  67. }
  68. this.ServeSuccessJSON(map[string]interface{}{
  69. "total_org_count": totalOrg,
  70. "regist_org_count_today": registOrgCountToday,
  71. "regist_org_count_week": registOrgCountThisWeek,
  72. "regist_org_count_month": registOrgCountThisMonth,
  73. "regist_org_count_months": monthRegistOrgCounts,
  74. "regist_orgs_week": registOrgsThisWeek,
  75. "regist_orgs_month": registOrgsThisMonth,
  76. })
  77. }
  78. // /admin/api/org/active [get] ActiveOrg
  79. func (this *AnalysisAPIController) ActiveOrg() {
  80. now := time.Now()
  81. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  82. activeOrgCountToday, getActiveTodayErr := service.GetActiveOrgCountFromDayToDay(zeroHourTimeOfToday, now)
  83. if getActiveTodayErr != nil {
  84. this.ErrorLog("获取今日活跃机构数失败:%v", getActiveTodayErr)
  85. }
  86. monday, sunday := utils.GetMondayAndSundayOfWeekDate(&now)
  87. activeOrgCountThisWeek, getActiveOrgCountThisWeekErr := service.GetActiveOrgCountFromDayToDay(monday, sunday)
  88. if getActiveOrgCountThisWeekErr != nil {
  89. this.ErrorLog("获取本周活跃机构数失败:%v", getActiveOrgCountThisWeekErr)
  90. }
  91. thisYear := now.Year()
  92. thisMonth := int(now.Month())
  93. beginningOfMonth, endOfMonth := utils.MonthBeginningToEnd(thisYear, thisMonth)
  94. activeOrgCountThisMonth, getActiveOrgCountThisMonthErr := service.GetActiveOrgCountFromDayToDay(beginningOfMonth, endOfMonth)
  95. if getActiveOrgCountThisMonthErr != nil {
  96. this.ErrorLog("获取本月活跃机构数失败:%v", getActiveOrgCountThisMonthErr)
  97. }
  98. monthActiveOrgCounts := make([]map[string]interface{}, 0, 12)
  99. for m := 1; m < 12; m++ {
  100. if m > thisMonth {
  101. break
  102. }
  103. if m == thisMonth {
  104. monthActiveOrgCounts = append(monthActiveOrgCounts, map[string]interface{}{
  105. "month": m,
  106. "count": activeOrgCountThisMonth,
  107. })
  108. } else {
  109. BOM, EOM := utils.MonthBeginningToEnd(thisYear, m)
  110. activeOrgCount, getActiveOrgCountErr := service.GetActiveOrgCountFromDayToDay(BOM, EOM)
  111. if getActiveOrgCountErr != nil {
  112. this.ErrorLog("获取%v月活跃机构数失败:%v", m, getActiveOrgCountErr)
  113. }
  114. monthActiveOrgCounts = append(monthActiveOrgCounts, map[string]interface{}{
  115. "month": m,
  116. "count": activeOrgCount,
  117. })
  118. }
  119. }
  120. activeOrgsThisWeek, getActiveOrgThisWeekErr := service.GetActiveOrgsFromDayToDay(monday, sunday)
  121. if getActiveOrgThisWeekErr != nil {
  122. this.ErrorLog("获取本周活跃机构失败:%v", getActiveOrgThisWeekErr)
  123. activeOrgsThisWeek = make([]*service.ActiveOrgListVM, 0)
  124. }
  125. activeOrgsThisMonth, getActiveOrgThisMonthErr := service.GetActiveOrgsFromDayToDay(beginningOfMonth, endOfMonth)
  126. if getActiveOrgThisMonthErr != nil {
  127. this.ErrorLog("获取本月活跃机构失败:%v", getActiveOrgThisMonthErr)
  128. activeOrgsThisMonth = make([]*service.ActiveOrgListVM, 0)
  129. }
  130. this.ServeSuccessJSON(map[string]interface{}{
  131. "active_org_count_today": activeOrgCountToday,
  132. "active_org_count_week": activeOrgCountThisWeek,
  133. "active_org_count_month": activeOrgCountThisMonth,
  134. "active_org_count_months": monthActiveOrgCounts,
  135. "active_orgs_week": activeOrgsThisWeek,
  136. "active_orgs_month": activeOrgsThisMonth,
  137. })
  138. }
  139. // /admin/api/user/active [get] ActiveUser
  140. func (this *AnalysisAPIController) ActiveUser() {
  141. now := time.Now()
  142. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  143. activeAdminCountToday, getActiveTodayErr := service.GetActiveAdminUserCountFromDayToDay(zeroHourTimeOfToday, now)
  144. if getActiveTodayErr != nil {
  145. this.ErrorLog("获取今日活跃账户数失败:%v", getActiveTodayErr)
  146. }
  147. monday, sunday := utils.GetMondayAndSundayOfWeekDate(&now)
  148. activeAdminCountThisWeek, getActiveAdminCountThisWeekErr := service.GetActiveAdminUserCountFromDayToDay(monday, sunday)
  149. if getActiveAdminCountThisWeekErr != nil {
  150. this.ErrorLog("获取本周活跃账户数失败:%v", getActiveAdminCountThisWeekErr)
  151. }
  152. thisYear := now.Year()
  153. thisMonth := int(now.Month())
  154. beginningOfMonth, endOfMonth := utils.MonthBeginningToEnd(thisYear, thisMonth)
  155. activeAdminCountThisMonth, getActiveAdminCountThisMonthErr := service.GetActiveAdminUserCountFromDayToDay(beginningOfMonth, endOfMonth)
  156. if getActiveAdminCountThisMonthErr != nil {
  157. this.ErrorLog("获取本月活跃账户数失败:%v", getActiveAdminCountThisMonthErr)
  158. }
  159. monthActiveAdminCounts := make([]map[string]interface{}, 0, 12)
  160. for m := 1; m < 12; m++ {
  161. if m > thisMonth {
  162. break
  163. }
  164. if m == thisMonth {
  165. monthActiveAdminCounts = append(monthActiveAdminCounts, map[string]interface{}{
  166. "month": m,
  167. "count": activeAdminCountThisMonth,
  168. })
  169. } else {
  170. BOM, EOM := utils.MonthBeginningToEnd(thisYear, m)
  171. activeAdminCount, getActiveAdminCountErr := service.GetActiveAdminUserCountFromDayToDay(BOM, EOM)
  172. if getActiveAdminCountErr != nil {
  173. this.ErrorLog("获取%v月活跃账户数失败:%v", m, getActiveAdminCountErr)
  174. }
  175. monthActiveAdminCounts = append(monthActiveAdminCounts, map[string]interface{}{
  176. "month": m,
  177. "count": activeAdminCount,
  178. })
  179. }
  180. }
  181. activeAdminsThisWeek, getActiveAdminThisWeekErr := service.GetActiveAdminUsersFromDayToDay(monday, sunday)
  182. if getActiveAdminThisWeekErr != nil {
  183. this.ErrorLog("获取本周活跃账户失败:%v", getActiveAdminThisWeekErr)
  184. activeAdminsThisWeek = make([]*service.ActiveAdminUserListVM, 0)
  185. }
  186. activeAdminsThisMonth, getActiveAdminThisMonthErr := service.GetActiveAdminUsersFromDayToDay(beginningOfMonth, endOfMonth)
  187. if getActiveAdminThisMonthErr != nil {
  188. this.ErrorLog("获取本月活跃账户失败:%v", getActiveAdminThisMonthErr)
  189. activeAdminsThisMonth = make([]*service.ActiveAdminUserListVM, 0)
  190. }
  191. this.ServeSuccessJSON(map[string]interface{}{
  192. "active_admin_count_today": activeAdminCountToday,
  193. "active_admin_count_week": activeAdminCountThisWeek,
  194. "active_admin_count_month": activeAdminCountThisMonth,
  195. "active_admin_count_months": monthActiveAdminCounts,
  196. "active_admins_week": activeAdminsThisWeek,
  197. "active_admins_month": activeAdminsThisMonth,
  198. })
  199. }
  200. // /admin/api/org/expiring [get] ExpiringOrg
  201. func (this *AnalysisAPIController) ExpiringOrg() {
  202. now := time.Now()
  203. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  204. // endTimeOfToday := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999, time.Local)
  205. timeAfter15Days := zeroHourTimeOfToday.AddDate(0, 0, 15)
  206. timeBefore15Days := zeroHourTimeOfToday.AddDate(0, 0, -15)
  207. countOfWillExpireOrg, getWillExpireOrgCountErr := service.GetWillExpireOrgCountFromDayToDay(zeroHourTimeOfToday, timeAfter15Days)
  208. if getWillExpireOrgCountErr != nil {
  209. this.ErrorLog("获取即将到期机构数失败:%v", getWillExpireOrgCountErr)
  210. }
  211. countOfExpiredOrg, getExpiredOrgCountErr := service.GetDidExpiredOrgCountFromDayToDay(timeBefore15Days, zeroHourTimeOfToday)
  212. if getExpiredOrgCountErr != nil {
  213. this.ErrorLog("获取刚到期机构数失败:%v", getExpiredOrgCountErr)
  214. }
  215. willExpireOrgs, getWillExpireOrgsErr := service.GetWillExpireOrgsFromDayToDay(zeroHourTimeOfToday, timeAfter15Days)
  216. if getWillExpireOrgsErr != nil {
  217. this.ErrorLog("获取即将到期机构失败:%v", getWillExpireOrgsErr)
  218. willExpireOrgs = make([]*service.ExpireOrgListVM, 0, 0)
  219. }
  220. expiredOrgs, getExpiredOrgsErr := service.GetDidExpireOrgsFromDayToDay(timeBefore15Days, zeroHourTimeOfToday)
  221. if getExpiredOrgsErr != nil {
  222. this.ErrorLog("获取已到期机构失败:%v", getExpiredOrgsErr)
  223. expiredOrgs = make([]*service.ExpireOrgListVM, 0, 0)
  224. }
  225. this.ServeSuccessJSON(map[string]interface{}{
  226. "will_expire_org_count": countOfWillExpireOrg,
  227. "did_expired_org_count": countOfExpiredOrg,
  228. "will_expire_orgs": willExpireOrgs,
  229. "did_expired_orgs": expiredOrgs,
  230. })
  231. }
  232. // /admin/api/dialysis/analysis [get] DialysisAnalysis
  233. func (this *AnalysisAPIController) DialysisAnalysis() {
  234. now := time.Now()
  235. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  236. endTimeOfToday := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999, time.Local)
  237. timesToday, getTimesTodayErr := service.GetDialysisTimesFromDayToDay(zeroHourTimeOfToday, endTimeOfToday)
  238. if getTimesTodayErr != nil {
  239. this.ErrorLog("获取今日透析次数失败:%v", getTimesTodayErr)
  240. }
  241. monday, sunday := utils.GetMondayAndSundayOfWeekDate(&now)
  242. timesThisWeek, getTimesThisWeekErr := service.GetDialysisTimesFromDayToDay(monday, sunday)
  243. if getTimesThisWeekErr != nil {
  244. this.ErrorLog("获取本周透析次数失败:%v", getTimesThisWeekErr)
  245. }
  246. thisYear := now.Year()
  247. thisMonth := int(now.Month())
  248. beginningOfMonth, endOfMonth := utils.MonthBeginningToEnd(thisYear, thisMonth)
  249. timesThisMonth, getTimesThisMonthErr := service.GetDialysisTimesFromDayToDay(beginningOfMonth, endOfMonth)
  250. if getTimesThisMonthErr != nil {
  251. this.ErrorLog("获取本月透析次数失败:%v", getTimesThisMonthErr)
  252. }
  253. monthTimes := make([]map[string]interface{}, 0, 12)
  254. for m := 1; m < 12; m++ {
  255. if m > thisMonth {
  256. break
  257. }
  258. if m == thisMonth {
  259. monthTimes = append(monthTimes, map[string]interface{}{
  260. "month": m,
  261. "count": timesThisMonth,
  262. })
  263. } else {
  264. BOM, EOM := utils.MonthBeginningToEnd(thisYear, m)
  265. times, getTimesErr := service.GetDialysisTimesFromDayToDay(BOM, EOM)
  266. if getTimesErr != nil {
  267. this.ErrorLog("获取%v月透析次数失败:%v", m, getTimesErr)
  268. }
  269. monthTimes = append(monthTimes, map[string]interface{}{
  270. "month": m,
  271. "count": times,
  272. })
  273. }
  274. }
  275. this.ServeSuccessJSON(map[string]interface{}{
  276. "times_today": timesToday,
  277. "times_week": timesThisWeek,
  278. "times_month": timesThisMonth,
  279. "times_months": monthTimes,
  280. })
  281. }
  282. // /admin/api/monitor/analysis [get] MonitorAnalysis
  283. func (this *AnalysisAPIController) MonitorAnalysis() {
  284. now := time.Now()
  285. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  286. endTimeOfToday := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999, time.Local)
  287. timesToday, getTimesTodayErr := service.GetMonitoringTimesFromDayToDay(zeroHourTimeOfToday, endTimeOfToday)
  288. if getTimesTodayErr != nil {
  289. this.ErrorLog("获取今日监控次数失败:%v", getTimesTodayErr)
  290. }
  291. monday, sunday := utils.GetMondayAndSundayOfWeekDate(&now)
  292. timesThisWeek, getTimesThisWeekErr := service.GetMonitoringTimesFromDayToDay(monday, sunday)
  293. if getTimesThisWeekErr != nil {
  294. this.ErrorLog("获取本周监控次数失败:%v", getTimesThisWeekErr)
  295. }
  296. thisYear := now.Year()
  297. thisMonth := int(now.Month())
  298. beginningOfMonth, endOfMonth := utils.MonthBeginningToEnd(thisYear, thisMonth)
  299. timesThisMonth, getTimesThisMonthErr := service.GetMonitoringTimesFromDayToDay(beginningOfMonth, endOfMonth)
  300. if getTimesThisMonthErr != nil {
  301. this.ErrorLog("获取本月监控次数失败:%v", getTimesThisMonthErr)
  302. }
  303. monthTimes := make([]map[string]interface{}, 0, 12)
  304. for m := 1; m < 12; m++ {
  305. if m > thisMonth {
  306. break
  307. }
  308. if m == thisMonth {
  309. monthTimes = append(monthTimes, map[string]interface{}{
  310. "month": m,
  311. "count": timesThisMonth,
  312. })
  313. } else {
  314. BOM, EOM := utils.MonthBeginningToEnd(thisYear, m)
  315. times, getTimesErr := service.GetMonitoringTimesFromDayToDay(BOM, EOM)
  316. if getTimesErr != nil {
  317. this.ErrorLog("获取%v月监控次数失败:%v", m, getTimesErr)
  318. }
  319. monthTimes = append(monthTimes, map[string]interface{}{
  320. "month": m,
  321. "count": times,
  322. })
  323. }
  324. }
  325. this.ServeSuccessJSON(map[string]interface{}{
  326. "times_today": timesToday,
  327. "times_week": timesThisWeek,
  328. "times_month": timesThisMonth,
  329. "times_months": monthTimes,
  330. })
  331. }
  332. // /admin/api/patient/analysis [get] PatientAnalysis
  333. func (this *AnalysisAPIController) PatientAnalysis() {
  334. now := time.Now()
  335. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  336. newCountToday, getNewCountTodayErr := service.GetNewPatientCountFromDayToDay(zeroHourTimeOfToday, now)
  337. if getNewCountTodayErr != nil {
  338. this.ErrorLog("获取今日新增病人数失败:%v", getNewCountTodayErr)
  339. }
  340. monday, sunday := utils.GetMondayAndSundayOfWeekDate(&now)
  341. newCountThisWeek, getNewCountThisWeekErr := service.GetNewPatientCountFromDayToDay(monday, sunday)
  342. if getNewCountThisWeekErr != nil {
  343. this.ErrorLog("获取本周新增病人数失败:%v", getNewCountThisWeekErr)
  344. }
  345. thisYear := now.Year()
  346. thisMonth := int(now.Month())
  347. beginningOfMonth, endOfMonth := utils.MonthBeginningToEnd(thisYear, thisMonth)
  348. newCountThisMonth, getNewCountThisMonthErr := service.GetNewPatientCountFromDayToDay(beginningOfMonth, endOfMonth)
  349. if getNewCountThisMonthErr != nil {
  350. this.ErrorLog("获取本月新增病人数失败:%v", getNewCountThisMonthErr)
  351. }
  352. monthNewCounts := make([]map[string]interface{}, 0, 12)
  353. for m := 1; m < 12; m++ {
  354. if m > thisMonth {
  355. break
  356. }
  357. if m == thisMonth {
  358. monthNewCounts = append(monthNewCounts, map[string]interface{}{
  359. "month": m,
  360. "count": newCountThisMonth,
  361. })
  362. } else {
  363. BOM, EOM := utils.MonthBeginningToEnd(thisYear, m)
  364. newCount, getNewCountErr := service.GetNewPatientCountFromDayToDay(BOM, EOM)
  365. if getNewCountErr != nil {
  366. this.ErrorLog("获取%v月新增病人数失败:%v", m, getNewCountErr)
  367. }
  368. monthNewCounts = append(monthNewCounts, map[string]interface{}{
  369. "month": m,
  370. "count": newCount,
  371. })
  372. }
  373. }
  374. maleCount, femaleCount, unknowGenderCount, getGenderCountErr := service.GetPatientGenderDistribution()
  375. if getGenderCountErr != nil {
  376. this.ErrorLog("获取病人性别分布失败:%v", getGenderCountErr)
  377. }
  378. // 1乙肝 2丙肝 3艾滋病 4肺结核 5梅毒
  379. // 不写错误日志了,累了
  380. normalPCount, _ := service.GetInfectiousDiseasePatientCount(0)
  381. type1PCount, _ := service.GetInfectiousDiseasePatientCount(1)
  382. type2PCount, _ := service.GetInfectiousDiseasePatientCount(2)
  383. type3PCount, _ := service.GetInfectiousDiseasePatientCount(3)
  384. type4PCount, _ := service.GetInfectiousDiseasePatientCount(4)
  385. type5PCount, _ := service.GetInfectiousDiseasePatientCount(5)
  386. ageDistribution, getAgeDistributionErr := service.GetPatientAgeDistribution()
  387. if getAgeDistributionErr != nil {
  388. this.ErrorLog("获取病人年龄分布失败:%v", getAgeDistributionErr)
  389. ageDistribution = make([]*service.PatientAgeCountStruct, 0, 0)
  390. }
  391. this.ServeSuccessJSON(map[string]interface{}{
  392. "new_patient_count_today": newCountToday,
  393. "new_patient_count_week": newCountThisWeek,
  394. "new_patient_count_month": newCountThisMonth,
  395. "new_patient_count_months": monthNewCounts,
  396. "male_count": maleCount,
  397. "female_count": femaleCount,
  398. "total_count": maleCount + femaleCount + unknowGenderCount,
  399. "disease_type_0_count": normalPCount,
  400. "disease_type_1_count": type1PCount,
  401. "disease_type_2_count": type2PCount,
  402. "disease_type_3_count": type3PCount,
  403. "disease_type_4_count": type4PCount,
  404. "disease_type_5_count": type5PCount,
  405. "age_counts": ageDistribution,
  406. })
  407. }