owner.go 30 KB

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