owner.go 28 KB

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