package service import ( "context" "database/sql" "fmt" "log" "time" "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" 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 []string //转办用户 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) args := []interface{}{} argsTask := []interface{}{} //判断处理方式 //转办 if this.Types == 2 { for _, v := range this.User { entuserid := encrypt.SE.Decode4Hex(v) i_entuserid := gconv.Int64(entuserid) resp, err := cm.UserCenterRpc.IdentityByEntUserId(ctx, &pb.IdentityReq{ Id: i_entuserid, }) if err != nil { log.Println("获取用户职位id信息出错", i_entuserid, "的信息出错", err) return 0 } else if resp == nil { log.Println("entuser用户", i_entuserid, "没有找到职位信息") return 0 } positionid := resp.PositionId //线索 //args = append(args, positionid, this.EntId, i_entuserid, this.EmployInfoId, this.EmployCustomId, this.Types, this.CustomAllName, this.CustomAbbreviation, this.Summary, this.CustomLevel, this.CustomIndustry, this.CustomDetailIndustry, this.Province, this.City, this.District, this.Address, this.CompanyPhone, this.Remarks, nowtime) //任务 //argsTask = append(argsTask, positionid, this.EntId, this.EntUserId, this.ClueName+"的跟进任务", this.CluesSource, this.EmployInfoId, positionid, 1, this.PositionId, nowtime, 1) if !SaveCustom(ctx, args, argsTask, this.EmployInfoId, this.EmployCustomId, positionid, this.CreateName) { return 0 } } } 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, this.CustomLevel, this.CustomIndustry, this.CustomDetailIndustry, this.Province, this.City, this.District, this.Address, this.CompanyPhone, this.Remarks, this.CreateName, nowtime) //任务 argsTask = append(argsTask, this.PositionId, this.EntId, this.EntUserId, this.CustomAllName+"的跟进任务", 3, this.PositionId, 1, nowtime, 1, nextFollowTime) //存库 if !SaveCustom(ctx, args, argsTask, this.EmployInfoId, this.EmployCustomId, this.PositionId, this.CreateName) { return 0 } } 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 } //EmployCustomUpdate 是否创建客户修改 func CustomUpdate(tx *sql.Tx, employ_info_id, employ_custom_id, positionId int64) 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{}{"is_create_custom": 1}) } func SaveCustom(ctx context.Context, argsCustom, argsTask []interface{}, employ_info_id, employ_custom_id, positionId int64, createPerson string) bool { //存库 return cm.CrmMysql.ExecTx("创建客户", func(tx *sql.Tx) bool { //插入客户 customId := SaleCustomAdd(tx, argsCustom) //传过来的argTask没有来源id,需要append argsTask = append(argsTask, customId) //任务车存储 taskId := TaskAdd(tx, argsTask) ok := CustomUpdate(tx, employ_info_id, employ_custom_id, positionId) //插入台账 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 }) } //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 }