package service import ( "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "bp.jydev.jianyu360.cn/BaseService/biService/entity" "bp.jydev.jianyu360.cn/BaseService/biService/rpc/pb" "database/sql" "encoding/json" "github.com/gogf/gf/v2/util/gconv" "go.mongodb.org/mongo-driver/bson" "time" ) // @Author jianghan // @Description 根据手机号查询线索, 线索不存在创建线索并插入记录 // @Date 2024/5/28 func CreateClue(in *pb.CreateCuleReq) (result *pb.BiReply, err error) { info := entity.JyBiTidb.FindOne("dwd_f_userbase_contacts", bson.M{"phone": in.Phone}, "id, baseinfo_id", "") if info == nil || len(*info) == 0 { return &pb.BiReply{ ErrorCode: -1, ErrorMsg: "手机号不存在", Data: nil, }, nil } uId := common.ObjToString((*info)["baseinfo_id"]) userId, userName := "", "" clueInfo := entity.JyBiTidb.FindOne("dwd_f_crm_clue_info", bson.M{"uid": uId}, "", "") if clueInfo != nil && len(*clueInfo) > 0 { return &pb.BiReply{ ErrorCode: -1, ErrorMsg: "线索已经存在", Data: nil, }, nil } userInfo := entity.JyBiTidb.FindOne("dwd_f_userbase_baseinfo", bson.M{"phone": in.Phone}, "userid,name", "") if userInfo != nil && len(*userInfo) > 0 { userId = common.ObjToString((*userInfo)["userid"]) userName = common.ObjToString((*userInfo)["name"]) } isGroup, isCommerce := CompanyType(in.Phone) //判断是否集团公司、工商库 clueId := int64(0) if entity.JyBiTidb.ExecTx("保存线索", func(tx *sql.Tx) bool { nowTime := time.Now().Format("2006-01-02 15:04:05") clueId = entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{ "userid": userId, "uid": uId, "is_assign": 0, "createtime": nowTime, "updatetime": nowTime, "cluename": in.Phone, "top_cluetype": "4", "sub_cluetype": "154", "trailstatus": "01", "name": userName, "phone": in.Phone, "company_nature": isGroup, "company_verification": isCommerce, "comeintime_open": nowTime, }) uodateId1 := entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": -1, "change_type": "创建线索", "new_value": "系统自动创建", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, "change_reason": "根据手机号创建线索(接口)", }) uodateId2 := entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "change_field": "top_cluetype", "position_id": -1, "change_type": "基本信息变更", "old_value": "/", "new_value": "新增注册", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, "change_reason": "根据手机号创建线索(接口)", }) uodateId3 := entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "change_field": "sub_cluetype", "position_id": 0, "change_type": "基本信息变更", "old_value": "/", "new_value": "新增注册用户", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, "change_reason": "根据手机号创建线索(接口)", }) return clueId > -1 && uodateId1 > -1 && uodateId2 > -1 && uodateId3 > -1 }) { return &pb.BiReply{ ErrorCode: 0, ErrorMsg: "线索创建成功", Data: gconv.Bytes(bson.M{"id": clueId}), }, nil } else { return &pb.BiReply{ ErrorCode: -1, ErrorMsg: "线索创建失败", Data: nil, }, nil } } // @Author jianghan // @Description 客成移交线索 // @Date 2024/5/28 func TransferClue(in *pb.TransferClueReq) (result *pb.BiReply, err error) { uId, entId, product := "", "", 0 query := make(map[string]interface{}) if in.OrderNo != "" { query["order_code"] = in.OrderNo } else if in.Phone != "" { query["phone"] = in.Phone } orderInfo := entity.JyBiTidb.FindOne("dwd_f_userbase_order_info", query, "", "") if orderInfo == nil || len(*orderInfo) == 0 { return &pb.BiReply{ ErrorCode: -1, ErrorMsg: "订单编号或手机号不存在", Data: nil, }, nil } uId = common.ObjToString((*orderInfo)["uid"]) company_name := common.ObjToString((*orderInfo)["company_name"]) starttime := common.ObjToString((*orderInfo)["vip_starttime"]) endtime := common.ObjToString((*orderInfo)["vip_endtime"]) t, _ := time.Parse(date.Date_Full_Layout, endtime) isExpired := false // 过期订单 if time.Now().Unix() > t.Unix() { return &pb.BiReply{ ErrorCode: 1, ErrorMsg: "订单已经过期", Data: nil, }, nil } if t.Unix() < time.Now().Unix()+3*30*24*3600 && t.Unix() > time.Now().Unix() { isExpired = true } buy_subject := common.IntAll((*orderInfo)["buy_subject"]) product_type := common.ObjToString((*orderInfo)["product_type"]) data_spec := common.ObjToString((*orderInfo)["data_spec"]) if buy_subject == 1 || buy_subject == 0 { entId = common.ObjToString((*orderInfo)["userid"]) } productMap := map[string]int{ "dhy6": 1, "dhy7": 2, "dhy3": 4, "dhy1": 5, "dhy2": 6, "dhy5": 7, "企业商机管理": 8, } if product_type == "企业商机管理" { product = productMap[product_type] } else { if data_spec == "dhy6" { product = 1 filter := common.ObjToString((*orderInfo)["filter"]) filterMap := map[string]interface{}{} json.Unmarshal([]byte(filter), &filterMap) if len(filterMap) > 0 { areaCount := common.IntAll(filterMap["areaCount"]) if areaCount == 1 { product = 3 } } } else { product = productMap[data_spec] } } clueData := entity.JyBiTidb.FindOne("dwd_f_crm_clue_info", bson.M{"uid": uId}, "", "") if clueData != nil && len(*clueData) > 0 { clueId := (*clueData)["id"] userName := common.ObjToString((*clueData)["name"]) nowTime := time.Now().Format("2006-01-02 15:04:05") csmdata := entity.JyBiTidb.FindOne("dwd_f_csm_customer_info", bson.M{"clue_id": clueId}, "", "") if csmdata != nil && len(*csmdata) > 0 { if common.IntAll((*csmdata)["is_transfer"]) == 0 { return &pb.BiReply{ ErrorCode: 0, ErrorMsg: "线索已经移交至客成", Data: gconv.Bytes(bson.M{"id": clueId}), }, nil } else { entity.JyBiTidb.Update("dwd_f_csm_customer_info", map[string]interface{}{"clue_id": clueId}, bson.M{ "is_transfer": 0, "is_renewal_protection": 0, "product_access": product, "buy_subject": buy_subject, "transfertime": nowTime, "service_starttime": starttime, "service_endtime": endtime, "ent_id": entId, "company_name": company_name, }) entity.JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": (*csmdata)["position_id"], "change_type": "成交客户移交", "new_value": "移交至客户成功组", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, "change_reason": "根据手机号或者订单号移交线索(接口)", }) entity.JyBiTidb.Update("dwd_f_crm_clue_info", bson.M{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName}) return &pb.BiReply{ ErrorCode: 0, ErrorMsg: "线索移交成功", Data: gconv.Bytes(bson.M{"id": clueId, "isExpired": isExpired}), }, nil } } else { saveMap := map[string]interface{}{ "clue_id": clueId, "transfertime": nowTime, "position_id": in.PositionId, "name": "", "service_starttime": starttime, "service_endtime": endtime, "ent_id": entId, "is_task": 1, "tasktime": nowTime, "taskstatus": 0, "tasksource": "1", "is_admin": 1, "product_access": product, "buy_subject": buy_subject, "relationship_building_way": 1, "inventory_way": 1, "training_way": 1, "is_pre_sales_training": 0, "service_stage": 1, "company_name": company_name, } cId, ok, updateId1 := int64(-1), false, int64(-1) if entity.JyBiTidb.ExecTx("保存客户", func(tx *sql.Tx) bool { cId = entity.JyBiTidb.InsertByTx(tx, "dwd_f_csm_customer_info", saveMap) ok = entity.JyBiTidb.UpdateByTx(tx, "dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, map[string]interface{}{"is_transfer": 1, "updatetime": nowTime, "name": userName}) updateId1 = entity.JyBiTidb.InsertByTx(tx, "dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "position_id": in.PositionId, "change_type": "成交客户移交", "new_value": "移交至客户成功组", "createtime": nowTime, "BCPCID": common.GetRandom(32), "operator_id": -1, "change_reason": "根据手机号或者订单号移交线索(接口)", }) return cId > -1 && ok && updateId1 > -1 }) { return &pb.BiReply{ ErrorCode: 0, ErrorMsg: "线索移交成功", Data: gconv.Bytes(bson.M{"id": clueId, "isExpired": isExpired}), }, nil } else { return &pb.BiReply{ ErrorCode: -1, ErrorMsg: "线索移交失败", Data: nil, }, nil } } } else { return &pb.BiReply{ ErrorCode: -1, ErrorMsg: "线索查询失败", Data: nil, }, nil } }