owner.go 28 KB

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