admin_service.go 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. "strings"
  10. )
  11. func GetAdminAccount(account string, password string) (*admin_models.AdminAccount, error) {
  12. var model admin_models.AdminAccount
  13. err := readChargeDb.Where("mobile = ? AND password = ? AND status = 1", account, password).First(&model).Error
  14. if err != nil {
  15. if err == gorm.ErrRecordNotFound {
  16. return nil, nil
  17. }
  18. return nil, err
  19. }
  20. return &model, nil
  21. }
  22. // 获取总机构数
  23. func GetTotalOrgCount() (int, error) {
  24. var count int
  25. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND import <> 1").Count(&count).Error
  26. if err != nil {
  27. return 0, err
  28. }
  29. return count, nil
  30. }
  31. func GetAdminUserSpecialPermission(orgID int64, appID int64, adminUserID int64, permissionType models.SpecialPermissionType) (*models.AdminUserSpecialPermission, error) {
  32. var record models.AdminUserSpecialPermission
  33. 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
  34. if err != nil {
  35. if err == gorm.ErrRecordNotFound {
  36. return nil, nil
  37. } else {
  38. return nil, err
  39. }
  40. }
  41. return &record, nil
  42. }
  43. // 获取一段时间内新增病人数量
  44. func GetNewPatientCount(from int64, to int64) (int, error) {
  45. var count int
  46. 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)
  47. rows, err := db.Rows()
  48. defer rows.Close()
  49. //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()
  50. if err != nil {
  51. return 0, err
  52. }
  53. if rows.Next() {
  54. rows.Scan(&count)
  55. }
  56. return count, nil
  57. }
  58. // 获取所有机构病人数量
  59. func GetPatientTotalCount() (int64, error) {
  60. var count int64
  61. db := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients")
  62. rows, err := db.Rows()
  63. defer rows.Close()
  64. //rows, err := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients").Rows()
  65. if err != nil {
  66. return 0, err
  67. }
  68. if rows.Next() {
  69. rows.Scan(&count)
  70. }
  71. return count, nil
  72. }
  73. // 获取一段时间内的活跃账户数
  74. func GetActiveAdminUserCount(from int64, to int64) (int64, error) {
  75. var count int64
  76. 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)
  77. rows, err := db.Rows()
  78. defer rows.Close()
  79. //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()
  80. if err != nil {
  81. return 0, err
  82. }
  83. if rows.Next() {
  84. rows.Scan(&count)
  85. }
  86. return count, nil
  87. }
  88. // 获取所有机构账户数量
  89. func GetAdminUserTotalCount() (int64, error) {
  90. var count int64
  91. 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")
  92. rows, err := db.Rows()
  93. defer rows.Close()
  94. //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()
  95. if err != nil {
  96. return 0, err
  97. }
  98. if rows.Next() {
  99. rows.Scan(&count)
  100. }
  101. return count, nil
  102. }
  103. // 获取一段时间内注册的机构数量
  104. func GetRegistedOrgCount(from int64, to int64) (int64, error) {
  105. var count int64
  106. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND ctime >= ? AND ctime <= ? AND import <> 1", from, to).Count(&count).Error
  107. if err != nil {
  108. return 0, err
  109. }
  110. return count, nil
  111. }
  112. // 获取一段时间内的活跃机构数
  113. func GetActiveOrgCount(from int64, to int64) (int64, error) {
  114. var org []*Org
  115. //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)
  116. 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)
  117. return int64(len(org)), nil
  118. }
  119. type Org struct {
  120. OrgId int64
  121. }
  122. // 获取所有活跃机构数
  123. func GetActiveOrgTotalCount() (int64, error) {
  124. var org []*Org
  125. 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)
  126. fmt.Println(org)
  127. return int64(len(org)), nil
  128. }
  129. // 获取昨天的注册机构总量
  130. func GetYesterDayRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  131. 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
  132. return
  133. }
  134. // 获取近昨天的的机构活跃总量
  135. func GetYesterDayActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  136. 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))")
  137. err = db.Scan(&weekStatistics).Error
  138. return
  139. }
  140. // 获取昨天的的机构活跃账号总量
  141. func GetYesterDayActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  142. 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
  143. return
  144. }
  145. // 获取昨天的的机构新增病人总量
  146. func GetYesterDayNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  147. 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
  148. return
  149. }
  150. // 获取近七天每天的注册机构总量
  151. func GetWeekRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  152. 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
  153. return
  154. }
  155. //func GetWeekActiveOrgCount() (weekStatistics []*admin_models.WeekStatistics, err error) {
  156. // 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
  157. // return
  158. //}
  159. // 获取近七天每天的机构活跃总量
  160. func GetWeekActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  161. //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));")
  162. 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))")
  163. err = db.Scan(&weekStatistics).Error
  164. return
  165. }
  166. // 获取近七天每天的机构活跃账号总量
  167. func GetWeekActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  168. 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
  169. return
  170. }
  171. // 获取近七天每天的机构新增病人总量
  172. func GetWeekNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  173. 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
  174. return
  175. }
  176. // 获取近30天每天的注册机构总量
  177. func GetMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  178. 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
  179. return
  180. }
  181. // 获取近30天每天的机构活跃总量
  182. func GetMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  183. 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
  184. return
  185. }
  186. // 获取近30天每天的机构活跃账号总量
  187. func GetMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  188. 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
  189. return
  190. }
  191. // 获取近30天每天的机构新增病人总量
  192. func GetMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  193. 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
  194. return
  195. }
  196. // 获取近3个月每个月的注册机构总量
  197. func GetThreeMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  198. 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
  199. return
  200. }
  201. // 获取近3个月每个月的机构活跃账号总量
  202. func GetThreeMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  203. 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
  204. return
  205. }
  206. // 获取近3个月每个月的机构活跃总量
  207. func GetThreeMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  208. 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
  209. return
  210. }
  211. // 获取近3个月每个月机构新增病人总量
  212. func GetThreeMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  213. 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
  214. return
  215. }
  216. // 获取近半年每个月的注册机构总量
  217. func GetSixMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  218. 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
  219. return
  220. }
  221. // 获取近半年每个月的机构活跃账号总量
  222. func GetSixMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  223. 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
  224. return
  225. }
  226. // 获取近半年每个月的机构活跃总量
  227. func GetSixMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  228. 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
  229. return
  230. }
  231. // 获取近半年每个月机构新增病人总量
  232. func GetSixMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  233. 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
  234. return
  235. }
  236. // 获取近1年每个月的注册机构总量
  237. func GetYearRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  238. 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
  239. return
  240. }
  241. // 获取近1年每个月的机构活跃账号总量
  242. func GetYearActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  243. 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
  244. return
  245. }
  246. // 获取近1年每个月的机构活跃总量
  247. func GetYearActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  248. 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
  249. return
  250. }
  251. // 获取近1年每个月机构新增病人总量
  252. func GetYearNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  253. 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
  254. return
  255. }
  256. //func GetAllAdmin() (admin []*admin_models.AdminAccount, err error) {
  257. // err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("is_admin <> 1 AND (status = 1 OR status = 2)").Find(&admin).Error
  258. // return
  259. //}
  260. func GetAllAdmin() (admin []*admin_models.AdminAccount, err error) {
  261. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("is_admin <> 1 AND (status = 1 OR status = 2)").Find(&admin).Error
  262. return
  263. }
  264. func CreateAdmin(admin *admin_models.AdminAccount) (err error) {
  265. err = readChargeDb.Model(&admin_models.AdminAccount{}).Create(&admin).Error
  266. return
  267. }
  268. func FindUserInfoByAccount(account string) (admin admin_models.AdminAccount, err error) {
  269. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("account = ? AND status = 1", account).First(&admin).Error
  270. return
  271. }
  272. func FindAdminById(id int64) (admin admin_models.AdminAccount, err error) {
  273. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).First(&admin).Error
  274. return
  275. }
  276. func UpdateAdmin(admin *admin_models.AdminAccount) (err error) {
  277. err = readChargeDb.Model(&admin_models.AdminAccount{}).Save(&admin).Error
  278. return
  279. }
  280. func DeleteAdmin(id int64) (err error) {
  281. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  282. //err = readChargeDb.Model(&models.OrgFollow{}).Where("admin_user_id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  283. return
  284. }
  285. func UpdateAdminTwo(id int64) (err error) {
  286. err = readChargeDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).Updates(map[string]interface{}{"status": 1}).Error
  287. //err = readChargeDb.Model(&models.OrgFollow{}).Where("admin_user_id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  288. return
  289. }
  290. func FindAllOrg() (org []*models.Org, err error) {
  291. err = readUserDb.Select("id, org_name").Where("status = 1").Find(&org).Error
  292. return
  293. }
  294. type FollowViewModel struct {
  295. models.OrgFollow
  296. OrgName string `gorm:"column:org_name" json:"org_name"`
  297. }
  298. func FindFollowOrg(admin_id int64) ([]*FollowViewModel, error) {
  299. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  300. 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()
  301. defer rows.Close()
  302. if err != nil {
  303. return nil, err
  304. }
  305. for rows.Next() {
  306. var vm FollowViewModel
  307. readDb.ScanRows(rows, &vm)
  308. vms = append(vms, &vm)
  309. }
  310. fmt.Println(vms)
  311. return vms, nil
  312. }
  313. type OrgViewModel struct {
  314. Id int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT" json:"id"` // 机构 ID
  315. OrgName string `gorm:"column:org_name" json:"org_name"`
  316. }
  317. func FindUnFollowOrgByIds(id int64) (orgViewModel []*OrgViewModel, err error) {
  318. 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
  319. return
  320. }
  321. func CreateFollowInfo(Follows []*models.OrgFollow) (err error) {
  322. if len(Follows) > 0 {
  323. utx := writeUserDb.Begin()
  324. if len(Follows) > 0 {
  325. thisSQL := "INSERT INTO sgj_org_follow (org_id,admin_user_id,ctime,mtime,status) VALUES "
  326. insertParams := make([]string, 0)
  327. insertData := make([]interface{}, 0)
  328. for _, info := range Follows {
  329. insertParams = append(insertParams, "(?,?,?,?,?)")
  330. insertData = append(insertData, info.OrgId)
  331. insertData = append(insertData, info.AdminUserId)
  332. insertData = append(insertData, info.Ctime)
  333. insertData = append(insertData, info.Mtime)
  334. insertData = append(insertData, info.Status)
  335. }
  336. thisSQL += strings.Join(insertParams, ", ")
  337. err = utx.Exec(thisSQL, insertData...).Error
  338. if err != nil {
  339. utx.Rollback()
  340. return
  341. }
  342. }
  343. utx.Commit()
  344. }
  345. return
  346. }
  347. func UpdateFollow(follow *models.OrgFollow) (err error) {
  348. err = readUserDb.Model(&models.OrgFollow{}).Save(follow).Error
  349. return
  350. }
  351. func FindFollowRecordByID(follow *models.OrgFollow) (info models.OrgFollow, err error) {
  352. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND admin_user_id = ?", follow.OrgId, follow.AdminUserId).First(&info).Error
  353. return
  354. }
  355. func FindFollowInfoById(org_id int64, admin_user_id int64) (info models.OrgFollow, err error) {
  356. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND status = 1 AND admin_user_id = ?", org_id, admin_user_id).First(&info).Error
  357. return
  358. }
  359. func GetAllWaitFollowOrgList(keyword string, page int64, limit int64, id int64) (orgViewModel []*OrgViewModel, err error, total int64) {
  360. type Total struct {
  361. Count int64
  362. }
  363. var totals Total
  364. offset := (page - 1) * limit
  365. db := readUserDb
  366. if len(keyword) > 0 {
  367. likeKey := "%" + keyword + "%"
  368. 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
  369. 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)
  370. } else {
  371. 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)
  372. err = db.Offset(offset).Limit(limit).Scan(&orgViewModel).Error
  373. 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)
  374. }
  375. return orgViewModel, err, totals.Count
  376. }
  377. func FindAllFollowOrg(keyword string, page int64, limit int64, admin_id int64) (follow []*FollowViewModel, err error, total int64) {
  378. type Total struct {
  379. Count int64
  380. }
  381. var totals Total
  382. offset := (page - 1) * limit
  383. if len(keyword) > 0 {
  384. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  385. var rows *sql.Rows
  386. var errs error
  387. likeKey := "%" + keyword + "%"
  388. 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()
  389. 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)
  390. defer rows.Close()
  391. if errs != nil {
  392. return nil, errs, totals.Count
  393. }
  394. for rows.Next() {
  395. var vm FollowViewModel
  396. readDb.ScanRows(rows, &vm)
  397. vms = append(vms, &vm)
  398. }
  399. return vms, nil, totals.Count
  400. } else {
  401. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  402. var rows *sql.Rows
  403. var errs error
  404. 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()
  405. 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)
  406. defer rows.Close()
  407. if errs != nil {
  408. return nil, errs, totals.Count
  409. }
  410. for rows.Next() {
  411. var vm FollowViewModel
  412. readDb.ScanRows(rows, &vm)
  413. vms = append(vms, &vm)
  414. }
  415. return vms, nil, totals.Count
  416. }
  417. }
  418. // 获取累积客户数
  419. func GetCustomCount() (int64, error) {
  420. var count int64
  421. err := readUserDb.Model(&models.KyyChargeCustom{}).Where("status = 1").Count(&count).Error
  422. if err != nil {
  423. return 0, err
  424. }
  425. return count, nil
  426. }
  427. // 本年新增收费客户
  428. func GetCurYearCustomCount() (int64, error) {
  429. var count int64
  430. err := readUserDb.Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE())").Count(&count).Error
  431. if err != nil {
  432. return 0, err
  433. }
  434. return count, nil
  435. }
  436. // 统计本年新增标准版客户
  437. func GetCurYearStandCustomCount() (int64, error) {
  438. var count int64
  439. err := readUserDb.Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE()) and custom_type = 1").Count(&count).Error
  440. if err != nil {
  441. return 0, err
  442. }
  443. return count, nil
  444. }
  445. // 统计本年新增尊享版客户
  446. func GetCurYearDeluxeCustomCount() (int64, error) {
  447. var count int64
  448. err := readUserDb.Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE()) and custom_type = 2").Count(&count).Error
  449. if err != nil {
  450. return 0, err
  451. }
  452. return count, nil
  453. }
  454. // 统计本年新增专业版客户
  455. func GetCurYearProfessionalCustomCount() (int64, error) {
  456. var count int64
  457. err := readUserDb.Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE()) and custom_type = 3").Count(&count).Error
  458. if err != nil {
  459. return 0, err
  460. }
  461. return count, nil
  462. }
  463. // 统计本年新增其他版客户
  464. func GetCurYearOtherCustomCount() (int64, error) {
  465. var count int64
  466. err := readUserDb.Model(&models.KyyChargeCustom{}).Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE()) and custom_type = 4").Count(&count).Error
  467. if err != nil {
  468. return 0, err
  469. }
  470. return count, nil
  471. }
  472. func GetCurYearContractPriceCount() (float64, error) {
  473. var count float64
  474. err := readUserDb.Model(&models.KyyChargeContract{}).Select("sum(sumamt) as count").Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE())").Find(&count).Error
  475. if err != nil {
  476. return 0, err
  477. }
  478. return count, nil
  479. }
  480. func GetCurYearSoftwarePriceCount() (float64, error) {
  481. var count float64
  482. err := readUserDb.Model(&models.KyyChargeContract{}).Select("sum(software_price) as count").Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE())").Find(&count).Error
  483. if err != nil {
  484. return 0, err
  485. }
  486. return count, nil
  487. }
  488. func GetCurYearHardwarePriceCount() (float64, error) {
  489. var count float64
  490. err := readUserDb.Model(&models.KyyChargeContract{}).Select("sum(hardware_price) as count").Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE())").Find(&count).Error
  491. if err != nil {
  492. return 0, err
  493. }
  494. return count, nil
  495. }
  496. func GetCurYearPaymentCollectionSumamtPriceCount() (float64, error) {
  497. var count float64
  498. err := readUserDb.Model(&models.KyyChargeContract{}).Select("sum(payment_collection_sumamt) as count").Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE())").Find(&count).Error
  499. if err != nil {
  500. return 0, err
  501. }
  502. return count, nil
  503. }
  504. func GetCurYearPaymentCollectionSoftwarePriceCount() (float64, error) {
  505. var count float64
  506. err := readUserDb.Model(&models.KyyChargeContract{}).Select("sum(software_sumamt) as count").Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE())").Find(&count).Error
  507. if err != nil {
  508. return 0, err
  509. }
  510. return count, nil
  511. }
  512. func GetCurYearPaymentCollectionHardwarePriceCount() (float64, error) {
  513. var count float64
  514. err := readUserDb.Model(&models.KyyChargeContract{}).Select("sum(hardware_sumamt) as count").Where("status = 1 and YEAR(FROM_UNIXTIME(ctime)) = YEAR(CURDATE())").Find(&count).Error
  515. if err != nil {
  516. return 0, err
  517. }
  518. return count, nil
  519. }