task.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package service
  2. import (
  3. "context"
  4. "os"
  5. "strconv"
  6. "strings"
  7. pb "go-common/app/service/main/sms/api"
  8. smsmdl "go-common/app/service/main/sms/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. xtime "go-common/library/time"
  12. )
  13. const _tableTask = "sms_tasks"
  14. func (s *Service) checkActTemplate(ctx context.Context, code string) (err error) {
  15. tpl, err := s.templateByCode(ctx, code)
  16. if err != nil {
  17. return
  18. }
  19. if tpl == nil {
  20. return ecode.SmsTemplateNotExist
  21. }
  22. if tpl.Stype != smsmdl.TypeActSms {
  23. return ecode.SmsTemplateNotAct
  24. }
  25. return
  26. }
  27. // AddTask add task
  28. func (s *Service) AddTask(ctx context.Context, req *pb.AddTaskReq) (res *pb.AddTaskReply, err error) {
  29. if err = s.checkActTemplate(ctx, req.TemplateCode); err != nil {
  30. return
  31. }
  32. t := &smsmdl.ModelTask{
  33. Type: req.Type,
  34. BusinessID: req.BusinessID,
  35. TemplateCode: req.TemplateCode,
  36. Desc: req.Desc,
  37. FileName: req.FileName,
  38. FilePath: req.FilePath,
  39. SendTime: xtime.Time(req.SendTime),
  40. Status: smsmdl.TaskStatusPrepared,
  41. }
  42. if err = s.db.Table(_tableTask).Create(t).Error; err != nil {
  43. log.Error("s.AddTask(%+v) error(%v)", req, err)
  44. }
  45. return
  46. }
  47. // UpdateTask update task
  48. func (s *Service) UpdateTask(ctx context.Context, req *pb.UpdateTaskReq) (res *pb.UpdateTaskReply, err error) {
  49. if err = s.checkActTemplate(ctx, req.TemplateCode); err != nil {
  50. return
  51. }
  52. data := map[string]interface{}{
  53. "type": req.Type,
  54. "business_id": req.BusinessID,
  55. "template_code": req.TemplateCode,
  56. "desc": req.Desc,
  57. "file_name": req.FileName,
  58. "file_path": req.FilePath,
  59. "send_time": xtime.Time(req.SendTime),
  60. }
  61. if err = s.db.Table(_tableTask).Where("id=?", req.ID).Update(data).Error; err != nil {
  62. log.Error("s.UpdateTask(%+v) error(%v)", req, err)
  63. }
  64. return
  65. }
  66. // DeleteTask delete task
  67. func (s *Service) DeleteTask(ctx context.Context, req *pb.DeleteTaskReq) (res *pb.DeleteTaskReply, err error) {
  68. if err = s.db.Table(_tableTask).Where("id=?", req.ID).Update("status", smsmdl.TaskStatusStop).Error; err != nil {
  69. log.Error("s.DeleteTask(%+v) error(%v)", req, err)
  70. }
  71. return
  72. }
  73. // TaskInfo get task info
  74. func (s *Service) TaskInfo(ctx context.Context, req *pb.TaskInfoReq) (res *pb.TaskInfoReply, err error) {
  75. res = &pb.TaskInfoReply{Info: new(smsmdl.ModelTask)}
  76. if err = s.db.Table(_tableTask).Where("id=?", req.ID).Find(&res.Info).Error; err != nil {
  77. log.Error("s.TaskInfo(%+v) error(%v)", req, err)
  78. return
  79. }
  80. tpl, err := s.templateByCode(ctx, res.Info.TemplateCode)
  81. if err != nil || tpl == nil {
  82. return
  83. }
  84. res.Info.TemplateCode = tpl.Code
  85. res.Info.TemplateContent = tpl.Template
  86. return
  87. }
  88. // TaskList get task list
  89. func (s *Service) TaskList(ctx context.Context, req *pb.TaskListReq) (res *pb.TaskListReply, err error) {
  90. res = &pb.TaskListReply{List: make([]*smsmdl.ModelTask, 0)}
  91. start := (req.Pn - 1) * req.Ps
  92. if err = s.db.Table(_tableTask).Order("id desc").Offset(start).Limit(req.Ps).Find(&res.List).Error; err != nil {
  93. log.Error("s.TaskList(%d,%d) error(%v)", req.Pn, req.Ps, err)
  94. return
  95. }
  96. if err = s.db.Table(_tableTask).Count(&res.Total).Error; err != nil {
  97. log.Error("s.TaskList(%d,%d) count error(%v)", req.Pn, req.Ps, err)
  98. return
  99. }
  100. return
  101. }
  102. // 上传说明:
  103. // 前端是批量上传,会随机按内容长度切割文件进行分批上传,有可能会切断原始行内容
  104. // 如果想在上传的时候同步判断文件格式,产生错误时需要忽略首行和末行
  105. // CheckUploadFile checks uploaded content validation.
  106. func (s *Service) CheckUploadFile(data []byte) (err error) {
  107. var (
  108. val int64
  109. lineNum int
  110. lines = strings.Split(string(data), "\n")
  111. total = len(lines)
  112. )
  113. for _, line := range lines {
  114. lineNum++
  115. line = strings.Trim(line, " \r\t")
  116. if line == "" {
  117. continue
  118. }
  119. if val, err = strconv.ParseInt(line, 10, 64); err != nil {
  120. log.Error("CheckUploadMid data(%s) error(%v)", line, err)
  121. return ecode.PushUploadInvalidErr
  122. }
  123. if val <= 0 {
  124. if lineNum == 1 || lineNum == total {
  125. continue
  126. }
  127. log.Error("CheckUploadMid data(%s) error(%v)", line, err)
  128. return ecode.PushUploadInvalidErr
  129. }
  130. }
  131. return
  132. }
  133. // Upload add mids file.
  134. func (s *Service) Upload(dir, path string, data []byte) (err error) {
  135. if _, err = os.Stat(dir); err != nil {
  136. if !os.IsNotExist(err) {
  137. log.Error("os.IsNotExist(%s) error(%v)", dir, err)
  138. return
  139. }
  140. if err = os.MkdirAll(dir, 0777); err != nil {
  141. log.Error("os.MkdirAll(%s) error(%v)", dir, err)
  142. return
  143. }
  144. }
  145. f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
  146. if err != nil {
  147. log.Error("s.Upload(%s) error(%v)", path, err)
  148. return
  149. }
  150. if _, err = f.Write(data); err != nil {
  151. log.Error("f.Write() error(%v)", err)
  152. return
  153. }
  154. if err = f.Close(); err != nil {
  155. log.Error("f.Close() error(%v)", err)
  156. }
  157. return
  158. }