org_service.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package service
  2. import (
  3. "XT_Admin_Api/models"
  4. "XT_Admin_Api/utils"
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. 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){
  9. offset := (page - 1) * limit
  10. db := readUserDb.Model(&models.Org{})
  11. if len(keyword) > 0{
  12. likeKey := "%" + keyword + "%"
  13. db = db.Where("contact_name LIKE ?",likeKey)
  14. }else{
  15. switch active_status {
  16. case 1:
  17. now := time.Now()
  18. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  19. 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())
  20. break
  21. case 2:
  22. 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")
  23. break
  24. case 3:
  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 1 MONTH) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND sgj_user_admin_login_log.app_type = 3")
  26. break
  27. case 4:
  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 Year) <= date(from_unixtime(sgj_user_admin_login_log.ctime)) AND sgj_user_admin_login_log.app_type = 3")
  29. break
  30. }
  31. switch depth_active_status {
  32. case 1:
  33. now := time.Now()
  34. zeroHourTimeOfToday := utils.ZeroHourTimeOfDay(now)
  35. 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())
  36. 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",zeroHourTimeOfToday.Unix(),now.Unix())
  37. break
  38. case 2:
  39. 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")
  40. 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")
  41. break
  42. case 3:
  43. 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")
  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_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")
  45. break
  46. case 4:
  47. 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")
  48. 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")
  49. break
  50. }
  51. if province > 0{
  52. db = db.Where("sgj_user_org.province = ?",province)
  53. }
  54. if city > 0{
  55. db = db.Where("sgj_user_org.province = ?",city)
  56. }
  57. if district > 0{
  58. db = db.Where("sgj_user_org.province = ?",district)
  59. }
  60. if start_time != 0 {
  61. db = db.Where("ctime>=?", start_time)
  62. }
  63. if end_time != 0 {
  64. db = db.Where("ctime<=?", end_time)
  65. }
  66. }
  67. err = db.Where("status = 1").Count(&total).Offset(offset).Limit(limit).Find(&list).Error
  68. return
  69. }
  70. func GetOrgDetailInfo(org_id int64) (info models.Org, err error){
  71. err = readUserDb.Model(&models.Org{}).Where("org_id = ? AND status = 1",org_id).Preload("AdminUserLoginLog", func(db *gorm.DB) *gorm.DB {
  72. return db.Model(&models.AdminUserLoginLog{}).Where("status = 1 AND id = (SELECT max(id))")
  73. }).Find(&info).Error
  74. return
  75. }
  76. func GetOrgNewPatientInfo(org_id int64, from int64,to int64) (count int64, err error){
  77. var counts int64
  78. rows, err := readDb.Raw("SELECT COUNT(DISTINCT id) AS count FROM xt_patients WHERE org_id = ? AND created_time >= ? AND created_time <= ?",org_id ,from, to).Rows()
  79. if err != nil {
  80. return 0, err
  81. }
  82. if rows.Next() {
  83. rows.Scan(&counts)
  84. }
  85. return counts, nil
  86. }
  87. func GetDistrictsByUpid(id int64) (dis []*models.District, err error) {
  88. err = readUserDb.Model(&models.District{}).Where("upid=?", id).Find(&dis).Error
  89. return
  90. }