org_service.go 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. package service
  2. import (
  3. "XT_Admin_Api/models"
  4. "XT_Admin_Api/models/admin_models"
  5. "XT_Admin_Api/utils"
  6. "fmt"
  7. "github.com/jinzhu/gorm"
  8. "time"
  9. )
  10. func GetAllOrgList(active_status int64, depth_active_status int64, start_time int64, end_time int64, province int64, city int64, district int64, follow int64, keyword string, page int64, limit int64, is_super_admin int64) (list []*models.Org, err error, total int64) {
  11. fmt.Println("--------- ")
  12. fmt.Println(follow)
  13. offset := (page - 1) * limit
  14. fmt.Println(offset)
  15. db := readUserDb.Model(&models.Org{})
  16. if len(keyword) > 0 {
  17. likeKey := "%" + keyword + "%"
  18. db = db.Where("contact_name LIKE ? Or org_name LIKE ? Or org_short_name Like ? ", likeKey, likeKey, likeKey)
  19. } else {
  20. switch active_status {
  21. case 1:
  22. now := time.Now()
  23. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  24. db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND sgj_user_admin_login_log.ctime >= ? AND sgj_user_admin_login_log.ctime <= ? AND sgj_user_admin_login_log.app_type = 3", zeroHourTimeOfToday.Unix(), now.Unix())
  25. break
  26. case 2:
  27. db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 7 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 ")
  28. break
  29. case 3:
  30. db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= 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 ")
  31. break
  32. case 4:
  33. db = db.Joins("JOIN sgj_user_admin_login_log on sgj_user_admin_login_log.org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 1 Year) <= 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 ")
  34. break
  35. }
  36. switch depth_active_status {
  37. case 1:
  38. now := time.Now()
  39. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  40. db = db.Joins("JOIN sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND sgj_xt.xt_dialysis_order.created_time >= ? AND sgj_xt.xt_dialysis_order.created_time <= ?", zeroHourTimeOfToday.Unix(), now.Unix())
  41. break
  42. case 2:
  43. db = db.Joins("JOIN sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(sgj_xt.xt_dialysis_order.created_time)) AND sgj_xt.xt_dialysis_order.status = 1")
  44. break
  45. case 3:
  46. db = db.Joins("JOIN sgj_xt.xt_dialysis_order ON sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(sgj_xt.xt_dialysis_order.created_time)) AND sgj_xt.xt_dialysis_order.status = 1")
  47. break
  48. case 4:
  49. db = db.Joins("JOIN sgj_xt.xt_dialysis_order on sgj_xt.xt_dialysis_order.user_org_id = sgj_user_org.id AND DATE_SUB(CURDATE(), INTERVAL 1 Year) <= date(from_unixtime(sgj_xt.xt_dialysis_order.created_time)) AND sgj_xt.xt_dialysis_order.status = 1")
  50. break
  51. }
  52. if province > 0 {
  53. db = db.Where("sgj_user_org.province = ?", province)
  54. }
  55. if city > 0 {
  56. db = db.Where("sgj_user_org.city = ?", city)
  57. }
  58. if district > 0 {
  59. db = db.Where("sgj_user_org.district = ?", district)
  60. }
  61. if start_time != 0 {
  62. db = db.Where("sgj_user_org.ctime>=?", start_time)
  63. }
  64. if end_time != 0 {
  65. db = db.Where("sgj_user_org.ctime<=?", end_time)
  66. }
  67. if follow != 0 {
  68. db = db.Joins("JOIN sgj_org_follow on sgj_org_follow.org_id = sgj_user_org.id AND sgj_org_follow.admin_user_id = ? AND sgj_org_follow.status = 1", follow)
  69. }
  70. }
  71. if is_super_admin == 1 {
  72. db = db.Where("sgj_user_org.status = 1 AND sgj_user_org.import <> 1").Group("sgj_user_org.id")
  73. } else {
  74. db = db.Where("sgj_user_org.status = 1 AND sgj_user_org.import <> 1 AND DATE_SUB(CURDATE(), INTERVAL 15 DAY) <= date(from_unixtime(sgj_user_org.ctime))").Group("sgj_user_org.id")
  75. }
  76. db = db.Count(&total)
  77. err = db.Preload("OrgFollow", func(db *gorm.DB) *gorm.DB {
  78. return db.Model(&models.OrgFollow{}).Where("status = 1").Preload("AdminAccount", "status = 1")
  79. }).Preload("AdminUserLoginLog", func(db *gorm.DB) *gorm.DB {
  80. return db.Model(&models.AdminUserLoginLog{}).Where("id = (SELECT max(id))")
  81. }).Preload("Citys").Offset(offset).Limit(limit).Order("sgj_user_org.ctime DESC").Find(&list).Error
  82. return
  83. }
  84. func GetOrgDetailInfo(org_id int64) (info models.Org, err error) {
  85. err = readUserDb.Model(&models.Org{}).Where("id = ? AND status = 1 AND import <> 1", org_id).Preload("AdminUserLoginLog", func(db *gorm.DB) *gorm.DB {
  86. return db.Model(&models.AdminUserLoginLog{}).Where("id = (SELECT max(id))")
  87. }).Preload("AdminUser", "status = 1").Find(&info).Error
  88. return
  89. }
  90. func GetDistrictsById(id int64) (district models.District, err error) {
  91. err = readUserDb.Model(&models.District{}).Where("id = ? ", id).First(&district).Error
  92. return
  93. }
  94. func GetAllOrgType() (types []*models.OrgType, err error) {
  95. err = readUserDb.Model(&models.OrgType{}).Find(&types).Error
  96. return
  97. }
  98. //设置今天
  99. func GetOrgDialysisCount(statices_type int64, org_id int64, from int64, to int64) (count int64, err error) {
  100. var counts int64
  101. switch statices_type {
  102. case 1: //病人管理
  103. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_patients WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
  104. defer rows.Close()
  105. if err != nil {
  106. return 0, err
  107. }
  108. if rows.Next() {
  109. rows.Scan(&counts)
  110. }
  111. break
  112. case 2: //透析处方
  113. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_dialysis_prescription WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
  114. defer rows.Close()
  115. if err != nil {
  116. return 0, err
  117. }
  118. if rows.Next() {
  119. rows.Scan(&counts)
  120. }
  121. break
  122. case 3: //接诊评估
  123. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_receive_treatment_asses WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
  124. defer rows.Close()
  125. if err != nil {
  126. return 0, err
  127. }
  128. if rows.Next() {
  129. rows.Scan(&counts)
  130. }
  131. break
  132. case 4: //透前评估
  133. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_assessment_before_dislysis WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
  134. defer rows.Close()
  135. if err != nil {
  136. return 0, err
  137. }
  138. if rows.Next() {
  139. rows.Scan(&counts)
  140. }
  141. break
  142. case 5: //临时医嘱
  143. rows, err := readDb.Raw("SELECT COUNT(DISTINCT patient_id) AS count FROM xt_doctor_advice WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
  144. defer rows.Close()
  145. if err != nil {
  146. return 0, err
  147. }
  148. if rows.Next() {
  149. rows.Scan(&counts)
  150. }
  151. break
  152. case 6: //双人核对
  153. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_double_check WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
  154. defer rows.Close()
  155. if err != nil {
  156. return 0, err
  157. }
  158. if rows.Next() {
  159. rows.Scan(&counts)
  160. }
  161. break
  162. case 7: //透析上机
  163. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_dialysis_order WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1 AND (stage = 2 OR stage = 1)", org_id, from, to).Rows()
  164. defer rows.Close()
  165. if err != nil {
  166. return 0, err
  167. }
  168. if rows.Next() {
  169. rows.Scan(&counts)
  170. }
  171. break
  172. case 8: //透析监测
  173. rows, err := readDb.Raw("SELECT COUNT(DISTINCT patient_id) AS count FROM xt_monitoring_record WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
  174. defer rows.Close()
  175. if err != nil {
  176. return 0, err
  177. }
  178. if rows.Next() {
  179. rows.Scan(&counts)
  180. }
  181. break
  182. case 9: //透析下机
  183. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_dialysis_order WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND stage = 2 AND status = 1", org_id, from, to).Rows()
  184. defer rows.Close()
  185. if err != nil {
  186. return 0, err
  187. }
  188. if rows.Next() {
  189. rows.Scan(&counts)
  190. }
  191. break
  192. case 10: //透后评估
  193. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_assessment_after_dislysis WHERE user_org_id = ? AND created_time >= ? AND created_time <= ? AND status = 1", org_id, from, to).Rows()
  194. defer rows.Close()
  195. if err != nil {
  196. return 0, err
  197. }
  198. if rows.Next() {
  199. rows.Scan(&counts)
  200. }
  201. break
  202. case 11: //入库单
  203. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_warehouse WHERE org_id = ? AND ctime >= ? AND mtime <= ? AND status = 1", org_id, from, to).Rows()
  204. defer rows.Close()
  205. if err != nil {
  206. return 0, err
  207. }
  208. if rows.Next() {
  209. rows.Scan(&counts)
  210. }
  211. break
  212. case 12: //出库单
  213. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_warehouse_out WHERE org_id = ? AND ctime >= ? AND mtime <= ? AND status = 1", org_id, from, to).Rows()
  214. defer rows.Close()
  215. if err != nil {
  216. return 0, err
  217. }
  218. if rows.Next() {
  219. rows.Scan(&counts)
  220. }
  221. break
  222. }
  223. return counts, nil
  224. }
  225. //获取昨天的数据
  226. func GetYesterDayOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
  227. fmt.Println(statices_type)
  228. switch statices_type {
  229. case 1: //病人管理
  230. err = readDb.Raw("select count(distinct id) 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)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  231. break
  232. case 2: //透析处方
  233. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_dialysis_prescription where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  234. break
  235. case 3: //接诊评估
  236. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_receive_treatment_asses where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  237. break
  238. case 4: //透前评估
  239. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_assessment_before_dislysis where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  240. break
  241. case 5: //临时医嘱
  242. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_doctor_advice where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  243. break
  244. case 6: //双人核对
  245. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_double_check where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  246. break
  247. case 7: //透析上机
  248. err = readDb.Raw("select count(distinct 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 AND user_org_id = ? AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
  249. break
  250. case 8: //透析监测
  251. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_monitoring_record where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  252. break
  253. case 9: //透析下机
  254. err = readDb.Raw("select count(distinct 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 stage = 2 AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  255. break
  256. case 10: //透后评估
  257. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_assessment_after_dislysis where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  258. break
  259. case 11: //入库单
  260. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from xt_warehouse where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  261. break
  262. case 12: //出库单
  263. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from xt_warehouse_out where DATE_SUB(CURDATE(), INTERVAL 1 DAY) = date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  264. break
  265. }
  266. return
  267. }
  268. //获取近七天的数据
  269. func GetWeekOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
  270. fmt.Println(statices_type)
  271. switch statices_type {
  272. case 1: //病人管理
  273. err = readDb.Raw("select count(distinct id) 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)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  274. break
  275. case 2: //透析处方
  276. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_dialysis_prescription where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  277. break
  278. case 3: //接诊评估
  279. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_receive_treatment_asses where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  280. break
  281. case 4: //透前评估
  282. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_assessment_before_dislysis where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  283. break
  284. case 5: //临时医嘱
  285. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_doctor_advice where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  286. break
  287. case 6: //双人核对
  288. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_double_check where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  289. break
  290. case 7: //透析上机
  291. err = readDb.Raw("select count(distinct 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 AND user_org_id = ? AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
  292. break
  293. case 8: //透析监测
  294. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_monitoring_record where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  295. break
  296. case 9: //透析下机
  297. err = readDb.Raw("select count(distinct 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 stage = 2 AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  298. break
  299. case 10: //透后评估
  300. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_assessment_after_dislysis where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  301. break
  302. case 11: //入库单
  303. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from xt_warehouse where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  304. break
  305. case 12: //出库单
  306. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from xt_warehouse_out where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  307. break
  308. }
  309. return
  310. }
  311. func GetMonthOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
  312. switch statices_type {
  313. case 1: //病人管理
  314. err = readDb.Raw("select count(distinct id) 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)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  315. break
  316. case 2: //透析处方
  317. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_dialysis_prescription where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  318. break
  319. case 3: //接诊评估
  320. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_receive_treatment_asses where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  321. break
  322. case 4: //透前评估
  323. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_assessment_before_dislysis where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  324. break
  325. case 5: //临时医嘱
  326. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_doctor_advice where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  327. break
  328. case 6: //双人核对
  329. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_double_check where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  330. break
  331. case 7: //透析下机
  332. err = readDb.Raw("select count(distinct 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 AND user_org_id = ? AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
  333. break
  334. case 8: //透析监测
  335. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_monitoring_record where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  336. break
  337. case 9: //透析下机
  338. err = readDb.Raw("select count(distinct 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 stage = 2 AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  339. break
  340. case 10: //透后评估
  341. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m-%d') as times from xt_assessment_after_dislysis where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  342. break
  343. case 11: //入库单
  344. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from xt_warehouse where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  345. break
  346. case 12: //出库单
  347. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m-%d') as times from xt_warehouse_out where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  348. break
  349. }
  350. return
  351. }
  352. func GetThreeMonthOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
  353. switch statices_type {
  354. case 1: //病人管理
  355. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_patients where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  356. break
  357. case 2: //透析处方
  358. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_dialysis_prescription where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  359. break
  360. case 3: //接诊评估
  361. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_receive_treatment_asses where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  362. break
  363. case 4: //透前评估
  364. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_assessment_before_dislysis where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  365. break
  366. case 5: //临时医嘱
  367. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_doctor_advice where DATE_SUB(CURDATE(),INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  368. break
  369. case 6: //双人核对
  370. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_double_check where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  371. break
  372. case 7: //透析上机
  373. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_dialysis_order where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
  374. break
  375. case 8: //透析监测
  376. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_monitoring_record where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
  377. break
  378. case 9: //透析下机
  379. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_dialysis_order where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND stage = 2 AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  380. break
  381. case 10: //透后评估
  382. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_assessment_after_dislysis where DATE_SUB(CURDATE(),INTERVAL 3 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  383. break
  384. case 11: //入库单
  385. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times from xt_warehouse where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
  386. break
  387. case 12: //出库单
  388. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times from xt_warehouse_out where DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  389. break
  390. }
  391. return
  392. }
  393. func GetSixMonthOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
  394. switch statices_type {
  395. case 1: //病人管理
  396. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_patients where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  397. break
  398. case 2: //透析处方
  399. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_dialysis_prescription where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  400. break
  401. case 3: //接诊评估
  402. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_receive_treatment_asses where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  403. break
  404. case 4: //透前评估
  405. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_assessment_before_dislysis where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  406. break
  407. case 5: //临时医嘱
  408. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_doctor_advice where DATE_SUB(CURDATE(),INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  409. break
  410. case 6: //双人核对
  411. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_double_check where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  412. break
  413. case 7: //透析上机
  414. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_dialysis_order where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
  415. break
  416. case 8: //透析监测
  417. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_monitoring_record where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
  418. break
  419. case 9: //透析下机
  420. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_dialysis_order where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND stage = 2 AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  421. break
  422. case 10: //透后评估
  423. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_assessment_after_dislysis where DATE_SUB(CURDATE(),INTERVAL 6 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  424. break
  425. case 11: //入库单
  426. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times from xt_warehouse where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
  427. break
  428. case 12: //出库单
  429. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times from xt_warehouse_out where DATE_SUB(CURDATE(), INTERVAL 6 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  430. break
  431. }
  432. return
  433. }
  434. func GetYearOrgDialysisCount(statices_type int64, org_id int64) (weekStatistics []*admin_models.Statistics, err error) {
  435. switch statices_type {
  436. case 1: //病人管理
  437. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_patients where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  438. break
  439. case 2: //透析处方
  440. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_dialysis_prescription where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  441. break
  442. case 3: //接诊评估
  443. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_receive_treatment_asses where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  444. break
  445. case 4: //透前评估
  446. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_assessment_before_dislysis where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  447. break
  448. case 5: //临时医嘱
  449. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_doctor_advice where DATE_SUB(CURDATE(),INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  450. break
  451. case 6: //双人核对
  452. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_double_check where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  453. break
  454. case 7: //透析上机
  455. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_dialysis_order where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? AND (stage = 2 OR stage = 1) GROUP BY times", org_id).Scan(&weekStatistics).Error
  456. break
  457. case 8: //透析监测
  458. err = readDb.Raw("select count(distinct patient_id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_monitoring_record where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
  459. break
  460. case 9: //透析下机
  461. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_dialysis_order where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND stage = 2 AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  462. break
  463. case 10: //透后评估
  464. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(created_time)) ,'%Y-%m') as times from xt_assessment_after_dislysis where DATE_SUB(CURDATE(),INTERVAL 12 MONTH) <= date(from_unixtime(created_time)) AND status = 1 AND user_org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  465. break
  466. case 11: //入库单
  467. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times from xt_warehouse where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times ", org_id).Scan(&weekStatistics).Error
  468. break
  469. case 12: //出库单
  470. err = readDb.Raw("select count(distinct id) as count, DATE_FORMAT(date(from_unixtime(ctime)) ,'%Y-%m') as times from xt_warehouse_out where DATE_SUB(CURDATE(), INTERVAL 12 MONTH) <= date(from_unixtime(ctime)) AND status = 1 AND org_id = ? GROUP BY times", org_id).Scan(&weekStatistics).Error
  471. break
  472. }
  473. return
  474. }
  475. func GetDistrictsByUpid(id int64) (dis []*models.District, err error) {
  476. err = readUserDb.Model(&models.District{}).Where("upid=?", id).Find(&dis).Error
  477. return
  478. }
  479. func FindAllFollower() (follower []*admin_models.AdminAccount, err error) {
  480. err = readUserDb.Model(&admin_models.AdminAccount{}).Where("status = 1 AND is_super_admin <> 1").Find(&follower).Error
  481. return
  482. }
  483. func FindPatientById(id int64, page int64, limit int64) (patients []*models.Patients, err error, total int64) {
  484. offset := (page - 1) * limit
  485. db := readDb.Model(&models.Patients{}).Where("user_org_id=? AND status = 1", id)
  486. db = db.Count(&total)
  487. err = db.Offset(offset).Limit(limit).Find(&patients).Error
  488. return
  489. }
  490. func GetAppTypeByOrgId(org_id int64) (appType models.OrgApp, err error) {
  491. err = readUserDb.Model(&models.OrgApp{}).Where("app_type = 3 AND org_id = ?", org_id).First(&appType).Error
  492. return
  493. }
  494. func GetRoles(orgID int64, appID int64, page int, count int) ([]*models.Role, int, error) {
  495. var roles []*models.Role
  496. var totalCount int
  497. err := readUserDb.Model(&models.Role{}).
  498. Where("org_id = ? AND app_id = ?", orgID, appID).
  499. Count(&totalCount).
  500. Order("ctime asc").Limit(count).Offset((page - 1) * count).
  501. Find(&roles).
  502. Error
  503. if err != nil {
  504. if err == gorm.ErrRecordNotFound {
  505. return make([]*models.Role, 0), 0, nil
  506. } else {
  507. return nil, 0, err
  508. }
  509. }
  510. return roles, totalCount, nil
  511. }
  512. type AdminUserManageViewModel struct {
  513. AdminUserId int `gorm:"admin_user_id" json:"user_id"`
  514. UserName string `gorm:"user_name" json:"user_name"`
  515. RoleName string `gorm:"role_name" json:"role_name"`
  516. UserTitle int `gorm:"user_title" json:"user_title"`
  517. Ip string `gorm:"ip" json:"ip"`
  518. LastTime int64 `gorm:"ctime" json:"last_login_time"`
  519. Status int `gorm:"status" json:"status"`
  520. Mobile string `gorm:"mobile" json:"mobile"`
  521. // LastLoginTimeStr string `gorm:"-" json:"last_login_time_formatted"`
  522. TitleName string `gorm:"-" json:"title_name"`
  523. }
  524. func GetAdminUsersAndLoginInfo(orgID int64, appID int64, page int64, count int64) ([]*AdminUserManageViewModel, int, error) {
  525. if count <= 0 {
  526. return []*AdminUserManageViewModel{}, 0, nil
  527. }
  528. if page < 1 {
  529. page = 1
  530. }
  531. var viewModels []*AdminUserManageViewModel = make([]*AdminUserManageViewModel, 0)
  532. rows, err := readUserDb.Raw("SELECT u_a_r.admin_user_id, u_a_r.user_name, u_r.role_name, u_a_r.user_title, u_l.ip, u_l.last_time, u_a_r.status, admin.mobile FROM sgj_user_admin_role AS u_a_r JOIN sgj_user_admin AS admin ON admin.id = u_a_r.admin_user_id INNER JOIN sgj_user_role AS u_r ON u_a_r.org_id = u_r.org_id AND u_a_r.app_id = u_r.app_id AND u_r.id = u_a_r.role_id LEFT JOIN (SELECT * FROM (SELECT admin_user_id, org_id, app_id, ip, MAX(ctime) as last_time FROM sgj_user_admin_login_log WHERE org_id = ? AND app_id = ? GROUP BY admin_user_id ORDER BY ctime DESC) AS t GROUP BY admin_user_id) AS u_l ON u_a_r.org_id = u_l.org_id AND u_a_r.app_id = u_l.app_id AND u_a_r.admin_user_id = u_l.admin_user_id WHERE u_a_r.org_id = ? AND u_a_r.app_id = ? GROUP BY u_a_r.admin_user_id LIMIT ? OFFSET ?;", orgID, appID, orgID, appID, count, (page-1)*count).Rows()
  533. defer rows.Close()
  534. if err != nil {
  535. if err == gorm.ErrRecordNotFound {
  536. return viewModels, 0, nil
  537. } else {
  538. return nil, 0, err
  539. }
  540. }
  541. for rows.Next() {
  542. var viewModel AdminUserManageViewModel
  543. readUserDb.ScanRows(rows, &viewModel)
  544. title, _ := models.UserTitle[viewModel.UserTitle]
  545. viewModel.TitleName = title
  546. viewModels = append(viewModels, &viewModel)
  547. }
  548. total := 0
  549. readUserDb.Table("sgj_user_admin_role as u_a_r").Joins("join sgj_user_role as u_r on u_r.org_id = u_a_r.org_id AND u_r.app_id = u_a_r.app_id AND u_r.id = u_a_r.role_id").Where("u_a_r.org_id = ? AND u_a_r.app_id = ?", orgID, appID).Count(&total)
  550. return viewModels, total, nil
  551. }
  552. func GetAllValidDeviceZones(orgID int64) ([]*models.DeviceZone, error) {
  553. var zones []*models.DeviceZone
  554. err := readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND status = 1", orgID).Find(&zones).Error
  555. if err != nil {
  556. return nil, err
  557. }
  558. return zones, nil
  559. }
  560. func GetAllValidDeviceGroups(orgID int64) ([]*models.DeviceGroup, error) {
  561. var groups []*models.DeviceGroup
  562. err := readDb.Model(&models.DeviceGroup{}).Where("org_id = ? AND status = 1", orgID).Find(&groups).Error
  563. if err != nil {
  564. return nil, err
  565. }
  566. return groups, nil
  567. }
  568. type DeviceNumberViewModel struct {
  569. models.DeviceNumber
  570. ZoneName string `gorm:"column:zone_name" json:"zone_name"`
  571. GroupName string `gorm:"column:group_name" json:"group_name"`
  572. }
  573. func GetAllValidDeviceNumbers(orgID int64) ([]*DeviceNumberViewModel, error) {
  574. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  575. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1)", orgID).Rows()
  576. defer rows.Close()
  577. if err != nil {
  578. return nil, err
  579. }
  580. for rows.Next() {
  581. var vm DeviceNumberViewModel
  582. readDb.ScanRows(rows, &vm)
  583. vms = append(vms, &vm)
  584. }
  585. return vms, nil
  586. }