admin_service.go 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 := readUserDb.Where("account = ? AND pwd = ? 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 = readUserDb.Model(&admin_models.AdminAccount{}).Where("is_super_admin <> 1 AND (status = 1 OR status = 2)").Find(&admin).Error
  258. return
  259. }
  260. func CreateAdmin(admin *admin_models.AdminAccount) (err error) {
  261. err = readUserDb.Model(&admin_models.AdminAccount{}).Create(&admin).Error
  262. return
  263. }
  264. func FindUserInfoByAccount(account string) (admin admin_models.AdminAccount, err error) {
  265. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("account = ? AND status = 1", account).First(&admin).Error
  266. return
  267. }
  268. func FindAdminById(id int64) (admin admin_models.AdminAccount, err error) {
  269. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).First(&admin).Error
  270. return
  271. }
  272. func UpdateAdmin(admin *admin_models.AdminAccount) (err error) {
  273. err = readUserDb.Model(&admin_models.AdminAccount{}).Save(&admin).Error
  274. return
  275. }
  276. func DeleteAdmin(id int64) (err error) {
  277. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  278. err = readUserDb.Model(&models.OrgFollow{}).Where("admin_user_id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  279. return
  280. }
  281. func FindAllOrg() (org []*models.Org, err error) {
  282. err = readUserDb.Select("id, org_name").Where("status = 1").Find(&org).Error
  283. return
  284. }
  285. type FollowViewModel struct {
  286. models.OrgFollow
  287. OrgName string `gorm:"column:org_name" json:"org_name"`
  288. }
  289. func FindFollowOrg(admin_id int64) ([]*FollowViewModel, error) {
  290. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  291. 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()
  292. defer rows.Close()
  293. if err != nil {
  294. return nil, err
  295. }
  296. for rows.Next() {
  297. var vm FollowViewModel
  298. readDb.ScanRows(rows, &vm)
  299. vms = append(vms, &vm)
  300. }
  301. fmt.Println(vms)
  302. return vms, nil
  303. }
  304. type OrgViewModel struct {
  305. Id int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT" json:"id"` // 机构 ID
  306. OrgName string `gorm:"column:org_name" json:"org_name"`
  307. }
  308. func FindUnFollowOrgByIds(id int64) (orgViewModel []*OrgViewModel, err error) {
  309. 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
  310. return
  311. }
  312. func CreateFollowInfo(Follows []*models.OrgFollow) (err error) {
  313. if len(Follows) > 0 {
  314. utx := writeUserDb.Begin()
  315. if len(Follows) > 0 {
  316. thisSQL := "INSERT INTO sgj_org_follow (org_id,admin_user_id,ctime,mtime,status) VALUES "
  317. insertParams := make([]string, 0)
  318. insertData := make([]interface{}, 0)
  319. for _, info := range Follows {
  320. insertParams = append(insertParams, "(?,?,?,?,?)")
  321. insertData = append(insertData, info.OrgId)
  322. insertData = append(insertData, info.AdminUserId)
  323. insertData = append(insertData, info.Ctime)
  324. insertData = append(insertData, info.Mtime)
  325. insertData = append(insertData, info.Status)
  326. }
  327. thisSQL += strings.Join(insertParams, ", ")
  328. err = utx.Exec(thisSQL, insertData...).Error
  329. if err != nil {
  330. utx.Rollback()
  331. return
  332. }
  333. }
  334. utx.Commit()
  335. }
  336. return
  337. }
  338. func UpdateFollow(follow *models.OrgFollow) (err error) {
  339. err = readUserDb.Model(&models.OrgFollow{}).Save(follow).Error
  340. return
  341. }
  342. func FindFollowRecordByID(follow *models.OrgFollow) (info models.OrgFollow, err error) {
  343. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND admin_user_id = ?", follow.OrgId, follow.AdminUserId).First(&info).Error
  344. return
  345. }
  346. func FindFollowInfoById(org_id int64, admin_user_id int64) (info models.OrgFollow, err error) {
  347. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND status = 1 AND admin_user_id = ?", org_id, admin_user_id).First(&info).Error
  348. return
  349. }
  350. func GetAllWaitFollowOrgList(keyword string, page int64, limit int64, id int64) (orgViewModel []*OrgViewModel, err error, total int64) {
  351. type Total struct {
  352. Count int64
  353. }
  354. var totals Total
  355. offset := (page - 1) * limit
  356. db := readUserDb
  357. if len(keyword) > 0 {
  358. likeKey := "%" + keyword + "%"
  359. 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
  360. 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)
  361. } else {
  362. 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)
  363. err = db.Offset(offset).Limit(limit).Scan(&orgViewModel).Error
  364. 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)
  365. }
  366. return orgViewModel, err, totals.Count
  367. }
  368. func FindAllFollowOrg(keyword string, page int64, limit int64, admin_id int64) (follow []*FollowViewModel, err error, total int64) {
  369. type Total struct {
  370. Count int64
  371. }
  372. var totals Total
  373. offset := (page - 1) * limit
  374. if len(keyword) > 0 {
  375. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  376. var rows *sql.Rows
  377. var errs error
  378. likeKey := "%" + keyword + "%"
  379. 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()
  380. 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)
  381. defer rows.Close()
  382. if errs != nil {
  383. return nil, errs, totals.Count
  384. }
  385. for rows.Next() {
  386. var vm FollowViewModel
  387. readDb.ScanRows(rows, &vm)
  388. vms = append(vms, &vm)
  389. }
  390. return vms, nil, totals.Count
  391. } else {
  392. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  393. var rows *sql.Rows
  394. var errs error
  395. 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()
  396. 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)
  397. defer rows.Close()
  398. if errs != nil {
  399. return nil, errs, totals.Count
  400. }
  401. for rows.Next() {
  402. var vm FollowViewModel
  403. readDb.ScanRows(rows, &vm)
  404. vms = append(vms, &vm)
  405. }
  406. return vms, nil, totals.Count
  407. }
  408. }