owner.go 34 KB

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