admin_service.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // 统计后台的 service
  2. package service
  3. import (
  4. "XT_New/models"
  5. "XT_New/models/admin_models"
  6. "time"
  7. "github.com/jinzhu/gorm"
  8. )
  9. func GetAdminAccount(account string, password string) (*admin_models.AdminAccount, error) {
  10. var model admin_models.AdminAccount
  11. err := readUserDb.Where("account = ? AND pwd = ? AND status = 1", account, password).First(&model).Error
  12. if err != nil {
  13. if err == gorm.ErrRecordNotFound {
  14. return nil, nil
  15. }
  16. return nil, err
  17. }
  18. return &model, nil
  19. }
  20. // 获取总机构数
  21. func GetTotalOrgCount() (int, error) {
  22. var count int
  23. err := readUserDb.Model(&models.Org{}).Where("status <> 0").Count(&count).Error
  24. if err != nil {
  25. return 0, err
  26. }
  27. return count, nil
  28. }
  29. // 获取一段时间内注册的机构数量
  30. func GetRegistedOrgCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  31. var count int
  32. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND ctime >= ? AND ctime <= ?", from.Unix(), to.Unix()).Count(&count).Error
  33. if err != nil {
  34. return 0, err
  35. }
  36. return count, nil
  37. }
  38. // 获取一段时间内注册的机构
  39. func GetRegistedOrgsFromDayToDay(from time.Time, to time.Time) ([]*models.Org, error) {
  40. var orgs []*models.Org
  41. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND ctime >= ? AND ctime <= ?", from.Unix(), to.Unix()).Find(&orgs).Error
  42. if err != nil {
  43. return nil, err
  44. }
  45. return orgs, nil
  46. }
  47. // 获取一段时间内的活跃机构数
  48. func GetActiveOrgCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  49. var count int
  50. rows, err := readUserDb.Raw("SELECT COUNT(DISTINCT org_id) AS count FROM sgj_user_admin_login_log WHERE ctime >= ? AND ctime <= ?", from.Unix(), to.Unix()).Rows()
  51. if err != nil {
  52. return 0, err
  53. }
  54. if rows.Next() {
  55. rows.Scan(&count)
  56. }
  57. return count, nil
  58. }
  59. type ActiveOrgListVM struct {
  60. models.Org
  61. ActiveTime int64 `gorm:"column:active_time" json:"active_time"`
  62. }
  63. // 获取一段时间内的活跃机构
  64. func GetActiveOrgsFromDayToDay(from time.Time, to time.Time) ([]*ActiveOrgListVM, error) {
  65. var orgs []*ActiveOrgListVM
  66. err := readUserDb.Raw("select * from (select log.org_id, org.*, log.ctime as active_time from sgj_user_admin_login_log as log left join sgj_user_org as org on log.org_id = org.id where log.ctime >= ? AND log.ctime <= ? AND org.status <> 0 order by log.ctime desc) as t group by t.org_id;", from.Unix(), to.Unix()).Scan(&orgs).Error
  67. if err != nil {
  68. return nil, err
  69. }
  70. return orgs, nil
  71. }
  72. // 获取一段时间内的活跃账户数
  73. func GetActiveAdminUserCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  74. var count int
  75. 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", from.Unix(), to.Unix()).Rows()
  76. if err != nil {
  77. return 0, err
  78. }
  79. if rows.Next() {
  80. rows.Scan(&count)
  81. }
  82. return count, nil
  83. }
  84. type ActiveAdminUserListVM struct {
  85. models.App_Role
  86. OrgName string `gorm:"column:org_name" json:"org_name"`
  87. ActiveTime int64 `gorm:"column:active_time" json:"active_time"`
  88. }
  89. // 获取一段时间内的活跃账户
  90. func GetActiveAdminUsersFromDayToDay(from time.Time, to time.Time) ([]*ActiveAdminUserListVM, error) {
  91. var users []*ActiveAdminUserListVM
  92. err := readUserDb.Raw("select * from (select log.admin_user_id as uid, admin.*, org.org_name, log.ctime as active_time from sgj_user_admin_login_log as log left join sgj_user_admin_role as admin on log.admin_user_id = admin.admin_user_id left join sgj_user_org as org on log.org_id = org.id where log.ctime >= ? AND log.ctime <= ? AND log.operate_type = 1 AND admin.status > 0 order by log.ctime desc) as t group by t.uid;", from.Unix(), to.Unix()).Scan(&users).Error
  93. if err != nil {
  94. return nil, err
  95. }
  96. return users, nil
  97. }
  98. // 获取即将到期机构数
  99. func GetWillExpireOrgCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  100. var count int
  101. rows, err := readUserDb.Raw("SELECT COUNT(DISTINCT org_id) AS count FROM sgj_serve_subscibe WHERE period_end >= ? AND period_end <= ? AND state <> 9 GROUP BY org_id;", from.Unix(), to.Unix()).Rows()
  102. if err != nil {
  103. return 0, err
  104. }
  105. if rows.Next() {
  106. rows.Scan(&count)
  107. }
  108. return count, nil
  109. }
  110. // 获取已到期机构数
  111. func GetDidExpiredOrgCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  112. var count int
  113. rows, err := readUserDb.Raw("SELECT COUNT(DISTINCT org_id) AS count FROM sgj_serve_subscibe WHERE period_end >= ? AND period_end <= ? AND state <> 9 GROUP BY org_id;", from.Unix(), to.Unix()).Rows()
  114. if err != nil {
  115. return 0, err
  116. }
  117. if rows.Next() {
  118. rows.Scan(&count)
  119. }
  120. return count, nil
  121. }
  122. type ExpireOrgListVM struct {
  123. OrgID int64 `gorm:"column:org_id" json:"org_id"`
  124. OrgName string `gorm:"column:org_name" json:"org_name"`
  125. PeriodEnd int64 `gorm:"column:period_end" json:"period_end"`
  126. Telephone string `gorm:"column:telephone" json:"telephone"`
  127. ExpireDays int `gorm:"-" json:"expire_days"`
  128. }
  129. // 获取即将到期机构
  130. func GetWillExpireOrgsFromDayToDay(from time.Time, to time.Time) ([]*ExpireOrgListVM, error) {
  131. var vms []*ExpireOrgListVM
  132. err := readUserDb.Raw("select s_o.org_id, s_o.period_end, o.org_name, o.telephone from (select ss.* from (select s.org_id as org_id, s.id as sid, s.period_end from sgj_serve_subscibe as s where s.period_end <= ? AND s.period_end >= ? AND s.state <> 9 order by s.id desc) as ss group by ss.org_id) as s_o left join sgj_user_org as o on o.id = s_o.org_id", to.Unix(), from.Unix()).Scan(&vms).Error
  133. if err != nil {
  134. return nil, err
  135. }
  136. for _, vm := range vms {
  137. vm.ExpireDays = time.Unix(vm.PeriodEnd, 0).Day() - from.Day()
  138. }
  139. return vms, nil
  140. }
  141. // 获取已到期机构
  142. func GetDidExpireOrgsFromDayToDay(from time.Time, to time.Time) ([]*ExpireOrgListVM, error) {
  143. var vms []*ExpireOrgListVM
  144. err := readUserDb.Raw("select s_o.org_id, s_o.period_end, o.org_name, o.telephone from (select ss.* from (select s.org_id as org_id, s.id as sid, s.period_end from sgj_serve_subscibe as s where s.period_end >= ? AND s.period_end <= ? AND s.state <> 9 order by s.id desc) as ss group by ss.org_id) as s_o left join sgj_user_org as o on o.id = s_o.org_id", from.Unix(), to.Unix()).Scan(&vms).Error
  145. if err != nil {
  146. return nil, err
  147. }
  148. for _, vm := range vms {
  149. vm.ExpireDays = to.Day() - time.Unix(vm.PeriodEnd, 0).Day()
  150. }
  151. return vms, nil
  152. }
  153. // 获取一段时间内的透析次数
  154. func GetDialysisTimesFromDayToDay(from time.Time, to time.Time) (int, error) {
  155. var count int
  156. err := readDb.Model(&models.DialysisOrder{}).Where("status > 0 AND dialysis_date >= ? AND dialysis_date <= ?", from.Unix(), to.Unix()).Count(&count).Error
  157. if err != nil {
  158. return 0, err
  159. }
  160. return count, nil
  161. }
  162. // 获取一段时间内的监控次数
  163. func GetMonitoringTimesFromDayToDay(from time.Time, to time.Time) (int, error) {
  164. var count int
  165. err := readDb.Model(&models.MonitoringRecord{}).Where("status > 0 AND monitoring_date >= ? AND monitoring_date <= ?", from.Unix(), to.Unix()).Count(&count).Error
  166. if err != nil {
  167. return 0, err
  168. }
  169. return count, nil
  170. }
  171. // 获取一段时间内新增病人数量
  172. func GetNewPatientCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  173. var count int
  174. err := readDb.Model(&models.Patients{}).Where("status > 0 AND created_time >= ? AND created_time <= ?", from.Unix(), to.Unix()).Count(&count).Error
  175. if err != nil {
  176. return 0, err
  177. }
  178. return count, nil
  179. }
  180. // 获取病人性别分布
  181. func GetPatientGenderDistribution() (int, int, int, error) {
  182. var maleCount, femaleCount, unknowGenderCount int
  183. rows, err := readDb.Raw("SELECT count(id), gender FROM xt_patients WHERE (status > 0) group by gender;").Rows()
  184. if err != nil {
  185. return 0, 0, 0, err
  186. }
  187. for rows.Next() {
  188. var gender, count int
  189. rows.Scan(&count, &gender)
  190. if gender == 1 {
  191. maleCount = count
  192. } else if gender == 2 {
  193. femaleCount = count
  194. } else {
  195. unknowGenderCount = count
  196. }
  197. }
  198. return maleCount, femaleCount, unknowGenderCount, nil
  199. }
  200. // 获取某种传染病的病人数,diseaseType = 0时为没有传染病
  201. // 1乙肝 2丙肝 3艾滋病 4肺结核 5梅毒
  202. func GetInfectiousDiseasePatientCount(diseaseType int) (int, error) {
  203. if diseaseType == 0 {
  204. var count int
  205. rows, err := readDb.Raw("select count(p.id) from xt_patients as p where id not in (select p_d.patient_id from xt_patients_infectious_diseases as p_d where status = 1 group by p_d.patient_id) and status = 1;").Rows()
  206. if err != nil {
  207. return 0, err
  208. }
  209. if rows.Next() {
  210. rows.Scan(&count)
  211. }
  212. return count, nil
  213. } else {
  214. var count int
  215. rows, err := readDb.Raw("select count(distinct patient_id) from xt_patients_infectious_diseases where status = 1 and disease_id = ?;", diseaseType).Rows()
  216. if err != nil {
  217. return 0, err
  218. }
  219. if rows.Next() {
  220. rows.Scan(&count)
  221. }
  222. return count, nil
  223. }
  224. }
  225. // 获取患者年龄分布
  226. func GetPatientAgeDistribution() ([]*PatientAgeCountStruct, error) {
  227. var counts []*PatientAgeCountStruct
  228. err := readDb.Raw(`SELECT nnd AS 'age',COUNT(*) AS 'count' FROM (
  229. SELECT
  230. CASE
  231. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d'))<10 THEN '10'
  232. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=10 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<20 THEN '20'
  233. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=20 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<30 THEN '30'
  234. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=30 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<40 THEN '40'
  235. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=40 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<50 THEN '50'
  236. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=50 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<60 THEN '60'
  237. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=60 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<70 THEN '70'
  238. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=70 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<80 THEN '80'
  239. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=80 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<90 THEN '90'
  240. WHEN (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )>=90 AND (YEAR(NOW())-DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%Y')-1) + (DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), INTERVAL birthday SECOND),'%m%d') <= DATE_FORMAT(NOW(), '%m%d') )<100 THEN '100'
  241. ELSE '-1'
  242. END
  243. AS nnd FROM xt_patients where status=1
  244. ) a GROUP BY nnd;`).Scan(&counts).Error
  245. if err != nil {
  246. return nil, err
  247. }
  248. return counts, nil
  249. }
  250. func GetAdminUserElectronicSignature(orgID int64, appID int64, userID int64) (*models.AdminUserElectronicSignature, error) {
  251. var es models.AdminUserElectronicSignature
  252. err := readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator=? and status=1", orgID, appID, userID).First(&es).Error
  253. if err == gorm.ErrRecordNotFound {
  254. return nil, nil
  255. }
  256. if err != nil {
  257. return nil, err
  258. }
  259. return &es, nil
  260. }
  261. func CreateAdminUserElectronicSignature(es *models.AdminUserElectronicSignature) error {
  262. err := writeUserDb.Create(es).Error
  263. return err
  264. }
  265. func SaveAdminUserElectronicSignature(es *models.AdminUserElectronicSignature) error {
  266. err := writeUserDb.Save(es).Error
  267. return err
  268. }
  269. func GetAdminUserSpecialPermission(orgID int64, appID int64, adminUserID int64, permissionType models.SpecialPermissionType) (*models.AdminUserSpecialPermission, error) {
  270. var record models.AdminUserSpecialPermission
  271. 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
  272. if err != nil {
  273. if err == gorm.ErrRecordNotFound {
  274. return nil, nil
  275. } else {
  276. return nil, err
  277. }
  278. }
  279. return &record, nil
  280. }
  281. func SaveAdminUserSpecialPermission(permission *models.AdminUserSpecialPermission) error {
  282. err := writeDb.Save(permission).Error
  283. return err
  284. }
  285. func GetAllValidAdminUsersWithSpecialPermission(orgID int64, appID int64, permissionType models.SpecialPermissionType) ([]*models.AdminUserSpecialPermission, error) {
  286. var records []*models.AdminUserSpecialPermission
  287. err := readDb.Model(&models.AdminUserSpecialPermission{}).Where("org_id = ? AND app_id = ? AND permission = ? AND status = 1", orgID, appID, permissionType).Find(&records).Error
  288. if err != nil {
  289. return nil, err
  290. }
  291. return records, nil
  292. }
  293. func GetAllSpecialPermissionAdminUsersWithoutStatus(orgID int64, appID int64, permissionType models.SpecialPermissionType) ([]*models.AdminUserSpecialPermission, error) {
  294. var records []*models.AdminUserSpecialPermission
  295. err := readDb.Model(&models.AdminUserSpecialPermission{}).Where("org_id = ? AND app_id = ? AND permission = ?", orgID, appID, permissionType).Find(&records).Error
  296. if err != nil {
  297. return nil, err
  298. }
  299. return records, nil
  300. }
  301. func CancelAllSpecialPermissionAdminUsers(orgID int64, appID int64, permissionType models.SpecialPermissionType) error {
  302. tx := writeDb.Begin()
  303. updateTime := time.Now().Unix()
  304. err := tx.Model(&models.AdminUserSpecialPermission{}).Where("org_id = ? AND app_id = ? AND status = 1 AND permission = ?", orgID, appID, permissionType).Updates(map[string]interface{}{"status": 0, "mtime": updateTime}).Error
  305. if err != nil {
  306. tx.Rollback()
  307. return err
  308. }
  309. tx.Commit()
  310. return nil
  311. }
  312. func BatchSaveSpecialPermissionAdminUsers(users []*models.AdminUserSpecialPermission) error {
  313. tx := writeDb.Begin()
  314. for _, user := range users {
  315. tx.Save(user)
  316. }
  317. return tx.Commit().Error
  318. }
  319. func ModifyAdminUserName(name string, id int64) error {
  320. tx := writeUserDb.Begin()
  321. updateTime := time.Now().Unix()
  322. err := tx.Model(&models.AdminUser{}).Where("id = ?", id).Updates(map[string]interface{}{"name": name, "mtime": updateTime}).Error
  323. if err != nil {
  324. tx.Rollback()
  325. return err
  326. }
  327. tx.Commit()
  328. return nil
  329. }