package plat import ( "context" distDao "dashoo.cn/micro/app/dao/base" "dashoo.cn/micro/app/dao/cust" projDao "dashoo.cn/micro/app/dao/proj" "dashoo.cn/opms_libary/myerrors" "database/sql" "github.com/gogf/gf/container/garray" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/os/gtime" "github.com/gogf/gf/util/gconv" "strconv" "strings" "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.DataScope(s.Ctx) 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) if platFollowup.TargetType == "20" { // 更新客户 最后跟进时间 字段 toUpdate := map[string]interface{}{ projDao.ProjBusiness.C.FinalFollowTime: req.FollowDate, projDao.ProjBusiness.C.FinalFollowId: s.GetCxtUserId(), projDao.ProjBusiness.C.FinalFollowName: s.GetCxtUserName(), } _, err = s.Dao.DB.Update(projDao.ProjBusiness.Table, toUpdate, "id = ?", req.TargetId) } return } // 跟进记录信息列表:按照日期显示,并附带评论 func (s *followupService) GetListByDay(req *model.SearchPlatFollowupReq) (total int, followupList []*model.FollowupInfoResp, err error) { filter := map[string]interface{}{} if req.TargetType == "20" && req.TargetId != "" { filter = map[string]interface{}{ "target_id": req.TargetId, "orcols": "target_id", } } else if req.CustId != "" { filter = map[string]interface{}{ "cust_id": req.CustId, "orcols": "cust_id", } } else if garray.NewStrArrayFrom(s.CxtUser.Roles, true).Contains("ProductLineManager") { var orCols []string filter = map[string]interface{}{} busIds, _ := projDao.NewProjBusinessDao(s.Tenant).DataScope(s.Ctx, "sale_id").Fields("id").Array() custIds, _ := cust.NewCustCustomerDao(s.Tenant).DataScope(s.Ctx, "sales_id").Where("is_public", "20").Fields("id").Array() distributorIds, _ := distDao.NewBaseDistributorDao(s.Tenant).Fields("id").Array() if len(busIds) > 0 { filter["target_type='20' AND target_id"] = busIds orCols = append(orCols, "target_type='20' AND target_id") } if len(custIds) > 0 { filter["target_type='10' AND target_id"] = custIds orCols = append(orCols, "target_type='10' AND target_id") } if len(distributorIds) > 0 { filter["target_type='50' AND target_id"] = distributorIds orCols = append(orCols, "target_type='50' AND target_id") } filter["orcols"] = orCols } else { if s.DataScope["userIds"] != "-1" { var orCols []string filter = map[string]interface{}{} custIds, _ := cust.NewCustCustomerDao(s.Tenant).DataScope(s.Ctx, "sales_id").Where("is_public", "20").Fields("id").Array() distributorModel := &distDao.NewBaseDistributorDao(s.Tenant).BaseDistributorDao if garray.NewStrArrayFrom(s.CxtUser.Roles, true).Contains("SalesEngineer") { distributorModel = distributorModel.DataScope(s.Ctx, "belong_sale_id") } distributorIds, _ := distributorModel.Fields("id").Array() if len(custIds) > 0 { filter["cust_id"] = custIds orCols = append(orCols, "cust_id") } if len(distributorIds) > 0 { filter["target_type='50' AND target_id"] = distributorIds orCols = append(orCols, "target_type='50' AND target_id") } filter["orcols"] = orCols } } followupModel := s.Dao.DataScope(s.Ctx, filter) // 用户仅有销售工程师角色展示自己的数据,其他人可以看到所有数据 //if garray.NewStrArrayFrom(s.CxtUser.Roles, true).Contains("SalesEngineer") { // followupModel = followupModel.WhereIn("created_by", s.DataScope["userIds"]) //} 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.CreatedName != "" { followupModel = followupModel.WhereLike("created_name", "%"+req.CreatedName+"%") } // 负责人查询 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) } if req.FollowType != "" { followupModel = followupModel.Where("follow_type", req.FollowType) } //跟进记录销售 添加销售人权限 arr := garray.NewStrArrayFrom(s.CxtUser.Roles, true) if arr.Len() == 1 && arr.Contains("SalesEngineer") && req.Sell != "" { followupModel = followupModel.Where("created_by", s.CxtUser.Id) } //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 }