org_service.go 42KB

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