owner.go 33 KB

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