statistics_service.go 5.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package service
  2. import (
  3. "XT_Admin_Api/models"
  4. )
  5. func GetDayDialysisCount(orgID, date int64) (total int64) {
  6. readDb.Model(&models.DialysisOrder{}).Where("user_org_id=? and dialysis_date=? and status=1", orgID, date).Count(&total)
  7. return
  8. }
  9. func GetTimebetweenDialysisCount(orgID, start, end int64) (total int64) {
  10. readDb.Model(&models.DialysisOrder{}).Where("user_org_id=? and dialysis_date>=? and dialysis_date<=? and status=1", orgID, start, end).Count(&total)
  11. return
  12. }
  13. type PatientContagionsCountStruct struct {
  14. DiseaseId int64 `json:"disease_id"`
  15. Count int64 `json:"count"`
  16. }
  17. func GetPatientContagionCounts(orgID int64) (counts []*PatientContagionsCountStruct) {
  18. readDb.Table("xt_patients_infectious_diseases AS pid").Joins("JOIN xt_patients AS p ON p.id=pid.patient_id").Where("p.user_org_id=? and p.status=1 and pid.status=1", orgID).Select("pid.disease_id, count(pid.disease_id) as count").Group("pid.disease_id").Scan(&counts)
  19. return
  20. }
  21. type PatientGenderCountStruct struct {
  22. Gender int64 `json:"gender"`
  23. Count int64 `json:"count"`
  24. }
  25. func GetPatientGenderCounts(orgID int64) (counts []*PatientGenderCountStruct) {
  26. readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID).Select("gender, count(gender) as count").Group("gender").Scan(&counts)
  27. return
  28. }
  29. type PatientAgeCountStruct struct {
  30. Age int64 `json:"age"`
  31. Count int64 `json:"count"`
  32. }
  33. func GetPatiendAgeBetweenCount(orgID int64) (counts []*PatientAgeCountStruct) {
  34. readDb.Raw(`SELECT nnd AS 'age',COUNT(*) AS 'count' FROM(
  35. SELECT
  36. CASE
  37. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<10 THEN '10'
  38. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=10 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<20 THEN '20'
  39. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=20 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<30 THEN '30'
  40. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=30 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<40 THEN '40'
  41. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=40 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<50 THEN '50'
  42. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=50 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<60 THEN '60'
  43. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=60 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<70 THEN '70'
  44. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=70 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<80 THEN '80'
  45. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=80 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<90 THEN '90'
  46. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=90 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + ( FROM_UNIXTIME(birthday, '%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<100 THEN '100'
  47. ELSE '-1'
  48. END
  49. AS nnd FROM xt_patients where user_org_id=? and status=1
  50. )a GROUP BY nnd`, orgID).Scan(&counts)
  51. return
  52. }
  53. type DialysisModeCountStruct struct {
  54. Date int64 `json:"date"`
  55. Mode int64 `json:"mode"`
  56. Count int64 `json:"count"`
  57. DateMonth string `json:"date_month"`
  58. }
  59. func GetPatientDialysisModeBetweenCount(orgID int64, start,end int64) (counts []*DialysisModeCountStruct) {
  60. readDb.Raw(`SELECT o.dialysis_date as date, p.mode_id as mode, count(p.mode_id) as count, FROM_UNIXTIME(o.dialysis_date, '%Y-%m') as date_month from xt_dialysis_order as o JOIN xt_dialysis_prescription as p ON p.record_date=o.dialysis_date WHERE o.user_org_id=? and p.user_org_id=? and o.status=1 and p.status=1 and o.dialysis_date>=? and o.dialysis_date<? group by FROM_UNIXTIME(o.dialysis_date, '%Y%m'), p.mode_id`, orgID, orgID, start, end).Scan(&counts)
  61. return
  62. }