| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- package plat
- import (
- "context"
- "database/sql"
- "strconv"
- "strings"
- "dashoo.cn/opms_libary/myerrors"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gconv"
- "dashoo.cn/micro/app/dao/plat"
- model "dashoo.cn/micro/app/model/plat"
- "dashoo.cn/micro/app/service"
- )
- type followupService struct {
- *service.ContextService
- Dao *plat.PlatFollowupDao
- }
- func NewFollowupService(ctx context.Context) (svc *followupService, err error) {
- svc = new(followupService)
- if svc.ContextService, err = svc.Init(ctx); err != nil {
- return nil, err
- }
- svc.Dao = plat.NewPlatFollowupDao(svc.Tenant)
- return svc, nil
- }
- // 跟进记录信息列表
- func (s *followupService) GetList(req *model.SearchPlatFollowupReq) (total int, followupList []*model.PlatFollowup, err error) {
- followupModel := s.Dao.M
- if req.CustId != "" {
- followupModel = followupModel.Where("cust_id", req.CustId)
- }
- if req.CustName != "" {
- followupModel = followupModel.WhereLike("cust_name", "%"+req.CustName+"%")
- }
- if req.TargetId != "" {
- followupModel = followupModel.Where("target_id", req.TargetId)
- }
- if req.TargetType != "" {
- followupModel = followupModel.Where("target_type", req.TargetType)
- }
- if req.TargetName != "" {
- followupModel = followupModel.WhereLike("target_name", "%"+req.TargetName+"%")
- }
- // 负责人查询
- if req.ManagerId != "" {
- followupModel = followupModel.Where("created_by", req.ManagerId)
- }
- if req.IsMyself == "1" {
- followupModel = followupModel.Where("created_by", s.GetCxtUserId())
- }
- total, err = followupModel.Count()
- if err != nil {
- g.Log().Error(err)
- err = myerrors.DbError("获取总行数失败。")
- return
- }
- err = followupModel.Page(req.GetPage()).Order("follow_date DESC").Scan(&followupList)
- return
- }
- // 添加信息
- func (s *followupService) Create(req *model.AddPlatFollowupReq) (err error) {
- platFollowup := new(model.PlatFollowup)
- var files []*model.PlatFollowupFile
- if err = gconv.Struct(req, platFollowup); err != nil {
- return
- }
- // 填充创建信息
- service.SetCreatedInfo(platFollowup, s.GetCxtUserId(), s.GetCxtUserName())
- // 填充更新信息
- //service.SetUpdatedInfo(platFollowup, s.GetCxtUserId(), s.GetCxtUserName())
- res, err := s.Dao.Insert(platFollowup)
- if err != nil {
- return
- }
- // 更新附件数据
- id, _ := res.LastInsertId()
- for _, file := range req.Files {
- var fileData model.PlatFollowupFile
- if err = gconv.Struct(file, &fileData); err != nil {
- return
- }
- fileData.FollowId = strconv.Itoa(int(id))
- // 填充创建信息
- service.SetCreatedInfo(&fileData, s.GetCxtUserId(), s.GetCxtUserName())
- // 填充更新信息
- //service.SetUpdatedInfo(fileData, s.GetCxtUserId(), s.GetCxtUserName())
- files = append(files, &fileData)
- }
- // 保存附件信息
- if len(files) > 0 {
- _, err = s.Dao.DB.Insert(plat.PlatFollowupFile.Table, files)
- if err != nil {
- return
- }
- }
- // 更新客户 最后跟进时间 字段
- toUpdate := map[string]interface{}{
- "follow_up_date": req.FollowDate,
- "follow_up_man": s.GetCxtUserName(),
- }
- _, err = s.Dao.DB.Update("cust_customer", toUpdate, "id = ?", req.CustId)
- return
- }
- // 跟进记录信息列表:按照日期显示,并附带评论
- func (s *followupService) GetListByDay(req *model.SearchPlatFollowupReq) (total int, followupList []*model.FollowupInfoResp, err error) {
- followupModel := s.Dao.M
- if req.CustId != "" {
- followupModel = followupModel.Where("cust_id", req.CustId)
- }
- if req.CustName != "" {
- followupModel = followupModel.WhereLike("cust_name", "%"+req.CustName+"%")
- }
- if req.TargetId != "" {
- followupModel = followupModel.Where("target_id", req.TargetId)
- }
- if req.TargetType != "" {
- followupModel = followupModel.Where("target_type", req.TargetType)
- }
- if req.TargetName != "" {
- followupModel = followupModel.WhereLike("target_name", "%"+req.TargetName+"%")
- }
- // 负责人查询
- if req.ManagerId != "" {
- followupModel = followupModel.Where("created_by", req.ManagerId)
- }
- if req.IsMyself == "1" {
- followupModel = followupModel.Where("created_by", s.GetCxtUserId())
- }
- // 日期条件
- if req.DaysBeforeToday >= 0 { // 获取前N天的跟进记录
- now := gtime.Now()
- begin := now.AddDate(0, 0, -req.DaysBeforeToday).Format("Y-m-d 00:00:00")
- followupModel = followupModel.Where("follow_date>=?", begin)
- }
- // 获取日期区间范围内的记录
- if req.BeginTime != "" && req.EndTime != "" {
- begin := strings.Split(req.BeginTime, " ")[0] + " 00:00:00"
- end := strings.Split(req.EndTime, " ")[0] + " 23:59:59"
- followupModel = followupModel.Where("follow_date>=? AND follow_date<=?", begin, end)
- }
- //total, err = followupModel.Count()
- //if err != nil {
- // g.Log().Error(err)
- // err = gerror.New("获取总行数失败")
- // return
- //}
- // 查询原始记录
- var originalFollowupList []model.FollowupInfo
- err = followupModel.Order("follow_date DESC").Scan(&originalFollowupList)
- if err != nil && err != sql.ErrNoRows {
- return
- }
- // 查询一级评论
- var comments []model.PlatFollowupComment
- err = s.Dao.InnerJoin(plat.PlatFollowupComment.Table, "plat_followup.id=plat_followup_comment.follow_id").Where("plat_followup_comment.pid=0 OR plat_followup_comment.pid IS NULL").Fields("plat_followup_comment.*").Structs(&comments)
- if err != nil && err != sql.ErrNoRows {
- return
- }
- // 构造数据
- var days []string
- followupMap := make(map[string][]*model.FollowupInfo, 0)
- commentMap := make(map[int][]*model.PlatFollowupComment, 0)
- // 评论数据map
- for index, comment := range comments {
- if _, ok := commentMap[comment.FollowId]; !ok {
- commentMap[comment.FollowId] = make([]*model.PlatFollowupComment, 0)
- }
- commentMap[comment.FollowId] = append(commentMap[comment.FollowId], &comments[index])
- }
- // 跟进记录map
- for index, followup := range originalFollowupList {
- if _, ok := followupMap[followup.FollowDate.Format("Y-m-d")]; !ok {
- days = append(days, followup.FollowDate.Format("Y-m-d"))
- followupMap[followup.FollowDate.Format("Y-m-d")] = make([]*model.FollowupInfo, 0)
- }
- if followup.Comments == nil {
- originalFollowupList[index].Comments = make([]*model.PlatFollowupComment, 0)
- }
- // 为跟进记录填充评论数据
- if _, ok := commentMap[followup.Id]; ok {
- originalFollowupList[index].Comments = append(originalFollowupList[index].Comments, commentMap[followup.Id]...)
- }
- originalFollowupList[index].CommentNumber = len(originalFollowupList[index].Comments)
- followupMap[followup.FollowDate.Format("Y-m-d")] = append(followupMap[followup.FollowDate.Format("Y-m-d")], &originalFollowupList[index])
- }
- for _, day := range days {
- var followup model.FollowupInfoResp
- followup.FollowDay = day
- followup.FollowupList = followupMap[day]
- followupList = append(followupList, &followup)
- }
- return
- }
|