admin_service.go 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // 统计后台的 service
  2. package service
  3. import (
  4. "XT_Admin_Api/models"
  5. "XT_Admin_Api/models/admin_models"
  6. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/jinzhu/gorm"
  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").Count(&count).Error
  26. if err != nil {
  27. return 0, err
  28. }
  29. return count, nil
  30. }
  31. // 获取一段时间内注册的机构数量
  32. func GetRegistedOrgCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  33. var count int
  34. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND ctime >= ? AND ctime <= ?", from.Unix(), to.Unix()).Count(&count).Error
  35. if err != nil {
  36. return 0, err
  37. }
  38. return count, nil
  39. }
  40. // 获取一段时间内注册的机构
  41. func GetRegistedOrgsFromDayToDay(from time.Time, to time.Time) ([]*models.Org, error) {
  42. var orgs []*models.Org
  43. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND ctime >= ? AND ctime <= ?", from.Unix(), to.Unix()).Find(&orgs).Error
  44. if err != nil {
  45. return nil, err
  46. }
  47. return orgs, nil
  48. }
  49. // 获取一段时间内的活跃机构数
  50. func GetActiveOrgCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  51. var count int
  52. 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()
  53. if err != nil {
  54. return 0, err
  55. }
  56. if rows.Next() {
  57. rows.Scan(&count)
  58. }
  59. return count, nil
  60. }
  61. type ActiveOrgListVM struct {
  62. models.Org
  63. ActiveTime int64 `gorm:"column:active_time" json:"active_time"`
  64. }
  65. // 获取一段时间内的活跃机构
  66. func GetActiveOrgsFromDayToDay(from time.Time, to time.Time) ([]*ActiveOrgListVM, error) {
  67. var orgs []*ActiveOrgListVM
  68. 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
  69. if err != nil {
  70. return nil, err
  71. }
  72. return orgs, nil
  73. }
  74. // 获取一段时间内的活跃账户数
  75. func GetActiveAdminUserCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  76. var count int
  77. 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()
  78. if err != nil {
  79. return 0, err
  80. }
  81. if rows.Next() {
  82. rows.Scan(&count)
  83. }
  84. return count, nil
  85. }
  86. type ActiveAdminUserListVM struct {
  87. models.App_Role
  88. OrgName string `gorm:"column:org_name" json:"org_name"`
  89. ActiveTime int64 `gorm:"column:active_time" json:"active_time"`
  90. }
  91. // 获取一段时间内的活跃账户
  92. func GetActiveAdminUsersFromDayToDay(from time.Time, to time.Time) ([]*ActiveAdminUserListVM, error) {
  93. var users []*ActiveAdminUserListVM
  94. 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
  95. if err != nil {
  96. return nil, err
  97. }
  98. return users, nil
  99. }
  100. // 获取即将到期机构数
  101. func GetWillExpireOrgCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  102. var count int
  103. 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()
  104. if err != nil {
  105. return 0, err
  106. }
  107. if rows.Next() {
  108. rows.Scan(&count)
  109. }
  110. return count, nil
  111. }
  112. // 获取已到期机构数
  113. func GetDidExpiredOrgCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  114. var count int
  115. 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()
  116. if err != nil {
  117. return 0, err
  118. }
  119. if rows.Next() {
  120. rows.Scan(&count)
  121. }
  122. return count, nil
  123. }
  124. type ExpireOrgListVM struct {
  125. OrgID int64 `gorm:"column:org_id" json:"org_id"`
  126. OrgName string `gorm:"column:org_name" json:"org_name"`
  127. PeriodEnd int64 `gorm:"column:period_end" json:"period_end"`
  128. Telephone string `gorm:"column:telephone" json:"telephone"`
  129. ExpireDays int `gorm:"-" json:"expire_days"`
  130. }
  131. // 获取即将到期机构
  132. func GetWillExpireOrgsFromDayToDay(from time.Time, to time.Time) ([]*ExpireOrgListVM, error) {
  133. var vms []*ExpireOrgListVM
  134. 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
  135. if err != nil {
  136. return nil, err
  137. }
  138. for _, vm := range vms {
  139. vm.ExpireDays = time.Unix(vm.PeriodEnd, 0).Day() - from.Day()
  140. }
  141. return vms, nil
  142. }
  143. // 获取已到期机构
  144. func GetDidExpireOrgsFromDayToDay(from time.Time, to time.Time) ([]*ExpireOrgListVM, error) {
  145. var vms []*ExpireOrgListVM
  146. 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
  147. if err != nil {
  148. return nil, err
  149. }
  150. for _, vm := range vms {
  151. vm.ExpireDays = to.Day() - time.Unix(vm.PeriodEnd, 0).Day()
  152. }
  153. return vms, nil
  154. }
  155. // 获取一段时间内的透析次数
  156. func GetDialysisTimesFromDayToDay(from time.Time, to time.Time) (int, error) {
  157. var count int
  158. err := readDb.Model(&models.DialysisOrder{}).Where("status > 0 AND dialysis_date >= ? AND dialysis_date <= ?", from.Unix(), to.Unix()).Count(&count).Error
  159. if err != nil {
  160. return 0, err
  161. }
  162. return count, nil
  163. }
  164. // 获取一段时间内的监控次数
  165. func GetMonitoringTimesFromDayToDay(from time.Time, to time.Time) (int, error) {
  166. var count int
  167. err := readDb.Model(&models.MonitoringRecord{}).Where("status > 0 AND monitoring_date >= ? AND monitoring_date <= ?", from.Unix(), to.Unix()).Count(&count).Error
  168. if err != nil {
  169. return 0, err
  170. }
  171. return count, nil
  172. }
  173. // 获取一段时间内新增病人数量
  174. func GetNewPatientCountFromDayToDay(from time.Time, to time.Time) (int, error) {
  175. var count int
  176. err := readDb.Model(&models.Patients{}).Where("status > 0 AND created_time >= ? AND created_time <= ?", from.Unix(), to.Unix()).Count(&count).Error
  177. if err != nil {
  178. return 0, err
  179. }
  180. return count, nil
  181. }
  182. // 获取病人性别分布
  183. func GetPatientGenderDistribution() (int, int, int, error) {
  184. var maleCount, femaleCount, unknowGenderCount int
  185. rows, err := readDb.Raw("SELECT count(id), gender FROM xt_patients WHERE (status > 0) group by gender;").Rows()
  186. if err != nil {
  187. return 0, 0, 0, err
  188. }
  189. for rows.Next() {
  190. var gender, count int
  191. rows.Scan(&count, &gender)
  192. if gender == 1 {
  193. maleCount = count
  194. } else if gender == 2 {
  195. femaleCount = count
  196. } else {
  197. unknowGenderCount = count
  198. }
  199. }
  200. return maleCount, femaleCount, unknowGenderCount, nil
  201. }
  202. // 获取某种传染病的病人数,diseaseType = 0时为没有传染病
  203. // 1乙肝 2丙肝 3艾滋病 4肺结核 5梅毒
  204. func GetInfectiousDiseasePatientCount(diseaseType int) (int, error) {
  205. if diseaseType == 0 {
  206. var count int
  207. 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()
  208. if err != nil {
  209. return 0, err
  210. }
  211. if rows.Next() {
  212. rows.Scan(&count)
  213. }
  214. return count, nil
  215. } else {
  216. var count int
  217. rows, err := readDb.Raw("select count(distinct patient_id) from xt_patients_infectious_diseases where status = 1 and disease_id = ?;", diseaseType).Rows()
  218. if err != nil {
  219. return 0, err
  220. }
  221. if rows.Next() {
  222. rows.Scan(&count)
  223. }
  224. return count, nil
  225. }
  226. }
  227. // 获取患者年龄分布
  228. func GetPatientAgeDistribution() ([]*PatientAgeCountStruct, error) {
  229. var counts []*PatientAgeCountStruct
  230. err := readDb.Raw(`SELECT nnd AS 'age',COUNT(*) AS 'count' FROM (
  231. SELECT
  232. CASE
  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'))<10 THEN '10'
  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') )>=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'
  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') )>=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'
  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') )>=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'
  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') )>=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'
  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') )>=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'
  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') )>=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'
  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') )>=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'
  241. 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'
  242. 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'
  243. ELSE '-1'
  244. END
  245. AS nnd FROM xt_patients where status=1
  246. ) a GROUP BY nnd;`).Scan(&counts).Error
  247. if err != nil {
  248. return nil, err
  249. }
  250. return counts, nil
  251. }
  252. func GetAdminUserElectronicSignature(orgID int64, appID int64, userID int64) (*models.AdminUserElectronicSignature, error) {
  253. var es models.AdminUserElectronicSignature
  254. err := readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator=? and status=1", orgID, appID, userID).First(&es).Error
  255. if err == gorm.ErrRecordNotFound {
  256. return nil, nil
  257. }
  258. if err != nil {
  259. return nil, err
  260. }
  261. return &es, nil
  262. }
  263. func CreateAdminUserElectronicSignature(es *models.AdminUserElectronicSignature) error {
  264. err := writeUserDb.Create(es).Error
  265. return err
  266. }
  267. func SaveAdminUserElectronicSignature(es *models.AdminUserElectronicSignature) error {
  268. err := writeUserDb.Save(es).Error
  269. return err
  270. }
  271. func GetAdminUserSpecialPermission(orgID int64, appID int64, adminUserID int64, permissionType models.SpecialPermissionType) (*models.AdminUserSpecialPermission, error) {
  272. var record models.AdminUserSpecialPermission
  273. 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
  274. if err != nil {
  275. if err == gorm.ErrRecordNotFound {
  276. return nil, nil
  277. } else {
  278. return nil, err
  279. }
  280. }
  281. return &record, nil
  282. }
  283. func SaveAdminUserSpecialPermission(permission *models.AdminUserSpecialPermission) error {
  284. err := writeDb.Save(permission).Error
  285. return err
  286. }
  287. func GetAllValidAdminUsersWithSpecialPermission(orgID int64, appID int64, permissionType models.SpecialPermissionType) ([]*models.AdminUserSpecialPermission, error) {
  288. var records []*models.AdminUserSpecialPermission
  289. err := readDb.Model(&models.AdminUserSpecialPermission{}).Where("org_id = ? AND app_id = ? AND permission = ? AND status = 1", orgID, appID, permissionType).Find(&records).Error
  290. if err != nil {
  291. return nil, err
  292. }
  293. return records, nil
  294. }
  295. func GetAllSpecialPermissionAdminUsersWithoutStatus(orgID int64, appID int64, permissionType models.SpecialPermissionType) ([]*models.AdminUserSpecialPermission, error) {
  296. var records []*models.AdminUserSpecialPermission
  297. err := readDb.Model(&models.AdminUserSpecialPermission{}).Where("org_id = ? AND app_id = ? AND permission = ?", orgID, appID, permissionType).Find(&records).Error
  298. if err != nil {
  299. return nil, err
  300. }
  301. return records, nil
  302. }
  303. func CancelAllSpecialPermissionAdminUsers(orgID int64, appID int64, permissionType models.SpecialPermissionType) error {
  304. tx := writeDb.Begin()
  305. updateTime := time.Now().Unix()
  306. 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
  307. if err != nil {
  308. tx.Rollback()
  309. return err
  310. }
  311. tx.Commit()
  312. return nil
  313. }
  314. func BatchSaveSpecialPermissionAdminUsers(users []*models.AdminUserSpecialPermission) error {
  315. tx := writeDb.Begin()
  316. for _, user := range users {
  317. tx.Save(user)
  318. }
  319. return tx.Commit().Error
  320. }
  321. // 获取一段时间内新增病人数量
  322. func GetNewPatientCount(from int64, to int64) (int, error) {
  323. var count int
  324. 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()
  325. if err != nil {
  326. return 0, err
  327. }
  328. if rows.Next() {
  329. rows.Scan(&count)
  330. }
  331. return count, nil
  332. }
  333. // 获取所有机构病人数量
  334. func GetPatientTotalCount() (int64, error) {
  335. var count int64
  336. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients").Rows()
  337. if err != nil {
  338. return 0, err
  339. }
  340. if rows.Next() {
  341. rows.Scan(&count)
  342. }
  343. return count, nil
  344. }
  345. // 获取一段时间内的活跃账户数
  346. func GetActiveAdminUserCount(from int64, to int64) (int64, error) {
  347. var count int64
  348. 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()
  349. if err != nil {
  350. return 0, err
  351. }
  352. if rows.Next() {
  353. rows.Scan(&count)
  354. }
  355. return count, nil
  356. }
  357. // 获取所有机构账户数量
  358. func GetAdminUserTotalCount() (int64, error) {
  359. var count int64
  360. 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()
  361. if err != nil {
  362. return 0, err
  363. }
  364. if rows.Next() {
  365. rows.Scan(&count)
  366. }
  367. return count, nil
  368. }
  369. // 获取一段时间内注册的机构数量
  370. func GetRegistedOrgCount(from int64, to int64) (int64, error) {
  371. var count int64
  372. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND ctime >= ? AND ctime <= ?", from, to).Count(&count).Error
  373. if err != nil {
  374. return 0, err
  375. }
  376. return count, nil
  377. }
  378. // 获取一段时间内的活跃机构数
  379. func GetActiveOrgCount(from int64, to int64) (int64, error) {
  380. var count int64
  381. rows, err := readUserDb.Raw("SELECT COUNT(DISTINCT org_id) AS count FROM sgj_user_admin_login_log JOIN sgj_xt.xt_dialysis_order ON sgj_user_admin_login_log.org_id = sgj_xt.xt_dialysis_order.user_org_id AND DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m-%d') = DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)),'%Y-%m-%d') where sgj_user_admin_login_log.app_type = 3 AND sgj_user_admin_login_log.operate_type = 1 AND sgj_user_admin_login_log.ctime >= ? AND sgj_user_admin_login_log.ctime <= ? ", from, to).Rows()
  382. if err != nil {
  383. return 0, err
  384. }
  385. if rows.Next() {
  386. rows.Scan(&count)
  387. }
  388. return count, nil
  389. }
  390. // 获取所有活跃机构数
  391. func GetActiveOrgTotalCount() (int64, error) {
  392. var count int64
  393. rows, err := readUserDb.Raw("SELECT COUNT(DISTINCT org_id) AS count FROM sgj_user_admin_login_log JOIN sgj_xt.xt_dialysis_order ON sgj_user_admin_login_log.org_id = sgj_xt.xt_dialysis_order.user_org_id AND DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m-%d') = DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)),'%Y-%m-%d') where sgj_user_admin_login_log.app_type = 3 AND sgj_user_admin_login_log.operate_type = 1").Rows()
  394. if err != nil {
  395. return 0, err
  396. }
  397. if rows.Next() {
  398. rows.Scan(&count)
  399. }
  400. return count, nil
  401. }
  402. //获取近七天每天的注册机构总量
  403. func GetWeekRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  404. 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)) GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  405. return
  406. }
  407. //func GetWeekActiveOrgCount() (weekStatistics []*admin_models.WeekStatistics, err error) {
  408. // 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
  409. // return
  410. //}
  411. //获取近七天每天的机构活跃总量
  412. func GetWeekActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  413. 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 join sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = sgj_user_admin_login_log.org_id AND DATE_FORMAT(date(from_unixtime(sgj_user_admin_login_log.ctime)) ,'%Y-%m-%d') = DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)) ,'%Y-%m-%d') 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));")
  414. err = db.Scan(&weekStatistics).Error
  415. return
  416. }
  417. //获取近七天每天的机构活跃账号总量
  418. func GetWeekActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  419. 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
  420. return
  421. }
  422. //获取近七天每天的机构新增病人总量
  423. func GetWeekNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  424. 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
  425. return
  426. }
  427. //获取近七天每天的注册机构总量
  428. func GetWeekRegistedOrgTotalCount() (int64, error) {
  429. var result admin_models.Count
  430. var err error
  431. err = readUserDb.Raw("select count(id) as count from sgj_user_org where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(sgj_user_org.ctime))").Scan(&result).Error
  432. return result.Count, err
  433. }
  434. //获取近七天每天的机构活跃账号总量
  435. func GetWeekActiveAdminUserTotalCount() (int64, error) {
  436. var result admin_models.Count
  437. var err error
  438. err = readUserDb.Raw("select count(DISTINCT(admin_user_id)) as count 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 ").Scan(&result).Error
  439. return result.Count, err
  440. }
  441. //获取近七天新增病人总量
  442. func GetWeekPatientTotalCount() (int64, error) {
  443. var result admin_models.Count
  444. var err error
  445. err = readDb.Raw("select count(DISTINCT(id_card_no)) as count from xt_patients where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time))").Scan(&result).Error
  446. return result.Count, err
  447. }
  448. //获取近30天每天的注册机构总量
  449. func GetMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  450. 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)) GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  451. return
  452. }
  453. //获取近30天每天的机构活跃总量
  454. func GetMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  455. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times,count(DISTINCT(org_id)) as count From (select ctime,org_id FROM sgj_user_admin_login_log where app_type = 3 AND org_id > 0 ) T join sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = T.org_id AND DATE_FORMAT(date(from_unixtime(T.ctime)) ,'%Y-%m') =DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)) ,'%Y-%m') where DATE_FORMAT(date(from_unixtime(T.ctime)),'%Y-%m')>DATE_FORMAT(date_sub(curdate(), interval 1 month),'%Y-%m') group by times").Scan(&weekStatistics).Error
  456. return
  457. }
  458. //获取近30天每天的机构活跃账号总量
  459. func GetMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  460. 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
  461. return
  462. }
  463. //获取近30天每天的机构新增病人总量
  464. func GetMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  465. 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
  466. return
  467. }
  468. //获取近30天每天的注册机构总量
  469. func GetMonthRegistedOrgTotalCount() (int64, error) {
  470. var result admin_models.Count
  471. var err error
  472. err = readUserDb.Raw("select count(id) as count from sgj_user_org where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(sgj_user_org.ctime))").Scan(&result).Error
  473. return result.Count, err
  474. }
  475. //获取近30天每天的机构活跃账号总量
  476. func GetMonthActiveAdminUserTotalCount() (int64, error) {
  477. var result admin_models.Count
  478. var err error
  479. err = readUserDb.Raw("select count(DISTINCT(admin_user_id)) as count from sgj_user_admin_login_log where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND operate_type = 1 ").Scan(&result).Error
  480. return result.Count, err
  481. }
  482. //获取近30天每天的机构新增病人总量
  483. func GetMonthNewPatientTotalCount() (int64, error) {
  484. var result admin_models.Count
  485. var err error
  486. err = readDb.Raw("select count(DISTINCT(id_card_no)) as count from xt_patients where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) ").Scan(&result).Error
  487. return result.Count, err
  488. }
  489. //获取近3个月每个月的注册机构总量
  490. func GetThreeMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  491. 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') group by times").Scan(&weekStatistics).Error
  492. return
  493. }
  494. //获取近3个月每个月的机构活跃账号总量
  495. func GetThreeMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  496. 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
  497. return
  498. }
  499. //获取近3个月每个月的机构活跃总量
  500. func GetThreeMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  501. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times,count(DISTINCT(org_id)) as count From (select ctime,org_id FROM sgj_user_admin_login_log where app_type = 3 AND org_id > 0 AND operate_type = 1 ) T join sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = T.org_id AND DATE_FORMAT(date(from_unixtime(T.ctime)) ,'%Y-%m') =DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)) ,'%Y-%m') where DATE_FORMAT(date(from_unixtime(T.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 3 month),'%Y-%m') group by times").Scan(&weekStatistics).Error
  502. return
  503. }
  504. //获取近3个月每个月机构新增病人总量
  505. func GetThreeMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  506. 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
  507. return
  508. }
  509. //获取近3个月每个月的注册机构总量
  510. func GetThreeMonthRegistedOrgTotalCount() (int64, error) {
  511. var result admin_models.Count
  512. var err error
  513. err = readUserDb.Raw("select 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')").Scan(&result).Error
  514. return result.Count, err
  515. }
  516. //获取近3个月每个月的机构活跃账号总量
  517. func GetThreeMonthActiveAdminUserTotalCount() (int64, error) {
  518. var result admin_models.Count
  519. var err error
  520. err = readUserDb.Raw("select 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 operate_type = 1 ").Scan(&result).Error
  521. return result.Count, err
  522. }
  523. //获取近3个月每个月机构新增病人总量
  524. func GetThreeMonthNewPatientTotalCount() (int64, error) {
  525. var result admin_models.Count
  526. var err error
  527. err = readDb.Raw("select 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')").Scan(&result).Error
  528. return result.Count, err
  529. }
  530. //获取近半年每个月的注册机构总量
  531. func GetSixMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  532. 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') group by times").Scan(&weekStatistics).Error
  533. return
  534. }
  535. //获取近半年每个月的机构活跃账号总量
  536. func GetSixMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  537. 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
  538. return
  539. }
  540. //获取近半年每个月的机构活跃总量
  541. func GetSixMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  542. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times,count(DISTINCT(org_id)) as count From (select ctime,org_id FROM sgj_user_admin_login_log where app_type = 3 AND org_id > 0 AND operate_type = 1 ) T join sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = T.org_id AND DATE_FORMAT(date(from_unixtime(T.ctime)) ,'%Y-%m') =DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)) ,'%Y-%m') where DATE_FORMAT(date(from_unixtime(T.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 6 month),'%Y-%m') group by times").Scan(&weekStatistics).Error
  543. return
  544. }
  545. //获取近半年每个月机构新增病人总量
  546. func GetSixMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  547. 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
  548. return
  549. }
  550. //获取近半年每个月的注册机构总量
  551. func GetSixMonthRegistedOrgTotalCount() (int64, error) {
  552. var result admin_models.Count
  553. var err error
  554. 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')").Scan(&result).Error
  555. return result.Count, err
  556. }
  557. //获取近半年每个月的机构活跃账号总量
  558. func GetSixMonthActiveAdminUserTotalCount() (int64, error) {
  559. var result admin_models.Count
  560. var err error
  561. 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')").Scan(&result).Error
  562. return result.Count, err
  563. }
  564. //获取近半年每个月机构新增病人总量
  565. func GetSixMonthNewPatientTotalCount() (int64, error) {
  566. var result admin_models.Count
  567. var err error
  568. 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')").Scan(&result).Error
  569. return result.Count, err
  570. }
  571. //获取近1年每个月的注册机构总量
  572. func GetYearRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  573. 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') group by times").Scan(&weekStatistics).Error
  574. return
  575. }
  576. //获取近1年每个月的机构活跃账号总量
  577. func GetYearActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  578. 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
  579. return
  580. }
  581. //获取近1年每个月的机构活跃总量
  582. func GetYearActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  583. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times,count(DISTINCT(org_id)) as count From (select ctime,org_id FROM sgj_user_admin_login_log where app_type = 3 AND org_id > 0 AND operate_type = 1 ) T join sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = T.org_id AND DATE_FORMAT(date(from_unixtime(T.ctime)) ,'%Y-%m') =DATE_FORMAT(date(from_unixtime(sgj_xt.xt_dialysis_order.dialysis_date)) ,'%Y-%m') where DATE_FORMAT(date(from_unixtime(T.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m') group by times").Scan(&weekStatistics).Error
  584. return
  585. }
  586. //获取近1年每个月机构新增病人总量
  587. func GetYearNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  588. 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
  589. return
  590. }
  591. //获取近1年每个月的注册机构总量
  592. func GetYearRegistedOrgTotalCount() (int64, error) {
  593. var result admin_models.Count
  594. var err error
  595. 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')").Scan(&result).Error
  596. return result.Count, err
  597. }
  598. //获取近1年每个月的机构活跃账号总量
  599. func GetYearActiveAdminUserTotalCount() (int64, error) {
  600. var result admin_models.Count
  601. var err error
  602. 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')").Scan(&result).Error
  603. return result.Count, err
  604. }
  605. //获取近1年每个月机构新增病人总量
  606. func GetYearNewPatientTotalCount() (int64, error) {
  607. var result admin_models.Count
  608. var err error
  609. err = readDb.Raw("select 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").Scan(&result).Error
  610. return result.Count, err
  611. }
  612. func GetAllAdmin() (admin []*admin_models.AdminAccount, err error) {
  613. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("is_super_admin <> 1 AND (status = 1 OR status = 2)").Find(&admin).Error
  614. return
  615. }
  616. func CreateAdmin(admin *admin_models.AdminAccount) (err error) {
  617. err = readUserDb.Model(&admin_models.AdminAccount{}).Create(&admin).Error
  618. return
  619. }
  620. func FindUserInfoByAccount(account string) (admin admin_models.AdminAccount, err error) {
  621. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("account = ?", account).First(&admin).Error
  622. return
  623. }
  624. func FindAdminById(id int64) (admin admin_models.AdminAccount, err error) {
  625. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).First(&admin).Error
  626. return
  627. }
  628. func UpdateAdmin(admin *admin_models.AdminAccount) (err error) {
  629. err = readUserDb.Model(&admin_models.AdminAccount{}).Save(&admin).Error
  630. return
  631. }
  632. func DeleteAdmin(id int64) (err error) {
  633. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  634. return
  635. }
  636. func FindAllOrg() (org []*models.Org, err error) {
  637. err = readUserDb.Select("id, org_name").Where("status = 1").Find(&org).Error
  638. return
  639. }
  640. type FollowViewModel struct {
  641. models.OrgFollow
  642. OrgName string `gorm:"column:org_name" json:"org_name"`
  643. }
  644. func FindFollowOrg(admin_id int64) ([]*FollowViewModel, error) {
  645. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  646. 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()
  647. defer rows.Close()
  648. if err != nil {
  649. return nil, err
  650. }
  651. for rows.Next() {
  652. var vm FollowViewModel
  653. readDb.ScanRows(rows, &vm)
  654. vms = append(vms, &vm)
  655. }
  656. fmt.Println(vms)
  657. return vms, nil
  658. }
  659. type OrgViewModel struct {
  660. Id int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT" json:"id"` // 机构 ID
  661. OrgName string `gorm:"column:org_name" json:"org_name"`
  662. }
  663. func FindUnFollowOrgByIds(id int64) (orgViewModel []*OrgViewModel, err error) {
  664. 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
  665. return
  666. }
  667. func CreateFollowInfo(Follows []*models.OrgFollow) (err error) {
  668. if len(Follows) > 0 {
  669. utx := writeUserDb.Begin()
  670. if len(Follows) > 0 {
  671. thisSQL := "INSERT INTO sgj_org_follow (org_id,admin_user_id,ctime,mtime,status) VALUES "
  672. insertParams := make([]string, 0)
  673. insertData := make([]interface{}, 0)
  674. for _, info := range Follows {
  675. insertParams = append(insertParams, "(?,?,?,?,?)")
  676. insertData = append(insertData, info.OrgId)
  677. insertData = append(insertData, info.AdminUserId)
  678. insertData = append(insertData, info.Ctime)
  679. insertData = append(insertData, info.Mtime)
  680. insertData = append(insertData, info.Status)
  681. }
  682. thisSQL += strings.Join(insertParams, ", ")
  683. err = utx.Exec(thisSQL, insertData...).Error
  684. if err != nil {
  685. utx.Rollback()
  686. return
  687. }
  688. }
  689. utx.Commit()
  690. }
  691. return
  692. }
  693. func UpdateFollow(follow *models.OrgFollow) (err error) {
  694. err = readUserDb.Model(&models.OrgFollow{}).Save(follow).Error
  695. return
  696. }
  697. func FindFollowRecordByID(follow *models.OrgFollow) (info models.OrgFollow, err error) {
  698. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND admin_user_id = ?", follow.OrgId, follow.AdminUserId).First(&info).Error
  699. return
  700. }
  701. func FindFollowInfoById(org_id int64, admin_user_id int64) (info models.OrgFollow, err error) {
  702. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND status = 1 AND admin_user_id = ?", org_id, admin_user_id).First(&info).Error
  703. return
  704. }