owner.go 29 KB

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