template.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package service
  2. import (
  3. "context"
  4. pb "go-common/app/service/main/sms/api"
  5. smsmdl "go-common/app/service/main/sms/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _tableTemplate = "sms_template_new"
  11. _searchTypeCode = "code"
  12. _searchTypeContent = "content"
  13. )
  14. func (s *Service) templateByID(ctx context.Context, id int64) (res *smsmdl.ModelTemplate, err error) {
  15. res = new(smsmdl.ModelTemplate)
  16. if err = s.db.Table(_tableTemplate).Where("id=?", id).First(&res).Error; err != nil {
  17. res = nil
  18. if err == ecode.NothingFound {
  19. err = nil
  20. return
  21. }
  22. log.Error("templateByID(%d) error(%v)", id, err)
  23. return
  24. }
  25. return
  26. }
  27. func (s *Service) templateByCode(ctx context.Context, code string) (res *smsmdl.ModelTemplate, err error) {
  28. res = new(smsmdl.ModelTemplate)
  29. if err = s.db.Table(_tableTemplate).Where("code=?", code).First(&res).Error; err != nil {
  30. res = nil
  31. if err == ecode.NothingFound {
  32. err = nil
  33. return
  34. }
  35. log.Error("templateByCode(%s) error(%v)", code, err)
  36. return
  37. }
  38. return
  39. }
  40. // AddTemplate add template
  41. func (s *Service) AddTemplate(ctx context.Context, req *pb.AddTemplateReq) (res *pb.AddTemplateReply, err error) {
  42. tpl, err := s.templateByCode(ctx, req.Tcode)
  43. if err != nil {
  44. return
  45. }
  46. if tpl != nil {
  47. err = ecode.SmsTemplateCodeExist
  48. return
  49. }
  50. t := &smsmdl.ModelTemplate{
  51. Code: req.Tcode,
  52. Template: req.Template,
  53. Stype: req.Stype,
  54. Status: smsmdl.TemplateStatusApprovel,
  55. Submitter: req.Submitter,
  56. }
  57. if err = s.db.Table(_tableTemplate).Create(t).Error; err != nil {
  58. log.Error("s.AddTemplate(%+v) error(%v)", req, err)
  59. }
  60. return
  61. }
  62. // UpdateTemplate update template
  63. func (s *Service) UpdateTemplate(ctx context.Context, req *pb.UpdateTemplateReq) (res *pb.UpdateTemplateReply, err error) {
  64. tpl, err := s.templateByID(ctx, req.ID)
  65. if err != nil {
  66. return
  67. }
  68. if tpl == nil {
  69. err = ecode.SmsTemplateNotExist
  70. return
  71. }
  72. if tpl.Code != req.Tcode {
  73. var t *smsmdl.ModelTemplate
  74. if t, err = s.templateByCode(ctx, req.Tcode); err != nil {
  75. return
  76. }
  77. if t != nil {
  78. err = ecode.SmsTemplateCodeExist
  79. return
  80. }
  81. }
  82. m := map[string]interface{}{
  83. "code": req.Tcode,
  84. "template": req.Template,
  85. "stype": req.Stype,
  86. "status": req.Status,
  87. "submitter": req.Submitter,
  88. }
  89. if err = s.db.Table(_tableTemplate).Where("id=?", req.ID).Update(m).Error; err != nil {
  90. log.Error("s.UpdateTemplate(%+v) error(%v)", req, err)
  91. }
  92. return
  93. }
  94. // TemplateList template list
  95. func (s *Service) TemplateList(ctx context.Context, req *pb.TemplateListReq) (res *pb.TemplateListReply, err error) {
  96. res = new(pb.TemplateListReply)
  97. cond := "1=1"
  98. if req.St == _searchTypeCode {
  99. cond = "code like '%" + req.Sw + "%'"
  100. } else if req.St == _searchTypeContent {
  101. cond = "template like '%" + req.Sw + "%'"
  102. }
  103. start := (req.Pn - 1) * req.Ps
  104. if err = s.db.Table(_tableTemplate).Where(cond).Order("id desc").Offset(start).Limit(req.Ps).Find(&res.List).Error; err != nil {
  105. log.Error("s.TemplateList(%d,%d,%s,%s) error(%v)", req.Pn, req.Ps, req.St, req.Sw, err)
  106. return
  107. }
  108. err = s.db.Table(_tableTemplate).Where(cond).Count(&res.Total).Error
  109. return
  110. }