123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- package sms_service
-
- import (
- "time"
-
- "Kya_New/models"
- "Kya_New/service"
- "Kya_New/utils"
- "github.com/jinzhu/gorm"
- )
-
- func GetBatchSendRecords(orgID int64, page int, count int) ([]*SMSBatchListViewModel, int, error) {
- if count <= 0 {
- return []*SMSBatchListViewModel{}, 0, nil
- }
- var viewModels []*SMSBatchListViewModel = make([]*SMSBatchListViewModel, 0)
- readDB := service.PatientReadDB()
- var total int
- getTotalErr := readDB.Model(&models.SMSBatch{}).Where("org_id = ?", orgID).Count(&total).Error
- if getTotalErr != nil {
- return nil, 0, getTotalErr
- }
-
- 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()
- defer rows.Close()
- if err != nil {
- return nil, 0, err
- }
- for rows.Next() {
- var viewModel SMSBatchListViewModel
- readDB.ScanRows(rows, &viewModel)
- viewModels = append(viewModels, &viewModel)
- }
- return viewModels, total, nil
- }
-
- // 获取指定模板id的模板
- func GetSMSTemplateWithTemplateID(templateID int64) (*models.SMSTemplate, error) {
- var template models.SMSTemplate
- err := service.PatientReadDB().Where("template_id = ? AND (status = 1 OR status = 2)", templateID).First(&template).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return &template, nil
- }
-
- // 获取审核中的短信批次
- func getInReviewSMSBatchWithTemplateID(templateID int) ([]*models.SMSBatch, error) {
- var batchs []*models.SMSBatch
- err := service.PatientReadDB().Where("sms_template_id = ? AND status = ?", templateID, models.SMSBatchStatusInReview).Find(&batchs).Error
- if err != nil {
- return nil, err
- } else {
- return batchs, nil
- }
- }
-
- // 获取用户的免费短信额度
- func GetUserSMSFreeLimit(orgID int64, date time.Time) (*models.UserSMSFreeLimit, error) {
- month := date.Format("200601")
- var freeLimit models.UserSMSFreeLimit
- if readErr := service.PatientReadDB().Where("org_id = ? AND valid_month = ?", orgID, month).First(&freeLimit).Error; readErr == gorm.ErrRecordNotFound {
- // 创建
- now := time.Now().Unix()
- freeLimit = models.UserSMSFreeLimit{
- OrgID: orgID,
- TotalCount: 200,
- UsedCount: 0,
- ValidMonth: month,
- Status: 1,
- CreateTime: now,
- ModifyTime: now,
- }
-
- if createErr := service.PatientWriteDB().Create(&freeLimit).Error; createErr != nil {
- utils.ErrorLog("用户短信免费额度创建失败: %v", createErr)
- return nil, createErr
- } else {
- return &freeLimit, nil
- }
-
- } else if readErr != nil {
- utils.ErrorLog("获取用户短信免费额度信息失败: %v", readErr)
- return nil, readErr
-
- } else {
- return &freeLimit, nil
- }
- }
-
- // 获取未发送的短信批次
- func GetUnsendSMSBatch() ([]*models.SMSBatch, error) {
- var batchs []*models.SMSBatch
- err := service.PatientReadDB().Where("status = ?", models.SMSBatchStatusUnsend).Find(&batchs).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- } else {
- return batchs, nil
- }
- }
-
- func DisableTemplate(templateID int64) error {
- err := service.PatientWriteDB().Model(&models.SMSTemplate{}).Where("template_id = ?", templateID).Updates(map[string]interface{}{
- "status": 0,
- "mtime": time.Now().Unix(),
- }).Error
- return err
- }
|