sms_db_service.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package sms_service
  2. import (
  3. "SCRM/models"
  4. "SCRM/service"
  5. "SCRM/utils"
  6. "time"
  7. "github.com/jinzhu/gorm"
  8. )
  9. func GetBatchSendRecords(orgID int64, page int, count int) ([]*SMSBatchListViewModel, int, error) {
  10. if count <= 0 {
  11. return []*SMSBatchListViewModel{}, 0, nil
  12. }
  13. var viewModels []*SMSBatchListViewModel = make([]*SMSBatchListViewModel, 0)
  14. readDB := service.PatientReadDB()
  15. var total int
  16. getTotalErr := readDB.Model(&models.SMSBatch{}).Where("org_id = ?", orgID).Count(&total).Error
  17. if getTotalErr != nil {
  18. return nil, 0, getTotalErr
  19. }
  20. rows, err := readDB.Raw("SELECT b.full_content, b.status, (SELECT COUNT(id) AS c FROM sgj_patient_sms_send_status WHERE batch_id = b.id) AS total_count, (SELECT COUNT(id) AS c FROM sgj_patient_sms_send_status WHERE batch_id = b.id AND STATUS = '1') AS success_count FROM sgj_patient_sms_batch AS b WHERE b.org_id = ? ORDER BY b.id DESC LIMIT ? OFFSET ?;", orgID, count, (page-1)*count).Rows()
  21. defer rows.Close()
  22. if err != nil {
  23. return nil, 0, err
  24. }
  25. for rows.Next() {
  26. var viewModel SMSBatchListViewModel
  27. readDB.ScanRows(rows, &viewModel)
  28. viewModels = append(viewModels, &viewModel)
  29. }
  30. return viewModels, total, nil
  31. }
  32. // 获取指定模板id的模板
  33. func GetSMSTemplateWithTemplateID(templateID int64) (*models.SMSTemplate, error) {
  34. var template models.SMSTemplate
  35. err := service.PatientReadDB().Where("template_id = ? AND (status = 1 OR status = 2)", templateID).First(&template).Error
  36. if err != nil {
  37. if err == gorm.ErrRecordNotFound {
  38. return nil, nil
  39. } else {
  40. return nil, err
  41. }
  42. }
  43. return &template, nil
  44. }
  45. // 获取审核中的短信批次
  46. func getInReviewSMSBatchWithTemplateID(templateID int) ([]*models.SMSBatch, error) {
  47. var batchs []*models.SMSBatch
  48. err := service.PatientReadDB().Where("sms_template_id = ? AND status = ?", templateID, models.SMSBatchStatusInReview).Find(&batchs).Error
  49. if err != nil {
  50. return nil, err
  51. } else {
  52. return batchs, nil
  53. }
  54. }
  55. // 获取用户的免费短信额度
  56. func GetUserSMSFreeLimit(orgID int64, date time.Time) (*models.UserSMSFreeLimit, error) {
  57. month := date.Format("200601")
  58. var freeLimit models.UserSMSFreeLimit
  59. if readErr := service.PatientReadDB().Where("org_id = ? AND valid_month = ?", orgID, month).First(&freeLimit).Error; readErr == gorm.ErrRecordNotFound {
  60. // 创建
  61. now := time.Now().Unix()
  62. freeLimit = models.UserSMSFreeLimit{
  63. OrgID: orgID,
  64. TotalCount: 200,
  65. UsedCount: 0,
  66. ValidMonth: month,
  67. Status: 1,
  68. CreateTime: now,
  69. ModifyTime: now,
  70. }
  71. if createErr := service.PatientWriteDB().Create(&freeLimit).Error; createErr != nil {
  72. utils.ErrorLog("用户短信免费额度创建失败: %v", createErr)
  73. return nil, createErr
  74. } else {
  75. return &freeLimit, nil
  76. }
  77. } else if readErr != nil {
  78. utils.ErrorLog("获取用户短信免费额度信息失败: %v", readErr)
  79. return nil, readErr
  80. } else {
  81. return &freeLimit, nil
  82. }
  83. }
  84. // 获取未发送的短信批次
  85. func GetUnsendSMSBatch() ([]*models.SMSBatch, error) {
  86. var batchs []*models.SMSBatch
  87. err := service.PatientReadDB().Where("status = ?", models.SMSBatchStatusUnsend).Find(&batchs).Error
  88. if err != nil {
  89. if err == gorm.ErrRecordNotFound {
  90. return nil, nil
  91. } else {
  92. return nil, err
  93. }
  94. } else {
  95. return batchs, nil
  96. }
  97. }
  98. func DisableTemplate(templateID int64) error {
  99. err := service.PatientWriteDB().Model(&models.SMSTemplate{}).Where("template_id = ?", templateID).Updates(map[string]interface{}{
  100. "status": 0,
  101. "mtime": time.Now().Unix(),
  102. }).Error
  103. return err
  104. }