1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package autoreply
-
- import (
- "errors"
- "strings"
- "wechat-conf/models/autoreply"
- "wechat-conf/models/model"
- "wechat-conf/service"
- "wechat-conf/utils"
- )
-
- // AutoreplyServ 用户
- type AutoreplyServ struct {
- ctx *utils.Context
- dao *autoreply.AutoreplyDAO
- }
-
- // NewAutoreplyServ 初始化
- func NewAutoreplyServ(ctx *utils.Context) *AutoreplyServ {
- return &AutoreplyServ{
- ctx: ctx,
- dao: autoreply.NewAutoreplyDAO(ctx),
- }
- }
- func (s *AutoreplyServ) GetAutoReplyList(orgId, autoType string, page, pageSize int) (map[string]interface{}, error) {
- if pageSize == 0 {
- pageSize = service.PAGENUM
- }
- autoreply, err := s.dao.GetAutoReplyList(orgId, autoType, page, pageSize)
- if err != nil {
- utils.LogError("获取自动回复列表失败: " + err.Error())
- return nil, errors.New("获取自动回复列表失败")
- }
- total, err := s.dao.GetAutoReplyListCount(orgId, autoType)
- if err != nil {
- utils.LogError("获取自动回复列表失败: " + err.Error())
- return nil, errors.New("获取自动回复列表失败")
- }
- return map[string]interface{}{
- "list": autoreply,
- "pageSize": pageSize,
- "pagenum": total,
- "page": page,
- }, nil
- }
-
- func (s *AutoreplyServ) GetAutoReplyById(autoReplyId string) (*model.TaAutoReply, error) {
- autoreply, err := s.dao.GetAutoReplyById(autoReplyId)
- if err != nil {
- utils.LogError("获取自动回复详情失败: " + err.Error())
- return nil, errors.New("获取自动回复详情失败")
- }
- return autoreply, nil
- }
-
- func (s *AutoreplyServ) SaveAutoReply(autoreply model.TaAutoReply) (*model.TaAutoReply, error) {
- var newAutoreply *model.TaAutoReply
- var err error
- if autoreply.AutoReplyId == "" {
- newAutoreply, err = s.dao.AddAutoReply(autoreply)
- } else {
- err = s.dao.DeleteKeywords(autoreply.AutoReplyId)
- err = s.dao.UpdateAutoRelpy(autoreply)
- newAutoreply = &autoreply
- }
- keywords := strings.Split(autoreply.KeyWords, ",")
- for i := 0; i < len(keywords); i++ {
- var keyword = model.TaAutoReplyKeywords{
- AutoReplyId: newAutoreply.AutoReplyId,
- Keywords: keywords[i],
- }
- err = s.dao.AddKeyword(keyword)
- }
- if err != nil {
- utils.LogError("保存自动回复失败: " + err.Error())
- return nil, errors.New("保存自动回复失败")
- }
- return newAutoreply, nil
- }
-
- func (s *AutoreplyServ) DeleteAutoReply(autoreplyId string) error {
- err := s.dao.DeleteAutoReply(autoreplyId)
- err = s.dao.DeleteKeywords(autoreplyId)
- if err != nil {
- utils.LogError("删除自动回复失败: " + err.Error())
- return errors.New("删除自动回复失败")
- }
- return nil
- }
|