owner.go 30 KB

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