owner.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "sort"
  7. "strings"
  8. "time"
  9. "app.yhyue.com/moapp/jybase/common"
  10. "app.yhyue.com/moapp/jybase/encrypt"
  11. "app.yhyue.com/moapp/jybase/redis"
  12. . "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
  13. T "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
  14. "bp.jydev.jianyu360.cn/CRM/networkManage/entity"
  15. "github.com/gogf/gf/v2/util/gconv"
  16. "github.com/shopspring/decimal"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. )
  19. const (
  20. EntINDEX = "ent_info"
  21. EntTYPE = "ent_info"
  22. NetworkManageOwnerlList = "networkManage_ownerlList_%d_%s"
  23. )
  24. type OwnerService struct {
  25. PartyA string
  26. Supplier string
  27. Heterotophy string
  28. Intermediary string
  29. Agency string
  30. SearchEntName string
  31. SourceType string
  32. Area string
  33. PositionId int64
  34. PageIndex int64
  35. PageSize int64
  36. BuyerId string
  37. BuyerName string
  38. WinnerId string
  39. WinnerName string
  40. CooperateType string
  41. EntAccountId int64
  42. ProjectType string
  43. Type string
  44. EntId int64
  45. UserId string
  46. }
  47. type BuyerProject struct {
  48. BuyerId string
  49. BuyerName string
  50. Project ProjectEntity
  51. IsMonitor bool
  52. Area string
  53. Zbtime int64
  54. BuyerType string
  55. CId string
  56. }
  57. type ProjectEntity struct {
  58. Number int64
  59. zbtime int64
  60. EstimatedAmount int64
  61. Connections []map[string]interface{}
  62. }
  63. type Recommend struct {
  64. BuyerId string `ch:"buyer_id"`
  65. Buyer string `ch:"buyer"`
  66. }
  67. type Project struct {
  68. BuyerId string `ch:"buyer_id"`
  69. Area string `ch:"area"`
  70. GroupCount uint64 `ch:"group_count"`
  71. Zbtime int64 `ch:"zbtime"`
  72. Money decimal.Decimal `ch:"money"`
  73. }
  74. type OwnerlListRes struct {
  75. ReturnData []BuyerProject
  76. ConnectionsNumber int64
  77. }
  78. // OwnerlList 执行主要的业务逻辑
  79. func (t *OwnerService) OwnerlList() *OwnerlListRes {
  80. // 先查询采购单位列表
  81. startTime := time.Now().Unix()
  82. businessStr := FindBusiness(t.EntId, t.UserId)
  83. // 初始化分页参数
  84. if t.PageSize == 0 {
  85. t.PageSize = 10
  86. }
  87. if t.PageIndex == 0 {
  88. t.PageIndex = 1
  89. }
  90. olr := &OwnerlListRes{
  91. ReturnData: []BuyerProject{},
  92. }
  93. // 如果没有业务字符串,直接返回空数据
  94. if businessStr == "" {
  95. return olr
  96. }
  97. //监控状态处理
  98. monitorMap := NetworkCom.EntMonitor(gconv.String(t.PositionId))
  99. comKey := common.GetMd5String(fmt.Sprintf("%+v business:%s", t, businessStr))
  100. listKey := fmt.Sprintf(NetworkManageOwnerlList, t.PositionId, comKey)
  101. getFromCache := false
  102. monitorBuyers := []string{}
  103. if t.SourceType != "2" {
  104. // 检查 Redis 中是否存在列表缓存
  105. if rb, err := redis.GetNewBytes("newother", listKey); err == nil && rb != nil {
  106. if json.Unmarshal(*rb, &olr) == nil {
  107. getFromCache = true
  108. }
  109. }
  110. } else {
  111. for k, _ := range monitorMap {
  112. monitorBuyers = append(monitorBuyers, k)
  113. }
  114. }
  115. // 没有缓存时的处理逻辑
  116. if !getFromCache {
  117. dataMap := BuyerList(t.PartyA, t.Intermediary, t.PositionId)
  118. if len(*dataMap) == 0 && t.Supplier == "" && t.Heterotophy == "" && t.Agency == "" {
  119. return olr
  120. }
  121. // 项目数量查询
  122. buyerArr := getBuyerArr(dataMap)
  123. // 项目数量处理
  124. projectMap, connectionsNumber := t.ProjectHandle(buyerArr, businessStr, monitorBuyers)
  125. endTime := time.Now().Unix()
  126. fmt.Println("用时时间:", endTime-startTime)
  127. if len(*projectMap) == 0 {
  128. return olr
  129. }
  130. olr.ConnectionsNumber = connectionsNumber
  131. // 采购单位和项目合并
  132. a1 := time.Now().Unix()
  133. olr.ReturnData = BuyerProjectMerge(dataMap, projectMap)
  134. a2 := time.Now().Unix()
  135. fmt.Println("组合数据", a2-a1)
  136. /*if len(returnData) > 0 {
  137. //redis.Put("newother", comKey, returnData, C.CacheTimeOut)
  138. sort.Slice(returnData, func(i, j int) bool {
  139. aa := fmt.Sprintf("%d%s", (returnData)[i].Zbtime, (returnData)[i].BuyerId)
  140. bb := fmt.Sprintf("%d%s", (returnData)[j].Zbtime, (returnData)[j].BuyerId)
  141. return aa > bb
  142. })
  143. }*/
  144. buyerIdArr := []string{}
  145. for _, value := range olr.ReturnData {
  146. if value.BuyerId != "" {
  147. buyerIdArr = append(buyerIdArr, value.BuyerId)
  148. }
  149. }
  150. // 可介绍业主人脉处理
  151. a3 := time.Now().Unix()
  152. routeList := ConnectionsHandle(buyerIdArr, t.PositionId, false)
  153. a4 := time.Now().Unix()
  154. fmt.Println("组合数1111据", a4-a3)
  155. updateReturnDataWithRoutes(olr.ReturnData, routeList)
  156. }
  157. if len(olr.ReturnData) > 0 {
  158. for k, value := range olr.ReturnData {
  159. if _, ok := monitorMap[value.BuyerName]; ok {
  160. olr.ReturnData[k].IsMonitor = true
  161. } else {
  162. olr.ReturnData[k].IsMonitor = false
  163. }
  164. }
  165. redis.Put("newother", listKey, olr, NetworkCom.CacheTimeout())
  166. }
  167. // 返回数据组装
  168. return olr
  169. }
  170. // getBuyerArr 从数据映射中获取买家数组
  171. func getBuyerArr(dataMap *map[string]map[string]interface{}) []string {
  172. buyerArr := []string{}
  173. for k, _ := range *dataMap {
  174. buyerArr = append(buyerArr, k)
  175. }
  176. return buyerArr
  177. }
  178. // updateReturnDataWithRoutes 将路线信息更新到返回数据中
  179. func updateReturnDataWithRoutes(returnData []BuyerProject, routeList []map[string]interface{}) {
  180. for _, v := range routeList {
  181. buyerId := gconv.String(v["a_id"])
  182. for i, v1 := range returnData {
  183. aBuyerId := v1.BuyerId
  184. if buyerId == aBuyerId {
  185. // 组装数据
  186. returnData[i].Project.Connections = append(returnData[i].Project.Connections, v)
  187. }
  188. }
  189. }
  190. }
  191. // 可介绍业主人脉列表
  192. func ConnectionsHandle(buyerArr []string, positionId int64, isAll bool) []map[string]interface{} {
  193. returnData := []map[string]interface{}{}
  194. a1 := time.Now().Unix()
  195. //中间人
  196. returnData = FindMiddleman(buyerArr, positionId, returnData)
  197. a2 := time.Now().Unix()
  198. fmt.Println("中间人", a2-a1)
  199. if isAll && len(returnData) > 0 {
  200. return returnData
  201. }
  202. //采购单位 投资关系 管辖关系
  203. //采购单位
  204. a3 := time.Now().Unix()
  205. returnData = Findfirstparty(buyerArr, returnData)
  206. a4 := time.Now().Unix()
  207. fmt.Println("采购单位", a4-a3)
  208. if isAll && len(returnData) > 0 {
  209. return returnData
  210. }
  211. a5 := time.Now().Unix()
  212. returnData = append(returnData, Findwinner(buyerArr)...)
  213. a6 := time.Now().Unix()
  214. logx.Info("供应商", a6-a5)
  215. return returnData
  216. }
  217. // 供应商处理
  218. func Findwinner(buyerArr []string) []map[string]interface{} {
  219. returnData := []map[string]interface{}{}
  220. if len(buyerArr) == 0 {
  221. return returnData
  222. }
  223. wh1, args1 := common.WhArgs(buyerArr)
  224. args1 = append(args1, args1...)
  225. //供应商 采购单位 供应商 招标代理机构 采购单位 招标代理机构 需要计算合作次数
  226. winnerSql := `select zbtime,project_id,project_name,buyer_id,buyer,wr_id,wr,agency_id,agency from transaction_info_all ARRAY JOIN winner_id as wr_id,winner as wr where buyer_id in (` + wh1 + `) and LENGTH(winner)=LENGTH(winner_id) order by zbtime DESC,project_id DESC`
  227. winnerArr := SupplierFindConnectionsHandle(winnerSql, args1...)
  228. if winnerArr != nil && len(winnerArr) > 0 {
  229. //企业联系人处理
  230. entIdArr := []string{}
  231. entIdMap := map[string]bool{}
  232. for _, v := range winnerArr {
  233. if v.Winner_id != "" {
  234. if !entIdMap[v.Winner_id] {
  235. entIdArr = append(entIdArr, v.Winner_id)
  236. }
  237. entIdMap[v.Winner_id] = true
  238. }
  239. if v.Agency_id != "" {
  240. if !entIdMap[v.Agency_id] {
  241. entIdArr = append(entIdArr, v.Agency_id)
  242. }
  243. entIdMap[v.Agency_id] = true
  244. }
  245. }
  246. personMap := map[string]string{}
  247. if len(entIdArr) > 0 {
  248. wh2, args2 := common.WhArgs(entIdArr)
  249. personSql := `select id,name as personName from ent_contact where id in (` + wh2 + `) ORDER by name`
  250. personArr := PersonFindConnectionsHandle(personSql, args2...)
  251. //联系人处理
  252. for _, v := range personArr {
  253. personMap[v.Id] = v.PersonName
  254. }
  255. }
  256. winnerMap := map[string]map[string]interface{}{}
  257. agencyMap := map[string]map[string]interface{}{}
  258. agencyIdMap := map[string]bool{}
  259. for _, m := range winnerArr {
  260. if m.Winner_id == "" && m.Agency_id == "" {
  261. continue
  262. }
  263. projectId := encrypt.EncodeArticleId2ByCheck(m.Project_id)
  264. projectName := m.Project_name
  265. zbtime := m.Zbtime
  266. if m.Winner_id != "" {
  267. key := fmt.Sprintf("%s_%s", m.Buyer_id, m.Winner_id)
  268. if _, ok := winnerMap[key]; ok {
  269. data := winnerMap[key]
  270. data["count"] = gconv.Int64(data["count"]) + 1
  271. dataList := gconv.Maps(data["list"])
  272. dataList = append(dataList, map[string]interface{}{
  273. "zbtime": zbtime,
  274. "projectId": projectId,
  275. "projectName": projectName,
  276. })
  277. data["list"] = dataList
  278. data["zbtime"] = zbtime
  279. winnerMap[key] = data
  280. } else {
  281. winnerMap[key] = map[string]interface{}{
  282. "b_id": m.Winner_id,
  283. "b_name": m.Winner,
  284. "a_name": m.Buyer,
  285. "a_id": m.Buyer_id,
  286. "sourceType": "supplier",
  287. "zbtime": zbtime,
  288. "personName": personMap[m.Winner_id],
  289. "count": 1,
  290. "list": []map[string]interface{}{
  291. {
  292. "zbtime": zbtime,
  293. "entName": m.Winner,
  294. "projectId": projectId,
  295. "projectName": projectName,
  296. },
  297. },
  298. }
  299. }
  300. }
  301. if m.Agency_id != "" {
  302. key := fmt.Sprintf("%s_%s", m.Buyer_id, m.Agency_id)
  303. if _, ok := agencyIdMap[key]; ok {
  304. data := agencyMap[key]
  305. data["count"] = gconv.Int64(data["count"]) + 1
  306. dataList := gconv.Maps(data["list"])
  307. dataList = append(dataList, map[string]interface{}{
  308. "zbtime": zbtime,
  309. "projectId": projectId,
  310. "projectName": projectName,
  311. })
  312. data["list"] = dataList
  313. data["zbtime"] = zbtime
  314. agencyMap[key] = data
  315. } else {
  316. agencyIdMap[key] = true
  317. agencyMap[key] = map[string]interface{}{
  318. "b_id": m.Agency_id,
  319. "b_name": m.Agency,
  320. "a_name": m.Buyer,
  321. "a_id": m.Buyer_id,
  322. "sourceType": "agency",
  323. "count": 1,
  324. "zbtime": zbtime,
  325. "personName": gconv.String(personMap[m.Agency_id]),
  326. "list": []map[string]interface{}{
  327. {
  328. "zbtime": zbtime,
  329. "projectId": projectId,
  330. "projectName": projectName,
  331. },
  332. },
  333. }
  334. }
  335. }
  336. }
  337. for _, m := range winnerMap {
  338. returnData = append(returnData, map[string]interface{}{
  339. "b_id": gconv.String(m["b_id"]),
  340. "b_name": gconv.String(m["b_name"]),
  341. "a_name": gconv.String(m["a_name"]),
  342. "a_id": gconv.String(m["a_id"]),
  343. "sourceType": gconv.String(m["sourceType"]),
  344. "relationship": gconv.String(m["relationship"]),
  345. "count": gconv.Int64(m["count"]),
  346. "personName": gconv.String(m["personName"]),
  347. "list": m["list"],
  348. "zbtime": m["zbtime"],
  349. })
  350. }
  351. for _, m := range agencyMap {
  352. returnData = append(returnData, map[string]interface{}{
  353. "b_id": gconv.String(m["b_id"]),
  354. "b_name": gconv.String(m["b_name"]),
  355. "a_name": gconv.String(m["a_name"]),
  356. "a_id": gconv.String(m["a_id"]),
  357. "sourceType": gconv.String(m["sourceType"]),
  358. "relationship": gconv.String(m["relationship"]),
  359. "personName": gconv.String(m["personName"]),
  360. "count": gconv.Int64(m["count"]),
  361. "list": m["list"],
  362. "zbtime": m["zbtime"],
  363. })
  364. }
  365. }
  366. sort.Slice(returnData, func(i, j int) bool {
  367. aa := fmt.Sprintf("%s%s", gconv.String(returnData[i]["zbtime"]),
  368. gconv.String(returnData[i]["b_id"]))
  369. bb := fmt.Sprintf("%s%s", gconv.String(returnData[j]["zbtime"]),
  370. gconv.String(returnData[j]["b_id"]))
  371. return aa > bb
  372. })
  373. return returnData
  374. }
  375. // 中间人查询
  376. func FindMiddleman(buyerArr []string, positionId int64, returnData []map[string]interface{}) []map[string]interface{} {
  377. if len(buyerArr) == 0 {
  378. return returnData
  379. }
  380. //中间人 作为可介绍业主 企业名称
  381. wh, args := common.WhArgs(buyerArr)
  382. newArgs := []interface{}{positionId}
  383. newArgs = append(newArgs, args...)
  384. companyArr := CrmMysql.SelectBySql(`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=? and a.connection_id=b.id and a.relate_Id in (`+wh+`) and a.itype =1 and b.itype=4 and b.status=1`, newArgs...)
  385. for _, v := range *companyArr {
  386. companyId := gconv.String(v["company_id"])
  387. companyName := gconv.String(v["company_name"])
  388. relateName := gconv.String(v["relate_name"])
  389. relateId := gconv.String(v["relate_id"])
  390. returnData = append(returnData, map[string]interface{}{
  391. "b_id": companyId,
  392. "b_name": companyName,
  393. "a_name": relateName,
  394. "a_id": relateId,
  395. "sourceType": "middleman",
  396. "relationship": "业主的关系人",
  397. "personName": gconv.String(v["contact_person"]),
  398. })
  399. }
  400. return returnData
  401. }
  402. // 企业之间关系查询
  403. func Findfirstparty(buyerArr []string, returnData []map[string]interface{}) []map[string]interface{} {
  404. if len(buyerArr) == 0 {
  405. return returnData
  406. }
  407. wh1, args1 := common.WhArgs(buyerArr)
  408. args1 = append(args1, args1...)
  409. buyerSql := `select DISTINCT
  410. a.a_id as a_id,
  411. a.b_id as b_id,
  412. a.a_name as a_name,
  413. a.b_name as b_name,
  414. 111 as status,
  415. a.code as code
  416. from
  417. ent_map_code a
  418. where
  419. a.a_id in (` + wh1 + `)
  420. and a.code in('0101', '0201')
  421. UNION ALL
  422. select
  423. a.b_id as a_id,
  424. a.a_id as b_id,
  425. a.b_name as a_name,
  426. a.a_name as b_name,
  427. 222 as status,
  428. a.code as code
  429. from
  430. ent_map_code a
  431. where
  432. a.b_id in (` + wh1 + `)
  433. and a.code in('0101', '0201')`
  434. relationshipArr := BuyerFindConnectionsHandle(buyerSql, args1...)
  435. if relationshipArr != nil && len(relationshipArr) > 0 {
  436. entIdArr := []string{}
  437. for _, v := range relationshipArr {
  438. if v.Bid != "" {
  439. entIdArr = append(entIdArr, v.Bid)
  440. }
  441. }
  442. personMap := map[string]string{}
  443. if len(entIdArr) > 0 {
  444. wh2, args2 := common.WhArgs(entIdArr)
  445. personSql := `select id,name as personName from ent_contact where id in (` + wh2 + `) ORDER by name`
  446. personArr := PersonFindConnectionsHandle(personSql, args2...)
  447. //联系人处理
  448. for _, v := range personArr {
  449. personMap[v.Id] = v.PersonName
  450. }
  451. }
  452. for _, v := range relationshipArr {
  453. code := v.Code
  454. status := v.Status
  455. if gconv.String(status) == "111" {
  456. //key := fmt.Sprintf("%s_%s", a_id, b_id)
  457. switch code {
  458. case "0101": //管辖关系
  459. returnData = append(returnData, map[string]interface{}{
  460. "b_id": v.Bid,
  461. "b_name": v.Bname,
  462. "a_name": v.Aname,
  463. "a_id": v.Aid,
  464. "sourceType": "firstparty",
  465. "relationship": "业主的下级机构",
  466. "personName": gconv.String(personMap[v.Bid]),
  467. })
  468. case "0201": //投资关系
  469. returnData = append(returnData, map[string]interface{}{
  470. "b_id": v.Bid,
  471. "b_name": v.Bname,
  472. "a_name": v.Aname,
  473. "a_id": v.Aid,
  474. "sourceType": "firstparty",
  475. "relationship": "业主的股东",
  476. "personName": gconv.String(personMap[v.Bid]),
  477. })
  478. }
  479. }
  480. if gconv.String(status) == "222" {
  481. //key := fmt.Sprintf("%s_%s", b_id, a_id)
  482. if code == "0101" {
  483. //"0101":管辖关系
  484. returnData = append(returnData, map[string]interface{}{
  485. "b_id": v.Bid,
  486. "b_name": v.Bname,
  487. "a_name": v.Aname,
  488. "a_id": v.Aid,
  489. "sourceType": "firstparty",
  490. "relationship": "业主的上级机构",
  491. "personName": gconv.String(personMap[v.Bid]),
  492. })
  493. }
  494. if code == "0201" {
  495. returnData = append(returnData, map[string]interface{}{
  496. "b_id": v.Bid,
  497. "b_name": v.Bname,
  498. "a_name": v.Aname,
  499. "a_id": v.Aid,
  500. "sourceType": "firstparty",
  501. "relationship": "业主的股东",
  502. "personName": gconv.String(personMap[v.Bid]),
  503. })
  504. }
  505. }
  506. }
  507. }
  508. return returnData
  509. }
  510. // 采购单位和项目合并
  511. func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}) []BuyerProject {
  512. returnData := []BuyerProject{}
  513. for buyerName, project := range *projectMap {
  514. buyerId := gconv.String(project["buyerId"])
  515. data := (*dataMap)[buyerId]
  516. returnData = append(returnData, BuyerProject{
  517. BuyerId: buyerId,
  518. BuyerName: buyerName,
  519. BuyerType: gconv.String(data["buyerType"]),
  520. Project: ProjectEntity{
  521. Number: gconv.Int64(project["count"]),
  522. EstimatedAmount: gconv.Int64(project["money"]),
  523. Connections: []map[string]interface{}{},
  524. },
  525. Area: gconv.String(project["area"]),
  526. Zbtime: gconv.Int64(project["area"]),
  527. IsMonitor: gconv.Bool(data["isMonitor"]),
  528. CId: gconv.String(data["cId"]),
  529. })
  530. }
  531. return returnData
  532. }
  533. func FindStatus(positionId int64) (map[string]bool, map[string]bool, map[string]bool) {
  534. //未处理
  535. untreatedMap := map[string]bool{}
  536. //已忽略
  537. ignoredMap := map[string]bool{}
  538. //已创建
  539. createdMap := map[string]bool{}
  540. processingArr := CrmMysql.Find(entity.CONNECTION_STATUS, map[string]interface{}{
  541. "itype": 1,
  542. "position_id": positionId,
  543. }, "relate_id,is_handle,is_ignore,is_create", "", -1, -1)
  544. if processingArr != nil && len(*processingArr) > 0 {
  545. for _, v := range *processingArr {
  546. entId := gconv.String(v["relate_id"])
  547. handle := gconv.Int64(v["is_handle"])
  548. ignore := gconv.Int64(v["is_ignore"])
  549. create := gconv.Int64(v["is_create"])
  550. if handle == 1 {
  551. untreatedMap[entId] = true
  552. }
  553. if ignore == 1 {
  554. ignoredMap[entId] = true
  555. }
  556. if create == 1 {
  557. createdMap[entId] = true
  558. }
  559. }
  560. }
  561. return untreatedMap, ignoredMap, createdMap
  562. }
  563. // 采购单位查询
  564. func BuyerList(partyA, intermediary string, positionId int64) *map[string]map[string]interface{} {
  565. dataMap := &map[string]map[string]interface{}{}
  566. //甲方
  567. if partyA != "" {
  568. for _, v := range strings.Split(partyA, ",") {
  569. (*dataMap)[v] = map[string]interface{}{
  570. "buyerType": "firstparty",
  571. }
  572. }
  573. // intermediaryArr := []string{}
  574. // for _, v := range strings.Split(partyA, ",") {
  575. // if v != "" {
  576. // intermediaryArr = append(intermediaryArr, fmt.Sprintf("'%s'", v))
  577. // }
  578. // }
  579. // if len(partyA) > 0 {
  580. // partyASql := fmt.Sprintf(`select id as buyer_id,company_name as buyer from ent_info where id in (%s)`, strings.Join(intermediaryArr, ","))
  581. // FindHandle(partyASql, dataMap, "firstparty")
  582. // }
  583. }
  584. //供应商 //同甲异业
  585. // findInSetArr := []string{}
  586. // if supplier != "" {
  587. // for _, v := range strings.Split(supplier, ",") {
  588. // if v != "" {
  589. // findInSetArr = append(findInSetArr, fmt.Sprintf("'%s'", v))
  590. // }
  591. // }
  592. // }
  593. // if len(findInSetArr) > 0 {
  594. // sqlStr := fmt.Sprintf(`SELECT buyer_id,buyer FROM transaction_info_all where hasAny(winner_id,[%s]) `, strings.Join(findInSetArr, ","))
  595. // FindHandle(sqlStr, dataMap, "supplier")
  596. // }
  597. //同甲异业
  598. // findheterotophyArr := []string{}
  599. // if heterotophy != "" {
  600. // for _, v := range strings.Split(heterotophy, ",") {
  601. // if v != "" {
  602. // findheterotophyArr = append(findheterotophyArr, fmt.Sprintf("'%s'", v))
  603. // }
  604. // }
  605. // }
  606. // if len(findheterotophyArr) > 0 {
  607. // sqlStr := fmt.Sprintf(`SELECT buyer_id,buyer FROM transaction_info_all where hasAny(winner_id,[%s]) `, strings.Join(findheterotophyArr, ","))
  608. // FindHandle(sqlStr, dataMap, "adiffb")
  609. // }
  610. //中间人
  611. if intermediary != "" {
  612. wh, args := common.WhArgs(strings.Split(intermediary, ","))
  613. args = append(args, positionId)
  614. intermediaryArr := CrmMysql.SelectBySql(`select a.id,b.relate_id as relate_id,b.relate_name as relate_name from crm.connection a inner join crm.connection_introduce b on ( a.id in (`+wh+`) and a.position_id=? and a.id=b.connection_id and b.itype=1)`, args...)
  615. if intermediaryArr != nil && len(*intermediaryArr) > 0 {
  616. for _, m := range *intermediaryArr {
  617. buyerId := gconv.String(m["relate_id"])
  618. buyerName := gconv.String(m["relate_name"])
  619. (*dataMap)[buyerId] = map[string]interface{}{
  620. "buyerName": buyerName,
  621. "buyerType": "middleman",
  622. "cId": gconv.String(m["id"]),
  623. }
  624. }
  625. }
  626. }
  627. //招标代理
  628. // if agency != "" {
  629. // agencyArr := []string{}
  630. // for _, s := range strings.Split(agency, ",") {
  631. // agencyArr = append(agencyArr, fmt.Sprintf("'%s'", s))
  632. // }
  633. // sqlStr := fmt.Sprintf(`SELECT buyer_id,buyer FROM transaction_info_all where agency_id in (%s) `, strings.Join(agencyArr, ","))
  634. // FindHandle(sqlStr, dataMap, "agency")
  635. // }
  636. return dataMap
  637. }
  638. // 项目数量查
  639. func (t *OwnerService) ProjectHandle(buyerArr []string, businessStr string, monitorBuyers []string) (*map[string]map[string]interface{}, int64) {
  640. projectMap := &map[string]map[string]interface{}{}
  641. startIndex := (t.PageIndex - 1) * t.PageSize
  642. whBs, allArgs := common.WhArgs(strings.Split(businessStr, ","))
  643. allArgs = append(allArgs, allArgs...)
  644. sqlStr := `select buyer,buyer_id,groupUniqArray(area) as areaStr,count(if(project_bidstatus>1 and hasAny(topscopeclass,[`
  645. sqlStr += whBs + `])=1,1,null)) as ocount,SUM(if(project_bidstatus>1 and hasAny(topscopeclass,[`
  646. sqlStr += whBs + `])=1,project_money,0)) as project_money,MAX(zbtime) as maxzbtime from transaction_info_all`
  647. ors1 := []string{}
  648. if len(buyerArr) > 0 {
  649. wh, args := common.WhArgs(buyerArr)
  650. ors1 = append(ors1, `buyer_id in (`+wh+`)`)
  651. allArgs = append(allArgs, args...)
  652. }
  653. wm := map[string]bool{}
  654. winner_ids := []string{}
  655. if t.Supplier != "" {
  656. for _, v := range strings.Split(t.Supplier, ",") {
  657. if wm[v] {
  658. continue
  659. }
  660. winner_ids = append(winner_ids, v)
  661. wm[v] = true
  662. }
  663. }
  664. if t.Heterotophy != "" {
  665. for _, v := range strings.Split(t.Heterotophy, ",") {
  666. if wm[v] {
  667. continue
  668. }
  669. winner_ids = append(winner_ids, v)
  670. wm[v] = true
  671. }
  672. }
  673. ors2 := []string{}
  674. if len(winner_ids) > 0 {
  675. wh, args := common.WhArgs(winner_ids)
  676. ors2 = append(ors2, `hasAny(winner_id,[`+wh+`])`)
  677. allArgs = append(allArgs, args...)
  678. }
  679. if t.Agency != "" {
  680. wh, args := common.WhArgs(strings.Split(t.Agency, ","))
  681. ors2 = append(ors2, `agency_id in (`+wh+`)`)
  682. allArgs = append(allArgs, args...)
  683. }
  684. if len(ors2) > 0 {
  685. ors1 = append(ors1, `(buyer_id in (select DISTINCT buyer_id from transaction_info_all where (`+strings.Join(ors2, " or ")+`) and buyer_id<>''))`)
  686. }
  687. allArgs = append(allArgs, NetworkCom.ProjectYearLimit())
  688. ands := []string{}
  689. if len(monitorBuyers) > 0 {
  690. wh, args := common.WhArgs(monitorBuyers)
  691. ands = append(ands, `buyer in (`+wh+`)`)
  692. allArgs = append(allArgs, args...)
  693. }
  694. if t.Area != "" {
  695. wh, args := common.WhArgs(strings.Split(t.Area, ","))
  696. ands = append(ands, `area in (`+wh+`)`)
  697. allArgs = append(allArgs, args...)
  698. }
  699. sqlStr += " where (" + strings.Join(ors1, " or ") + ") and zbtime>?"
  700. if len(ands) > 0 {
  701. sqlStr += " and " + strings.Join(ands, " and ")
  702. }
  703. sqlStr += " GROUP by buyer,buyer_id"
  704. if t.SourceType == "1" {
  705. sqlStr += " HAVING ocount>2 "
  706. }
  707. countSql := fmt.Sprintf("select count(1) as count from (%s) b ", sqlStr)
  708. sqlStr += fmt.Sprintf(" order by maxzbtime desc limit %d,%d", startIndex, t.PageSize)
  709. logx.Info("查询列表sql", sqlStr, allArgs)
  710. //总数查询
  711. ass := []BuyerAggStruct{}
  712. rows, err1 := ClickhouseConn.Query(context.TODO(), sqlStr, allArgs...)
  713. if err1 != nil {
  714. logx.Error(err1)
  715. return projectMap, 0
  716. }
  717. defer rows.Close()
  718. for rows.Next() {
  719. as := BuyerAggStruct{}
  720. if err := rows.ScanStruct(&as); err != nil {
  721. logx.Error(err)
  722. continue
  723. }
  724. ass = append(ass, as)
  725. }
  726. for _, object := range ass {
  727. buyerName := object.Buyer
  728. buyerId := object.BuyerId
  729. projectMoney, _ := object.ProjectMoney.Float64()
  730. (*projectMap)[buyerName] = map[string]interface{}{
  731. "area": strings.Join(object.Area, ","),
  732. "zbtime": object.MaxZbtime,
  733. "money": common.RetainDecimal(projectMoney/10000, 2),
  734. "count": object.Ocount,
  735. "buyerId": buyerId,
  736. "buyerName": buyerName,
  737. }
  738. }
  739. return projectMap, T.NetworkCom.Count(countSql, allArgs...)
  740. }
  741. type BuyerAggStruct struct {
  742. Buyer string `ch:"buyer"`
  743. BuyerId string `ch:"buyer_id"`
  744. Area []string `ch:"areaStr"`
  745. ProjectMoney decimal.Decimal `ch:"project_money"`
  746. MaxZbtime int64 `ch:"maxzbtime"`
  747. Ocount uint64 `ch:"ocount"`
  748. }
  749. // 采购单位查询sql
  750. func FindHandle(sqlStr string, dataMap *map[string]map[string]interface{}, buyerType string) {
  751. rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
  752. if err != nil {
  753. logx.Error("查询失败", sqlStr, err)
  754. }
  755. for rows.Next() {
  756. data := Recommend{}
  757. rows.ScanStruct(&data)
  758. if data.BuyerId != "" {
  759. (*dataMap)[data.BuyerId] = map[string]interface{}{
  760. "buyerName": data.Buyer,
  761. "buyerType": buyerType,
  762. }
  763. }
  764. }
  765. return
  766. }
  767. type ConnectionsEntity struct {
  768. Aid string `ch:"a_id"`
  769. Bid string `ch:"b_id"`
  770. Aname string `ch:"a_name"`
  771. Bname string `ch:"b_name"`
  772. Status uint8 `ch:"status"`
  773. Code string `ch:"code"`
  774. Cperson string `ch:"c_person"`
  775. }
  776. type PersonEntity struct {
  777. Id string `ch:"id"`
  778. PersonName string `ch:"personName"`
  779. }
  780. type SupplierConnectionsEntity struct {
  781. Zbtime int64 `ch:"zbtime"`
  782. Project_id string `ch:"project_id"`
  783. Project_name string `ch:"project_name"`
  784. Buyer_id string `ch:"buyer_id"`
  785. Buyer string `ch:"buyer"`
  786. Winner_id string `ch:"wr_id"`
  787. Winner string `ch:"wr"`
  788. Agency_id string `ch:"agency_id"`
  789. Agency string `ch:"agency"`
  790. }
  791. type TimeConnectionsEntity struct {
  792. Zbtime int64 `ch:"zbtime"`
  793. Project_id string `ch:"project_id"`
  794. Project_name string `ch:"project_name"`
  795. }
  796. // 采购单位人脉信息查询sql
  797. func BuyerFindConnectionsHandle(sqlStr string, args ...interface{}) []ConnectionsEntity {
  798. returnData := []ConnectionsEntity{}
  799. rows, err := ClickhouseConn.Query(context.Background(), sqlStr, args...)
  800. if err != nil {
  801. logx.Error("查询失败", sqlStr, err)
  802. }
  803. for rows.Next() {
  804. data := ConnectionsEntity{}
  805. err := rows.ScanStruct(&data)
  806. logx.Info(err)
  807. returnData = append(returnData, data)
  808. }
  809. return returnData
  810. }
  811. func PersonFindConnectionsHandle(sqlStr string, args ...interface{}) []PersonEntity {
  812. returnData := []PersonEntity{}
  813. rows, err := ClickhouseConn.Query(context.Background(), sqlStr, args...)
  814. if err != nil {
  815. logx.Error("查询失败", sqlStr, err)
  816. }
  817. for rows.Next() {
  818. data := PersonEntity{}
  819. rows.ScanStruct(&data)
  820. returnData = append(returnData, data)
  821. }
  822. return returnData
  823. }
  824. // 供应商
  825. func SupplierFindConnectionsHandle(sqlStr string, args ...interface{}) []SupplierConnectionsEntity {
  826. returnData := []SupplierConnectionsEntity{}
  827. rows, err := ClickhouseConn.Query(context.Background(), sqlStr, args...)
  828. if err != nil {
  829. logx.Error("查询失败", sqlStr, err)
  830. }
  831. for rows.Next() {
  832. data := SupplierConnectionsEntity{}
  833. err = rows.ScanStruct(&data)
  834. if err != nil {
  835. logx.Error(err)
  836. }
  837. returnData = append(returnData, data)
  838. }
  839. return returnData
  840. }
  841. // 合作时间查询
  842. func TimeFindConnectionsHandle(sqlStr string) []TimeConnectionsEntity {
  843. returnData := []TimeConnectionsEntity{}
  844. rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
  845. if err != nil {
  846. logx.Error("查询失败", sqlStr, err)
  847. }
  848. for rows.Next() {
  849. data := TimeConnectionsEntity{}
  850. rows.ScanStruct(&data)
  851. returnData = append(returnData, data)
  852. }
  853. return returnData
  854. }
  855. // 去重处理
  856. func Deduplication(a, b string) string {
  857. newArr := []string{}
  858. if b == "全国" {
  859. return a
  860. }
  861. if a != "" {
  862. newArr = append(newArr, a)
  863. }
  864. if b != "" {
  865. newArr = append(newArr, b)
  866. }
  867. return strings.Join(newArr, ",")
  868. }
  869. // 路径列表
  870. func (t *OwnerService) OwnerRoute() []map[string]interface{} {
  871. if t.BuyerId == "" {
  872. return []map[string]interface{}{}
  873. }
  874. routeList := ConnectionsHandle([]string{t.BuyerId}, t.PositionId, false)
  875. for i, routeMap := range routeList {
  876. sourceType := gconv.String(routeMap["sourceType"])
  877. if sourceType == "agency" || sourceType == "supplier" {
  878. data := routeList[i]
  879. data["cooperateCount"] = len(gconv.Maps(data["list"]))
  880. delete(data, "list")
  881. routeList[i] = data
  882. }
  883. }
  884. return routeList
  885. }
  886. // 合作列表
  887. func (t *OwnerService) OwnerCooperate() []map[string]interface{} {
  888. returData := []map[string]interface{}{}
  889. dataArr := []TimeConnectionsEntity{}
  890. if t.CooperateType == "supplier" {
  891. //供应商
  892. supplierSql := fmt.Sprintf(`SELECT
  893. zbtime,
  894. project_id,
  895. project_name
  896. FROM
  897. transaction_info_all
  898. ARRAY
  899. JOIN winner_id as winnerId
  900. where buyer_id= '%s' and winnerId='%s' order by zbtime DESC,project_id DESC `, t.BuyerId, t.WinnerId)
  901. dataArr = TimeFindConnectionsHandle(supplierSql)
  902. } else if t.CooperateType == "agency" {
  903. //代理机构
  904. agentSql := fmt.Sprintf(`select zbtime,project_id,project_name from transaction_info_all where buyer_id ='%s' and agency_id ='%s' order by zbtime DESC,project_id DESC `, t.BuyerId, t.WinnerId)
  905. dataArr = TimeFindConnectionsHandle(agentSql)
  906. }
  907. if dataArr != nil && len(dataArr) > 0 {
  908. for _, m := range dataArr {
  909. zbtime := m.Zbtime
  910. returData = append(returData, map[string]interface{}{
  911. "zbtime": common.If(zbtime == 0, "", time.Unix(zbtime, 10).Format("2006.01.02")),
  912. "entName": t.WinnerName,
  913. "projectId": encrypt.EncodeArticleId2ByCheck(m.Project_id),
  914. "projectName": m.Project_name,
  915. })
  916. }
  917. }
  918. return returData
  919. }
  920. // 物业业务类型查询
  921. func FindBusiness(endId int64, userId string) string {
  922. businessStr := ""
  923. if endId != 0 {
  924. //企业
  925. entInfo := JianyuMysql.FindOne("entniche_info", map[string]interface{}{
  926. "id": endId,
  927. }, "", "")
  928. if (*entInfo)["identity_info"] != nil {
  929. entMap := gconv.Map((*entInfo)["identity_info"])
  930. businessStr = gconv.String(entMap["business"])
  931. }
  932. } else {
  933. //个人
  934. user, ok := Mgo.FindById("user", userId, "")
  935. if ok && user != nil && len(*user) > 0 {
  936. if _, ok1 := (*user)["identity_info"]; !ok1 {
  937. return ""
  938. }
  939. identityInfo := gconv.Map((*user)["identity_info"])
  940. if _, ok1 := identityInfo["is_init"]; !ok1 {
  941. return ""
  942. }
  943. if !gconv.Bool(identityInfo["is_init"]) {
  944. //设置过
  945. businessStr = gconv.String(identityInfo["ent_businessType"])
  946. }
  947. }
  948. }
  949. return businessStr
  950. }