package service import ( "context" "database/sql" "fmt" "log" "strings" "time" qu "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "bp.jydev.jianyu360.cn/BaseService/userCenter/rpc/pb" cm "bp.jydev.jianyu360.cn/CRM/application/api/common" "bp.jydev.jianyu360.cn/CRM/application/entity" baseC "bp.jydev.jianyu360.cn/CRM/baseCenter/rpc/pb" "github.com/gogf/gf/v2/util/gconv" ) //客户相关 type CustomService struct { BaseUserId int64 PositionId int64 EntUserId int64 EntId int64 CustomType int64 //客户类型 Summary string //概要信息 CustomAllName string //客户全称 CustomAbbreviation string //客户简称 CustomLevel int64 //客户级别 CustomIndustry int64 //客户行业 CustomDetailIndustry int64 //客户细分行业 Province string //省份 City string //城市 District string //地区 Address string //详细地址 CompanyPhone string //公司电话 NextfollowUpTime int64 //下次跟进时间戳 Types int64 //处理方式 1自办;2转办 User []int64 //转办用户 EmployCustomId int64 //客户收录id EmployInfoId int64 //资讯收录id Remarks string //备注 CreateName string //创建人 } //Add 创建客户 func (this *CustomService) Add(ctx context.Context) int64 { nowtime := time.Now().Format(date.Date_Full_Layout) nextFollowTime := time.Unix(this.NextfollowUpTime, 0).Format(date.Date_Full_Layout) if this.NextfollowUpTime == 0 { nextFollowTime = time.Now().AddDate(0, 0, cm.C.NextFollowUpTime).Format(date.Date_Full_Layout) } args := []interface{}{} argsTask := []interface{}{} argsTaskTeam := []interface{}{} //判断处理方式 //转办 baseUserIdArr := []int64{} customId, 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 } else if resp == nil { log.Println("entuser用户", i_entuserid, "没有找到职位信息") return -1 } transferArr = append(transferArr, resp.PositionId) baseUserIdArr = append(baseUserIdArr, resp.UserId) } //客户 args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.EmployCustomId, this.CustomType, this.CustomAllName, this.CustomAbbreviation, this.Summary, qu.If(this.CustomLevel == 0, nil, this.CustomLevel), this.CustomIndustry, this.CustomDetailIndustry, qu.If(this.Province == "", nil, this.Province), qu.If(this.City == "", nil, this.City), qu.If(this.District == "", nil, this.District), qu.If(this.Address == "", nil, this.Address), qu.If(this.CompanyPhone == "", nil, this.CompanyPhone), qu.If(this.Remarks == "", nil, this.Remarks), this.CreateName, nowtime) //任务 argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime)) customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, transferArr) if customId < 0 { return -1 } } else if this.Types == 1 { //客户 args = append(args, this.PositionId, this.EntId, this.EntUserId, this.EmployInfoId, this.EmployCustomId, this.CustomType, this.CustomAllName, this.CustomAbbreviation, this.Summary, qu.If(this.CustomLevel == 0, nil, this.CustomLevel), this.CustomIndustry, this.CustomDetailIndustry, qu.If(this.Province == "", nil, this.Province), qu.If(this.City == "", nil, this.City), qu.If(this.District == "", nil, this.District), qu.If(this.Address == "", nil, this.Address), qu.If(this.CompanyPhone == "", nil, this.CompanyPhone), qu.If(this.Remarks == "", nil, this.Remarks), this.CreateName, nowtime) //任务s argsTask = append(argsTask, this.EntId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 2, nowtime, 1, 0, qu.If(this.NextfollowUpTime == 0, nil, nextFollowTime)) //任务团队 argsTaskTeam = append(argsTaskTeam, this.PositionId, this.EntUserId, this.CreateName, 1, nowtime) //存库 customId, taskId = SaveCustom(ctx, args, argsTask, argsTaskTeam, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName, []int64{}) if customId < 0 { return -1 } baseUserIdArr = append(baseUserIdArr, this.BaseUserId) } u := &User{BaseUserIds: baseUserIdArr} 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(), ",") StationMailPush(userId, positionId, cm.Push.Custom.Create.Title, fmt.Sprintf(cm.Push.Custom.Create.Content, this.CreateName, strings.Replace(date.NowFormat(YYYYMMDDHHMM), " ", "%20", -1), strings.Replace(time.Unix(this.NextfollowUpTime, 0).Format(YYYYMMDDHHMM), " ", "%20", -1)), fmt.Sprintf(cm.Push.Custom.Create.PcHref, positionId, taskId, customId), cm.Push.Custom.Create.MobileHref) } return 1 } // func SaleCustomAdd(tx *sql.Tx, args []interface{}) int64 { fields := []string{"position_id", "ent_id", "ent_user_id", "employ_info_id", "employ_custom_id", "type", "full_name", "sort_name", "summary", "level", "industry", "subdivision_industry", "province", "city", "county", "address", "phone", "remark", "create_person", "create_time"} _, id := cm.CrmMysql.InsertBatchByTx(tx, entity.CUSTOM, fields, args) return id } //EmployUpdate 是否创建线索/机会/客户修改 func EmployUpdate(tx *sql.Tx, employ_info_id, employ_custom_id, positionId int64, key string) bool { tablename := "" id := int64(0) if employ_custom_id > 0 { tablename = entity.EMPLOY_CUSTOM id = employ_custom_id } else if employ_info_id > 0 { tablename = entity.EMPLOY_INFO id = employ_info_id } if tablename == "" || id == 0 { return true } return cm.CrmMysql.UpdateByTx(tx, tablename, map[string]interface{}{"id": id, "position_id": positionId}, map[string]interface{}{key: 1}) } func SaveCustom(ctx context.Context, argsCustom, argsTask, argsTaskTeam []interface{}, employ_info_id, employ_custom_id, positionId int64, createPerson string, transferArr []int64) (int64, int64) { customId := int64(-1) taskId := int64(-1) //存库 cm.CrmMysql.ExecTx("创建客户", func(tx *sql.Tx) bool { //插入客户 customId = SaleCustomAdd(tx, argsCustom) //传过来的argTask没有来源id,需要append argsTask = append(argsTask, customId) //任务车存储 taskId = TaskAdd(tx, argsTask, argsTaskTeam, transferArr, positionId) ok := EmployUpdate(tx, employ_info_id, employ_custom_id, positionId, "is_create_custom") //插入台账 ok2 := SaveLedger(ctx, positionId, customId, taskId, "创建客户", fmt.Sprintf("%s创建了客户", createPerson), createPerson) if customId > 0 && taskId > 0 && ok && ok2 { return true } log.Println("SaveCustom err:", customId, taskId, ok) return false }) return customId, taskId } //SaveLedger 操作台帐相关 func SaveLedger(ctx context.Context, positionId, businessId, taskId int64, types, content, createPerson string) bool { //操作台账 resp, err := cm.BaseCenterRpc.LedgerAdd(ctx, &baseC.LedgerAddReq{ PositionId: positionId, BusinessId: businessId, //业务id TaskId: taskId, //任务id Types: types, //类型 Content: content, //内容 CreateWay: 1, //创建方式 1:人 2:系统 CreatePerson: createPerson, }) if err != nil { log.Println("save ledger err:", err) return false } if resp == nil { log.Println("save ledger resp is nil") return false } return resp.State }