owner.go 36 KB

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