owner.go 31 KB

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