owner.go 38 KB

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