123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- package service
- import (
- "github.com/gogf/gf/v2/util/gconv"
- "log"
- "strconv"
- "strings"
- "time"
- "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/biservice"
- "github.com/zeromicro/go-zero/core/logx"
- )
- // 判断是不是集团公司 判断在不在工商库 return 1集团公司 2工商库 3都是 -1都不是
- func GetCompanyType(this *biservice.CompanyReq) int {
- status := -1
- if this.CompanyName == "" {
- return status
- }
- isGroup, isCommerce := CompanyType(this.CompanyName)
- //判断
- if isGroup && isCommerce {
- status = 3
- } else if isGroup {
- status = 1
- } else if isCommerce {
- status = 2
- }
- return status
- }
- func CompanyType(companyName string) (bool, bool) {
- isGroup, isCommerce := false, false
- //是否是集团
- if c := JyBiTidb.CountBySql(`select count(1) from group_company_name where company_name=?`, companyName); c > 0 {
- isGroup = true
- }
- //是否在工商库
- if c := MgoQyxy.Count("qyxy_std", map[string]interface{}{"company_name": companyName, "company_type": map[string]interface{}{"$ne": "个体工商户"}}); c > 0 {
- isCommerce = true
- }
- return isGroup, isCommerce
- }
- // 查询是否达上限
- func FindUpperLimit(positionId int64) bool {
- if positionId == 0 {
- return false
- }
- return JyBiTidb.CountBySql(`select count(1) from dwd_f_crm_clue_info where position_id=? and is_transfer != 1 `, positionId) >= 400
- }
- // CompanyCount 分配的数量
- // CompanyCounts 目前数量
- // ClueCount 线索的数量
- func DistributeClueShow(this *biservice.DistributeClueShowReq, allocationCap int64) *biservice.DistributeClueShowResp {
- rdata := []*biservice.DistributeClueShowss{}
- pArr := []string{}
- for _, c := range this.Datas { //初始化返回数据
- m := &biservice.DistributeClueShowss{}
- m.PositionId = c.PositionId
- m.ClueCount = 0
- m.CompanyCounts = 0
- //现有数据查询
- existingCount := JyBiTidb.CountBySql(`select count(1) from dwd_f_crm_clue_info where position_id=? and is_assign=1 and is_transfer != 1 `, m.PositionId)
- m.ExistingCount = existingCount
- if existingCount >= allocationCap {
- if this.DataType != 1 {
- return &biservice.DistributeClueShowResp{
- ErrorCode: 1,
- ErrorMsg: "超出私海线索上限",
- }
- } else {
- continue
- }
- }
- if this.DataType == 1 {
- m.CompanyCount = 0
- m.MinCount = 0
- } else {
- if c.DistributedCount <= 0 {
- continue
- }
- m.CompanyCount = c.DistributedCount
- }
- rdata = append(rdata, m)
- pArr = append(pArr, strconv.FormatInt(c.PositionId, 10))
- }
- if len(rdata) == 0 {
- return &biservice.DistributeClueShowResp{
- ErrorCode: 0,
- Data: rdata,
- }
- }
- clueArrs1 := []string{}
- clueArrs1 = strings.Split(this.ClueIdList, ",")
- logx.Info("clueArrs1 ", clueArrs1)
- for _, v := range strings.Split(this.ClueIdList, ",") {
- count := 1
- id := v
- minCount, minIndex := int64(0), -1
- isindexok := false
- k1 := 0
- for _, r := range rdata {
- if !r.FullStatus {
- if this.DataType == 1 {
- if k1 == 0 {
- minCount = r.CompanyCounts
- break
- }
- } else {
- if r.CompanyCounts < r.CompanyCount {
- minCount = r.CompanyCounts
- }
- }
- }
- }
- for _, r := range rdata {
- if !r.FullStatus {
- if this.DataType == 1 {
- if r.CompanyCounts < minCount {
- minCount = r.CompanyCounts
- }
- } else {
- if r.CompanyCounts < minCount && r.CompanyCounts < r.CompanyCount {
- minCount = r.CompanyCounts
- }
- }
- }
- }
- for k, r := range rdata {
- countNumber := r.ExistingCount + r.ClueCount + int64(count)
- if this.DataType == 1 {
- if countNumber <= allocationCap {
- if r.CompanyCounts <= minCount {
- minCount = r.CompanyCounts
- minIndex = k
- }
- }
- } else {
- if countNumber <= allocationCap {
- if r.CompanyCounts <= minCount && r.CompanyCounts < r.CompanyCount {
- minCount = r.CompanyCounts
- minIndex = k
- }
- }
- }
- }
- if minIndex == -1 {
- continue
- }
- if this.DataType == 1 {
- countNumber := rdata[minIndex].ExistingCount + rdata[minIndex].ClueCount + int64(count)
- if countNumber == allocationCap {
- rdata[minIndex].FullStatus = true
- }
- rdata[minIndex].CompanyCount++
- rdata[minIndex].CompanyCounts++
- rdata[minIndex].ClueCount += int64(count)
- } else {
- countNumber := rdata[minIndex].ExistingCount + rdata[minIndex].ClueCount + int64(count)
- if rdata[minIndex].CompanyCounts < rdata[minIndex].CompanyCount {
- isindexok = true
- rdata[minIndex].CompanyCounts++
- rdata[minIndex].ClueCount += int64(count)
- if countNumber == allocationCap {
- rdata[minIndex].FullStatus = true
- }
- }
- }
- if this.DataType == 3 && isindexok {
- dclue(gconv.Int64(id), rdata[minIndex].PositionId, this.IsTask, this.PositionId)
- log.Println("3333", id, rdata[minIndex].PositionId)
- }
- }
- /*if this.DataType == 2 {
- for _, v := range rdata {
- if this.UpdatePositionID == v.PositionId {
- if v.CompanyCounts < v.CompanyCount {
- return &biservice.DistributeClueShowResp{
- ErrorCode: 1,
- ErrorMsg: "超出私海线索上限",
- }
- }
- }
- }
- }*/
- return &biservice.DistributeClueShowResp{
- ErrorCode: 0,
- Data: rdata,
- }
- }
- func dclue(v, positionId, isTask, thispositionId int64) {
- clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, "", "")
- nowTime := time.Now().Format(date.Date_Full_Layout)
- if clueData != nil && len(*clueData) > 0 {
- isAssign := common.IntAll((*clueData)["is_assign"])
- clueSeatNumber := common.ObjToString((*clueData)["seatNumber"])
- oldName, seatNumber, name := "", "", ""
- trailstatus := common.ObjToString((*clueData)["trailstatus"])
- saleData := JyBiTidb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number != "" and seat_number is not null`)
- if saleData != nil && len(*saleData) > 0 {
- for _, v := range *saleData {
- if common.ObjToString(v["seat_number"]) == clueSeatNumber {
- oldName = common.ObjToString(v["name"])
- }
- if common.Int64All(v["position_id"]) == positionId {
- seatNumber = common.ObjToString(v["seat_number"])
- name = common.ObjToString(v["name"])
- }
- }
- }
- if isAssign == 1 {
- oldpositionId := common.Int64All((*clueData)["position_id"])
- updateClue := map[string]interface{}{
- "position_id": positionId,
- "seatNumber": seatNumber,
- "is_assign": 1,
- "updatetime": nowTime,
- "comeintime": nowTime,
- "comeinsource_private": 4,
- "level_open": nil,
- "clue_level": nil,
- "out_task_time": nil,
- "out_task_status": nil,
- }
- if oldName != name {
- updateClue["start_trail_time"] = nil
- updateClue["next_trail_time"] = nil
- }
- if isTask == int64(1) {
- updateClue["is_task"] = 1
- updateClue["task_time"] = nowTime
- updateClue["tasktime"] = nowTime
- updateClue["taskstatus"] = 0
- updateClue["tasksource"] = "主动分配客户"
- JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": v,
- "position_id": positionId,
- "change_type": "加入任务车",
- "new_value": "主动分配客户",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": thispositionId,
- })
- }
- ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
- if ok {
- JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": v,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": oldName,
- "new_value": name,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": thispositionId,
- })
- //标签变更
- label := gconv.Int64((*clueData)["label"])
- taskstatus := gconv.Int64(updateClue["taskstatus"])
- if isTask == int64(1) {
- taskstatus = 0
- }
- LabelHandel(nil, label, 3, v, "", trailstatus, trailstatus, name, oldName, taskstatus)
- JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": v,
- "position_id": oldpositionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": CodeTrail[trailstatus],
- "new_value": "流失",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": thispositionId,
- })
- JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": v,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": CodeTrail[trailstatus],
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": thispositionId,
- })
- } else {
- logx.Info("私海修改失败 ", v, positionId, seatNumber)
- }
- } else {
- updateClue := map[string]interface{}{
- "position_id": positionId,
- "seatNumber": seatNumber,
- "is_assign": 1,
- "updatetime": nowTime,
- "comeintime": nowTime,
- "comeinsource_private": 4,
- "is_task": 0,
- "taskstatus": 0,
- "level_open": nil,
- "clue_level": nil,
- "out_task_time": nil,
- "out_task_status": nil,
- "next_trail_time": nil,
- "start_trail_time": nil,
- // "comeinsource_open": nil,
- }
- updateClue["trailstatus"] = "01"
- if isTask == int64(1) {
- updateClue["is_task"] = 1
- updateClue["task_time"] = nowTime
- updateClue["tasktime"] = nowTime
- updateClue["taskstatus"] = 0
- updateClue["tasksource"] = "主动分配客户"
- JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": v,
- "position_id": positionId,
- "change_type": "加入任务车",
- "new_value": "主动分配客户",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": thispositionId,
- })
- }
- ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
- if ok {
- JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": v,
- "position_id": positionId,
- "change_field": "position_id",
- "change_type": "所属人变更",
- "old_value": "/",
- "new_value": name,
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": thispositionId,
- })
- label := gconv.Int64((*clueData)["label"])
- taskstatus := gconv.Int64(updateClue["taskstatus"])
- if isTask == int64(1) {
- taskstatus = 0
- }
- LabelHandel(nil, label, 3, v, "", "01", trailstatus, name, "/", taskstatus)
- JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": v,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": "商机线索",
- "new_value": "新增",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": thispositionId,
- })
- JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
- "clue_id": v,
- "position_id": positionId,
- "change_field": "trailstatus",
- "change_type": "基本信息变更",
- "old_value": CodeTrail[trailstatus],
- "new_value": "商机线索",
- "createtime": nowTime,
- "BCPCID": common.GetRandom(32),
- "operator_id": thispositionId,
- })
- } else {
- logx.Info("私海插入失败 ", v, positionId, seatNumber)
- }
- }
- }
- }
|