owner.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/encrypt"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "sort"
  8. "strings"
  9. "app.yhyue.com/moapp/jybase/common"
  10. elastic "app.yhyue.com/moapp/jybase/es"
  11. . "bp.jydev.jianyu360.cn/CRM/application/api/common"
  12. "bp.jydev.jianyu360.cn/CRM/application/entity"
  13. "github.com/gogf/gf/v2/util/gconv"
  14. "github.com/shopspring/decimal"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. const (
  18. EntINDEX = "ent_info"
  19. EntTYPE = "ent_info"
  20. )
  21. type OwnerService struct {
  22. PartyA string
  23. Supplier string
  24. Heterotophy string
  25. Intermediary string
  26. Agency string
  27. SearchEntName string
  28. SourceType string
  29. ProcessingStatus string
  30. Area string
  31. PositionId int64
  32. PageIndex int64
  33. PageSize int64
  34. BuyerId string
  35. BuyerName string
  36. WinnerId string
  37. WinnerName string
  38. CooperateType string
  39. EntAccountId int64
  40. }
  41. type BuyerProject struct {
  42. BuyerId string
  43. BuyerName string
  44. Project ProjectEntity
  45. IsMonitor bool
  46. IsCreateCustomer bool
  47. IsIgnore bool
  48. Area string
  49. Zbtime int64
  50. }
  51. type ProjectEntity struct {
  52. Number int64
  53. zbtime int64
  54. EstimatedAmount int64
  55. Connections []map[string]interface{}
  56. }
  57. type Recommend struct {
  58. buyerId string `ch:"buyer_id"`
  59. buyer string `ch:"buyer"`
  60. }
  61. type Project struct {
  62. BuyerId string `ch:"buyer_id"`
  63. Area string `ch:"area"`
  64. GroupCount uint64 `ch:"group_count"`
  65. Zbtime int64 `ch:"zbtime"`
  66. Money decimal.Decimal `ch:"money"`
  67. }
  68. func (t *OwnerService) OwnerlList() map[string]interface{} {
  69. //先查询采购单位列表
  70. dataMap := &map[string]map[string]interface{}{}
  71. projectMap := &map[string]map[string]interface{}{}
  72. if t.PartyA != "" || t.Supplier != "" || t.Heterotophy != "" || t.Intermediary != "" || t.Agency != "" {
  73. dataMap = BuyerList(t.PartyA, t.Supplier, t.Heterotophy, t.Intermediary, t.Agency)
  74. //处理状态初始化
  75. if t.ProcessingStatus != "" {
  76. ProcessingStatusInit(t.PositionId, dataMap, t.ProcessingStatus)
  77. }
  78. //监控状态处理
  79. MonitorStatusInit(t.PositionId, dataMap, t.SourceType)
  80. //项目数量查询
  81. buyerArr := []string{}
  82. for _, value := range *dataMap {
  83. buyerArr = append(buyerArr, fmt.Sprintf(`"%s"`, gconv.String(value["buyerName"])))
  84. }
  85. //项目数量处理
  86. _, projectMap = ProjectHandle(buyerArr, t.PositionId, t.SearchEntName, t.Area, t.SourceType, t.ProcessingStatus)
  87. logx.Info(projectMap)
  88. } else if t.SearchEntName != "" {
  89. //1 只看转介绍成功率高2只看已监控的
  90. //先查找采购单位数据
  91. projectMap, dataMap = ProjectHandle([]string{}, t.PositionId, t.SearchEntName, t.Area, t.SourceType, t.ProcessingStatus)
  92. if t.ProcessingStatus != "" {
  93. ProcessingStatusInit(t.PositionId, dataMap, t.ProcessingStatus)
  94. }
  95. //监控状态处理
  96. MonitorStatusInit(t.PositionId, dataMap, t.SourceType)
  97. }
  98. if len(*dataMap) == 0 {
  99. return map[string]interface{}{}
  100. }
  101. //采购单位和项目合并
  102. returnData, connectionsNumber, highSuccessNumber, monitorNumber := BuyerProjectMerge(dataMap, projectMap, t.SourceType)
  103. //数组排序
  104. //分页数据处理
  105. if t.PageSize == 0 {
  106. t.PageSize = 10
  107. }
  108. if t.PageIndex == 0 {
  109. t.PageIndex = 1
  110. }
  111. startIndex := (t.PageIndex - 1) * t.PageSize
  112. endIndex := t.PageIndex * t.PageSize
  113. sort.Slice(returnData, func(i, j int) bool {
  114. return returnData[i].Zbtime < returnData[j].Zbtime
  115. })
  116. if startIndex < gconv.Int64(len(returnData)) {
  117. if endIndex > gconv.Int64(len(returnData)) {
  118. endIndex = gconv.Int64(len(returnData))
  119. }
  120. returnData = returnData[startIndex:endIndex]
  121. } else {
  122. return map[string]interface{}{}
  123. }
  124. buyerIdArr := []string{}
  125. for _, value := range returnData {
  126. buyerIdArr = append(buyerIdArr, fmt.Sprintf("'%s'", value.BuyerId))
  127. }
  128. //可介绍业主人脉处理
  129. routeList := ConnectionsHandle(buyerIdArr, t.PositionId, false)
  130. for _, v := range routeList {
  131. buyerId := gconv.String(v["a_id"])
  132. for i, v1 := range returnData {
  133. aBuyerId := v1.BuyerId
  134. if buyerId == aBuyerId {
  135. //组装数据
  136. returnData[i].Project.Connections = append(returnData[i].Project.Connections, v)
  137. }
  138. }
  139. }
  140. //返回数据组装
  141. return map[string]interface{}{
  142. "connectionsNumber": connectionsNumber,
  143. "highSuccessNumber": highSuccessNumber,
  144. "monitorNumber": monitorNumber,
  145. "list": returnData,
  146. }
  147. }
  148. // 可介绍业主人脉列表
  149. func ConnectionsHandle(buyerArr []string, positionId int64, isAll bool) []map[string]interface{} {
  150. returnData := []map[string]interface{}{}
  151. //中间人 作为可介绍业主 企业名称
  152. companyArr := CrmMysql.SelectBySql(
  153. fmt.Sprintf("select DISTINCT b.company_id,b.company_name,a.relate_id,a.relate_name,b.contact_person 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.itype =1 and b.itype=4 and b.status=1 ", positionId, strings.Join(buyerArr, ",")))
  154. for _, v := range *companyArr {
  155. companyId := gconv.String(v["company_id"])
  156. companyName := gconv.String(v["company_name"])
  157. relateName := gconv.String(v["relate_name"])
  158. relateId := gconv.String(v["relate_id"])
  159. //key := fmt.Sprintf("%s_%s", relateId, companyId)
  160. returnData = append(returnData, map[string]interface{}{
  161. "b_id": companyId,
  162. "b_name": companyName,
  163. "a_name": relateName,
  164. "a_id": relateId,
  165. "sourceType": "middleman",
  166. "relationship": "业主的关系人",
  167. "personName": gconv.String(v["contact_person"]),
  168. })
  169. }
  170. if isAll && len(returnData) > 0 {
  171. return returnData
  172. }
  173. //采购单位 投资关系 管辖关系
  174. //buyerSql := fmt.Sprintf("select a_id,b_id,a_name,b_name,code from ent_map_code a where ( a_name in (%s) or b_name in (%s) ) and code in('0101','0201')", strings.Join(buyerArr, ","), strings.Join(buyerArr, ","))
  175. buyerSql := fmt.Sprintf(`select a.a_id as a_id,a.b_id as b_id,a.a_name as a_name,a.b_name as b_name,a.code as code,c.legal_person as c_person,d.legal_person d_person from ent_map_code a
  176. left join ent_info c on (a.a_id in (%s) or a.b_id in (%s) ) and a.code in('0101','0201') and a.a_id =c.id
  177. left join ent_info d on (a.a_id in (%s) or a.b_id in (%s) ) and a.code in('0101','0201') and a.b_id =d.id
  178. where (a.a_id in (%s) or a.b_id in (%s) ) and a.code in('0101','0201')`,
  179. strings.Join(buyerArr, ","),
  180. strings.Join(buyerArr, ","),
  181. strings.Join(buyerArr, ","),
  182. strings.Join(buyerArr, ","),
  183. strings.Join(buyerArr, ","),
  184. strings.Join(buyerArr, ","))
  185. relationshipArr := BuyerFindConnectionsHandle(buyerSql)
  186. if relationshipArr != nil && len(relationshipArr) > 0 {
  187. for _, v := range relationshipArr {
  188. for _, v1 := range buyerArr {
  189. code := v.Code
  190. if fmt.Sprintf("'%s'", v.Aid) == v1 {
  191. //key := fmt.Sprintf("%s_%s", a_id, b_id)
  192. switch code {
  193. case "0101": //管辖关系
  194. returnData = append(returnData, map[string]interface{}{
  195. "b_id": v.Bid,
  196. "b_name": v.Bname,
  197. "a_name": v.Aname,
  198. "a_id": v.Aid,
  199. "sourceType": "firstparty",
  200. "relationship": "业主的上级机构",
  201. "personName": v.Dperson,
  202. })
  203. case "0201": //投资关系
  204. returnData = append(returnData, map[string]interface{}{
  205. "b_id": v.Bid,
  206. "b_name": v.Bname,
  207. "a_name": v.Aname,
  208. "a_id": v.Aid,
  209. "sourceType": "firstparty",
  210. "relationship": "业主的股东",
  211. "personName": v.Dperson,
  212. })
  213. }
  214. }
  215. if fmt.Sprintf("'%s'", v.Bid) == v1 {
  216. //key := fmt.Sprintf("%s_%s", b_id, a_id)
  217. if code == "0101" {
  218. //"0101":管辖关系
  219. returnData = append(returnData, map[string]interface{}{
  220. "b_id": v.Aid,
  221. "b_name": v.Aname,
  222. "a_name": v.Bname,
  223. "a_id": v.Bid,
  224. "sourceType": "firstparty",
  225. "relationship": "业主的下级机构",
  226. "personName": v.Cperson,
  227. })
  228. }
  229. }
  230. }
  231. }
  232. }
  233. if isAll && len(returnData) > 0 {
  234. return returnData
  235. }
  236. //供应商 采购单位 供应商 招标代理机构 采购单位 招标代理机构 需要计算合作次数
  237. winnerSql := fmt.Sprintf(`SELECT DISTINCT a.project_name as project_name,a.project_id as project_id , a.zbtime as zbtime ,a.buyer_id as buyer_id,a.buyer as buyer,a.agency_id as agency_id,a.agency as agency,c.company_name as company_name ,b.name as personName ,a.winnerId as awinnerId
  238. FROM
  239. (select zbtime,project_id,project_name,project_id,buyer_id,buyer,agency_id,agency,winner_id as winnerId from transaction_info
  240. ARRAY JOIN winner_id ) a
  241. inner join ent_contact b on a.buyer_id in (%s) and a.winnerId=b.id
  242. inner join ent_info c on a.winnerId=c.id order by a.zbtime,a.winnerId`, strings.Join(buyerArr, ","))
  243. winnerArr := SupplierFindConnectionsHandle(winnerSql)
  244. if winnerArr != nil && len(winnerArr) > 0 {
  245. winnerMap := map[string]map[string]interface{}{}
  246. agencyMap := map[string]map[string]interface{}{}
  247. for _, m := range winnerArr {
  248. buyerId := m.Buyer_id
  249. winner := m.Company_name
  250. buyer := m.Buyer
  251. winnerId := m.AwinnerId
  252. agency := m.Agency
  253. agencyId := m.Agency_id
  254. projectId := encrypt.EncodeArticleId2ByCheck(m.Project_id)
  255. projectName := m.Project_name
  256. zbtime := m.Zbtime
  257. personName := m.PersonName
  258. key := fmt.Sprintf("%s_%s", buyerId, winnerId)
  259. returnData = append(returnData, map[string]interface{}{
  260. "b_id": winnerId,
  261. "b_name": winner,
  262. "a_name": buyer,
  263. "a_id": buyerId,
  264. "sourceType": "supplier",
  265. "personName": personName,
  266. })
  267. if _, ok := winnerMap[key]; ok {
  268. data := winnerMap[key]
  269. data["count"] = gconv.Int64(data["data"]) + 1
  270. dataList := gconv.Maps(data["list"])
  271. dataList = append(dataList, map[string]interface{}{
  272. "zbtime": zbtime,
  273. "entName": winner,
  274. "projectId": projectId,
  275. "projectName": projectName,
  276. })
  277. data["list"] = dataList
  278. winnerMap[key] = data
  279. } else {
  280. winnerMap[key] = map[string]interface{}{
  281. "b_id": winnerId,
  282. "b_name": winner,
  283. "a_name": buyer,
  284. "a_id": buyerId,
  285. "sourceType": "supplier",
  286. "personName": personName,
  287. "count": 1,
  288. "list": []map[string]interface{}{
  289. {
  290. "zbtime": zbtime,
  291. "entName": winner,
  292. "projectId": projectId,
  293. "projectName": projectName,
  294. },
  295. },
  296. }
  297. }
  298. key = fmt.Sprintf("%s_%s_%s", buyerId, agencyId, projectId)
  299. if _, ok := agencyMap[key]; ok {
  300. data := agencyMap[key]
  301. data["count"] = gconv.Int64(data["data"]) + 1
  302. dataList := gconv.Maps(data["list"])
  303. dataList = append(dataList, map[string]interface{}{
  304. "zbtime": zbtime,
  305. "entName": winner,
  306. "projectId": projectId,
  307. "projectName": projectName,
  308. })
  309. data["list"] = dataList
  310. agencyMap[key] = data
  311. } else {
  312. agencyMap[key] = map[string]interface{}{
  313. "b_id": agencyId,
  314. "b_name": agency,
  315. "a_name": buyer,
  316. "a_id": buyerId,
  317. "sourceType": "agency",
  318. "count": 1,
  319. "personName": personName,
  320. "list": []map[string]interface{}{
  321. {
  322. "zbtime": zbtime,
  323. "entName": agency,
  324. "projectId": projectId,
  325. "projectName": projectName,
  326. },
  327. },
  328. }
  329. }
  330. }
  331. for _, m := range winnerMap {
  332. returnData = append(returnData, map[string]interface{}{
  333. "b_id": gconv.String(m["b_id"]),
  334. "b_name": gconv.String(m["b_name"]),
  335. "a_name": gconv.String(m["a_name"]),
  336. "a_id": gconv.String(m["a_id"]),
  337. "sourceType": gconv.String(m["sourceType"]),
  338. "relationship": gconv.String(m["relationship"]),
  339. "count": gconv.Int64(m["count"]),
  340. "list": m["list"],
  341. })
  342. }
  343. for _, m := range agencyMap {
  344. returnData = append(returnData, map[string]interface{}{
  345. "b_id": gconv.String(m["b_id"]),
  346. "b_name": gconv.String(m["b_name"]),
  347. "a_name": gconv.String(m["a_name"]),
  348. "a_id": gconv.String(m["a_id"]),
  349. "sourceType": gconv.String(m["sourceType"]),
  350. "relationship": gconv.String(m["relationship"]),
  351. "count": gconv.Int64(m["count"]),
  352. "list": m["list"],
  353. })
  354. }
  355. }
  356. return returnData
  357. }
  358. // 采购单位和项目合并
  359. func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}, sourceType string) ([]BuyerProject, int64, int64, int64) {
  360. returnData := []BuyerProject{}
  361. connectionsNumber := int64(0)
  362. highSuccessNumber := int64(0)
  363. monitorNumber := int64(0)
  364. for buyerId, buyerMap := range *dataMap {
  365. buyerName := gconv.String(buyerMap["buyerName"])
  366. if _, ok := (*projectMap)[buyerName]; ok {
  367. projectMap := (*projectMap)[buyerName]
  368. if isMonitor := gconv.Bool(buyerMap["isMonitor"]); isMonitor {
  369. monitorNumber++
  370. }
  371. count := gconv.Int64(projectMap["count"])
  372. money := gconv.Int64(projectMap["money"])
  373. zbtime := gconv.Int64(projectMap["zbtime"])
  374. if sourceType == "1" {
  375. if count < 2 {
  376. continue
  377. }
  378. highSuccessNumber++
  379. returnData = append(returnData, BuyerProject{
  380. BuyerId: buyerId,
  381. BuyerName: buyerName,
  382. Project: ProjectEntity{
  383. Number: count,
  384. EstimatedAmount: money,
  385. Connections: []map[string]interface{}{},
  386. },
  387. Area: gconv.String(projectMap["area"]),
  388. Zbtime: zbtime,
  389. })
  390. } else {
  391. if count > 2 {
  392. highSuccessNumber++
  393. }
  394. returnData = append(returnData, BuyerProject{
  395. BuyerId: buyerId,
  396. BuyerName: buyerName,
  397. Project: ProjectEntity{
  398. Number: count,
  399. EstimatedAmount: money,
  400. Connections: []map[string]interface{}{},
  401. },
  402. Area: gconv.String(projectMap["area"]),
  403. Zbtime: zbtime,
  404. })
  405. }
  406. } else {
  407. returnData = append(returnData, BuyerProject{
  408. BuyerId: buyerId,
  409. BuyerName: buyerName,
  410. Project: ProjectEntity{
  411. Number: 0,
  412. EstimatedAmount: 0,
  413. Connections: []map[string]interface{}{},
  414. },
  415. Area: "",
  416. Zbtime: 0,
  417. })
  418. }
  419. connectionsNumber++
  420. }
  421. return returnData, connectionsNumber, highSuccessNumber, monitorNumber
  422. }
  423. // 已监控数据处理
  424. func MonitorStatusInit(positionId int64, dataMap *map[string]map[string]interface{}, sourceType string) {
  425. monitorMap := map[string]bool{}
  426. query := map[string]interface{}{
  427. "userId": positionId,
  428. }
  429. clist, ok := Mgo.Find("follow_customer", query, ``, `{"name":1}`, false, -1, -1)
  430. if ok && clist != nil && len(*clist) > 0 {
  431. for _, v := range *clist {
  432. monitorMap[gconv.String(v["name"])] = true
  433. }
  434. }
  435. //采购单位和自己监控的对比
  436. newMap := &map[string]map[string]interface{}{}
  437. for k, v := range *dataMap {
  438. buyerName := gconv.String(v["buyerName"])
  439. if _, ok1 := monitorMap[gconv.String(buyerName)]; ok1 {
  440. v["isMonitor"] = true
  441. (*newMap)[k] = v
  442. } else {
  443. if sourceType != "2" && sourceType != "1" {
  444. v["isMonitor"] = false
  445. (*newMap)[k] = v
  446. }
  447. }
  448. }
  449. if newMap != nil {
  450. dataMap = newMap
  451. }
  452. }
  453. // 处理状态初始化
  454. func ProcessingStatusInit(positionId int64, dataMap *map[string]map[string]interface{}, processingStatus string) {
  455. //未处理
  456. untreatedMap := &map[string]bool{}
  457. //已忽略
  458. ignoredMap := &map[string]bool{}
  459. //已创建
  460. createdMap := &map[string]bool{}
  461. processingArr := CrmMysql.Find(entity.CONNECTION_STATUS, map[string]interface{}{
  462. "type": 1,
  463. "position id": positionId,
  464. }, "relate_id,is_handle,is_ignore,is_create", "", -1, -1)
  465. if len(*processingArr) > 0 {
  466. for _, v := range *processingArr {
  467. entId := gconv.String(v["relate_id"])
  468. handle := gconv.Int64(v["is_handle"])
  469. ignore := gconv.Int64(v["is_ignore"])
  470. create := gconv.Int64(v["is_create"])
  471. if handle == 1 {
  472. (*untreatedMap)[entId] = true
  473. }
  474. if ignore == 1 {
  475. (*ignoredMap)[entId] = true
  476. }
  477. if create == 1 {
  478. (*createdMap)[entId] = true
  479. }
  480. }
  481. }
  482. newMap := &map[string]map[string]interface{}{}
  483. //所有采购单位和处理状态对比
  484. for buyerId, value := range *dataMap {
  485. for _, v := range strings.Split(processingStatus, ",") {
  486. switch v {
  487. case "1":
  488. if _, ok := (*untreatedMap)[buyerId]; !ok {
  489. (*newMap)[buyerId] = value
  490. }
  491. case "2":
  492. if _, ok := (*ignoredMap)[buyerId]; ok {
  493. (*newMap)[buyerId] = value
  494. }
  495. case "3":
  496. if _, ok := (*createdMap)[buyerId]; ok {
  497. (*newMap)[buyerId] = value
  498. }
  499. }
  500. }
  501. }
  502. if newMap != nil {
  503. dataMap = newMap
  504. }
  505. }
  506. // 采购单位查询
  507. func BuyerList(partyA, supplier, heterotophy, intermediary, agency string) *map[string]map[string]interface{} {
  508. dataMap := &map[string]map[string]interface{}{}
  509. //甲方
  510. if partyA != "" {
  511. for _, v := range strings.Split(partyA, ",") {
  512. intermediaryArr := strings.Split(v, "_")
  513. if len(intermediaryArr) == 2 {
  514. (*dataMap)[intermediaryArr[0]] = map[string]interface{}{
  515. "buyerName": intermediaryArr[1],
  516. }
  517. }
  518. }
  519. }
  520. //供应商 //同甲异业
  521. if supplier != "" || heterotophy != "" {
  522. findInSetArr := []string{}
  523. for _, v := range strings.Split(supplier, ",") {
  524. findInSetArr = append(findInSetArr, fmt.Sprintf("'%s'", v))
  525. }
  526. for _, v := range strings.Split(heterotophy, ",") {
  527. findInSetArr = append(findInSetArr, fmt.Sprintf("'%s'", v))
  528. }
  529. sqlStr := fmt.Sprintf(`SELECT buyer, buyer_id FROM transaction_info where hasAny(winner_id,[%s]) `, strings.Join(findInSetArr, ","))
  530. FindHandle(sqlStr, dataMap)
  531. }
  532. //中间人
  533. if intermediary != "" {
  534. for _, v := range strings.Split(intermediary, ",") {
  535. intermediaryArr := strings.Split(v, "_")
  536. if len(intermediaryArr) == 2 {
  537. (*dataMap)[intermediaryArr[0]] = map[string]interface{}{
  538. "buyerName": intermediaryArr[1],
  539. }
  540. }
  541. }
  542. }
  543. //招标代理
  544. if agency != "" {
  545. agencyArr := []string{}
  546. for _, s := range strings.Split(agency, ",") {
  547. agencyArr = append(agencyArr, fmt.Sprintf("'%s'", s))
  548. }
  549. sqlStr := fmt.Sprintf(`SELECT buyer, buyer_id FROM transaction_info where agency_id in (%s) order by userid`, strings.Join(agencyArr, ","))
  550. FindHandle(sqlStr, dataMap)
  551. }
  552. return dataMap
  553. }
  554. // 项目数量查询
  555. func ProjectHandle(buyerArr []string, positionId int64, entName, area, sourceType, processingStatus string) (*map[string]map[string]interface{}, *map[string]map[string]interface{}) {
  556. projectMap := &map[string]map[string]interface{}{}
  557. returnMap := &map[string]map[string]interface{}{}
  558. sql := ""
  559. sqlStr := ""
  560. if entName != "" {
  561. sql = `{"query":{"bool":{"must":[{"multi_match":{"query":"%s","type":"phrase","fields":["buyer.mbuyer"]}}%s]}},"aggs":{"buyer_count":{"terms":{"field":"buyer","size":1000,"order":{"zbtime_count":"desc"}},"aggs":{"area_count":{"terms":{"field":"area"}},"buyer_id":{"terms":{"field":"id"}},"money_count":{"sum":{"field":"bidendtime"}},"zbtime_count":{"max":{"field":"bidendtime"}}}}},"size":0}`
  562. sqlStr = fmt.Sprintf(sql, entName, common.If(area != "", fmt.Sprintf(`,{"term":{"area":"%s"}}`, area), ""))
  563. } else {
  564. sql = `{"query":{"bool":{"must":[{"terms":{"buyer":[%s]}}%s]}},"aggs":{"buyer_count":{"terms":{"field":"buyer","size":1000,"order":{"zbtime_count":"desc"}},"aggs":{"area_count":{"terms":{"field":"area"}},"buyer_id":{"terms":{"field":"id"}},"money_count":{"sum":{"field":"bidendtime"}},"zbtime_count":{"max":{"field":"bidendtime"}}}}},"size":0}`
  565. sqlStr = fmt.Sprintf(sql, strings.Join(buyerArr, ","), common.If(area != "", fmt.Sprintf(`,{"term":{"area":"%s"}}`, area), ""))
  566. }
  567. data, _, _ := elastic.GetAggs(INDEX, TYPE, sqlStr)
  568. if len(data) == 0 {
  569. return projectMap, returnMap
  570. }
  571. for _, object := range data {
  572. buyerArr := BuyerAggStruct{}
  573. bs, ok := object.MarshalJSON()
  574. if ok != nil {
  575. logx.Error("解析项目数据失败", object)
  576. continue
  577. }
  578. A := gconv.Map(bs)
  579. logx.Info(A)
  580. err := json.Unmarshal(bs, &buyerArr)
  581. logx.Info(err)
  582. for _, v := range buyerArr.Buckets {
  583. buyerName := v.Key
  584. area := ""
  585. buyerId := ""
  586. for _, areaEntity := range v.Area_count.Buckets {
  587. area = Deduplication(area, areaEntity.Key)
  588. }
  589. for _, buyerIdEntity := range v.Buyer_id.Buckets {
  590. if buyerIdEntity.Key != "" {
  591. buyerId = buyerIdEntity.Key
  592. }
  593. }
  594. (*projectMap)[buyerName] = map[string]interface{}{
  595. "area": area,
  596. "zbtime": v.Zbtime_count.Value,
  597. "money": v.Money_count.Value,
  598. "count": v.Doc_count,
  599. "buyerId": buyerId,
  600. }
  601. (*returnMap)[buyerId] = map[string]interface{}{
  602. "buyerName": buyerName,
  603. }
  604. }
  605. }
  606. return projectMap, returnMap
  607. }
  608. type BuyerAggStruct struct {
  609. Buckets []struct {
  610. Key string `json:"key,omitempty"`
  611. Doc_count int64 `json:"doc_count,omitempty"`
  612. Zbtime_count struct {
  613. Value interface{} `json:"value,omitempty"`
  614. } `json:"zbtime_count,omitempty"`
  615. Money_count struct {
  616. Value interface{} `json:"value,omitempty"`
  617. } `json:"money_count,omitempty"`
  618. Area_count struct {
  619. Buckets []struct {
  620. Key string `json:"key,omitempty"`
  621. Doc_count int64 `json:"doc_count,omitempty"`
  622. }
  623. } `json:"area_count"`
  624. Buyer_id struct {
  625. Buckets []struct {
  626. Key string `json:"key,omitempty"`
  627. Doc_count int64 `json:"doc_count,omitempty"`
  628. }
  629. } `json:"buyer_id"`
  630. }
  631. }
  632. // 采购单位查询sql
  633. func FindHandle(sqlStr string, dataMap *map[string]map[string]interface{}) {
  634. rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
  635. if err != nil {
  636. logx.Error("查询失败", sqlStr, err)
  637. }
  638. for rows.Next() {
  639. data := Recommend{}
  640. rows.ScanStruct(&data)
  641. (*dataMap)[data.buyerId] = map[string]interface{}{
  642. "buyerName": data.buyer,
  643. }
  644. }
  645. return
  646. }
  647. type ConnectionsEntity struct {
  648. Aid string `ch:"a_id"`
  649. Bid string `ch:"b_id"`
  650. Aname string `ch:"a_name"`
  651. Bname string `ch:"b_name"`
  652. Code string `ch:"code"`
  653. Cperson string `ch:"c_person"`
  654. Dperson string `ch:"d_person"`
  655. }
  656. type SupplierConnectionsEntity struct {
  657. Project_name string `ch:"project_name"`
  658. Project_id string `ch:"project_id"`
  659. Zbtime int64 `ch:"zbtime"`
  660. Buyer_id string `ch:"buyer_id"`
  661. Buyer string `ch:"buyer"`
  662. Agency_id string `ch:"agency_id"`
  663. Agency string `ch:"agency"`
  664. Company_name string `ch:"company_name"`
  665. PersonName string `ch:"personName"`
  666. AwinnerId string `ch:"awinnerId"`
  667. }
  668. type TimeConnectionsEntity struct {
  669. Zbtime int64 `ch:"zbtime"`
  670. Project_id string `ch:"project_id"`
  671. Project_name string `ch:"project_name"`
  672. }
  673. // 采购单位人脉信息查询sql
  674. func BuyerFindConnectionsHandle(sqlStr string) []ConnectionsEntity {
  675. returnData := []ConnectionsEntity{}
  676. rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
  677. if err != nil {
  678. logx.Error("查询失败", sqlStr, err)
  679. }
  680. for rows.Next() {
  681. data := ConnectionsEntity{}
  682. rows.ScanStruct(&data)
  683. returnData = append(returnData, data)
  684. }
  685. return returnData
  686. }
  687. // 供应商
  688. func SupplierFindConnectionsHandle(sqlStr string) []SupplierConnectionsEntity {
  689. returnData := []SupplierConnectionsEntity{}
  690. rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
  691. if err != nil {
  692. logx.Error("查询失败", sqlStr, err)
  693. }
  694. for rows.Next() {
  695. data := SupplierConnectionsEntity{}
  696. err = rows.ScanStruct(&data)
  697. logx.Info(err)
  698. returnData = append(returnData, data)
  699. }
  700. return returnData
  701. }
  702. // 合作时间查询
  703. func TimeFindConnectionsHandle(sqlStr string) []TimeConnectionsEntity {
  704. returnData := []TimeConnectionsEntity{}
  705. rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
  706. if err != nil {
  707. logx.Error("查询失败", sqlStr, err)
  708. }
  709. for rows.Next() {
  710. data := TimeConnectionsEntity{}
  711. rows.ScanStruct(&data)
  712. returnData = append(returnData, data)
  713. }
  714. return returnData
  715. }
  716. // 去重处理
  717. func Deduplication(a, b string) string {
  718. newArr := []string{}
  719. if b == "全国" {
  720. return a
  721. }
  722. if a != "" {
  723. newArr = append(newArr, a)
  724. }
  725. if b != "" {
  726. newArr = append(newArr, b)
  727. }
  728. return strings.Join(newArr, ",")
  729. }
  730. // 路径列表
  731. func (t *OwnerService) OwnerRoute() []map[string]interface{} {
  732. routeList := ConnectionsHandle([]string{
  733. fmt.Sprintf("'%s'", t.BuyerId),
  734. }, t.PositionId, false)
  735. for i, routeMap := range routeList {
  736. sourceType := gconv.String(routeMap["sourceType"])
  737. if sourceType == "agency" || sourceType == "supplier" {
  738. data := routeList[i]
  739. data["cooperateCount"] = len(gconv.Maps(data["list"]))
  740. delete(data, "list")
  741. routeList[i] = data
  742. }
  743. }
  744. return routeList
  745. }
  746. // 合作列表
  747. func (t *OwnerService) OwnerCooperate() []map[string]interface{} {
  748. returData := []map[string]interface{}{}
  749. dataArr := []TimeConnectionsEntity{}
  750. if t.CooperateType == "supplier" {
  751. //供应商
  752. supplierSql := fmt.Sprintf(`SELECT DISTINCT a.zbtime as zbtime,a.project_id as project_id,a.project_name as project_name
  753. FROM
  754. (select zbtime,project_id,project_name,winner_id as winnerId from transaction_info
  755. ARRAY JOIN winner_id ) a
  756. inner join ent_contact b on a.buyer_id = '%s' and a.winnerId='%s' and a.winnerId=b.id
  757. inner join ent_info c on a.winnerId=c.id order by a.zbtime,a.winnerId`, t.BuyerId, t.WinnerId)
  758. dataArr = TimeFindConnectionsHandle(supplierSql)
  759. } else if t.CooperateType == "agency" {
  760. //代理机构
  761. agentSql := fmt.Sprintf(`select zbtime,project_id,project_name from transaction_info where buyer_id ='%s' and agency_id ='%s' order by zbtime,winner_id `, t.BuyerId, t.WinnerId)
  762. dataArr = TimeFindConnectionsHandle(agentSql)
  763. }
  764. if dataArr != nil && len(dataArr) > 0 {
  765. for _, m := range dataArr {
  766. zbtime := m.Zbtime
  767. returData = append(returData, map[string]interface{}{
  768. "zbtime": zbtime,
  769. "entName": t.WinnerName,
  770. "project_id": encrypt.EncodeArticleId2ByCheck(m.Project_id),
  771. "project_name": m.Project_name,
  772. })
  773. }
  774. }
  775. return returData
  776. }
  777. func (t *OwnerService) CandidateChannel() []*ResultData {
  778. returnData := []*ResultData{}
  779. dataMap := &map[string]map[string]interface{}{}
  780. if t.PartyA != "" || t.Supplier != "" || t.Heterotophy != "" || t.Intermediary != "" || t.Agency != "" {
  781. dataMap = BuyerList(t.PartyA, t.Supplier, t.Heterotophy, t.Intermediary, t.Agency)
  782. }
  783. if len(*dataMap) > 0 && dataMap != nil {
  784. propertyForm := ""
  785. m1 := CrmMysql.FindOne("config_tenant", map[string]interface{}{"account_id": t.EntAccountId}, "probusfor", "")
  786. if m1 != nil && len(*m1) > 0 {
  787. propertyForm = common.ObjToString((*m1)["probusfor"])
  788. }
  789. for buyerId := range *dataMap {
  790. logx.Info(buyerId)
  791. r1, r2 := GetData(propertyForm, buyerId)
  792. if r1 != nil && len(r1) > 0 {
  793. tmp := &ResultData{
  794. channelType: 1,
  795. channel: "同甲异业渠道",
  796. size: len(r1),
  797. data: r1,
  798. }
  799. returnData = append(returnData, tmp)
  800. }
  801. if r2 != nil && len(r2) > 0 {
  802. tmp := &ResultData{
  803. channelType: 1,
  804. channel: "招标代理机构",
  805. size: len(r2),
  806. data: r2,
  807. }
  808. returnData = append(returnData, tmp)
  809. }
  810. r3 := GetData1(t.PositionId, buyerId)
  811. if r3 != nil && len(r3) > 0 {
  812. tmp := &ResultData{
  813. channelType: 1,
  814. channel: "中间人",
  815. size: len(r3),
  816. data: r3,
  817. }
  818. returnData = append(returnData, tmp)
  819. }
  820. r4 := GetData2(buyerId)
  821. if r4 != nil && len(r4) > 0 {
  822. tmp := &ResultData{
  823. channelType: 1,
  824. channel: "业主关联单位",
  825. size: len(r4),
  826. data: r4,
  827. }
  828. returnData = append(returnData, tmp)
  829. }
  830. }
  831. }
  832. return returnData
  833. }