company.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package service
  2. import (
  3. "strconv"
  4. "strings"
  5. "sync"
  6. "time"
  7. "app.yhyue.com/moapp/jybase/common"
  8. "app.yhyue.com/moapp/jybase/date"
  9. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  10. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. // 判断是不是集团公司 判断在不在工商库 return 1集团公司 2工商库 3都是 -1都不是
  14. func GetCompanyType(this *biservice.CompanyReq) int {
  15. status := -1
  16. if this.CompanyName == "" {
  17. return status
  18. }
  19. isGroup, isCommerce := CompanyType(this.CompanyName)
  20. //判断
  21. if isGroup && isCommerce {
  22. status = 3
  23. } else if isGroup {
  24. status = 1
  25. } else if isCommerce {
  26. status = 2
  27. }
  28. return status
  29. }
  30. func CompanyType(companyName string) (bool, bool) {
  31. isGroup, isCommerce := false, false
  32. //是否是集团
  33. if c := JyBiTidb.CountBySql(`select count(1) from group_company_name where company_name=?`, companyName); c > 0 {
  34. isGroup = true
  35. }
  36. //是否在工商库
  37. if c := MgoQyxy.Count("qyxy_std", map[string]interface{}{"company_name": companyName}); c > 0 {
  38. isCommerce = true
  39. }
  40. return isGroup, isCommerce
  41. }
  42. func DistributeClueShow(this *biservice.DistributeClueShowReq) *biservice.DistributeClueShowResp {
  43. rdata := []*biservice.DistributeClueShowss{}
  44. pArr := []string{}
  45. for _, c := range this.Datas { //初始化返回数据
  46. m := &biservice.DistributeClueShowss{}
  47. m.PositionId = c.PositionId
  48. m.ClueCount = 0
  49. m.CompanyCounts = 0
  50. if this.DataType == 1 {
  51. m.CompanyCount = 0
  52. m.MinCount = 0
  53. } else {
  54. m.CompanyCount = c.DistributedCount
  55. }
  56. rdata = append(rdata, m)
  57. pArr = append(pArr, strconv.FormatInt(c.PositionId, 10))
  58. }
  59. clueArrs1 := []string{}
  60. clueArrs := []string{}
  61. clueArrSync := &sync.Mutex{}
  62. 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`
  63. logx.Info("sql1 ", sql1)
  64. sdata := JyBiTidb.SelectBySql(sql1) //查这一批线索中属于当前分配人员的线索提前分配数量
  65. if sdata != nil && len(*sdata) > 0 {
  66. for _, v := range *sdata {
  67. sPositionId := common.Int64All(v["position_id"])
  68. count := common.IntAll(v["count"])
  69. for k, r := range rdata {
  70. if r.PositionId == sPositionId {
  71. if this.DataType == 1 {
  72. rdata[k].CompanyCount++
  73. rdata[k].MinCount++
  74. rdata[k].CompanyCounts++
  75. rdata[k].ClueCount += int64(count)
  76. } else {
  77. if rdata[k].CompanyCounts < rdata[k].CompanyCount {
  78. rdata[k].CompanyCounts++
  79. rdata[k].ClueCount += int64(count)
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. 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 + `)`
  87. logx.Info("sql2 ", sql2) //查这一批线索中属于当前分配人员的线索id,分出去,并且排除掉
  88. ldata := JyBiTidb.SelectBySql(sql2)
  89. if ldata != nil && len(*ldata) > 0 {
  90. wg := new(sync.WaitGroup)
  91. ch := make(chan bool, 20)
  92. for _, a := range strings.Split(this.ClueIdList, ",") {
  93. isOk := true
  94. wg.Add(1)
  95. ch <- true
  96. go func(a string, isOk bool) {
  97. defer func() {
  98. wg.Done()
  99. <-ch
  100. }()
  101. for _, v := range *ldata {
  102. id := common.Int64All(v["id"])
  103. positionId := common.Int64All(v["position_id"])
  104. ids := strconv.FormatInt(id, 10)
  105. if a == ids {
  106. isOk = false
  107. if this.DataType == 3 {
  108. dclue(id, positionId, this.IsTask, this.PositionId) //分线索
  109. }
  110. }
  111. }
  112. if isOk {
  113. clueArrSync.Lock()
  114. clueArrs = append(clueArrs, a)
  115. clueArrSync.Unlock()
  116. }
  117. }(a, isOk)
  118. }
  119. wg.Wait()
  120. } else {
  121. clueArrs = strings.Split(this.ClueIdList, ",")
  122. }
  123. logx.Info("clueArrs ", clueArrs)
  124. if len(clueArrs) > 0 {
  125. 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, ",") + `)`
  126. logx.Info("sql0", sql0)
  127. mdata := JyBiTidb.SelectBySql(sql0)
  128. if mdata != nil && len(*mdata) > 0 {
  129. nameMap := map[string][]int64{}
  130. namePMap := map[string]int64{}
  131. idMap := map[string]bool{}
  132. for _, v := range *mdata {
  133. cluename := common.ObjToString(v["cluename"])
  134. id := common.Int64All(v["id"])
  135. positionId := common.Int64All(v["position_id"])
  136. nameMap[cluename] = append(nameMap[cluename], id)
  137. namePMap[cluename] = positionId
  138. ids := strconv.FormatInt(id, 10)
  139. idMap[ids] = true
  140. }
  141. logx.Info("nameMap ", nameMap)
  142. logx.Info("namePMap ", namePMap)
  143. for _, v := range clueArrs {
  144. if !idMap[v] {
  145. clueArrs1 = append(clueArrs1, v)
  146. }
  147. }
  148. for k, v := range nameMap {
  149. 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])
  150. if ppdata != nil && len(*ppdata) > 0 {
  151. ppMap := map[int64]int{}
  152. for _, vv := range *ppdata {
  153. positionId := common.Int64All(vv["position_id"])
  154. ppMap[positionId] = 0
  155. }
  156. isOk, sindex, paid := false, 0, int64(0)
  157. L:
  158. for pa, _ := range ppMap {
  159. if pa > 0 {
  160. for n, r := range rdata {
  161. if r.PositionId == pa {
  162. if this.DataType == 1 {
  163. isOk = true
  164. sindex = n
  165. paid = pa
  166. break L
  167. } else {
  168. if rdata[n].CompanyCounts < rdata[n].CompanyCount {
  169. isOk = true
  170. sindex = n
  171. paid = pa
  172. break L
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. logx.Info("RDATA ", rdata[sindex].CompanyCount, rdata[sindex].MinCount, rdata[sindex].CompanyCounts, rdata[sindex].ClueCount, len(v))
  180. if isOk {
  181. if this.DataType == 1 {
  182. rdata[sindex].CompanyCount++
  183. rdata[sindex].MinCount++
  184. rdata[sindex].CompanyCounts++
  185. rdata[sindex].ClueCount += int64(len(v))
  186. } else {
  187. rdata[sindex].CompanyCounts++
  188. rdata[sindex].ClueCount += int64(len(v))
  189. if this.DataType == 3 {
  190. for _, vv := range v {
  191. dclue(vv, paid, this.IsTask, this.PositionId) //分线索
  192. }
  193. }
  194. }
  195. }
  196. logx.Info("RDATA2 ", rdata[sindex].CompanyCount, rdata[sindex].MinCount, rdata[sindex].CompanyCounts, rdata[sindex].ClueCount, len(v))
  197. }
  198. }
  199. } else {
  200. clueArrs1 = clueArrs
  201. }
  202. }
  203. logx.Info("clueArrs1 ", clueArrs1)
  204. if len(clueArrs1) > 0 {
  205. 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`
  206. logx.Info("sql3 ", sql3) //从大到小排列这一列线索名称分组及数量情况
  207. cdata := JyBiTidb.SelectBySql(sql3)
  208. if cdata != nil && len(*cdata) > 0 {
  209. for _, v := range *cdata {
  210. count := common.IntAll(v["count"])
  211. cluename := common.ObjToString(v["cluename"])
  212. minCount, minIndex := int64(0), 0
  213. for k, r := range rdata {
  214. if this.DataType == 1 {
  215. if k == 0 {
  216. minCount = r.CompanyCounts
  217. }
  218. } else {
  219. if r.CompanyCounts < r.CompanyCount {
  220. minCount = r.CompanyCounts
  221. }
  222. }
  223. }
  224. for _, r := range rdata {
  225. if this.DataType == 1 {
  226. if r.CompanyCounts < minCount {
  227. minCount = r.CompanyCounts
  228. }
  229. } else {
  230. if r.CompanyCounts < minCount && r.CompanyCounts < r.CompanyCount {
  231. minCount = r.CompanyCounts
  232. }
  233. }
  234. }
  235. for k, r := range rdata {
  236. if this.DataType == 1 {
  237. if r.CompanyCounts <= minCount {
  238. minCount = r.CompanyCounts
  239. minIndex = k
  240. }
  241. } else {
  242. if r.CompanyCounts <= minCount && r.CompanyCounts < r.CompanyCount {
  243. minCount = r.CompanyCounts
  244. minIndex = k
  245. }
  246. }
  247. }
  248. if this.DataType == 1 {
  249. rdata[minIndex].CompanyCount++
  250. rdata[minIndex].CompanyCounts++
  251. rdata[minIndex].ClueCount += int64(count)
  252. } else {
  253. if rdata[minIndex].CompanyCounts < rdata[minIndex].CompanyCount {
  254. rdata[minIndex].CompanyCounts++
  255. rdata[minIndex].ClueCount += int64(count)
  256. }
  257. }
  258. if this.DataType == 3 {
  259. iddata := JyBiTidb.SelectBySql(`SELECT id FROM dwd_f_crm_clue_info WHERE cluename = "` + cluename + `" and id in (` + strings.Join(clueArrs1, ",") + `)`)
  260. if iddata != nil { //分线索
  261. wg := new(sync.WaitGroup)
  262. ch := make(chan bool, 10)
  263. for _, i := range *iddata {
  264. wg.Add(1)
  265. ch <- true
  266. go func(i map[string]interface{}) {
  267. defer func() {
  268. wg.Done()
  269. <-ch
  270. }()
  271. id := common.Int64All(i["id"])
  272. dclue(id, rdata[minIndex].PositionId, this.IsTask, this.PositionId)
  273. }(i)
  274. }
  275. wg.Wait()
  276. }
  277. }
  278. }
  279. }
  280. }
  281. return &biservice.DistributeClueShowResp{
  282. ErrorCode: 0,
  283. Data: rdata,
  284. }
  285. }
  286. func dclue(v, positionId, isTask, thispositionId int64) {
  287. clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, "", "")
  288. nowTime := time.Now().Format(date.Date_Full_Layout)
  289. if clueData != nil && len(*clueData) > 0 {
  290. isAssign := common.IntAll((*clueData)["is_assign"])
  291. clueSeatNumber := common.ObjToString((*clueData)["seatNumber"])
  292. oldName, seatNumber, name := "", "", ""
  293. trailstatus := common.ObjToString((*clueData)["trailstatus"])
  294. saleData := JyBiTidb.SelectBySql(`select * from dwd_f_crm_personnel_management where seat_number != "" and seat_number is not null`)
  295. if saleData != nil && len(*saleData) > 0 {
  296. for _, v := range *saleData {
  297. if common.ObjToString(v["seat_number"]) == clueSeatNumber {
  298. oldName = common.ObjToString(v["name"])
  299. }
  300. if common.Int64All(v["position_id"]) == positionId {
  301. seatNumber = common.ObjToString(v["seat_number"])
  302. name = common.ObjToString(v["name"])
  303. }
  304. }
  305. }
  306. if isAssign == 1 {
  307. oldpositionId := common.Int64All((*clueData)["position_id"])
  308. updateClue := map[string]interface{}{
  309. "position_id": positionId,
  310. "seatNumber": seatNumber,
  311. "is_assign": 1,
  312. "updatetime": nowTime,
  313. "comeintime": nowTime,
  314. "comeinsource_private": 4,
  315. "level_open": nil,
  316. "clue_level": nil,
  317. "out_task_time": nil,
  318. "out_task_status": nil,
  319. }
  320. if oldName != name {
  321. updateClue["start_trail_time"] = nil
  322. updateClue["next_trail_time"] = nil
  323. }
  324. if isTask == int64(1) {
  325. updateClue["is_task"] = 1
  326. updateClue["task_time"] = nowTime
  327. updateClue["tasktime"] = nowTime
  328. updateClue["taskstatus"] = 0
  329. updateClue["tasksource"] = "主动分配客户"
  330. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  331. "clue_id": v,
  332. "position_id": positionId,
  333. "change_type": "加入任务车",
  334. "new_value": "主动分配客户",
  335. "createtime": nowTime,
  336. "BCPCID": common.GetRandom(32),
  337. "operator_id": thispositionId,
  338. })
  339. }
  340. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  341. if ok {
  342. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  343. "clue_id": v,
  344. "position_id": positionId,
  345. "change_field": "position_id",
  346. "change_type": "所属人变更",
  347. "old_value": oldName,
  348. "new_value": name,
  349. "createtime": nowTime,
  350. "BCPCID": common.GetRandom(32),
  351. "operator_id": thispositionId,
  352. })
  353. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  354. "clue_id": v,
  355. "position_id": oldpositionId,
  356. "change_field": "trailstatus",
  357. "change_type": "基本信息变更",
  358. "old_value": CodeTrail[trailstatus],
  359. "new_value": "流失",
  360. "createtime": nowTime,
  361. "BCPCID": common.GetRandom(32),
  362. "operator_id": thispositionId,
  363. })
  364. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  365. "clue_id": v,
  366. "position_id": positionId,
  367. "change_field": "trailstatus",
  368. "change_type": "基本信息变更",
  369. "old_value": CodeTrail[trailstatus],
  370. "new_value": "新增",
  371. "createtime": nowTime,
  372. "BCPCID": common.GetRandom(32),
  373. "operator_id": thispositionId,
  374. })
  375. } else {
  376. logx.Info("私海修改失败 ", v, positionId, seatNumber)
  377. }
  378. } else {
  379. updateClue := map[string]interface{}{
  380. "position_id": positionId,
  381. "seatNumber": seatNumber,
  382. "is_assign": 1,
  383. "updatetime": nowTime,
  384. "comeintime": nowTime,
  385. "comeinsource_private": 4,
  386. "is_task": 0,
  387. "taskstatus": 0,
  388. "level_open": nil,
  389. "clue_level": nil,
  390. "out_task_time": nil,
  391. "out_task_status": nil,
  392. "next_trail_time": nil,
  393. "start_trail_time": nil,
  394. // "comeinsource_open": nil,
  395. }
  396. if trailstatus != "08" {
  397. updateClue["trailstatus"] = "01"
  398. }
  399. if isTask == int64(1) {
  400. updateClue["is_task"] = 1
  401. updateClue["task_time"] = nowTime
  402. updateClue["tasktime"] = nowTime
  403. updateClue["taskstatus"] = 0
  404. updateClue["tasksource"] = "主动分配客户"
  405. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  406. "clue_id": v,
  407. "position_id": positionId,
  408. "change_type": "加入任务车",
  409. "new_value": "主动分配客户",
  410. "createtime": nowTime,
  411. "BCPCID": common.GetRandom(32),
  412. "operator_id": thispositionId,
  413. })
  414. }
  415. ok := JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": v}, updateClue)
  416. if ok {
  417. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  418. "clue_id": v,
  419. "position_id": positionId,
  420. "change_field": "position_id",
  421. "change_type": "所属人变更",
  422. "old_value": "/",
  423. "new_value": name,
  424. "createtime": nowTime,
  425. "BCPCID": common.GetRandom(32),
  426. "operator_id": thispositionId,
  427. })
  428. if trailstatus != "08" {
  429. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  430. "clue_id": v,
  431. "position_id": positionId,
  432. "change_field": "trailstatus",
  433. "change_type": "基本信息变更",
  434. "old_value": "商机线索",
  435. "new_value": "新增",
  436. "createtime": nowTime,
  437. "BCPCID": common.GetRandom(32),
  438. "operator_id": thispositionId,
  439. })
  440. JyBiTidb.Insert("dwd_f_crm_clue_change_record", map[string]interface{}{
  441. "clue_id": v,
  442. "position_id": positionId,
  443. "change_field": "trailstatus",
  444. "change_type": "基本信息变更",
  445. "old_value": CodeTrail[trailstatus],
  446. "new_value": "商机线索",
  447. "createtime": nowTime,
  448. "BCPCID": common.GetRandom(32),
  449. "operator_id": thispositionId,
  450. })
  451. }
  452. } else {
  453. logx.Info("私海插入失败 ", v, positionId, seatNumber)
  454. }
  455. }
  456. }
  457. }