autoreply.go 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package autoreply
  2. import (
  3. "errors"
  4. "strings"
  5. "wechat-conf/models/autoreply"
  6. "wechat-conf/models/model"
  7. "wechat-conf/service"
  8. "wechat-conf/utils"
  9. )
  10. // AutoreplyServ 用户
  11. type AutoreplyServ struct {
  12. ctx *utils.Context
  13. dao *autoreply.AutoreplyDAO
  14. }
  15. // NewAutoreplyServ 初始化
  16. func NewAutoreplyServ(ctx *utils.Context) *AutoreplyServ {
  17. return &AutoreplyServ{
  18. ctx: ctx,
  19. dao: autoreply.NewAutoreplyDAO(ctx),
  20. }
  21. }
  22. func (s *AutoreplyServ) GetAutoReplyList(orgId, autoType string, page, pageSize int) (map[string]interface{}, error) {
  23. if pageSize == 0 {
  24. pageSize = service.PAGENUM
  25. }
  26. autoreply, err := s.dao.GetAutoReplyList(orgId, autoType, page, pageSize)
  27. if err != nil {
  28. utils.LogError("获取自动回复列表失败: " + err.Error())
  29. return nil, errors.New("获取自动回复列表失败")
  30. }
  31. total, err := s.dao.GetAutoReplyListCount(orgId, autoType)
  32. if err != nil {
  33. utils.LogError("获取自动回复列表失败: " + err.Error())
  34. return nil, errors.New("获取自动回复列表失败")
  35. }
  36. return map[string]interface{}{
  37. "list": autoreply,
  38. "pageSize": pageSize,
  39. "pagenum": total,
  40. "page": page,
  41. }, nil
  42. }
  43. func (s *AutoreplyServ) GetAutoReplyById(autoReplyId string) (*model.TaAutoReply, error) {
  44. autoreply, err := s.dao.GetAutoReplyById(autoReplyId)
  45. if err != nil {
  46. utils.LogError("获取自动回复详情失败: " + err.Error())
  47. return nil, errors.New("获取自动回复详情失败")
  48. }
  49. return autoreply, nil
  50. }
  51. func (s *AutoreplyServ) SaveAutoReply(autoreply model.TaAutoReply) (*model.TaAutoReply, error) {
  52. var newAutoreply *model.TaAutoReply
  53. var err error
  54. if autoreply.AutoReplyId == "" {
  55. newAutoreply, err = s.dao.AddAutoReply(autoreply)
  56. } else {
  57. err = s.dao.DeleteKeywords(autoreply.AutoReplyId)
  58. err = s.dao.UpdateAutoRelpy(autoreply)
  59. newAutoreply = &autoreply
  60. }
  61. keywords := strings.Split(autoreply.KeyWords, ",")
  62. for i := 0; i < len(keywords); i++ {
  63. var keyword = model.TaAutoReplyKeywords{
  64. AutoReplyId: newAutoreply.AutoReplyId,
  65. Keywords: keywords[i],
  66. }
  67. err = s.dao.AddKeyword(keyword)
  68. }
  69. if err != nil {
  70. utils.LogError("保存自动回复失败: " + err.Error())
  71. return nil, errors.New("保存自动回复失败")
  72. }
  73. return newAutoreply, nil
  74. }
  75. func (s *AutoreplyServ) DeleteAutoReply(autoreplyId string) error {
  76. err := s.dao.DeleteAutoReply(autoreplyId)
  77. err = s.dao.DeleteKeywords(autoreplyId)
  78. if err != nil {
  79. utils.LogError("删除自动回复失败: " + err.Error())
  80. return errors.New("删除自动回复失败")
  81. }
  82. return nil
  83. }