package service import ( "context" "database/sql" "fmt" "log" "strings" "time" qu "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "app.yhyue.com/moapp/jybase/encrypt" "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb" cm "bp.jydev.jianyu360.cn/CRM/application/api/common" "bp.jydev.jianyu360.cn/CRM/application/entity" "github.com/gogf/gf/v2/util/gconv" ) var Se = encrypt.SimpleEncrypt{Key: "topnet"} var YYYYMMDDHHMM = "2006-01-02 15:04" //%20用于转义 // 线索相关 type SaleClueService struct { AppId string PositionId int64 EntId int64 EntUserId int64 BaseUserId int64 AccountId int64 EntDeptId int64 ClueName string CluesSource string Summary string EmployInfoId int64 Types int64 User []int64 FollowUpTime int64 CreateName string Channel string } // Add 创建线索 // return 是否成功,群组id func (this *SaleClueService) Add(ctx context.Context) (int64, string) { nowtime := time.Now().Format(date.Date_Full_Layout) nextFollowTime := time.Unix(this.FollowUpTime, 0).Format(date.Date_Full_Layout) groupId := "" args := []interface{}{} argsTask := []interface{}{} argsTaskTeam := []interface{}{} baseUserIdArr := []int64{} clueId, taskId := int64(-1), int64(-1) //判断处理方式 //转办 if this.Types == 2 { transferArr := []int64{} for _, v := range this.User { i_entuserid := v resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{ Id: i_entuserid, }) if err != nil { log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err) return -1, groupId } else if resp == nil { log.Println("entuser用户", i_entuserid, "没有找到职位信息") return -1, groupId } transferArr = append(transferArr, resp.PositionId) baseUserIdArr = append(baseUserIdArr, resp.UserId) } //线索 args = append(args, this.PositionId, this.EntId, this.EntUserId, this.ClueName, this.CluesSource, this.Summary, this.EmployInfoId, 0, nowtime, this.CreateName, this.Channel) //任务 argsTask = append(argsTask, this.EntId, this.ClueName+"的跟进任务", 1, this.PositionId, 1, nowtime, 1, 0, qu.If(this.FollowUpTime == 0, nil, nextFollowTime), nil, nil) clueId, taskId = Save(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, 0, this.PositionId, this.CreateName, transferArr, this.EntId, this.EntUserId, this.EntDeptId) if clueId < 0 { return -1, groupId } } else if this.Types == 1 { //线索 args = append(args, this.PositionId, this.EntId, this.EntUserId, this.ClueName, this.CluesSource, this.Summary, this.EmployInfoId, 0, time.Now().Format(date.Date_Full_Layout), this.CreateName, this.Channel) //任务 argsTask = append(argsTask, this.EntId, this.ClueName+"的跟进任务", 1, this.PositionId, 2, nowtime, 1, 0, qu.If(this.FollowUpTime == 0, nil, nextFollowTime), nil, nil) //任务团队 argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime) //存库 clueId, taskId = Save(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, 0, this.PositionId, this.CreateName, []int64{}, this.EntId, this.EntUserId, this.EntDeptId) if clueId < 0 { return -1, groupId } baseUserIdArr = append(baseUserIdArr, this.BaseUserId) //自办 创建 群聊 // gp := &Group{ // EntId: this.EntId, // PositionId: this.PositionId, // UserIdArr: []int64{}, // AppId: this.AppId, // EntUserId: this.EntUserId, // AccountId: this.AccountId, // } // groupId = gp.GroupAdd() } else if this.Types == 3 { transferArr := []int64{} transferArr = append(transferArr, this.PositionId) //线索 args = append(args, this.PositionId, this.EntId, this.EntUserId, this.ClueName, this.CluesSource, this.Summary, this.EmployInfoId, 0, nowtime, this.CreateName, this.Channel) //任务 argsTask = append(argsTask, this.EntId, this.ClueName+"的跟进任务", 1, this.PositionId, 1, nowtime, 1, 0, qu.If(this.FollowUpTime == 0, nil, nextFollowTime), nil, nil) clueId, taskId = Save(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, 0, this.PositionId, this.CreateName, transferArr, this.EntId, this.EntUserId, this.EntDeptId) if clueId < 0 { return -1, groupId } baseUserIdArr = append(baseUserIdArr, this.BaseUserId) } u := &User{BaseUserIds: baseUserIdArr} if this.Types != 3 { var kb strings.Builder var vb strings.Builder for k, v := range u.GetUserId(this.EntId) { kb.WriteString(k + ",") vb.WriteString(gconv.String(v) + ",") userId := strings.TrimRight(kb.String(), ",") positionId := strings.TrimRight(vb.String(), ",") content, pcHref := "", "" next := strings.Replace(time.Unix(this.FollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1) if this.Types == 1 { pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.MyselfPcHref, positionId, taskId, clueId) content = fmt.Sprintf(cm.Push.SaleClue.Create.MySelfContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next) } else if this.Types == 2 { pcHref = fmt.Sprintf(cm.Push.SaleClue.Create.TransferPcHref, positionId) content = fmt.Sprintf(cm.Push.SaleClue.Create.TransferContent, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), next) } StationMailPush(userId, positionId, cm.Push.SaleClue.Create.Title, content, pcHref, cm.Push.SaleClue.Create.MobileHref, "11") } } return 1, groupId } // SaleClueAdd 线索存储 func SaleClueAdd(tx *sql.Tx, args []interface{}) int64 { fields := []string{"position_id", "ent_id", "ent_user_id", "name", "source", "summary", "employ_info_id", "is_close", "create_time", "create_person", "channel"} _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.SALE_CLUE, fields, args) return id } //TaskAdd 任务车存储 /*创建任务 taskTransferMap 自办的map transferArr 转办的用户职位id */ func TaskAdd(tx *sql.Tx, args, taskTeamArgs []interface{}, transferArr []int64, positionId int64) int64 { fields := []string{"ent_id", "name", "source", "create_person", "status", "create_time", "create_way", "join_task_vehicle", "next_follow_time", "tpl_id", "current_stage", "source_id"} _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.TASK, fields, args) teamId, transferId := int64(0), int64(0) if len(taskTeamArgs) > 0 { //自办 taskTeamFields := []string{"position_id", "ent_user_id", "name", "role", "create_time", "task_id"} taskTeamArgs = append(taskTeamArgs, id) _, teamId = cm.CrmMysql.InsertBatchByTx(tx, entity.TASK_TEAM, taskTeamFields, taskTeamArgs) } else if len(transferArr) > 0 { //转办 taskTransferFields := []string{"transfer_id", "responsible_id", "create_time", "task_id"} taskTransferArgs := []interface{}{} for _, v := range transferArr { taskTransferArgs = append(taskTransferArgs, positionId, v, time.Now().Format(date.Date_Full_Layout), id) } _, transferId = cm.CrmMysql.InsertBatchByTx(tx, entity.TASK_TRANSFER, taskTransferFields, taskTransferArgs) } if transferId > 0 || teamId > 0 { return id } log.Println("task add err :", id, len(taskTeamArgs), teamId, len(transferArr), transferId) return -1 } // Save 存库 func Save(ctx context.Context, argsClue, argsTask, argsTaskTeam []interface{}, employ_info_id, employ_custom_id, positionId int64, createName string, transferArr []int64, entId, entUserId, entDeptId int64) (int64, int64) { clueId, taskId := int64(-1), int64(-1) //存库 cm.CrmMysql.ExecTx("创建线索", func(tx *sql.Tx) bool { //是否创建销售线索 employ_info_id_new, _ := EmployUpdate(tx, employ_info_id, employ_custom_id, positionId, "is_create_clue", entId, entUserId, entDeptId) //插入线索 argsClue[6] = employ_info_id_new clueId = SaleClueAdd(tx, argsClue) //传过来的argTask没有来源id,需要append argsTask = append(argsTask, clueId) //任务车存储 taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId) //操作台帐 ok2 := SaveLedger(ctx, positionId, clueId, taskId, "创建销售线索", fmt.Sprintf("%s创建了销售线索", createName), createName) if clueId > 0 && taskId > 0 && ok2 { return true } log.Println("save clue err: ", clueId, taskId, ok2) return false }) return clueId, taskId }