123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- package service
- import (
- "strconv"
- "strings"
- "sync"
- "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}); c > 0 {
- isCommerce = true
- }
- return isGroup, isCommerce
- }
- func DistributeClueShow(this *biservice.DistributeClueShowReq) *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
- if this.DataType == 1 {
- m.CompanyCount = 0
- m.MinCount = 0
- } else {
- m.CompanyCount = c.DistributedCount
- }
- rdata = append(rdata, m)
- pArr = append(pArr, strconv.FormatInt(c.PositionId, 10))
- }
- clueArrs1 := []string{}
- clueArrs := []string{}
- clueArrSync := &sync.Mutex{}
- sql1 := `SELECT b.cluename,b.position_id,count(*) as count FROM dwd_f_crm_clue_info a INNER JOIN (SELECT cluename,position_id FROM dwd_f_crm_clue_info WHERE position_id in (` + strings.Join(pArr, ",") + `) AND id not in (` + this.ClueIdList + `) AND company_nature = 0 AND company_verification = 1 GROUP BY cluename) AS b ON a.cluename = b.cluename WHERE id in (` + this.ClueIdList + `) GROUP BY cluename`
- logx.Info("sql1 ", sql1)
- sdata := JyBiTidb.SelectBySql(sql1) //查这一批线索中属于当前分配人员的线索提前分配数量
- if sdata != nil && len(*sdata) > 0 {
- for _, v := range *sdata {
- sPositionId := common.Int64All(v["position_id"])
- count := common.IntAll(v["count"])
- for k, r := range rdata {
- if r.PositionId == sPositionId {
- if this.DataType == 1 {
- rdata[k].CompanyCount++
- rdata[k].MinCount++
- rdata[k].CompanyCounts++
- rdata[k].ClueCount += int64(count)
- } else {
- if rdata[k].CompanyCounts < rdata[k].CompanyCount {
- rdata[k].CompanyCounts++
- rdata[k].ClueCount += int64(count)
- }
- }
- }
- }
- }
- }
- sql2 := `SELECT a.id,b.position_id FROM dwd_f_crm_clue_info a INNER JOIN (SELECT id,cluename,position_id FROM dwd_f_crm_clue_info WHERE position_id in (` + strings.Join(pArr, ",") + `) AND id not in (` + this.ClueIdList + `) AND company_nature = 0 AND company_verification = 1 GROUP BY cluename) AS b ON a.cluename = b.cluename WHERE a.id in (` + this.ClueIdList + `)`
- logx.Info("sql2 ", sql2) //查这一批线索中属于当前分配人员的线索id,分出去,并且排除掉
- ldata := JyBiTidb.SelectBySql(sql2)
- if ldata != nil && len(*ldata) > 0 {
- wg := new(sync.WaitGroup)
- ch := make(chan bool, 20)
- for _, a := range strings.Split(this.ClueIdList, ",") {
- isOk := true
- wg.Add(1)
- ch <- true
- go func(a string, isOk bool) {
- defer func() {
- wg.Done()
- <-ch
- }()
- for _, v := range *ldata {
- id := common.Int64All(v["id"])
- positionId := common.Int64All(v["position_id"])
- ids := strconv.FormatInt(id, 10)
- if a == ids {
- isOk = false
- if this.DataType == 3 {
- dclue(id, positionId, this.IsTask, this.PositionId) //分线索
- }
- }
- }
- if isOk {
- clueArrSync.Lock()
- clueArrs = append(clueArrs, a)
- clueArrSync.Unlock()
- }
- }(a, isOk)
- }
- wg.Wait()
- } else {
- clueArrs = strings.Split(this.ClueIdList, ",")
- }
- logx.Info("clueArrs ", clueArrs)
- if len(clueArrs) > 0 {
- sql0 := `SELECT a.cluename,a.id,b.position_id FROM dwd_f_crm_clue_info a INNER JOIN (SELECT a.cluename,a.position_id FROM dwd_f_crm_clue_info a INNER JOIN dwd_f_crm_personnel_management b on a.position_id = b.position_id WHERE a.cluename in (SELECT cluename FROM dwd_f_crm_clue_info WHERE id in (` + strings.Join(clueArrs, ",") + `) GROUP BY cluename) AND a.id not in (` + strings.Join(clueArrs, ",") + `) AND b.resign = 1 GROUP BY a.cluename) as b on b.cluename = a.cluename where a.id in (` + strings.Join(clueArrs, ",") + `)`
- logx.Info("sql0", sql0)
- mdata := JyBiTidb.SelectBySql(sql0)
- if mdata != nil && len(*mdata) > 0 {
- nameMap := map[string][]int64{}
- namePMap := map[string]int64{}
- idMap := map[string]bool{}
- for _, v := range *mdata {
- cluename := common.ObjToString(v["cluename"])
- id := common.Int64All(v["id"])
- positionId := common.Int64All(v["position_id"])
- nameMap[cluename] = append(nameMap[cluename], id)
- namePMap[cluename] = positionId
- ids := strconv.FormatInt(id, 10)
- idMap[ids] = true
- }
- logx.Info("nameMap ", nameMap)
- logx.Info("namePMap ", namePMap)
- for _, v := range clueArrs {
- if !idMap[v] {
- clueArrs1 = append(clueArrs1, v)
- }
- }
- for k, v := range nameMap {
- ppdata := JyBiTidb.SelectBySql(`SELECT b.name,b.position_id,b.seat_number from dwd_d_crm_department_level_succbi a INNER JOIN dwd_f_crm_personnel_management b on a.position_id = b.position_id where a.bi_pcode = (SELECT bi_pcode from dwd_d_crm_department_level_succbi where position_id = ?) AND b.position_id != ?`, namePMap[k], namePMap[k])
- if ppdata != nil && len(*ppdata) > 0 {
- ppMap := map[int64]int{}
- for _, vv := range *ppdata {
- positionId := common.Int64All(vv["position_id"])
- ppMap[positionId] = 0
- }
- isOk, sindex, paid := false, 0, int64(0)
- L:
- for pa, _ := range ppMap {
- if pa > 0 {
- for n, r := range rdata {
- if r.PositionId == pa {
- if this.DataType == 1 {
- isOk = true
- sindex = n
- paid = pa
- break L
- } else {
- if rdata[n].CompanyCounts < rdata[n].CompanyCount {
- isOk = true
- sindex = n
- paid = pa
- break L
- }
- }
- }
- }
- }
- }
- logx.Info("RDATA ", rdata[sindex].CompanyCount, rdata[sindex].MinCount, rdata[sindex].CompanyCounts, rdata[sindex].ClueCount, len(v))
- if isOk {
- if this.DataType == 1 {
- rdata[sindex].CompanyCount++
- rdata[sindex].MinCount++
- rdata[sindex].CompanyCounts++
- rdata[sindex].ClueCount += int64(len(v))
- } else {
- rdata[sindex].CompanyCounts++
- rdata[sindex].ClueCount += int64(len(v))
- if this.DataType == 3 {
- for _, vv := range v {
- dclue(vv, paid, this.IsTask, this.PositionId) //分线索
- }
- }
- }
- }
- logx.Info("RDATA2 ", rdata[sindex].CompanyCount, rdata[sindex].MinCount, rdata[sindex].CompanyCounts, rdata[sindex].ClueCount, len(v))
- }
- }
- } else {
- clueArrs1 = clueArrs
- }
- }
- logx.Info("clueArrs1 ", clueArrs1)
- if len(clueArrs1) > 0 {
- sql3 := `SELECT cluename,COUNT(*) AS count FROM dwd_f_crm_clue_info WHERE id in (` + strings.Join(clueArrs1, ",") + `) GROUP BY cluename ORDER BY count desc`
- logx.Info("sql3 ", sql3) //从大到小排列这一列线索名称分组及数量情况
- cdata := JyBiTidb.SelectBySql(sql3)
- if cdata != nil && len(*cdata) > 0 {
- for _, v := range *cdata {
- count := common.IntAll(v["count"])
- cluename := common.ObjToString(v["cluename"])
- minCount, minIndex := int64(0), 0
- for k, r := range rdata {
- if this.DataType == 1 {
- if k == 0 {
- minCount = r.CompanyCounts
- }
- } else {
- if r.CompanyCounts < r.CompanyCount {
- minCount = r.CompanyCounts
- }
- }
- }
- for _, r := range rdata {
- 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 {
- if this.DataType == 1 {
- if r.CompanyCounts <= minCount {
- minCount = r.CompanyCounts
- minIndex = k
- }
- } else {
- if r.CompanyCounts <= minCount && r.CompanyCounts < r.CompanyCount {
- minCount = r.CompanyCounts
- minIndex = k
- }
- }
- }
- if this.DataType == 1 {
- rdata[minIndex].CompanyCount++
- rdata[minIndex].CompanyCounts++
- rdata[minIndex].ClueCount += int64(count)
- } else {
- if rdata[minIndex].CompanyCounts < rdata[minIndex].CompanyCount {
- rdata[minIndex].CompanyCounts++
- rdata[minIndex].ClueCount += int64(count)
- }
- }
- if this.DataType == 3 {
- iddata := JyBiTidb.SelectBySql(`SELECT id FROM dwd_f_crm_clue_info WHERE cluename = "` + cluename + `" and id in (` + strings.Join(clueArrs1, ",") + `)`)
- if iddata != nil { //分线索
- wg := new(sync.WaitGroup)
- ch := make(chan bool, 10)
- for _, i := range *iddata {
- wg.Add(1)
- ch <- true
- go func(i map[string]interface{}) {
- defer func() {
- wg.Done()
- <-ch
- }()
- id := common.Int64All(i["id"])
- dclue(id, rdata[minIndex].PositionId, this.IsTask, this.PositionId)
- }(i)
- }
- wg.Wait()
- }
- }
- }
- }
- }
- 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,
- })
- 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,
- }
- if trailstatus != "08" {
- 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,
- })
- if trailstatus != "08" {
- 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)
- }
- }
- }
- }
|