package plat import ( "context" "dashoo.cn/opms_libary/myerrors" "database/sql" "fmt" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gconv" "strings" "dashoo.cn/micro/app/dao/plat" model "dashoo.cn/micro/app/model/plat" "dashoo.cn/micro/app/service" ) type followupCommentService struct { *service.ContextService Dao *plat.PlatFollowupCommentDao FollowupDao *plat.PlatFollowupDao } func NewFollowupCommentService(ctx context.Context) (svc *followupCommentService, err error) { svc = new(followupCommentService) if svc.ContextService, err = svc.Init(ctx); err != nil { return nil, err } svc.Dao = plat.NewPlatFollowupCommentDao(svc.Tenant) svc.FollowupDao = plat.NewPlatFollowupDao(svc.Tenant) return svc, nil } // 跟进记录留言信息列表 func (s *followupCommentService) GetList(req *model.SearchPlatFollowupCommentReq) (total int, followupCommentList []*model.FollowupCommentEx, err error) { followupCommentModel := s.Dao.M if req.FollowId != "" { followupCommentModel = followupCommentModel.Where("plat_followup_comment.follow_id", req.FollowId) } if req.Pid != "" { followupCommentModel = followupCommentModel.Where("plat_followup_comment.pid", req.Pid) } total, err = followupCommentModel.Count() if err != nil { g.Log().Error(err) err = myerrors.DbError("获取总行数失败。") return } err = followupCommentModel.Order("plat_followup_comment.created_time DESC").Scan(&followupCommentList) if err != nil && err != sql.ErrNoRows { g.Log().Error(err) err = myerrors.DbError("获取数据失败。") return } // 获取回复数据 var replyComments []*model.FollowupCommentEx err = followupCommentModel.InnerJoin("plat_followup_comment b", "b.pid=plat_followup_comment.id").Fields("b.*").Order("b.id ASC").Scan(&replyComments) if err != nil && err != sql.ErrNoRows { g.Log().Error(err) err = myerrors.DbError("获取数据失败。") return } commentMap := make(map[int]*model.FollowupCommentEx) for index, comment := range followupCommentList { commentMap[comment.Id] = followupCommentList[index] } // 构造回复数据 for _, comment := range replyComments { if commentMap[comment.Pid].ReplyComments == nil { commentMap[comment.Pid].ReplyComments = make([]*model.FollowupCommentEx, 0) } commentMap[comment.Pid].ReplyComments = append(commentMap[comment.Pid].ReplyComments, comment) } return } // NeedReplyList 本人需要回复的评论列表 func (s *followupCommentService) NeedReplyList(req *model.SearchNeedReplyCommentsReq) (total int, followupCommentList []*model.PlatFollowupComment, err error) { followupModel := s.FollowupDao.As("a").M.InnerJoin(plat.PlatFollowupComment.Table+" b", "a.id=b.follow_id").LeftJoin(plat.PlatFollowupComment.Table+" reply", "b.id=reply.pid") // 本人创建的评论 followupModel = followupModel.Where("a."+s.Dao.C.CreatedBy, s.GetCxtUserId()) // 第一级的 未回复的 评论 followupModel = followupModel.Where("(b.pid=0 OR b.pid IS NULL) AND reply.id IS NULL") // 分组去重 followupModel = followupModel.Group("b.id") total, err = followupModel.Count() if err != nil { g.Log().Error(err) err = myerrors.DbError("获取总行数失败。") return } err = followupModel.Order("b.created_time ASC").Fields("b.*").Scan(&followupCommentList) return } // NeedReplyCount 本人需要回复的评论数量 func (s *followupCommentService) NeedReplyCount(req *model.SearchNeedReplyCommentsReq) (total int, err error) { followupModel := s.FollowupDao.As("a").M.InnerJoin(plat.PlatFollowupComment.Table+" b", "a.id=b.follow_id").LeftJoin(plat.PlatFollowupComment.Table+" reply", "b.id=reply.pid") // 本人创建的评论 followupModel = followupModel.Where("a."+s.Dao.C.CreatedBy, s.GetCxtUserId()) // 第一级的 未回复的 评论 followupModel = followupModel.Where("(b.pid=0 OR b.pid IS NULL) AND reply.id IS NULL") // 分组去重 followupModel = followupModel.Group("b.id") total, err = followupModel.Count() if err != nil { g.Log().Error(err) err = myerrors.DbError("获取总行数失败。") return } return } // LatestComments 获取最新50条数据 func (s *followupCommentService) LatestComments(req *model.SearchPlatFollowupCommentReq) (total int, followupCommentList []*model.FollowupCommentEx2, err error) { // 默认50条数据 if req.PageSize == 0 { req.PageSize = 50 } if req.PageNum == 0 { req.PageNum = 1 } followupCommentModel := s.Dao.M.InnerJoin("plat_followup c", "plat_followup_comment.follow_id=c.id") // 只查询第一层评论 followupCommentModel = followupCommentModel.Where("plat_followup_comment.pid=0 OR plat_followup_comment.pid IS NULL") err = followupCommentModel.Order("plat_followup_comment.created_time DESC").Page(req.GetPage()).Fields("plat_followup_comment.*,c.target_type,c.target_name").Scan(&followupCommentList) if err != nil && err != sql.ErrNoRows { g.Log().Error(err) err = myerrors.DbError("获取数据失败。") return } // 获取回复数据 var replyComments []*model.FollowupCommentEx2 err = followupCommentModel.InnerJoin("plat_followup_comment b", "b.pid=plat_followup_comment.id").Fields("b.*,c.target_type,c.target_name").Order("b.id ASC").Scan(&replyComments) if err != nil && err != sql.ErrNoRows { g.Log().Error(err) err = myerrors.DbError("获取数据失败。") return } commentMap := make(map[int]*model.FollowupCommentEx2) for index, comment := range followupCommentList { commentMap[comment.Id] = followupCommentList[index] } // 构造回复数据 for _, comment := range replyComments { if commentMap[comment.Pid].ReplyComments == nil { commentMap[comment.Pid].ReplyComments = make([]*model.FollowupCommentEx2, 0) } commentMap[comment.Pid].ReplyComments = append(commentMap[comment.Pid].ReplyComments, comment) } return } // 添加评论 func (s *followupCommentService) Create(req *model.AddPlatFollowupCommentReq) (err error) { followup, err := s.FollowupDao.WherePri(req.FollowId).One() if err != nil { return err } platFollowupComment := new(model.PlatFollowupComment) if err = gconv.Struct(req, platFollowupComment); err != nil { return } // 填充创建信息 service.SetCreatedInfo(platFollowupComment, s.GetCxtUserId(), s.GetCxtUserName()) // 填充更新信息 //service.SetUpdatedInfo(platFollowupComment, s.GetCxtUserId(), s.GetCxtUserName()) _, err = s.Dao.Insert(platFollowupComment) if err != nil { return } // 从配置中获取消息提醒设置 销售总监、销售助理 configs, err := g.DB(s.Tenant).Model("sys_config").Where("config_key IN ('SalesDirector','SalesAssociate')").FindAll() if err != nil && err != sql.ErrNoRows { g.Log().Error(err) return } var recvUserIds []string for _, config := range configs { recvUserIds = append(recvUserIds, strings.Split(config["config_value"].String(), ",")...) } recvUserIds = append(recvUserIds, gconv.String(followup.CreatedBy)) msg := g.MapStrStr{ "msgTitle": "跟进记录评论提醒", "msgContent": fmt.Sprintf("%v %v 评论:%v", followup.TargetName, platFollowupComment.CreatedName, platFollowupComment.Content), "msgType": "20", "recvUserIds": strings.Join(recvUserIds, ","), "msgStatus": "10", "sendType": "30", } if err := service.CreateSystemMessage(msg); err != nil { g.Log().Error("消息提醒异常:", err) } return } // Reply 回复评论 func (s *followupCommentService) Reply(req *model.AddPlatFollowupCommentReq) (err error) { platFollowupComment := new(model.PlatFollowupComment) if err = gconv.Struct(req, platFollowupComment); err != nil { return } // 填充创建信息 service.SetCreatedInfo(platFollowupComment, s.GetCxtUserId(), s.GetCxtUserName()) // 填充更新信息 //service.SetUpdatedInfo(platFollowupComment, s.GetCxtUserId(), s.GetCxtUserName()) _, err = s.Dao.Insert(platFollowupComment) if err != nil { return } return }