admin_service.go 42KB

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