admin_service.go 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // 统计后台的 service
  2. package service
  3. import (
  4. "XT_Admin_Api/models"
  5. "XT_Admin_Api/models/admin_models"
  6. "database/sql"
  7. "fmt"
  8. "github.com/jinzhu/gorm"
  9. "strings"
  10. )
  11. func GetAdminAccount(account string, password string) (*admin_models.AdminAccount, error) {
  12. var model admin_models.AdminAccount
  13. err := readUserDb.Where("account = ? AND pwd = ? AND status = 1", account, password).First(&model).Error
  14. if err != nil {
  15. if err == gorm.ErrRecordNotFound {
  16. return nil, nil
  17. }
  18. return nil, err
  19. }
  20. return &model, nil
  21. }
  22. // 获取总机构数
  23. func GetTotalOrgCount() (int, error) {
  24. var count int
  25. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND import <> 1").Count(&count).Error
  26. if err != nil {
  27. return 0, err
  28. }
  29. return count, nil
  30. }
  31. func GetAdminUserSpecialPermission(orgID int64, appID int64, adminUserID int64, permissionType models.SpecialPermissionType) (*models.AdminUserSpecialPermission, error) {
  32. var record models.AdminUserSpecialPermission
  33. err := readDb.Model(&models.AdminUserSpecialPermission{}).Where("org_id = ? AND app_id = ? AND admin_user_id = ? AND permission = ? AND status = 1", orgID, appID, adminUserID, permissionType).First(&record).Error
  34. if err != nil {
  35. if err == gorm.ErrRecordNotFound {
  36. return nil, nil
  37. } else {
  38. return nil, err
  39. }
  40. }
  41. return &record, nil
  42. }
  43. // 获取一段时间内新增病人数量
  44. func GetNewPatientCount(from int64, to int64) (int, error) {
  45. var count int
  46. db := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients WHERE status > 0 AND created_time >= ? AND created_time <= ?", from, to)
  47. rows, err := db.Rows()
  48. defer rows.Close()
  49. //rows, err := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients WHERE status > 0 AND created_time >= ? AND created_time <= ?", from, to).Rows()
  50. if err != nil {
  51. return 0, err
  52. }
  53. if rows.Next() {
  54. rows.Scan(&count)
  55. }
  56. return count, nil
  57. }
  58. // 获取所有机构病人数量
  59. func GetPatientTotalCount() (int64, error) {
  60. var count int64
  61. db := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients")
  62. rows, err := db.Rows()
  63. defer rows.Close()
  64. //rows, err := readDb.Raw("SELECT COUNT(DISTINCT id_card_no) AS count FROM xt_patients").Rows()
  65. if err != nil {
  66. return 0, err
  67. }
  68. if rows.Next() {
  69. rows.Scan(&count)
  70. }
  71. return count, nil
  72. }
  73. // 获取一段时间内的活跃账户数
  74. func GetActiveAdminUserCount(from int64, to int64) (int64, error) {
  75. var count int64
  76. db := readUserDb.Raw("SELECT COUNT(DISTINCT admin_user_id) AS count FROM sgj_user_admin_login_log WHERE ctime >= ? AND ctime <= ? AND operate_type = 1 AND app_type = 3", from, to)
  77. rows, err := db.Rows()
  78. defer rows.Close()
  79. //rows, err := readUserDb.Raw("SELECT COUNT(DISTINCT admin_user_id) AS count FROM sgj_user_admin_login_log WHERE ctime >= ? AND ctime <= ? AND operate_type = 1 AND app_type = 3", from, to).Rows()
  80. if err != nil {
  81. return 0, err
  82. }
  83. if rows.Next() {
  84. rows.Scan(&count)
  85. }
  86. return count, nil
  87. }
  88. // 获取所有机构账户数量
  89. func GetAdminUserTotalCount() (int64, error) {
  90. var count int64
  91. db := readUserDb.Raw("SELECT COUNT(DISTINCT admin_user_id) AS count FROM sgj_user_admin_login_log WHERE operate_type = 1 AND app_type = 3")
  92. rows, err := db.Rows()
  93. defer rows.Close()
  94. //rows, err := readUserDb.Raw("SELECT COUNT(DISTINCT admin_user_id) AS count FROM sgj_user_admin_login_log WHERE operate_type = 1 AND app_type = 3").Rows()
  95. if err != nil {
  96. return 0, err
  97. }
  98. if rows.Next() {
  99. rows.Scan(&count)
  100. }
  101. return count, nil
  102. }
  103. // 获取一段时间内注册的机构数量
  104. func GetRegistedOrgCount(from int64, to int64) (int64, error) {
  105. var count int64
  106. err := readUserDb.Model(&models.Org{}).Where("status <> 0 AND ctime >= ? AND ctime <= ? AND import <> 1", from, to).Count(&count).Error
  107. if err != nil {
  108. return 0, err
  109. }
  110. return count, nil
  111. }
  112. // 获取一段时间内的活跃机构数
  113. func GetActiveOrgCount(from int64, to int64) (int64, error) {
  114. var count int64
  115. db := 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)
  116. rows, err := db.Rows()
  117. defer rows.Close()
  118. if err != nil {
  119. return 0, err
  120. }
  121. if rows.Next() {
  122. rows.Scan(&count)
  123. }
  124. return count, nil
  125. }
  126. // 获取所有活跃机构数
  127. func GetActiveOrgTotalCount() (int64, error) {
  128. var count int64
  129. db := readDb.Raw("SELECT COUNT(DISTINCT user_org_id) AS count FROM sgj_xt.xt_dialysis_order Where status = 1")
  130. rows, err := db.Rows()
  131. defer rows.Close()
  132. //rows, err := readDb.Raw("SELECT COUNT(DISTINCT user_org_id) AS count FROM sgj_xt.xt_dialysis_order Where status = 1").Rows()
  133. if err != nil {
  134. return 0, err
  135. }
  136. if rows.Next() {
  137. rows.Scan(&count)
  138. }
  139. return count, nil
  140. }
  141. //获取昨天的注册机构总量
  142. func GetYesterDayRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  143. err = readUserDb.Raw("select count(id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_org where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(sgj_user_org.ctime)) AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  144. return
  145. }
  146. //获取近昨天的的机构活跃总量
  147. func GetYesterDayActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  148. 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 1 DAY) = date(from_unixtime(created_time)) AND status = 1 GROUP BY date(from_unixtime(created_time));")
  149. err = db.Scan(&weekStatistics).Error
  150. return
  151. }
  152. //获取昨天的的机构活跃账号总量
  153. func GetYesterDayActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  154. err = readUserDb.Raw("select count(DISTINCT(admin_user_id)) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_admin_login_log where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(sgj_user_admin_login_log.ctime)) AND operate_type = 1 AND app_type = 3 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  155. return
  156. }
  157. //获取昨天的的机构新增病人总量
  158. func GetYesterDayNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  159. err = readDb.Raw("select count(DISTINCT(id_card_no)) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_patients where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) GROUP BY date(from_unixtime(created_time));").Scan(&weekStatistics).Error
  160. return
  161. }
  162. //获取近七天每天的注册机构总量
  163. func GetWeekRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  164. err = readUserDb.Raw("select count(id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_org where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(sgj_user_org.ctime)) AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  165. return
  166. }
  167. //func GetWeekActiveOrgCount() (weekStatistics []*admin_models.WeekStatistics, err error) {
  168. // 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
  169. // return
  170. //}
  171. //获取近七天每天的机构活跃总量
  172. func GetWeekActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  173. 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));")
  174. err = db.Scan(&weekStatistics).Error
  175. return
  176. }
  177. //获取近七天每天的机构活跃账号总量
  178. func GetWeekActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  179. 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
  180. return
  181. }
  182. //获取近七天每天的机构新增病人总量
  183. func GetWeekNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  184. 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
  185. return
  186. }
  187. //获取近30天每天的注册机构总量
  188. func GetMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  189. err = readUserDb.Raw("select count(id) as count,DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from sgj_user_org where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(sgj_user_org.ctime)) AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 GROUP BY date(from_unixtime(ctime));").Scan(&weekStatistics).Error
  190. return
  191. }
  192. //获取近30天每天的机构活跃总量
  193. func GetMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  194. 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
  195. return
  196. }
  197. //获取近30天每天的机构活跃账号总量
  198. func GetMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  199. 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
  200. return
  201. }
  202. //获取近30天每天的机构新增病人总量
  203. func GetMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  204. 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
  205. return
  206. }
  207. //获取近3个月每个月的注册机构总量
  208. func GetThreeMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  209. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)) ,'%Y-%m') as times,count(DISTINCT(id)) as count from sgj_user_org where DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 3 month),'%Y-%m') AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 group by times").Scan(&weekStatistics).Error
  210. return
  211. }
  212. //获取近3个月每个月的机构活跃账号总量
  213. func GetThreeMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  214. 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
  215. return
  216. }
  217. //获取近3个月每个月的机构活跃总量
  218. func GetThreeMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  219. 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
  220. return
  221. }
  222. //获取近3个月每个月机构新增病人总量
  223. func GetThreeMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  224. 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
  225. return
  226. }
  227. //获取近半年每个月的注册机构总量
  228. func GetSixMonthRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  229. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)) ,'%Y-%m') as times,count(DISTINCT(id)) as count from sgj_user_org where DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 6 month),'%Y-%m') AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 group by times").Scan(&weekStatistics).Error
  230. return
  231. }
  232. //获取近半年每个月的机构活跃账号总量
  233. func GetSixMonthActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  234. 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
  235. return
  236. }
  237. //获取近半年每个月的机构活跃总量
  238. func GetSixMonthActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  239. 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
  240. return
  241. }
  242. //获取近半年每个月机构新增病人总量
  243. func GetSixMonthNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  244. 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
  245. return
  246. }
  247. //获取近1年每个月的注册机构总量
  248. func GetYearRegistedOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  249. err = readUserDb.Raw("select DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)) ,'%Y-%m') as times,count(DISTINCT(id)) as count from sgj_user_org where DATE_FORMAT(date(from_unixtime(sgj_user_org.ctime)),'%Y-%m')>=DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m') AND sgj_user_org.status = 1 AND sgj_user_org.import <> 1 group by times").Scan(&weekStatistics).Error
  250. return
  251. }
  252. //获取近1年每个月的机构活跃账号总量
  253. func GetYearActiveAdminUserCount() (weekStatistics []*admin_models.Statistics, err error) {
  254. 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
  255. return
  256. }
  257. //获取近1年每个月的机构活跃总量
  258. func GetYearActiveOrgCount() (weekStatistics []*admin_models.Statistics, err error) {
  259. 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
  260. return
  261. }
  262. //获取近1年每个月机构新增病人总量
  263. func GetYearNewPatientCount() (weekStatistics []*admin_models.Statistics, err error) {
  264. 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
  265. return
  266. }
  267. func GetAllAdmin() (admin []*admin_models.AdminAccount, err error) {
  268. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("is_super_admin <> 1 AND (status = 1 OR status = 2)").Find(&admin).Error
  269. return
  270. }
  271. func CreateAdmin(admin *admin_models.AdminAccount) (err error) {
  272. err = readUserDb.Model(&admin_models.AdminAccount{}).Create(&admin).Error
  273. return
  274. }
  275. func FindUserInfoByAccount(account string) (admin admin_models.AdminAccount, err error) {
  276. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("account = ? AND status = 1", account).First(&admin).Error
  277. return
  278. }
  279. func FindAdminById(id int64) (admin admin_models.AdminAccount, err error) {
  280. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).First(&admin).Error
  281. return
  282. }
  283. func UpdateAdmin(admin *admin_models.AdminAccount) (err error) {
  284. err = readUserDb.Model(&admin_models.AdminAccount{}).Save(&admin).Error
  285. return
  286. }
  287. func DeleteAdmin(id int64) (err error) {
  288. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("id = ?", id).Updates(map[string]interface{}{"status": 0}).Error
  289. return
  290. }
  291. func FindAllOrg() (org []*models.Org, err error) {
  292. err = readUserDb.Select("id, org_name").Where("status = 1").Find(&org).Error
  293. return
  294. }
  295. type FollowViewModel struct {
  296. models.OrgFollow
  297. OrgName string `gorm:"column:org_name" json:"org_name"`
  298. }
  299. func FindFollowOrg(admin_id int64) ([]*FollowViewModel, error) {
  300. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  301. 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()
  302. defer rows.Close()
  303. if err != nil {
  304. return nil, err
  305. }
  306. for rows.Next() {
  307. var vm FollowViewModel
  308. readDb.ScanRows(rows, &vm)
  309. vms = append(vms, &vm)
  310. }
  311. fmt.Println(vms)
  312. return vms, nil
  313. }
  314. type OrgViewModel struct {
  315. Id int64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT" json:"id"` // 机构 ID
  316. OrgName string `gorm:"column:org_name" json:"org_name"`
  317. }
  318. func FindUnFollowOrgByIds(id int64) (orgViewModel []*OrgViewModel, err error) {
  319. 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
  320. return
  321. }
  322. func CreateFollowInfo(Follows []*models.OrgFollow) (err error) {
  323. if len(Follows) > 0 {
  324. utx := writeUserDb.Begin()
  325. if len(Follows) > 0 {
  326. thisSQL := "INSERT INTO sgj_org_follow (org_id,admin_user_id,ctime,mtime,status) VALUES "
  327. insertParams := make([]string, 0)
  328. insertData := make([]interface{}, 0)
  329. for _, info := range Follows {
  330. insertParams = append(insertParams, "(?,?,?,?,?)")
  331. insertData = append(insertData, info.OrgId)
  332. insertData = append(insertData, info.AdminUserId)
  333. insertData = append(insertData, info.Ctime)
  334. insertData = append(insertData, info.Mtime)
  335. insertData = append(insertData, info.Status)
  336. }
  337. thisSQL += strings.Join(insertParams, ", ")
  338. err = utx.Exec(thisSQL, insertData...).Error
  339. if err != nil {
  340. utx.Rollback()
  341. return
  342. }
  343. }
  344. utx.Commit()
  345. }
  346. return
  347. }
  348. func UpdateFollow(follow *models.OrgFollow) (err error) {
  349. err = readUserDb.Model(&models.OrgFollow{}).Save(follow).Error
  350. return
  351. }
  352. func FindFollowRecordByID(follow *models.OrgFollow) (info models.OrgFollow, err error) {
  353. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND admin_user_id = ?", follow.OrgId, follow.AdminUserId).First(&info).Error
  354. return
  355. }
  356. func FindFollowInfoById(org_id int64, admin_user_id int64) (info models.OrgFollow, err error) {
  357. err = readUserDb.Model(&models.OrgFollow{}).Where("org_id = ? AND status = 1 AND admin_user_id = ?", org_id, admin_user_id).First(&info).Error
  358. return
  359. }
  360. func GetAllWaitFollowOrgList(keyword string, page int64, limit int64, id int64) (orgViewModel []*OrgViewModel, err error, total int64) {
  361. type Total struct {
  362. Count int64
  363. }
  364. var totals Total
  365. offset := (page - 1) * limit
  366. db := readUserDb
  367. if len(keyword) > 0 {
  368. likeKey := "%" + keyword + "%"
  369. err = db.Raw("Select id, org_name From sgj_user_org Where NOT EXISTS (Select id, org_name FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1 ) AND import <> 1 AND (sgj_user_org.org_name Like ? OR sgj_user_org.org_short_name Like ?) AND status = 1 ORDER BY sgj_user_org.ctime DESC", id, likeKey, likeKey).Offset(offset).Limit(limit).Scan(&orgViewModel).Error
  370. readUserDb.Raw("Select count(id) as count From sgj_user_org Where NOT EXISTS (Select id, org_name FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1) AND import <> 1 AND (sgj_user_org.org_name Like ? OR sgj_user_org.org_short_name Like ?) AND status = 1", id, likeKey, likeKey).Scan(&totals)
  371. } else {
  372. db = db.Raw("Select id, org_name From sgj_user_org Where NOT EXISTS (Select id, org_name FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1) AND import <> 1 AND status = 1 ORDER BY sgj_user_org.ctime DESC", id)
  373. err = db.Offset(offset).Limit(limit).Scan(&orgViewModel).Error
  374. readUserDb.Raw("Select count(id) as count From sgj_user_org Where NOT EXISTS (Select id, org_name FROM sgj_org_follow Where sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1) AND import <> 1 AND status = 1", id).Scan(&totals)
  375. }
  376. return orgViewModel, err, totals.Count
  377. }
  378. func FindAllFollowOrg(keyword string, page int64, limit int64, admin_id int64) (follow []*FollowViewModel, err error, total int64) {
  379. type Total struct {
  380. Count int64
  381. }
  382. var totals Total
  383. offset := (page - 1) * limit
  384. if len(keyword) > 0 {
  385. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  386. var rows *sql.Rows
  387. var errs error
  388. likeKey := "%" + keyword + "%"
  389. rows, errs = readUserDb.Raw("SELECT follow.*, org.org_name as org_name, org.ctime as time FROM sgj_org_follow as follow join sgj_user_org as org on org.id = follow.org_id AND (org.org_name Like ? OR org.org_short_name Like ?) AND org.import <> 1 AND org.status = 1 WHERE (follow.admin_user_id = ? AND follow.status = 1) ORDER BY time DESC", likeKey, likeKey, admin_id).Offset(offset).Limit(limit).Rows()
  390. readUserDb.Raw("SELECT count(follow.id) as count FROM sgj_org_follow as follow join sgj_user_org as org on org.id = follow.org_id AND (org.org_name Like ? OR org.org_short_name Like ?) AND org.import <> 1 AND org.status = 1 WHERE follow.admin_user_id = ? AND follow.status = 1", likeKey, likeKey, admin_id).Scan(&totals)
  391. defer rows.Close()
  392. if errs != nil {
  393. return nil, errs, totals.Count
  394. }
  395. for rows.Next() {
  396. var vm FollowViewModel
  397. readDb.ScanRows(rows, &vm)
  398. vms = append(vms, &vm)
  399. }
  400. return vms, nil, totals.Count
  401. } else {
  402. var vms []*FollowViewModel = make([]*FollowViewModel, 0)
  403. var rows *sql.Rows
  404. var errs error
  405. rows, errs = readUserDb.Raw("SELECT follow.*, org.org_name as org_name, org.ctime as time FROM sgj_org_follow as follow join sgj_user_org as org on org.id = follow.org_id AND org.import <> 1 AND org.status = 1 WHERE follow.admin_user_id = ? AND follow.status = 1 ORDER BY time DESC", admin_id).Offset(offset).Limit(limit).Rows()
  406. readUserDb.Raw("SELECT count(follow.id) as count FROM sgj_org_follow as follow join sgj_user_org as org on org.id = follow.org_id AND org.import <> 1 AND org.status = 1 WHERE follow.admin_user_id = ? AND follow.status = 1", admin_id).Scan(&totals)
  407. defer rows.Close()
  408. if errs != nil {
  409. return nil, errs, totals.Count
  410. }
  411. for rows.Next() {
  412. var vm FollowViewModel
  413. readDb.ScanRows(rows, &vm)
  414. vms = append(vms, &vm)
  415. }
  416. return vms, nil, totals.Count
  417. }
  418. }