admin_service.go 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. // 统计后台的 service
  2. package service
  3. import (
  4. "XT_Admin_Api/models"
  5. "XT_Admin_Api/models/admin_models"
  6. "database/sql"
  7. "fmt"
  8. "github.com/jinzhu/gorm"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func GetAdminAccount(account string, password string) (*admin_models.AdminAccount, error) {
  14. var model admin_models.AdminAccount
  15. err := readChargeDb.Where("mobile = ? AND password = ? AND status = 1", account, password).First(&model).Error
  16. if err != nil {
  17. if err == gorm.ErrRecordNotFound {
  18. return nil, nil
  19. }
  20. return nil, err
  21. }
  22. return &model, nil
  23. }
  24. // 获取总机构数
  25. func GetTotalOrgCount() (int, error) {
  26. var count int
  27. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND import <> 1").Count(&count).Error
  28. if err != nil {
  29. return 0, err
  30. }
  31. return count, nil
  32. }
  33. func GetAdminUserSpecialPermission(orgID int64, appID int64, adminUserID int64, permissionType models.SpecialPermissionType) (*models.AdminUserSpecialPermission, error) {
  34. var record models.AdminUserSpecialPermission
  35. err := readDb.Model(&models.AdminUserSpecialPermission{}).Where("org_id = ? AND app_id = ? AND admin_user_id = ? AND permission = ? AND status = 1", orgID, appID, adminUserID, permissionType).First(&record).Error
  36. if err != nil {
  37. if err == gorm.ErrRecordNotFound {
  38. return nil, nil
  39. } else {
  40. return nil, err
  41. }
  42. }
  43. return &record, nil
  44. }
  45. // 获取一段时间内新增病人数量
  46. func GetNewPatientCount(from int64, to int64) (int, error) {
  47. var count int
  48. db := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients WHERE status > 0 AND created_time >= ? AND created_time <= ?", from, to)
  49. rows, err := db.Rows()
  50. defer rows.Close()
  51. //rows, err := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients WHERE status > 0 AND created_time >= ? AND created_time <= ?", from, to).Rows()
  52. if err != nil {
  53. return 0, err
  54. }
  55. if rows.Next() {
  56. rows.Scan(&count)
  57. }
  58. return count, nil
  59. }
  60. // 获取所有机构病人数量
  61. func GetPatientTotalCount() (int64, error) {
  62. var count int64
  63. db := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients")
  64. rows, err := db.Rows()
  65. defer rows.Close()
  66. //rows, err := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients").Rows()
  67. if err != nil {
  68. return 0, err
  69. }
  70. if rows.Next() {
  71. rows.Scan(&count)
  72. }
  73. return count, nil
  74. }
  75. // 获取一段时间内的活跃账户数
  76. func GetActiveAdminUserCount(from int64, to int64) (int64, error) {
  77. var count int64
  78. db := readUserDb.Raw("SELECT COUNT(DISTINCT admin_user_id) AS count FROM sgj_user_admin_login_log WHERE ctime >= ? AND ctime <= ? AND operate_type = 1 AND app_type = 3", from, to)
  79. rows, err := db.Rows()
  80. defer rows.Close()
  81. //rows, err := readUserDb.Raw("SELECT COUNT(DISTINCT admin_user_id) AS count FROM sgj_user_admin_login_log WHERE ctime >= ? AND ctime <= ? AND operate_type = 1 AND app_type = 3", from, to).Rows()
  82. if err != nil {
  83. return 0, err
  84. }
  85. if rows.Next() {
  86. rows.Scan(&count)
  87. }
  88. return count, nil
  89. }
  90. // 获取所有机构账户数量
  91. func GetAdminUserTotalCount() (int64, error) {
  92. var count int64
  93. db := readUserDb.Raw("SELECT COUNT(DISTINCT admin_user_id) AS count FROM sgj_user_admin_login_log WHERE operate_type = 1 AND app_type = 3")
  94. rows, err := db.Rows()
  95. defer rows.Close()
  96. //rows, err := readUserDb.Raw("SELECT COUNT(DISTINCT admin_user_id) AS count FROM sgj_user_admin_login_log WHERE operate_type = 1 AND app_type = 3").Rows()
  97. if err != nil {
  98. return 0, err
  99. }
  100. if rows.Next() {
  101. rows.Scan(&count)
  102. }
  103. return count, nil
  104. }
  105. // 获取一段时间内注册的机构数量
  106. func GetRegistedOrgCount(from int64, to int64) (int64, error) {
  107. var count int64
  108. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND ctime >= ? AND ctime <= ? AND import <> 1", from, to).Count(&count).Error
  109. if err != nil {
  110. return 0, err
  111. }
  112. return count, nil
  113. }
  114. // 获取一段时间内的活跃机构数
  115. func GetActiveOrgCount(from int64, to int64) (int64, error) {
  116. var org []*Org
  117. //readUserDb.Raw("Select count(DISTINCT(org_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_admin_login_log log JOIN (Select d.user_org_id from (Select new_role.user_org_id from (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role where role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients where status = 1 Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id where ctime >= ? AND ctime <= ? Group by log.org_id", from, to).Scan(&org)
  118. readUserDb.Raw("Select org_id from sgj_user_admin_login_log log JOIN (Select d.user_org_id from (Select new_role.user_org_id from (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role where role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients where status = 1 Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id where ctime >= ? AND ctime <= ? Group by log.org_id", from, to).Scan(&org)
  119. return int64(len(org)), nil
  120. }
  121. type Org struct {
  122. OrgId int64
  123. }
  124. // 获取所有活跃机构数
  125. func GetActiveOrgTotalCount() (int64, error) {
  126. var org []*Org
  127. readUserDb.Raw("Select org_id from sgj_user_admin_login_log log JOIN (Select d.user_org_id from (Select new_role.user_org_id from (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role where role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients where status = 1 Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id Group by log.org_id").Scan(&org)
  128. fmt.Println(org)
  129. return int64(len(org)), nil
  130. }
  131. // 获取昨天的注册机构总量
  132. func GetYesterDayRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  133. err = readUserDb.Raw("select count(id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_org where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(sgj_user_org.ctime)) AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  134. return
  135. }
  136. // 获取近昨天的的机构活跃总量
  137. func GetYesterDayActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  138. db := readUserDb.Raw(" Select count(DISTINCT(org_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_admin_login_log log JOIN (Select d.user_org_id from (Select new_role.user_org_id from (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role where role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients where status = 1 Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(ctime)) Group by date(from_unixtime(ctime))")
  139. err = db.Scan(&weekStatistics).Error
  140. return
  141. }
  142. // 获取昨天的的机构活跃账号总量
  143. func GetYesterDayActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  144. err = readUserDb.Raw("select count(DISTINCT(admin_user_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_admin_login_log where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(sgj_user_admin_login_log.ctime)) AND operate_type = 1 AND app_type = 3 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  145. return
  146. }
  147. // 获取昨天的的机构新增病人总量
  148. func GetYesterDayNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  149. err = readDb.Raw("select count(DISTINCT(id_card_no)) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_patients where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) GROUP BY date(from_unixtime(created_time));").Scan(&weekStatistics).Error
  150. return
  151. }
  152. // 获取近七天每天的注册机构总量
  153. func GetWeekRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  154. err = readUserDb.Raw("select count(id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_org where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(sgj_user_org.ctime)) AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  155. return
  156. }
  157. //func GetWeekActiveOrgCount() (weekStatistics []*admin_models.WeekStatistics, err error) {
  158. // err = readUserDb.Raw("select count(id) as count, date(from_unixtime(ctime)) as times from sgj_user_org where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(sgj_user_org.ctime)) GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  159. // return
  160. //}
  161. // 获取近七天每天的机构活跃总量
  162. func GetWeekActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  163. //db := readDb.Raw("select count(DISTINCT(user_org_id)) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_dialysis_order where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 GROUP BY date(from_unixtime(created_time));")
  164. db := readUserDb.Raw(" Select count(DISTINCT(org_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_admin_login_log log JOIN (Select d.user_org_id from (Select new_role.user_org_id from (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role where role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients where status = 1 Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(ctime)) Group by date(from_unixtime(ctime))")
  165. err = db.Scan(&weekStatistics).Error
  166. return
  167. }
  168. // 获取近七天每天的机构活跃账号总量
  169. func GetWeekActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  170. err = readUserDb.Raw("select count(DISTINCT(admin_user_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_admin_login_log where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND operate_type = 1 AND app_type = 3 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  171. return
  172. }
  173. // 获取近七天每天的机构新增病人总量
  174. func GetWeekNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  175. err = readDb.Raw("select count(DISTINCT(id_card_no)) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_patients where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) GROUP BY date(from_unixtime(created_time));").Scan(&weekStatistics).Error
  176. return
  177. }
  178. // 获取近30天每天的注册机构总量
  179. func GetMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  180. err = readUserDb.Raw("select count(id) as count,DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_org where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(sgj_user_org.ctime)) AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  181. return
  182. }
  183. // 获取近30天每天的机构活跃总量
  184. func GetMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  185. err = readUserDb.Raw("Select count(DISTINCT(org_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_admin_login_log log JOIN (Select d.user_org_id from (Select new_role.user_org_id from (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role where role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients where status = 1 Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(ctime)) Group by date(from_unixtime(ctime))").Scan(&weekStatistics).Error
  186. return
  187. }
  188. // 获取近30天每天的机构活跃账号总量
  189. func GetMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  190. err = readUserDb.Raw("select count(DISTINCT(admin_user_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d')as times from sgj_user_admin_login_log where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND sgj_user_admin_login_log.app_type = 3 AND sgj_user_admin_login_log.operate_type = 1 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  191. return
  192. }
  193. // 获取近30天每天的机构新增病人总量
  194. func GetMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  195. err = readDb.Raw("select count(DISTINCT(id_card_no)) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_patients where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) GROUP BY date(from_unixtime(created_time));").Scan(&weekStatistics).Error
  196. return
  197. }
  198. // 获取近3个月每个月的注册机构总量
  199. func GetThreeMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  200. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)) ,'%Y-%m') as times,count(DISTINCT(id)) as count from sgj_user_org where DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 3 month),'%Y-%m') AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 group by times").Scan(&weekStatistics).Error
  201. return
  202. }
  203. // 获取近3个月每个月的机构活跃账号总量
  204. func GetThreeMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  205. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m') as times,count(DISTINCT(admin_user_id)) as count from sgj_user_admin_login_log where DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 3 month),'%Y-%m') AND sgj_user_admin_login_log.app_type = 3 AND sgj_user_admin_login_log.operate_type = 1 group by times").Scan(&weekStatistics).Error
  206. return
  207. }
  208. // 获取近3个月每个月的机构活跃总量
  209. func GetThreeMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  210. err = readUserDb.Raw(" Select count(DISTINCT(org_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times from sgj_user_admin_login_log log JOIN (Select d.user_org_id from (Select new_role.user_org_id from (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role where role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients where status = 1 Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id where DATE_FORMAT(date(from_unixtime(ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 3 month),'%Y-%m') Group by times").Scan(&weekStatistics).Error
  211. return
  212. }
  213. // 获取近3个月每个月机构新增病人总量
  214. func GetThreeMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  215. err = readDb.Raw("select DATE_FORMAT(date(from_unixtime(xt_patients.created_time)) ,'%Y-%m') as times,count(DISTINCT(id_card_no)) as count from xt_patients where DATE_FORMAT(date(from_unixtime(xt_patients.created_time)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 3 month),'%Y-%m') group by times").Scan(&weekStatistics).Error
  216. return
  217. }
  218. // 获取近半年每个月的注册机构总量
  219. func GetSixMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  220. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)) ,'%Y-%m') as times,count(DISTINCT(id)) as count from sgj_user_org where DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 6 month),'%Y-%m') AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 group by times").Scan(&weekStatistics).Error
  221. return
  222. }
  223. // 获取近半年每个月的机构活跃账号总量
  224. func GetSixMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  225. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m') as times,count(DISTINCT(admin_user_id)) as count from sgj_user_admin_login_log where DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 6 month),'%Y-%m') AND sgj_user_admin_login_log.app_type = 3 AND sgj_user_admin_login_log.operate_type = 1 group by times").Scan(&weekStatistics).Error
  226. return
  227. }
  228. // 获取近半年每个月的机构活跃总量
  229. func GetSixMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  230. err = readUserDb.Raw(" Select count(DISTINCT(org_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times from sgj_user_admin_login_log log JOIN (Select d.user_org_id from (Select new_role.user_org_id from (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role where role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients where status = 1 Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id where DATE_FORMAT(date(from_unixtime(ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 6 month),'%Y-%m') Group by times").Scan(&weekStatistics).Error
  231. return
  232. }
  233. // 获取近半年每个月机构新增病人总量
  234. func GetSixMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  235. err = readDb.Raw("select DATE_FORMAT(date(from_unixtime(xt_patients.created_time)) ,'%Y-%m') as times,count(DISTINCT(id_card_no)) as count from xt_patients where DATE_FORMAT(date(from_unixtime(xt_patients.created_time)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 6 month),'%Y-%m') group by times").Scan(&weekStatistics).Error
  236. return
  237. }
  238. // 获取近1年每个月的注册机构总量
  239. func GetYearRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  240. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)) ,'%Y-%m') as times,count(DISTINCT(id)) as count from sgj_user_org where DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m') AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 group by times").Scan(&weekStatistics).Error
  241. return
  242. }
  243. // 获取近1年每个月的机构活跃账号总量
  244. func GetYearActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  245. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m') as times,count(DISTINCT(admin_user_id)) as count from sgj_user_admin_login_log where DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m') AND sgj_user_admin_login_log.app_type = 3 AND sgj_user_admin_login_log.operate_type = 1 group by times").Scan(&weekStatistics).Error
  246. return
  247. }
  248. // 获取近1年每个月的机构活跃总量
  249. func GetYearActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  250. err = readUserDb.Raw(" Select count(DISTINCT(org_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times from sgj_user_admin_login_log log JOIN (Select d.user_org_id from (Select new_role.user_org_id from (Select count(*) as count,org_id as user_org_id from sgj_users.sgj_user_admin_role role where role.`user_type` = 2 OR role.`user_type` = 3 Group by role.org_id) new_role where new_role.count > 2) d JOIN (Select user_org_id from (Select count(*) as count,user_org_id from sgj_xt.xt_patients where status = 1 Group by user_org_id) new_patient where new_patient.count > 3) c On d.user_org_id = c.user_org_id) new_user_org ON new_user_org.user_org_id = log.org_id where DATE_FORMAT(date(from_unixtime(ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m') Group by times").Scan(&weekStatistics).Error
  251. return
  252. }
  253. // 获取近1年每个月机构新增病人总量
  254. func GetYearNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  255. err = readDb.Raw("select DATE_FORMAT(date(from_unixtime(xt_patients.created_time)) ,'%Y-%m') as times,count(DISTINCT(id_card_no)) as count from xt_patients where DATE_FORMAT(date(from_unixtime(xt_patients.created_time)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m') AND status > 0 group by times").Scan(&weekStatistics).Error
  256. return
  257. }
  258. //func GetAllAdmin() (admin []*admin_models.AdminAccount, err error) {
  259. // err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("is_admin <> 1 AND (status = 1 OR status = 2)").Find(&admin).Error
  260. // return
  261. //}
  262. func GetAllAdmin() (admin []*admin_models.AdminAccount, err error) {
  263. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("is_admin <> 1 AND (status = 1 OR status = 2)").Find(&admin).Error
  264. return
  265. }
  266. func CreateAdmin(admin *admin_models.AdminAccount) (err error) {
  267. err = readChargeDb.Model(&admin_models.AdminAccount{}).Create(&admin).Error
  268. return
  269. }
  270. func FindUserInfoByAccount(account string) (admin admin_models.AdminAccount, err error) {
  271. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("mobile = ? AND status = 1", account).First(&admin).Error
  272. return
  273. }
  274. func FindAdminById(id int64) (admin admin_models.AdminAccount, err error) {
  275. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).First(&admin).Error
  276. return
  277. }
  278. func UpdateAdmin(admin *admin_models.AdminAccount) (err error) {
  279. err = readChargeDb.Model(&admin_models.AdminAccount{}).Save(&admin).Error
  280. return
  281. }
  282. func DeleteAdmin(id int64) (err error) {
  283. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  284. //err = readChargeDb.Model(&models.OrgFollow{}).Where("admin_user_id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  285. return
  286. }
  287. func UpdateAdminTwo(id int64) (err error) {
  288. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).Updates(map[string]interface{}{"status": 1}).Error
  289. //err = readChargeDb.Model(&models.OrgFollow{}).Where("admin_user_id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  290. return
  291. }
  292. func FindAllOrg() (org []*models.Org, err error) {
  293. err = readUserDb.Select("id, org_name").Where("status = 1").Find(&org).Error
  294. return
  295. }
  296. type FollowViewModel struct {
  297. models.OrgFollow
  298. OrgName string `gorm:"column:org_name" json:"org_name"`
  299. }
  300. func FindFollowOrg(admin_id int64) ([]*FollowViewModel, error) {
  301. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  302. rows, err := readUserDb.Raw("SELECT follow.*, org.org_name as org_name FROM sgj_org_follow as follow join sgj_user_org as org on org.id = follow.org_id WHERE (follow.admin_user_id = ? AND follow.status = 1)", admin_id).Rows()
  303. defer rows.Close()
  304. if err != nil {
  305. return nil, err
  306. }
  307. for rows.Next() {
  308. var vm FollowViewModel
  309. readDb.ScanRows(rows, &vm)
  310. vms = append(vms, &vm)
  311. }
  312. fmt.Println(vms)
  313. return vms, nil
  314. }
  315. type OrgViewModel struct {
  316. Id int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT" json:"id"` // 机构 ID
  317. OrgName string `gorm:"column:org_name" json:"org_name"`
  318. }
  319. func FindUnFollowOrgByIds(id int64) (orgViewModel []*OrgViewModel, err error) {
  320. err = readUserDb.Raw("Select id, org_name From sgj_user_org Where NOT EXISTS (Select id, org_name FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1)", id).Scan(&orgViewModel).Error
  321. return
  322. }
  323. func CreateFollowInfo(Follows []*models.OrgFollow) (err error) {
  324. if len(Follows) > 0 {
  325. utx := writeUserDb.Begin()
  326. if len(Follows) > 0 {
  327. thisSQL := "INSERT INTO sgj_org_follow (org_id,admin_user_id,ctime,mtime,status) VALUES "
  328. insertParams := make([]string, 0)
  329. insertData := make([]interface{}, 0)
  330. for _, info := range Follows {
  331. insertParams = append(insertParams, "(?,?,?,?,?)")
  332. insertData = append(insertData, info.OrgId)
  333. insertData = append(insertData, info.AdminUserId)
  334. insertData = append(insertData, info.Ctime)
  335. insertData = append(insertData, info.Mtime)
  336. insertData = append(insertData, info.Status)
  337. }
  338. thisSQL += strings.Join(insertParams, ", ")
  339. err = utx.Exec(thisSQL, insertData...).Error
  340. if err != nil {
  341. utx.Rollback()
  342. return
  343. }
  344. }
  345. utx.Commit()
  346. }
  347. return
  348. }
  349. func UpdateFollow(follow *models.OrgFollow) (err error) {
  350. err = readUserDb.Model(&models.OrgFollow{}).Save(follow).Error
  351. return
  352. }
  353. func FindFollowRecordByID(follow *models.OrgFollow) (info models.OrgFollow, err error) {
  354. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND admin_user_id = ?", follow.OrgId, follow.AdminUserId).First(&info).Error
  355. return
  356. }
  357. func FindFollowInfoById(org_id int64, admin_user_id int64) (info models.OrgFollow, err error) {
  358. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND status = 1 AND admin_user_id = ?", org_id, admin_user_id).First(&info).Error
  359. return
  360. }
  361. func GetAllWaitFollowOrgList(keyword string, page int64, limit int64, id int64) (orgViewModel []*OrgViewModel, err error, total int64) {
  362. type Total struct {
  363. Count int64
  364. }
  365. var totals Total
  366. offset := (page - 1) * limit
  367. db := readUserDb
  368. if len(keyword) > 0 {
  369. likeKey := "%" + keyword + "%"
  370. err = db.Raw("Select id, org_name From sgj_user_org Where NOT EXISTS (Select id, org_name FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1 ) AND import <> 1 AND (sgj_user_org.org_name Like ? OR sgj_user_org.org_short_name Like ?) AND status = 1 ORDER BY sgj_user_org.ctime DESC", id, likeKey, likeKey).Offset(offset).Limit(limit).Scan(&orgViewModel).Error
  371. readUserDb.Raw("Select count(id) as count From sgj_user_org Where NOT EXISTS (Select id, org_name FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1) AND import <> 1 AND (sgj_user_org.org_name Like ? OR sgj_user_org.org_short_name Like ?) AND status = 1", id, likeKey, likeKey).Scan(&totals)
  372. } else {
  373. db = db.Raw("Select id, org_name From sgj_user_org Where NOT EXISTS (Select id, org_name FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1) AND import <> 1 AND status = 1 ORDER BY sgj_user_org.ctime DESC", id)
  374. err = db.Offset(offset).Limit(limit).Scan(&orgViewModel).Error
  375. readUserDb.Raw("Select count(id) as count From sgj_user_org Where NOT EXISTS (Select id, org_name FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1) AND import <> 1 AND status = 1", id).Scan(&totals)
  376. }
  377. return orgViewModel, err, totals.Count
  378. }
  379. func FindAllFollowOrg(keyword string, page int64, limit int64, admin_id int64) (follow []*FollowViewModel, err error, total int64) {
  380. type Total struct {
  381. Count int64
  382. }
  383. var totals Total
  384. offset := (page - 1) * limit
  385. if len(keyword) > 0 {
  386. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  387. var rows *sql.Rows
  388. var errs error
  389. likeKey := "%" + keyword + "%"
  390. rows, errs = readUserDb.Raw("SELECT follow.*, org.org_name as org_name, org.ctime as time FROM sgj_org_follow as follow join sgj_user_org as org on org.id = follow.org_id AND (org.org_name Like ? OR org.org_short_name Like ?) AND org.import <> 1 AND org.status = 1 WHERE (follow.admin_user_id = ? AND follow.status = 1) ORDER BY time DESC", likeKey, likeKey, admin_id).Offset(offset).Limit(limit).Rows()
  391. readUserDb.Raw("SELECT count(follow.id) as count FROM sgj_org_follow as follow join sgj_user_org as org on org.id = follow.org_id AND (org.org_name Like ? OR org.org_short_name Like ?) AND org.import <> 1 AND org.status = 1 WHERE follow.admin_user_id = ? AND follow.status = 1", likeKey, likeKey, admin_id).Scan(&totals)
  392. defer rows.Close()
  393. if errs != nil {
  394. return nil, errs, totals.Count
  395. }
  396. for rows.Next() {
  397. var vm FollowViewModel
  398. readDb.ScanRows(rows, &vm)
  399. vms = append(vms, &vm)
  400. }
  401. return vms, nil, totals.Count
  402. } else {
  403. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  404. var rows *sql.Rows
  405. var errs error
  406. rows, errs = readUserDb.Raw("SELECT follow.*, org.org_name as org_name, org.ctime as time FROM sgj_org_follow as follow join sgj_user_org as org on org.id = follow.org_id AND org.import <> 1 AND org.status = 1 WHERE follow.admin_user_id = ? AND follow.status = 1 ORDER BY time DESC", admin_id).Offset(offset).Limit(limit).Rows()
  407. readUserDb.Raw("SELECT count(follow.id) as count FROM sgj_org_follow as follow join sgj_user_org as org on org.id = follow.org_id AND org.import <> 1 AND org.status = 1 WHERE follow.admin_user_id = ? AND follow.status = 1", admin_id).Scan(&totals)
  408. defer rows.Close()
  409. if errs != nil {
  410. return nil, errs, totals.Count
  411. }
  412. for rows.Next() {
  413. var vm FollowViewModel
  414. readDb.ScanRows(rows, &vm)
  415. vms = append(vms, &vm)
  416. }
  417. return vms, nil, totals.Count
  418. }
  419. }
  420. // 获取累积客户数
  421. func GetCustomCount() (int64, error) {
  422. var count int64
  423. err := ChargeReadDB().Model(&models.KyyChargeCustom{}).Where("status = 1").Count(&count).Error
  424. if err != nil {
  425. return 0, err
  426. }
  427. return count, nil
  428. }
  429. // 本年新增收费客户
  430. func GetCurYearCustomCount() (int64, error) {
  431. var count int64
  432. err := ChargeReadDB().Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(sign_time) = YEAR(CURDATE())").Count(&count).Error
  433. if err != nil {
  434. return 0, err
  435. }
  436. return count, nil
  437. }
  438. // 统计本年新增标准版客户
  439. func GetCurYearStandCustomCount() (int64, error) {
  440. var count int64
  441. err := ChargeReadDB().Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(sign_time) = YEAR(CURDATE()) and custom_type = 1").Count(&count).Error
  442. if err != nil {
  443. return 0, err
  444. }
  445. return count, nil
  446. }
  447. // 统计本年新增尊享版客户
  448. func GetCurYearDeluxeCustomCount() (int64, error) {
  449. var count int64
  450. err := ChargeReadDB().Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(sign_time) = YEAR(CURDATE()) and custom_type = 2").Count(&count).Error
  451. if err != nil {
  452. return 0, err
  453. }
  454. return count, nil
  455. }
  456. // 统计本年新增专业版客户
  457. func GetCurYearProfessionalCustomCount() (int64, error) {
  458. var count int64
  459. err := ChargeReadDB().Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(sign_time) = YEAR(CURDATE()) and custom_type = 3").Count(&count).Error
  460. if err != nil {
  461. return 0, err
  462. }
  463. return count, nil
  464. }
  465. // 统计本年新增其他版客户
  466. func GetCurYearOtherCustomCount() (int64, error) {
  467. var count int64
  468. err := ChargeReadDB().Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(sign_time) = YEAR(CURDATE()) and custom_type = 4").Count(&count).Error
  469. if err != nil {
  470. return 0, err
  471. }
  472. return count, nil
  473. }
  474. func GetCurYearContractPriceCount() (float64, error) {
  475. type Count struct {
  476. Count float64
  477. }
  478. var count Count
  479. err := ChargeReadDB().Model(&models.KyyChargeContract{}).Select("sum(sumamt) as count").Where("status = 1 and YEAR(start_time) = YEAR(CURDATE())").Scan(&count).Error
  480. if err != nil {
  481. return 0, err
  482. }
  483. return count.Count, nil
  484. }
  485. func GetCurYearSoftwarePriceCount() (float64, error) {
  486. type Count struct {
  487. Count float64
  488. }
  489. var count Count
  490. err := ChargeReadDB().Model(&models.KyyChargeContract{}).Select("sum(software_price) as count").Where("status = 1 and YEAR(start_time) = YEAR(CURDATE())").Scan(&count).Error
  491. if err != nil {
  492. return 0, err
  493. }
  494. return count.Count, nil
  495. }
  496. func GetCurYearHardwarePriceCount() (float64, error) {
  497. type Count struct {
  498. Count float64
  499. }
  500. var count Count
  501. err := ChargeReadDB().Model(&models.KyyChargeContract{}).Select("sum(hardware_price) as count").Where("status = 1 and YEAR(start_time) = YEAR(CURDATE())").Scan(&count).Error
  502. if err != nil {
  503. return 0, err
  504. }
  505. return count.Count, nil
  506. }
  507. func GetCurYearPaymentCollectionSumamtPriceCount() (float64, error) {
  508. type Count struct {
  509. Count float64
  510. }
  511. var count Count
  512. err := ChargeReadDB().Model(&models.KyyChargePaymentCollection{}).Select("sum(payment_collection_sumamt) as count").Where("status = 1 and YEAR(payment_collection_date) = YEAR(CURDATE())").Scan(&count).Error
  513. if err != nil {
  514. return 0, err
  515. }
  516. return count.Count, nil
  517. }
  518. func GetCurYearPaymentCollectionSoftwarePriceCount() (float64, error) {
  519. type Count struct {
  520. Count float64
  521. }
  522. var count Count
  523. err := ChargeReadDB().Model(&models.KyyChargePaymentCollection{}).Select("sum(software_sumamt) as count").Where("status = 1 and YEAR(payment_collection_date) = YEAR(CURDATE())").Scan(&count).Error
  524. if err != nil {
  525. return 0, err
  526. }
  527. return count.Count, nil
  528. }
  529. func GetCurYearPaymentCollectionHardwarePriceCount() (float64, error) {
  530. type Count struct {
  531. Count float64
  532. }
  533. var count Count
  534. err := ChargeReadDB().Model(&models.KyyChargePaymentCollection{}).Select("sum(hardware_sumamt) as count").Where("status = 1 and YEAR(payment_collection_date) = YEAR(CURDATE())").Scan(&count).Error
  535. if err != nil {
  536. return 0, err
  537. }
  538. return count.Count, nil
  539. }
  540. type ChargeCustom struct {
  541. ID uint
  542. Name string
  543. SignTime string
  544. SignEndTime string
  545. ContractPrice string
  546. SoftwarePrice string
  547. HardwarePrice string
  548. Address string
  549. IsImplement int
  550. Remark string
  551. }
  552. type ChargeRenewal struct {
  553. ID uint
  554. CustomID uint
  555. SignEndTime string
  556. }
  557. type Results struct {
  558. ChargeCustom
  559. SignEndTime string
  560. }
  561. func GetOneMonthEndDate() (results []Results) {
  562. currentDate := time.Now()
  563. //threeMonthsLater := currentDate.AddDate(0, 3, 0)
  564. oneMonthLater := currentDate.AddDate(0, 1, 0)
  565. ChargeReadDB().Table("kyy_charge_custom as kc").
  566. Select("kc.*, COALESCE(kr.end_time, kc.sign_end_time) as sign_end_time").
  567. Joins("LEFT JOIN kyy_charge_renewal as kr ON kc.id = kr.custom_id").
  568. Where("(kr.end_time BETWEEN ? AND ?) OR (kr.end_time BETWEEN ? AND ?) OR (kr.end_time IS NULL AND kc.sign_end_time BETWEEN ? AND ?) OR (kr.end_time IS NULL AND kc.sign_end_time BETWEEN ? AND ?)",
  569. currentDate, oneMonthLater,
  570. currentDate, oneMonthLater,
  571. currentDate, oneMonthLater,
  572. currentDate, oneMonthLater).
  573. Scan(&results)
  574. return
  575. }
  576. func GetThreeMonthEndDate() (results []Results) {
  577. currentDate := time.Now()
  578. threeMonthsLater := currentDate.AddDate(0, 3, 0)
  579. ChargeReadDB().Table("kyy_charge_custom as kc").
  580. Select("kc.*, COALESCE(kr.end_time, kc.sign_end_time) as sign_end_time").
  581. Joins("LEFT JOIN kyy_charge_renewal as kr ON kc.id = kr.custom_id").
  582. Where("(kr.end_time BETWEEN ? AND ?) OR (kr.end_time BETWEEN ? AND ?) OR (kr.end_time IS NULL AND kc.sign_end_time BETWEEN ? AND ?) OR (kr.end_time IS NULL AND kc.sign_end_time BETWEEN ? AND ?)",
  583. currentDate, threeMonthsLater,
  584. currentDate, threeMonthsLater,
  585. currentDate, threeMonthsLater,
  586. currentDate, threeMonthsLater).
  587. Scan(&results)
  588. return
  589. }
  590. func GetCurYearRenewal() (err error, count int64) {
  591. currentYear := time.Now().Year()
  592. err = ChargeReadDB().Model(&models.KyyChargeRenewal{}).Where("YEAR(start_time) = ?", currentYear).Count(&count).Error
  593. if err != nil {
  594. fmt.Println(err)
  595. }
  596. return
  597. }
  598. func GetCurYearPaymentCollection() (err error, count int64) {
  599. currentYear := time.Now().Year()
  600. var kpc []models.KyyChargePaymentCollection
  601. err = ChargeReadDB().Model(&models.KyyChargePaymentCollection{}).Where("YEAR(payment_collection_date) = ? and payment_collection_type = 1", currentYear).Find(&kpc).Error
  602. if err != nil {
  603. fmt.Println(err)
  604. }
  605. count = 0
  606. for _, item := range kpc {
  607. pcs, _ := strconv.ParseInt(item.PaymentCollectionSumamt, 10, 64)
  608. count = count + pcs
  609. }
  610. return
  611. }
  612. func GetCurYearWaitPutIntoEffect() (count int64, err error) {
  613. currentYear := time.Now().Year()
  614. err = ChargeReadDB().Model(&models.KyyChargeCustom{}).Where("YEAR(sign_time) = ? and is_implement = 0", currentYear).Count(&count).Error
  615. if err != nil {
  616. fmt.Println(err)
  617. }
  618. return
  619. }
  620. func GetCurYearWaitAbutment() (count int64, err error) {
  621. currentYear := time.Now().Year()
  622. err = ChargeReadDB().Model(&models.KyyChargeCustom{}).Where("(YEAR(sign_time) = ? and lis_status = 0) or (YEAR(sign_time) = ? and qc_status = 0) or (YEAR(sign_time) = ? and yb_status = 0)", currentYear, currentYear, currentYear).Count(&count).Error
  623. if err != nil {
  624. fmt.Println(err)
  625. }
  626. return
  627. }
  628. type ContractData struct {
  629. Month int `json:"month"`
  630. TotalContractAmount float64 `json:"total_contract_amount"`
  631. SoftwareContractAmount float64 `json:"software_contract_amount"`
  632. HardwareContractAmount float64 `json:"hardware_contract_amount"`
  633. }
  634. type PaymentData struct {
  635. Month int `json:"month"`
  636. TotalPaymentAmount float64 `json:"total_payment_amount"`
  637. SoftwarePaymentAmount float64 `json:"software_payment_amount"`
  638. HardwarePaymentAmount float64 `json:"hardware_payment_amount"`
  639. }
  640. type MonthlyData struct {
  641. Month int `json:"month"`
  642. TotalContractAmount float64 `json:"total_contract_amount"`
  643. SoftwareContractAmount float64 `json:"software_contract_amount"`
  644. HardwareContractAmount float64 `json:"hardware_contract_amount"`
  645. TotalPaymentAmount float64 `json:"total_payment_amount"`
  646. SoftwarePaymentAmount float64 `json:"software_payment_amount"`
  647. HardwarePaymentAmount float64 `json:"hardware_payment_amount"`
  648. RenewalPrice float64 `json:"renewal_price"`
  649. }
  650. type MonthlyRenewalData struct {
  651. Month int `json:"month"`
  652. RenewalPrice float64 `json:"renewal_price"`
  653. }
  654. func GetCurePrice() (aa map[int]*MonthlyData) {
  655. // 查询合同金额
  656. var contractResults []ContractData
  657. ChargeReadDB().Raw(`
  658. SELECT
  659. MONTH(sign_time) AS month,
  660. SUM(CAST(contract_price AS DECIMAL(10, 2))) AS total_contract_amount,
  661. SUM(CAST(software_price AS DECIMAL(10, 2))) AS software_contract_amount,
  662. SUM(CAST(hardware_price AS DECIMAL(10, 2))) AS hardware_contract_amount
  663. FROM kyy_charge_custom
  664. GROUP BY MONTH((sign_time))
  665. ORDER BY MONTH((sign_time));
  666. `).Scan(&contractResults)
  667. // 查询回款金额
  668. var paymentResults []PaymentData
  669. ChargeReadDB().Raw(`
  670. SELECT
  671. MONTH(payment_collection_date) AS month,
  672. SUM(CAST(payment_collection_sumamt AS DECIMAL(10, 2))) AS total_payment_amount,
  673. SUM(CAST(software_sumamt AS DECIMAL(10, 2))) AS software_payment_amount,
  674. SUM(CAST(hardware_sumamt AS DECIMAL(10, 2))) AS hardware_payment_amount
  675. FROM kyy_charge_payment_collection
  676. GROUP BY MONTH(payment_collection_date)
  677. ORDER BY MONTH(payment_collection_date);
  678. `).Scan(&paymentResults)
  679. // 查询续约总汇款金额
  680. var paymentRenewalResults []MonthlyRenewalData
  681. ChargeReadDB().Raw(`
  682. SELECT
  683. MONTH(sign_time) AS month,
  684. SUM(CAST(renewal_price AS DECIMAL(10, 2))) AS renewal_price
  685. FROM kyy_charge_renewal
  686. GROUP BY MONTH(sign_time)
  687. ORDER BY MONTH(sign_time)
  688. `).Scan(&paymentRenewalResults)
  689. // 合并结果
  690. monthlyData := make(map[int]*MonthlyData)
  691. for _, c := range contractResults {
  692. monthlyData[c.Month] = &MonthlyData{
  693. Month: c.Month,
  694. TotalContractAmount: c.TotalContractAmount,
  695. SoftwareContractAmount: c.SoftwareContractAmount,
  696. HardwareContractAmount: c.HardwareContractAmount,
  697. }
  698. }
  699. for _, p := range paymentResults {
  700. if md, ok := monthlyData[p.Month]; ok {
  701. md.TotalPaymentAmount = p.TotalPaymentAmount
  702. md.SoftwarePaymentAmount = p.SoftwarePaymentAmount
  703. md.HardwarePaymentAmount = p.HardwarePaymentAmount
  704. } else {
  705. monthlyData[p.Month] = &MonthlyData{
  706. Month: p.Month,
  707. TotalPaymentAmount: p.TotalPaymentAmount,
  708. SoftwarePaymentAmount: p.SoftwarePaymentAmount,
  709. HardwarePaymentAmount: p.HardwarePaymentAmount,
  710. }
  711. }
  712. }
  713. for _, c := range paymentRenewalResults {
  714. monthlyData[c.Month] = &MonthlyData{
  715. Month: c.Month,
  716. RenewalPrice: c.RenewalPrice,
  717. }
  718. }
  719. return monthlyData
  720. }