package main import ( "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/mongodb" "fmt" "github.com/gin-gonic/gin" "github.com/gogf/gf/v2/util/gconv" "log" "net/http" "strings" "time" ) func setupRouter() *gin.Engine { r := gin.Default() // 添加中间件(如认证、日志等) r.Use(gin.Logger()) r.Use(gin.Recovery()) api := r.Group("/clue") { api.POST("/ClueBefore", clueBefore) api.POST("/GetClueState", getClueState) } return r } type FetchDeptAdminRequest struct { PositionId string `json:"position_id"` SourceDesc string `json:"source_desc"` Action string `json:"action"` } type FetchDeptAdminResponse struct { Name string `json:"name"` Wxer string `json:"wxer"` Phone string `json:"phone"` } // 留资以前处理 func clueBefore(c *gin.Context) { var req FetchDeptAdminRequest var returnData FetchDeptAdminResponse if err := c.ShouldBindJSON(&req); err != nil { log.Println(err) c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求参数"}) return } log.Println("clueBefore", req) if req.Action == "" { req.Action = "saleLeads" } kcPositionId, positionId, is_transfer, belong_to := int64(0), int64(0), int64(0), "" nowTime := time.Now().Format("2006-01-02 15:04:05") //人员信息查询 uId, userId, source, belong_to, phone, cluename := processUserInfo(req) if cluename == "" { cluename = phone } clueData := &map[string]interface{}{} isGroup, isCommerce := 0, 0 if uId != "" { isGroup, isCommerce, kcPositionId, is_transfer, clueData = processClueInfo(uId, cluename) if clueData != nil { positionId = gconv.Int64((*clueData)["position_id"]) } } else { if uId == "" || phone == "" { data := TiDb.FindOne("temporary_assignment", map[string]interface{}{ "userId": userId, "status": 0, }, "", "") if data != nil && len(*data) > 0 { positionId = gconv.Int64((*data)["positionId"]) returnData.Phone, returnData.Wxer, returnData.Name = GetPersonSubsidiary(positionId) log.Println(returnData) c.JSON(http.StatusOK, returnData) return } } belong_to = GetUserChannel(userId) } if source == "0104" || strings.HasPrefix(belong_to, "02") || source == "0102" || source == "0103" || phone == "" || strings.HasPrefix(belong_to, "03") { c.JSON(http.StatusOK, returnData) return } top_cluetype, sub_cluetype, level, topname, subname := getCodeId(req.Action, req.SourceDesc) seatNumber, saleName, saleData, pIsOk, isFreeze, noticePositionId := "", "", []map[string]interface{}{}, true, true, int64(0) if positionId != 0 { //线索上边已经有人了 personnel := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{ "position_id": positionId, "resign": 0, "ent_Id": db.EntId, }, "", "") if personnel != nil && len(*personnel) > 0 { seatNumber = gconv.String((*personnel)["seat_number"]) saleName = gconv.String((*personnel)["name"]) saleData = *(TiDb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number is not null and seat_number != ""`)) } } else { //需要查找一个人 positionId, seatNumber, saleName, saleData, pIsOk, isFreeze, noticePositionId = autoDraw(userId, level, cluename, phone, isGroup, isCommerce) } //用户没有创建 //用户创建没有手机号 if uId == "" || phone == "" { //加一条记录分配 TiDb.Insert("temporary_assignment", map[string]interface{}{ "userId": userId, "positionId": positionId, "seatNumber": seatNumber, "saleName": saleName, "status": 0, }) returnData.Phone, returnData.Wxer, returnData.Name = GetPersonSubsidiary(positionId) log.Println(returnData) c.JSON(http.StatusOK, returnData) return } log.Println("data -------", positionId, seatNumber, saleName, pIsOk, isFreeze, noticePositionId) inData := Clue{ Item: req.Action, UserId: userId, UId: uId, TopCluetype: top_cluetype, SubCluetype: sub_cluetype, Topname: topname, Subname: subname, Cluename: cluename, SaleName: saleName, Phone: phone, SeatNumber: seatNumber, Level: level, PositionId: positionId, Source: "", SourceCode: "", BelongTo: belong_to, IsGroup: isGroup, IsCommerce: isCommerce, PIsOk: pIsOk, Demand: "", IsFreeze: isFreeze, } if positionId > 0 { //找到分配人了 oks := true if clueData != nil && len(*clueData) > 0 { isUpdate := gconv.Int64((*clueData)["is_artificially_modified"]) if isUpdate == 1 { cluename = "" } oks = UpdateClue(*clueData, saleData, inData) } else { //不存在走新增 oks = SaveClue(inData) if oks { //新增成功,销售分配次数+1 TiDb.UpdateOrDeleteBySql(`update dwd_f_crm_clue_autodraw_record set count = count + 1 where position_id = ? and clue_level = ?`, positionId, level) } } if is_transfer == 1 { returnData.Phone, returnData.Wxer, returnData.Name = GetPersonSubsidiary(kcPositionId) } else { returnData.Phone, returnData.Wxer, returnData.Name = GetPersonSubsidiary(positionId) } } else { if isFreeze { //达上限放公海处理 uCount, oks := TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, "", ""), true //查当前线索是否已存在 if uCount == nil || len(*uCount) == 0 { oks = SaveClue0(inData) log.Println(oks) } else { //留资线索更新 if req.Action != "orders" && req.Action != "users" { //新用户和订单之外的替换一下新的留资信息 BCPCID := common.GetRandom(32) clueId := common.Int64All((*uCount)["id"]) old_cluename := gconv.String((*uCount)["cluename"]) old_top_cluetype := gconv.String((*uCount)["top_cluetype"]) old_sub_cluetype := gconv.String((*uCount)["sub_cluetype"]) label := gconv.Int64((*uCount)["label"]) trailstatusCode := gconv.String((*uCount)["trailstatus"]) clueUpdateData := map[string]interface{}{} e_label := gconv.String((*uCount)["e_label"]) Ehandle(clueId, uId, userId, cluename, trailstatusCode, e_label) if old_cluename != cluename && cluename != "" { clueUpdateData["cluename"] = cluename //新的线索名不为空才替换 TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "change_field": "cluename", "change_type": "基本信息变更", "old_value": common.If(old_cluename != "", old_cluename, "/"), "new_value": common.If(cluename != "", cluename, "/"), "createtime": nowTime, "BCPCID": BCPCID, "position_id": noticePositionId, "operator_id": -1}) } old_topname := "" old_subname := "" if old_top_cluetype != "" { pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_top_cluetype}, "", "") if pcodeData != nil && len(*pcodeData) > 0 { old_topname = gconv.String((*pcodeData)["name"]) } } if old_sub_cluetype != "" { pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": old_sub_cluetype}, "", "") if pcodeData != nil && len(*pcodeData) > 0 { old_subname = gconv.String((*pcodeData)["name"]) } } if old_top_cluetype != top_cluetype && top_cluetype != "" { clueUpdateData["top_cluetype"] = top_cluetype //新的线索名不为空才替换 TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "change_field": "top_cluetype", "position_id": noticePositionId, "change_type": "基本信息变更", "old_value": common.If(old_topname != "", old_topname, "/"), "new_value": common.If(topname != "", topname, "/"), "createtime": nowTime, "BCPCID": BCPCID, "operator_id": -1}) } if old_sub_cluetype != sub_cluetype && sub_cluetype != "" { clueUpdateData["sub_cluetype"] = sub_cluetype //新的线索名不为空才替换 TiDb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{ "clue_id": clueId, "change_field": "sub_cluetype", //111 "position_id": noticePositionId, "change_type": "基本信息变更", "old_value": common.If(old_subname != "", old_subname, "/"), "new_value": common.If(subname != "", subname, "/"), "createtime": nowTime, "BCPCID": BCPCID, "operator_id": -1}) //标签处理 LabelHandel(nil, label, 2, uId, level, trailstatusCode, gconv.String(clueUpdateData["trailstatus"]), 1) } clueUpdateData["company_nature"] = isGroup clueUpdateData["company_verification"] = isCommerce TiDb.Update("dwd_f_crm_clue_info", map[string]interface{}{"uid": uId}, clueUpdateData) } } //某人已经满了处理 AssFail(noticePositionId, cluename, phone) //查找人 } if is_transfer == 1 { returnData.Phone, returnData.Wxer, returnData.Name = GetPersonSubsidiary(kcPositionId) } else { returnData.Phone, returnData.Wxer, returnData.Name = GetPersonSubsidiary(positionId) } } log.Println(returnData) c.JSON(http.StatusOK, returnData) return } // 来源获取 func getCodeId(item, name string) (pcode, code, level, topname, subname string) { switch item { case "user": pcode = "4" code = "154" level = "C" topname = "新增注册" subname = "新增注册用户" default: codeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"name": name}, "", "") if codeData != nil && len(*codeData) > 0 { pcode = common.ObjToString((*codeData)["pcode"]) code = common.ObjToString((*codeData)["code"]) level = common.ObjToString((*codeData)["clue_level"]) subname = name pcodeData := TiDb.FindOne("dwd_d_crm_cluetype_code", map[string]interface{}{"code": pcode}, "", "") if pcodeData != nil && len(*pcodeData) > 0 { topname = common.ObjToString((*pcodeData)["name"]) } } } return } // 查找人员附属信息(工作手机号,二维码) func GetPersonSubsidiary(positionId int64) (string, string, string) { //手机号获取 phone, qrCode, name := "", "", "" personnel := TiDb.FindOne("dwd_f_crm_personnel_management", map[string]interface{}{ "position_id": positionId, "resign": 0, "ent_Id": db.EntId, }, "", "") if personnel != nil && len(*personnel) > 0 { phone = gconv.String((*personnel)["phone"]) name = gconv.String((*personnel)["name"]) } //二维码获取 if name != "" { qrData := Mysql.SelectBySql(`SELECT b.qr_code FROM wx_qy_user a INNER JOIN wx_qy_code b ON a.user_name like ? AND a.config_id = b.config_id AND b.is_del = 0 ORDER BY b.create_date DESC`, fmt.Sprintf("%%%s%%", name)) if qrData != nil && len(*qrData) > 0 { qrCode = gconv.String((*qrData)[0]["qr_code"]) } } return phone, qrCode, name } // 处理用户信息查询 func processUserInfo(in FetchDeptAdminRequest) (uId, userId, source, belong_to, phone, companyName string) { userMap := &map[string]interface{}{} if mongodb.IsObjectIdHex(in.PositionId) { //个人 select * from data_service.user_system where userid=? limit 0,1 [65378f1f060d58518d50fb85] userMap = TiDb.FindOne("data_service.user_system", map[string]interface{}{ "userid": in.PositionId, "status": 1, }, "", "") } else { //企业 userMap = TiDb.FindOne("data_service.user_system", map[string]interface{}{ "position_id": in.PositionId, "status": 1, }, "", "") } if userMap == nil || len(*userMap) == 0 { //查询用户原始表 userData := &[]map[string]interface{}{} if mongodb.IsObjectIdHex(in.PositionId) { userData, _ = Mgo.Find("user", map[string]interface{}{"_id": mongodb.StringTOBsonId(in.PositionId)}, ``, `{"_id":1,"s_phone":1,"s_m_phone":1}`, false, -1, -1) } else { sql := `select b.id from base_position a INNER JOIN base_user b on a.id =? and a.user_id=b.id` baseData := Base.SelectBySql(sql, in.PositionId) if baseData == nil || len(*baseData) == 0 { return } userData, _ = Mgo.Find("user", map[string]interface{}{"base_user_id": gconv.Int64((*baseData)[0]["id"])}, ``, `{"_id":1,"s_phone":1,"s_m_phone":1}`, false, -1, -1) } if userData == nil || len(*userData) == 0 { return } userId = mongodb.BsonIdToSId((*userData)[0]["_id"]) phone = gconv.String((*userData)[0]["s_phone"]) if phone == "" { phone = gconv.String((*userData)[0]["s_m_phone"]) } return } else { uId = gconv.String((*userMap)["uid"]) userId = gconv.String((*userMap)["userid"]) userBaseData := TiDb.FindOne("dwd_f_userbase_baseinfo", map[string]interface{}{ "uid": uId, "status": 1, }, "", "") if userBaseData != nil && len(*userBaseData) > 0 { source = gconv.String((*userBaseData)["source"]) belong_to = gconv.String((*userBaseData)["belong_to"]) phone = gconv.String((*userBaseData)["phone"]) companyName = gconv.String((*userBaseData)["company_name"]) } } return } // 处理线索信息 func processClueInfo(uId, cluename string) (isGroup, isCommerce int, kcPositionId, is_transfer int64, clueData *map[string]interface{}) { isGroup, isCommerce = GetCompanyType(cluename, uId) //查询一下是否在客成 clueData = TiDb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{ "uid": uId, }, "", "") if clueData != nil && len(*clueData) > 0 { clueId := gconv.Int64((*clueData)["id"]) is_transfer = gconv.Int64((*clueData)["is_transfer"]) if is_transfer == 1 { //在客成 customData := TiDb.FindOne("dwd_f_csm_customer_info", map[string]interface{}{ "clue_id": clueId, }, "", "") if customData != nil && len(*customData) > 0 { kcPositionId = gconv.Int64((*customData)["position_id"]) } } } return } // 线索状态查询 func getClueState(c *gin.Context) { // 1. 参数绑定和验证 positionId := int64(0) var req FetchDeptAdminRequest response := FetchDeptAdminResponse{} if err := c.ShouldBindJSON(&req); err != nil { log.Println(err) c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求参数"}) return } log.Println("getClueState", req) // 2. 处理用户信息 uId, userId, source, belong_to, _, _ := processUserInfo(req) if source == "0104" || strings.HasPrefix(belong_to, "02") || source == "0102" || source == "0103" || strings.HasPrefix(belong_to, "03") { c.JSON(http.StatusOK, response) return } if uId == "" { data := TiDb.FindOne("temporary_assignment", map[string]interface{}{ "userId": userId, "status": 0, }, "", "") if data != nil && len(*data) > 0 { positionId = gconv.Int64((*data)["positionId"]) response.Phone, response.Wxer, response.Name = GetPersonSubsidiary(positionId) log.Println(response) c.JSON(http.StatusOK, response) return } } // 3. 处理线索信息 _, _, kcPositionId, isTransfer, clueData := processClueInfo(uId, "") // 4. 根据是否转移获取不同数据 if clueData != nil { positionId = gconv.Int64((*clueData)["position_id"]) } else { data := TiDb.FindOne("temporary_assignment", map[string]interface{}{ "userId": userId, "status": 0, }, "", "") if data != nil { positionId = gconv.Int64((*data)["positionId"]) } } if isTransfer == 1 { response.Phone, response.Wxer, response.Name = GetPersonSubsidiary(kcPositionId) } else { response.Phone, response.Wxer, response.Name = GetPersonSubsidiary(positionId) } // 5. 返回响应 log.Println(response) c.JSON(http.StatusOK, response) }