owner.go 28 KB

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