owner.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. package service
  2. import (
  3. . "bp.jydev.jianyu360.cn/CRM/application/api/common"
  4. "bp.jydev.jianyu360.cn/CRM/application/entity"
  5. "context"
  6. "fmt"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. "strings"
  10. )
  11. type OwnerService struct {
  12. PartyA string
  13. Supplier string
  14. Heterotophy string
  15. Intermediary string
  16. Agency string
  17. SearchEntName string
  18. SourceType string
  19. ProcessingStatus string
  20. Area string
  21. PositionId int64
  22. PageIndex int64
  23. PageSize int64
  24. }
  25. type BuyerProject struct {
  26. BuyerId string
  27. BuyerName string
  28. Project ProjectEntity
  29. IsMonitor bool
  30. IsCreateCustomer bool
  31. IsIgnore bool
  32. Area string
  33. Zbtime int64
  34. }
  35. type ProjectEntity struct {
  36. Number int64
  37. zbtime int64
  38. EstimatedAmount int64
  39. Connections []map[string]interface{}
  40. }
  41. type Recommend struct {
  42. buyerId string `ch:"buyer_id"`
  43. buyer string `ch:"buyer"`
  44. }
  45. type Project struct {
  46. BuyerId string `ch:"buyer_id"`
  47. Area string `ch:"area"`
  48. GroupCount int64 `ch:"group_count"`
  49. Money int64 `ch:"money"`
  50. Zbtime int64 `ch:"zbtime"`
  51. }
  52. func (t *OwnerService) OwnerlList() map[string]interface{} {
  53. //先查询采购单位列表
  54. dataMap := &map[string]map[string]interface{}{}
  55. if t.PartyA != "" || t.Supplier != "" || t.Heterotophy != "" || t.Intermediary != "" || t.Agency != "" {
  56. dataMap = BuyerList(t.PartyA, t.Supplier, t.Heterotophy, t.Intermediary, t.Agency)
  57. }
  58. if t.SearchEntName != "" {
  59. //es查询
  60. }
  61. //处理状态初始化
  62. if t.ProcessingStatus != "" {
  63. ProcessingStatusInit(t.PositionId, dataMap, t.ProcessingStatus)
  64. }
  65. //监控状态处理
  66. MonitorStatusInit(t.PositionId, dataMap, t.SourceType)
  67. //项目数量查询
  68. buyerArr := []string{}
  69. for key := range *dataMap {
  70. buyerArr = append(buyerArr, fmt.Sprintf(`"%s"`, gconv.String(key)))
  71. }
  72. //项目数量处理
  73. projectMap := ProjectHandle(buyerArr, t.Area)
  74. //采购单位和项目合并
  75. returnData, monitorNumber, highSuccessNumber, connectionsNumber := BuyerProjectMerge(dataMap, projectMap, t.SourceType)
  76. //分页数据处理
  77. if t.PageSize == 0 {
  78. t.PageSize = 10
  79. }
  80. if t.PageIndex == 0 {
  81. t.PageIndex = 10
  82. }
  83. startIndex := (t.PageIndex - 1) * t.PageSize
  84. endIndex := t.PageIndex * t.PageSize
  85. if startIndex < connectionsNumber {
  86. if endIndex > connectionsNumber {
  87. endIndex = connectionsNumber
  88. }
  89. returnData = returnData[startIndex:endIndex]
  90. } else {
  91. return map[string]interface{}{}
  92. }
  93. buyerIdArr := []string{}
  94. for _, value := range returnData {
  95. buyerIdArr = append(buyerIdArr, fmt.Sprintf("'%s'", value.BuyerId))
  96. }
  97. //业主人脉处理
  98. projectList := ConnectionsHandle(buyerIdArr, t.PositionId, false)
  99. for _, v := range projectList {
  100. buyerId := gconv.String(v["a_id"])
  101. for i, v1 := range returnData {
  102. aBuyerId := v1.BuyerId
  103. if buyerId == aBuyerId {
  104. //组装数据
  105. returnData[i].Project.Connections = append(returnData[i].Project.Connections, v)
  106. }
  107. }
  108. }
  109. //返回数据组装
  110. return map[string]interface{}{
  111. "connectionsNumber": connectionsNumber,
  112. "highSuccessNumber": highSuccessNumber,
  113. "monitorNumber": monitorNumber,
  114. "list": returnData,
  115. }
  116. }
  117. func ConnectionsHandle(buyerArr []string, positionId int64, isAll bool) []map[string]interface{} {
  118. returnData := []map[string]interface{}{}
  119. //中间人 作为可介绍业主 企业名称
  120. companyArr := CrmMysql.SelectBySql(
  121. fmt.Sprintf("select b.company_id,b.company_name,b.contact_name,a.relate_id,a.relate_name from connection_introduce a INNER JOIN connection b on a.position_id=%d and a.connection_id=b.id and a.relate_Id in (%s) and a.type =1 and b.type=4 and b.state=1 ", positionId, strings.Join(buyerArr, ",")))
  122. for _, v := range *companyArr {
  123. companyId := gconv.String(v["company_id"])
  124. companyName := gconv.String(v["company_name"])
  125. relateName := gconv.String(v["relate_name"])
  126. relateId := gconv.String(v["relate_id"])
  127. //key := fmt.Sprintf("%s_%s", relateId, companyId)
  128. returnData = append(returnData, map[string]interface{}{
  129. "b_id": companyId,
  130. "b_name": companyName,
  131. "a_name": relateName,
  132. "a_id": relateId,
  133. "sourceType": "中间人",
  134. "relationship": "业主的关系人",
  135. })
  136. }
  137. if isAll && len(returnData) > 0 {
  138. return returnData
  139. }
  140. //采购单位 投资关系 管辖关系
  141. buyerSql := fmt.Sprintf("select a_id,b_id,a_name,b_name,code from ent_map_code where ( a_name in (%s) or b_name in (%s) ) and code in('0101','0201')", strings.Join(buyerArr, ","), strings.Join(buyerArr, ","))
  142. relationshipArr := FindConnectionsHandle(buyerSql)
  143. if relationshipArr != nil && len(relationshipArr) > 0 {
  144. for _, v := range relationshipArr {
  145. a_id := gconv.String(v["a_id"])
  146. b_id := gconv.String(v["b_id"])
  147. a_name := gconv.String(v["a_name"])
  148. b_name := gconv.String(v["b_name"])
  149. for _, v1 := range buyerArr {
  150. code := gconv.String(v["a_id"])
  151. if a_id == v1 {
  152. //key := fmt.Sprintf("%s_%s", a_id, b_id)
  153. switch code {
  154. case "0101": //管辖关系
  155. returnData = append(returnData, map[string]interface{}{
  156. "b_id": b_id,
  157. "b_name": b_name,
  158. "a_name": a_name,
  159. "a_id": a_id,
  160. "sourceType": "甲方",
  161. "relationship": "业主的上级机构",
  162. })
  163. case "0201": //投资关系
  164. returnData = append(returnData, map[string]interface{}{
  165. "b_id": b_id,
  166. "b_name": b_name,
  167. "a_name": a_name,
  168. "a_id": a_id,
  169. "sourceType": "业主的股东",
  170. })
  171. }
  172. }
  173. if b_id == v1 {
  174. //key := fmt.Sprintf("%s_%s", b_id, a_id)
  175. if code == "0101" {
  176. //"0101":管辖关系
  177. returnData = append(returnData, map[string]interface{}{
  178. "b_id": a_id,
  179. "b_name": a_name,
  180. "a_name": b_name,
  181. "a_id": b_id,
  182. "sourceType": "甲方",
  183. "relationship": "业主的下级机构",
  184. })
  185. }
  186. }
  187. }
  188. }
  189. }
  190. //供应商 采购单位 供应商 招标代理机构 采购单位 招标代理机构 需要计算合作次数
  191. winnerSql := fmt.Sprintf("SELECT zbtime,project_id,buyer_id,buyer,winner,winner_id,agency_id,agency FROM transaction_info WHERE buyer_id in (%s)", strings.Join(buyerArr, ","))
  192. winnerArr := FindConnectionsHandle(winnerSql)
  193. if winnerArr != nil && len(winnerArr) > 0 {
  194. winnerMap := map[string]map[string]interface{}{}
  195. agencyMap := map[string]map[string]interface{}{}
  196. for _, m := range winnerArr {
  197. buyerId := gconv.String(m["buyer_id"])
  198. winner := gconv.String(m["winner"])
  199. buyer := gconv.String(m["buyer"])
  200. winnerId := gconv.String(m["winner_id"])
  201. agency := gconv.String(m["agency"])
  202. agencyId := gconv.String(m["agency_id"])
  203. projectId := gconv.String(m["project_id"])
  204. zbtime := gconv.String(m["zbtime"])
  205. key := fmt.Sprintf("%s_%s", buyerId, winnerId)
  206. returnData = append(returnData, map[string]interface{}{
  207. "b_id": winnerId,
  208. "b_name": winner,
  209. "a_name": buyer,
  210. "a_id": buyerId,
  211. "sourceType": "供应商",
  212. })
  213. if _, ok := winnerMap[key]; ok {
  214. data := winnerMap[key]
  215. data["count"] = gconv.Int64(data["data"]) + 1
  216. dataList := gconv.Maps(data["list"])
  217. dataList = append(dataList, map[string]interface{}{
  218. "zbtime": zbtime,
  219. "entName": winner,
  220. })
  221. data["list"] = dataList
  222. winnerMap[key] = data
  223. } else {
  224. winnerMap[key] = map[string]interface{}{
  225. "b_id": winnerId,
  226. "b_name": winner,
  227. "a_name": buyer,
  228. "a_id": buyerId,
  229. "sourceType": "供应商",
  230. "count": 1,
  231. "list": []map[string]interface{}{
  232. {
  233. "zbtime": zbtime,
  234. "entName": winner,
  235. },
  236. },
  237. }
  238. }
  239. key = fmt.Sprintf("%s_%s_%s", buyerId, agencyId, projectId)
  240. if _, ok := agencyMap[key]; ok {
  241. data := agencyMap[key]
  242. data["count"] = gconv.Int64(data["data"]) + 1
  243. dataList := gconv.Maps(data["list"])
  244. dataList = append(dataList, map[string]interface{}{
  245. "zbtime": zbtime,
  246. "entName": winner,
  247. })
  248. data["list"] = dataList
  249. agencyMap[key] = data
  250. } else {
  251. agencyMap[key] = map[string]interface{}{
  252. "b_id": agencyId,
  253. "b_name": agency,
  254. "a_name": buyer,
  255. "a_id": buyerId,
  256. "sourceType": "招标代理",
  257. "count": 1,
  258. "list": []map[string]interface{}{
  259. {
  260. "zbtime": zbtime,
  261. "entName": agency,
  262. },
  263. },
  264. }
  265. }
  266. }
  267. for _, m := range winnerMap {
  268. returnData = append(returnData, map[string]interface{}{
  269. "b_id": gconv.String(m["b_id"]),
  270. "b_name": gconv.String(m["b_name"]),
  271. "a_name": gconv.String(m["a_name"]),
  272. "a_id": gconv.String(m["a_id"]),
  273. "sourceType": gconv.String(m["sourceType"]),
  274. "relationship": gconv.String(m["relationship"]),
  275. "count": gconv.Int64(m["count"]),
  276. })
  277. }
  278. for _, m := range agencyMap {
  279. returnData = append(returnData, map[string]interface{}{
  280. "b_id": gconv.String(m["b_id"]),
  281. "b_name": gconv.String(m["b_name"]),
  282. "a_name": gconv.String(m["a_name"]),
  283. "a_id": gconv.String(m["a_id"]),
  284. "sourceType": gconv.String(m["sourceType"]),
  285. "relationship": gconv.String(m["relationship"]),
  286. "count": gconv.Int64(m["count"]),
  287. })
  288. }
  289. }
  290. return returnData
  291. }
  292. // 采购单位和项目合并
  293. func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}, sourceType string) ([]BuyerProject, int64, int64, int64) {
  294. returnData := []BuyerProject{}
  295. connectionsNumber := int64(0)
  296. highSuccessNumber := int64(0)
  297. monitorNumber := int64(0)
  298. for buyerId, buyerMap := range *dataMap {
  299. if _, ok := (*projectMap)[buyerId]; ok {
  300. projectMap := (*projectMap)[buyerId]
  301. buyerName := gconv.String(buyerMap["buyerName"])
  302. if isMonitor := gconv.Bool(buyerMap["isMonitor"]); isMonitor {
  303. monitorNumber++
  304. }
  305. count := gconv.Int64(projectMap["count"])
  306. money := gconv.Int64(projectMap["money"])
  307. zbtime := gconv.Int64(projectMap["zbtime"])
  308. if sourceType == "1" {
  309. if count < 2 {
  310. continue
  311. }
  312. highSuccessNumber++
  313. returnData = append(returnData, BuyerProject{
  314. BuyerId: buyerId,
  315. BuyerName: buyerName,
  316. Project: ProjectEntity{
  317. Number: count,
  318. EstimatedAmount: money,
  319. Connections: nil,
  320. },
  321. Area: gconv.String(projectMap["area"]),
  322. Zbtime: zbtime,
  323. })
  324. } else {
  325. if count > 2 {
  326. highSuccessNumber++
  327. }
  328. returnData = append(returnData, BuyerProject{
  329. BuyerId: buyerId,
  330. BuyerName: buyerName,
  331. Project: ProjectEntity{
  332. Number: count,
  333. EstimatedAmount: money,
  334. Connections: nil,
  335. },
  336. Area: gconv.String(projectMap["area"]),
  337. Zbtime: zbtime,
  338. })
  339. }
  340. }
  341. connectionsNumber++
  342. }
  343. return returnData, connectionsNumber, highSuccessNumber, monitorNumber
  344. }
  345. // 已监控数据处理
  346. func MonitorStatusInit(positionId int64, dataMap *map[string]map[string]interface{}, sourceType string) {
  347. monitorMap := map[string]bool{}
  348. query := map[string]interface{}{
  349. "userId": positionId,
  350. }
  351. clist, ok := Mgo.Find("follow_customer", query, ``, `{"name":1}`, false, -1, -1)
  352. if ok && clist != nil && len(*clist) > 0 {
  353. for _, v := range *clist {
  354. monitorMap[gconv.String(v["name"])] = true
  355. }
  356. }
  357. //采购单位和自己监控的对比
  358. newMap := &map[string]map[string]interface{}{}
  359. for k, v := range *dataMap {
  360. buyerName := gconv.String(v["buyerName"])
  361. if _, ok1 := monitorMap[gconv.String(buyerName)]; ok1 {
  362. v["isMonitor"] = true
  363. (*newMap)[k] = v
  364. } else {
  365. if sourceType != "2" && sourceType != "1" {
  366. v["isMonitor"] = false
  367. (*newMap)[k] = v
  368. }
  369. }
  370. }
  371. if newMap != nil {
  372. dataMap = newMap
  373. }
  374. }
  375. // 处理状态初始化
  376. func ProcessingStatusInit(positionId int64, dataMap *map[string]map[string]interface{}, processingStatus string) {
  377. //未处理
  378. untreatedMap := &map[string]bool{}
  379. //已忽略
  380. ignoredMap := &map[string]bool{}
  381. //已创建
  382. createdMap := &map[string]bool{}
  383. processingArr := CrmMysql.Find(entity.CONNECTION_STATUS, map[string]interface{}{
  384. "type": 1,
  385. "position id": positionId,
  386. }, "relate_id,is_handle,is_ignore,is_create", "", -1, -1)
  387. if len(*processingArr) > 0 {
  388. for _, v := range *processingArr {
  389. entId := gconv.String(v["relate_id"])
  390. handle := gconv.Int64(v["is_handle"])
  391. ignore := gconv.Int64(v["is_ignore"])
  392. create := gconv.Int64(v["is_create"])
  393. if handle == 1 {
  394. (*untreatedMap)[entId] = true
  395. }
  396. if ignore == 1 {
  397. (*ignoredMap)[entId] = true
  398. }
  399. if create == 1 {
  400. (*createdMap)[entId] = true
  401. }
  402. }
  403. }
  404. newMap := &map[string]map[string]interface{}{}
  405. //所有采购单位和处理状态对比
  406. for buyerId, value := range *dataMap {
  407. for _, v := range strings.Split(processingStatus, ",") {
  408. switch v {
  409. case "1":
  410. if _, ok := (*untreatedMap)[buyerId]; !ok {
  411. (*newMap)[buyerId] = value
  412. }
  413. case "2":
  414. if _, ok := (*ignoredMap)[buyerId]; ok {
  415. (*newMap)[buyerId] = value
  416. }
  417. case "3":
  418. if _, ok := (*createdMap)[buyerId]; ok {
  419. (*newMap)[buyerId] = value
  420. }
  421. }
  422. }
  423. }
  424. if newMap != nil {
  425. dataMap = newMap
  426. }
  427. }
  428. // 采购单位查询
  429. func BuyerList(partyA, supplier, heterotophy, intermediary, agency string) *map[string]map[string]interface{} {
  430. dataMap := &map[string]map[string]interface{}{}
  431. //甲方
  432. if partyA != "" {
  433. for _, v := range strings.Split(intermediary, ",") {
  434. intermediaryArr := strings.Split(v, "_")
  435. if len(intermediaryArr) == 2 {
  436. (*dataMap)[intermediaryArr[1]] = map[string]interface{}{
  437. "buyerName": intermediaryArr[0],
  438. }
  439. }
  440. }
  441. }
  442. //供应商 //同甲异业
  443. if supplier != "" || heterotophy != "" {
  444. findInSetArr := []string{}
  445. for _, v := range strings.Split(supplier, ",") {
  446. findInSetArr = append(findInSetArr, fmt.Sprintf(" (%s) in winner_id", v))
  447. }
  448. for _, v := range strings.Split(heterotophy, ",") {
  449. findInSetArr = append(findInSetArr, fmt.Sprintf(" (%s) in winner_id", v))
  450. }
  451. sqlStr := fmt.Sprintf(`SELECT buyer, buyer_id FROM transaction_info where (%s) `, strings.Join(findInSetArr, " or "))
  452. FindHandle(sqlStr, dataMap)
  453. }
  454. //中间人
  455. if intermediary != "" {
  456. for _, v := range strings.Split(intermediary, ",") {
  457. intermediaryArr := strings.Split(v, "_")
  458. if len(intermediaryArr) == 2 {
  459. (*dataMap)[intermediaryArr[1]] = map[string]interface{}{
  460. "buyerName": intermediaryArr[0],
  461. }
  462. }
  463. }
  464. }
  465. //招标代理
  466. if agency != "" {
  467. sqlStr := fmt.Sprintf(`SELECT buyer, buyer_id FROM transaction_info where agency_id in (%s) order by userid`, agency)
  468. FindHandle(sqlStr, dataMap)
  469. }
  470. return dataMap
  471. }
  472. // 项目数量查询
  473. func ProjectHandle(buyerArr []string, area string) *map[string]map[string]interface{} {
  474. sqlStr := ""
  475. if area != "" {
  476. sqlStr = fmt.Sprintf(`SELECT buyer_id,area, count(1) AS group_count FROM information.transaction_info WHERE buyer_id IN (%s) and area ="%s" GROUP BY buyer_id,area `, strings.Join(buyerArr, ","), area)
  477. } else {
  478. sqlStr = fmt.Sprintf(`SELECT buyer_id,area, count(1) AS group_count FROM information.transaction_info WHERE buyer_id IN (%s) GROUP BY buyer_id,area `, strings.Join(buyerArr, ","))
  479. }
  480. logx.Info(sqlStr)
  481. rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
  482. projectMap := &map[string]map[string]interface{}{}
  483. if err != nil {
  484. return projectMap
  485. }
  486. for rows.Next() {
  487. data := Project{}
  488. rows.ScanStruct(&data)
  489. if (*projectMap)[data.BuyerId] != nil {
  490. dataMap := (*projectMap)[data.BuyerId]
  491. dataMap[data.BuyerId] = map[string]interface{}{
  492. "area": Deduplication(gconv.String(dataMap["area"]), data.Area),
  493. "count": gconv.Int64(dataMap["count"]) + data.GroupCount,
  494. "money": gconv.Int64(dataMap["money"]) + data.Money,
  495. "zbtime": gconv.Int64(dataMap["Zbtime"]),
  496. }
  497. } else {
  498. (*projectMap)[data.BuyerId] = map[string]interface{}{
  499. "area": gconv.String(data.Area),
  500. "count": data.GroupCount,
  501. "money": data.Money,
  502. "zbtime": data.Zbtime,
  503. }
  504. }
  505. }
  506. return projectMap
  507. }
  508. // 采购单位查询sql
  509. func FindHandle(sqlStr string, dataMap *map[string]map[string]interface{}) {
  510. rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
  511. if err != nil {
  512. logx.Error("查询失败", sqlStr, err)
  513. }
  514. for rows.Next() {
  515. data := Recommend{}
  516. rows.ScanStruct(&data)
  517. (*dataMap)[data.buyerId] = map[string]interface{}{
  518. "buyerName": data.buyer,
  519. }
  520. }
  521. return
  522. }
  523. // 人脉信息查询sql
  524. func FindConnectionsHandle(sqlStr string) []map[string]interface{} {
  525. returnData := []map[string]interface{}{}
  526. rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
  527. if err != nil {
  528. logx.Error("查询失败", sqlStr, err)
  529. }
  530. for rows.Next() {
  531. data := map[string]interface{}{}
  532. rows.ScanStruct(&data)
  533. returnData = append(returnData, data)
  534. }
  535. return returnData
  536. }
  537. // 去重处理
  538. func Deduplication(a, b string) string {
  539. newArr := []string{}
  540. if a != "" || b != "" {
  541. foo := false
  542. newArr := strings.Split(a, ",")
  543. for _, v := range newArr {
  544. if v == b {
  545. foo = true
  546. }
  547. }
  548. if !foo {
  549. newArr = append(newArr, b)
  550. }
  551. }
  552. return strings.Join(newArr, ",")
  553. }