org_service.go 39KB

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