1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006 |
- package service
- import (
- "context"
- "encoding/json"
- "fmt"
- "sort"
- "strings"
- "time"
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/encrypt"
- "app.yhyue.com/moapp/jybase/redis"
- . "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
- T "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
- "bp.jydev.jianyu360.cn/CRM/networkManage/entity"
- "github.com/gogf/gf/v2/util/gconv"
- "github.com/shopspring/decimal"
- "github.com/zeromicro/go-zero/core/logx"
- )
- const (
- EntINDEX = "ent_info"
- EntTYPE = "ent_info"
- NetworkManageOwnerlList = "networkManage_ownerlList_%d_%s"
- )
- type OwnerService struct {
- PartyA string
- Supplier string
- Heterotophy string
- Intermediary string
- Agency string
- SearchEntName string
- SourceType string
- Area string
- PositionId int64
- PageIndex int64
- PageSize int64
- BuyerId string
- BuyerName string
- WinnerId string
- WinnerName string
- CooperateType string
- EntAccountId int64
- ProjectType string
- Type string
- EntId int64
- UserId string
- }
- type BuyerProject struct {
- BuyerId string
- BuyerName string
- Project ProjectEntity
- IsMonitor bool
- Area string
- Zbtime int64
- BuyerType string
- CId string
- }
- type ProjectEntity struct {
- Number int64
- zbtime int64
- EstimatedAmount int64
- Connections []map[string]interface{}
- }
- type Recommend struct {
- BuyerId string `ch:"buyer_id"`
- Buyer string `ch:"buyer"`
- }
- type Project struct {
- BuyerId string `ch:"buyer_id"`
- Area string `ch:"area"`
- GroupCount uint64 `ch:"group_count"`
- Zbtime int64 `ch:"zbtime"`
- Money decimal.Decimal `ch:"money"`
- }
- type OwnerlListRes struct {
- ReturnData []BuyerProject
- ConnectionsNumber int64
- }
- // OwnerlList 执行主要的业务逻辑
- func (t *OwnerService) OwnerlList() *OwnerlListRes {
- // 先查询采购单位列表
- startTime := time.Now().Unix()
- businessStr := FindBusiness(t.EntId, t.UserId)
- // 初始化分页参数
- if t.PageSize == 0 {
- t.PageSize = 10
- }
- if t.PageIndex == 0 {
- t.PageIndex = 1
- }
- olr := &OwnerlListRes{
- ReturnData: []BuyerProject{},
- }
- // 如果没有业务字符串,直接返回空数据
- if businessStr == "" {
- return olr
- }
- //监控状态处理
- monitorMap := NetworkCom.EntMonitor(gconv.String(t.PositionId))
- comKey := common.GetMd5String(fmt.Sprintf("%+v business:%s", t, businessStr))
- listKey := fmt.Sprintf(NetworkManageOwnerlList, t.PositionId, comKey)
- getFromCache := false
- monitorBuyers := []string{}
- if t.SourceType != "2" {
- // 检查 Redis 中是否存在列表缓存
- if rb, err := redis.GetNewBytes("newother", listKey); err == nil && rb != nil {
- if json.Unmarshal(*rb, &olr) == nil {
- getFromCache = true
- }
- }
- } else {
- for k, _ := range monitorMap {
- monitorBuyers = append(monitorBuyers, k)
- }
- }
- // 没有缓存时的处理逻辑
- if !getFromCache {
- dataMap := BuyerList(t.PartyA, t.Intermediary, t.PositionId)
- if len(*dataMap) == 0 && t.Supplier == "" && t.Heterotophy == "" && t.Agency == "" {
- return olr
- }
- // 项目数量查询
- buyerArr := getBuyerArr(dataMap)
- // 项目数量处理
- projectMap, connectionsNumber := t.ProjectHandle(buyerArr, businessStr, monitorBuyers)
- endTime := time.Now().Unix()
- fmt.Println("用时时间:", endTime-startTime)
- if len(*projectMap) == 0 {
- return olr
- }
- olr.ConnectionsNumber = connectionsNumber
- // 采购单位和项目合并
- a1 := time.Now().Unix()
- olr.ReturnData = BuyerProjectMerge(dataMap, projectMap)
- a2 := time.Now().Unix()
- fmt.Println("组合数据", a2-a1)
- /*if len(returnData) > 0 {
- //redis.Put("newother", comKey, returnData, C.CacheTimeOut)
- sort.Slice(returnData, func(i, j int) bool {
- aa := fmt.Sprintf("%d%s", (returnData)[i].Zbtime, (returnData)[i].BuyerId)
- bb := fmt.Sprintf("%d%s", (returnData)[j].Zbtime, (returnData)[j].BuyerId)
- return aa > bb
- })
- }*/
- buyerIdArr := []string{}
- for _, value := range olr.ReturnData {
- if value.BuyerId != "" {
- buyerIdArr = append(buyerIdArr, value.BuyerId)
- }
- }
- // 可介绍业主人脉处理
- a3 := time.Now().Unix()
- routeList := ConnectionsHandle(buyerIdArr, t.PositionId, false)
- a4 := time.Now().Unix()
- fmt.Println("组合数1111据", a4-a3)
- updateReturnDataWithRoutes(olr.ReturnData, routeList)
- }
- if len(olr.ReturnData) > 0 {
- for k, value := range olr.ReturnData {
- if _, ok := monitorMap[value.BuyerName]; ok {
- olr.ReturnData[k].IsMonitor = true
- } else {
- olr.ReturnData[k].IsMonitor = false
- }
- }
- redis.Put("newother", listKey, olr, C.CacheTimeOut)
- }
- // 返回数据组装
- return olr
- }
- // getBuyerArr 从数据映射中获取买家数组
- func getBuyerArr(dataMap *map[string]map[string]interface{}) []string {
- buyerArr := []string{}
- for k, _ := range *dataMap {
- buyerArr = append(buyerArr, k)
- }
- return buyerArr
- }
- // updateReturnDataWithRoutes 将路线信息更新到返回数据中
- func updateReturnDataWithRoutes(returnData []BuyerProject, routeList []map[string]interface{}) {
- for _, v := range routeList {
- buyerId := gconv.String(v["a_id"])
- for i, v1 := range returnData {
- aBuyerId := v1.BuyerId
- if buyerId == aBuyerId {
- // 组装数据
- returnData[i].Project.Connections = append(returnData[i].Project.Connections, v)
- }
- }
- }
- }
- // 可介绍业主人脉列表
- func ConnectionsHandle(buyerArr []string, positionId int64, isAll bool) []map[string]interface{} {
- returnData := []map[string]interface{}{}
- a1 := time.Now().Unix()
- //中间人
- returnData = FindMiddleman(buyerArr, positionId, returnData)
- a2 := time.Now().Unix()
- fmt.Println("中间人", a2-a1)
- if isAll && len(returnData) > 0 {
- return returnData
- }
- //采购单位 投资关系 管辖关系
- //采购单位
- a3 := time.Now().Unix()
- returnData = Findfirstparty(buyerArr, returnData)
- a4 := time.Now().Unix()
- fmt.Println("采购单位", a4-a3)
- if isAll && len(returnData) > 0 {
- return returnData
- }
- a5 := time.Now().Unix()
- returnData = append(returnData, Findwinner(buyerArr)...)
- a6 := time.Now().Unix()
- logx.Info("供应商", a6-a5)
- return returnData
- }
- // 供应商处理
- func Findwinner(buyerArr []string) []map[string]interface{} {
- returnData := []map[string]interface{}{}
- if len(buyerArr) == 0 {
- return returnData
- }
- wh1, args1 := common.WhArgs(buyerArr)
- args1 = append(args1, args1...)
- //供应商 采购单位 供应商 招标代理机构 采购单位 招标代理机构 需要计算合作次数
- winnerSql := `select * from (select
- zbtime,
- project_id,
- project_name,
- buyer_id,
- buyer,
- entId,
- 'supplier' as entType
- from
- transaction_info_all ARRAY
- JOIN winner_id as entId
- where
- buyer_id in (` + wh1 + `)
- UNION ALL
- select
- zbtime,
- project_id,
- project_name,
- buyer_id,
- buyer,
- agency_id as entId,
- 'agent' as entType
- from
- transaction_info_all
- where
- buyer_id in (` + wh1 + `) and agency_id!='') order by zbtime DESC,project_id DESC`
- winnerArr := SupplierFindConnectionsHandle(winnerSql, args1...)
- if winnerArr != nil && len(winnerArr) > 0 {
- //企业联系人处理
- entIdArr := []string{}
- for _, v := range winnerArr {
- if v.EntId != "" {
- entIdArr = append(entIdArr, v.EntId)
- }
- }
- personMap := map[string]string{}
- entNameMap := map[string]string{}
- if len(entIdArr) > 0 {
- wh2, args2 := common.WhArgs(entIdArr)
- personSql := `select id,name as personName from ent_contact where id in (` + wh2 + `) ORDER by name`
- personArr := PersonFindConnectionsHandle(personSql, args2...)
- //联系人处理
- for _, v := range personArr {
- personMap[v.Id] = v.PersonName
- }
- entSql := `select id ,company_name as personName from ent_info where id in (` + wh2 + `) ORDER by company_name`
- entNameArr := PersonFindConnectionsHandle(entSql, args2...)
- for _, v := range entNameArr {
- entNameMap[v.Id] = v.PersonName
- }
- }
- winnerMap := map[string]map[string]interface{}{}
- agencyMap := map[string]map[string]interface{}{}
- agencyIdMap := map[string]bool{}
- for _, m := range winnerArr {
- if m.EntId == "" {
- continue
- }
- projectId := encrypt.EncodeArticleId2ByCheck(m.Project_id)
- projectName := m.Project_name
- zbtime := m.Zbtime
- key := fmt.Sprintf("%s_%s", m.Buyer_id, m.EntId)
- if m.EntType == "supplier" {
- buyerId := m.Buyer_id
- winner := entNameMap[m.EntId]
- buyer := m.Buyer
- if _, ok := winnerMap[key]; ok {
- data := winnerMap[key]
- data["count"] = gconv.Int64(data["count"]) + 1
- dataList := gconv.Maps(data["list"])
- dataList = append(dataList, map[string]interface{}{
- "zbtime": zbtime,
- "projectId": projectId,
- "projectName": projectName,
- })
- data["list"] = dataList
- data["zbtime"] = zbtime
- winnerMap[key] = data
- } else {
- winnerMap[key] = map[string]interface{}{
- "b_id": m.EntId,
- "b_name": entNameMap[m.EntId],
- "a_name": buyer,
- "a_id": buyerId,
- "sourceType": "supplier",
- "zbtime": zbtime,
- "personName": personMap[m.EntId],
- "count": 1,
- "list": []map[string]interface{}{
- {
- "zbtime": zbtime,
- "entName": winner,
- "projectId": projectId,
- "projectName": projectName,
- },
- },
- }
- }
- } else {
- agencyId := m.EntId
- key = fmt.Sprintf("%s_%s", m.Buyer_id, agencyId)
- if _, ok := agencyIdMap[key]; ok {
- data := agencyMap[key]
- data["count"] = gconv.Int64(data["count"]) + 1
- dataList := gconv.Maps(data["list"])
- dataList = append(dataList, map[string]interface{}{
- "zbtime": zbtime,
- "projectId": projectId,
- "projectName": projectName,
- })
- data["list"] = dataList
- data["zbtime"] = zbtime
- agencyMap[key] = data
- } else {
- agencyIdMap[key] = true
- agencyMap[key] = map[string]interface{}{
- "b_id": m.EntId,
- "b_name": entNameMap[m.EntId],
- "a_name": m.Buyer,
- "a_id": m.Buyer_id,
- "sourceType": "agency",
- "count": 1,
- "zbtime": zbtime,
- "personName": gconv.String(personMap[agencyId]),
- "list": []map[string]interface{}{
- {
- "zbtime": zbtime,
- "projectId": projectId,
- "projectName": projectName,
- },
- },
- }
- }
- }
- }
- for _, m := range winnerMap {
- returnData = append(returnData, map[string]interface{}{
- "b_id": gconv.String(m["b_id"]),
- "b_name": gconv.String(m["b_name"]),
- "a_name": gconv.String(m["a_name"]),
- "a_id": gconv.String(m["a_id"]),
- "sourceType": gconv.String(m["sourceType"]),
- "relationship": gconv.String(m["relationship"]),
- "count": gconv.Int64(m["count"]),
- "personName": gconv.String(m["personName"]),
- "list": m["list"],
- "zbtime": m["zbtime"],
- })
- }
- for _, m := range agencyMap {
- returnData = append(returnData, map[string]interface{}{
- "b_id": gconv.String(m["b_id"]),
- "b_name": gconv.String(m["b_name"]),
- "a_name": gconv.String(m["a_name"]),
- "a_id": gconv.String(m["a_id"]),
- "sourceType": gconv.String(m["sourceType"]),
- "relationship": gconv.String(m["relationship"]),
- "personName": gconv.String(m["personName"]),
- "count": gconv.Int64(m["count"]),
- "list": m["list"],
- "zbtime": m["zbtime"],
- })
- }
- }
- sort.Slice(returnData, func(i, j int) bool {
- aa := fmt.Sprintf("%s%s", gconv.String(returnData[i]["zbtime"]),
- gconv.String(returnData[i]["b_id"]))
- bb := fmt.Sprintf("%s%s", gconv.String(returnData[j]["zbtime"]),
- gconv.String(returnData[j]["b_id"]))
- return aa > bb
- })
- return returnData
- }
- // 中间人查询
- func FindMiddleman(buyerArr []string, positionId int64, returnData []map[string]interface{}) []map[string]interface{} {
- if len(buyerArr) == 0 {
- return returnData
- }
- //中间人 作为可介绍业主 企业名称
- wh, args := common.WhArgs(buyerArr)
- newArgs := []interface{}{positionId}
- newArgs = append(newArgs, args...)
- 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...)
- for _, v := range *companyArr {
- companyId := gconv.String(v["company_id"])
- companyName := gconv.String(v["company_name"])
- relateName := gconv.String(v["relate_name"])
- relateId := gconv.String(v["relate_id"])
- returnData = append(returnData, map[string]interface{}{
- "b_id": companyId,
- "b_name": companyName,
- "a_name": relateName,
- "a_id": relateId,
- "sourceType": "middleman",
- "relationship": "业主的关系人",
- "personName": gconv.String(v["contact_person"]),
- })
- }
- return returnData
- }
- // 企业之间关系查询
- func Findfirstparty(buyerArr []string, returnData []map[string]interface{}) []map[string]interface{} {
- if len(buyerArr) == 0 {
- return returnData
- }
- wh1, args1 := common.WhArgs(buyerArr)
- args1 = append(args1, args1...)
- buyerSql := `select DISTINCT
- a.a_id as a_id,
- a.b_id as b_id,
- a.a_name as a_name,
- a.b_name as b_name,
- 111 as status,
- a.code as code
- from
- ent_map_code a
- where
- a.a_id in (` + wh1 + `)
- and a.code in('0101', '0201')
- UNION ALL
- select
- a.b_id as a_id,
- a.a_id as b_id,
- a.b_name as a_name,
- a.a_name as b_name,
- 222 as status,
- a.code as code
- from
- ent_map_code a
- where
- a.b_id in (` + wh1 + `)
- and a.code in('0101', '0201')`
- relationshipArr := BuyerFindConnectionsHandle(buyerSql, args1...)
- if relationshipArr != nil && len(relationshipArr) > 0 {
- entIdArr := []string{}
- for _, v := range relationshipArr {
- if v.Bid != "" {
- entIdArr = append(entIdArr, v.Bid)
- }
- }
- personMap := map[string]string{}
- if len(entIdArr) > 0 {
- wh2, args2 := common.WhArgs(entIdArr)
- personSql := `select id,name as personName from ent_contact where id in (` + wh2 + `) ORDER by name`
- personArr := PersonFindConnectionsHandle(personSql, args2...)
- //联系人处理
- for _, v := range personArr {
- personMap[v.Id] = v.PersonName
- }
- }
- for _, v := range relationshipArr {
- code := v.Code
- status := v.Status
- if gconv.String(status) == "111" {
- //key := fmt.Sprintf("%s_%s", a_id, b_id)
- switch code {
- case "0101": //管辖关系
- returnData = append(returnData, map[string]interface{}{
- "b_id": v.Bid,
- "b_name": v.Bname,
- "a_name": v.Aname,
- "a_id": v.Aid,
- "sourceType": "firstparty",
- "relationship": "业主的下级机构",
- "personName": gconv.String(personMap[v.Bid]),
- })
- case "0201": //投资关系
- returnData = append(returnData, map[string]interface{}{
- "b_id": v.Bid,
- "b_name": v.Bname,
- "a_name": v.Aname,
- "a_id": v.Aid,
- "sourceType": "firstparty",
- "relationship": "业主的股东",
- "personName": gconv.String(personMap[v.Bid]),
- })
- }
- }
- if gconv.String(status) == "222" {
- //key := fmt.Sprintf("%s_%s", b_id, a_id)
- if code == "0101" {
- //"0101":管辖关系
- returnData = append(returnData, map[string]interface{}{
- "b_id": v.Bid,
- "b_name": v.Bname,
- "a_name": v.Aname,
- "a_id": v.Aid,
- "sourceType": "firstparty",
- "relationship": "业主的上级机构",
- "personName": gconv.String(personMap[v.Bid]),
- })
- }
- if code == "0201" {
- returnData = append(returnData, map[string]interface{}{
- "b_id": v.Bid,
- "b_name": v.Bname,
- "a_name": v.Aname,
- "a_id": v.Aid,
- "sourceType": "firstparty",
- "relationship": "业主的股东",
- "personName": gconv.String(personMap[v.Bid]),
- })
- }
- }
- }
- }
- return returnData
- }
- // 采购单位和项目合并
- func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}) []BuyerProject {
- returnData := []BuyerProject{}
- for buyerName, project := range *projectMap {
- buyerId := gconv.String(project["buyerId"])
- data := (*dataMap)[buyerId]
- returnData = append(returnData, BuyerProject{
- BuyerId: buyerId,
- BuyerName: buyerName,
- BuyerType: gconv.String(data["buyerType"]),
- Project: ProjectEntity{
- Number: gconv.Int64(project["count"]),
- EstimatedAmount: gconv.Int64(project["money"]),
- Connections: []map[string]interface{}{},
- },
- Area: gconv.String(project["area"]),
- Zbtime: gconv.Int64(project["area"]),
- IsMonitor: gconv.Bool(data["isMonitor"]),
- CId: gconv.String(data["cId"]),
- })
- }
- return returnData
- }
- func FindStatus(positionId int64) (map[string]bool, map[string]bool, map[string]bool) {
- //未处理
- untreatedMap := map[string]bool{}
- //已忽略
- ignoredMap := map[string]bool{}
- //已创建
- createdMap := map[string]bool{}
- processingArr := CrmMysql.Find(entity.CONNECTION_STATUS, map[string]interface{}{
- "itype": 1,
- "position_id": positionId,
- }, "relate_id,is_handle,is_ignore,is_create", "", -1, -1)
- if processingArr != nil && len(*processingArr) > 0 {
- for _, v := range *processingArr {
- entId := gconv.String(v["relate_id"])
- handle := gconv.Int64(v["is_handle"])
- ignore := gconv.Int64(v["is_ignore"])
- create := gconv.Int64(v["is_create"])
- if handle == 1 {
- untreatedMap[entId] = true
- }
- if ignore == 1 {
- ignoredMap[entId] = true
- }
- if create == 1 {
- createdMap[entId] = true
- }
- }
- }
- return untreatedMap, ignoredMap, createdMap
- }
- // 采购单位查询
- func BuyerList(partyA, intermediary string, positionId int64) *map[string]map[string]interface{} {
- dataMap := &map[string]map[string]interface{}{}
- //甲方
- if partyA != "" {
- for _, v := range strings.Split(partyA, ",") {
- (*dataMap)[v] = map[string]interface{}{
- "buyerType": "firstparty",
- }
- }
- // intermediaryArr := []string{}
- // for _, v := range strings.Split(partyA, ",") {
- // if v != "" {
- // intermediaryArr = append(intermediaryArr, fmt.Sprintf("'%s'", v))
- // }
- // }
- // if len(partyA) > 0 {
- // partyASql := fmt.Sprintf(`select id as buyer_id,company_name as buyer from ent_info where id in (%s)`, strings.Join(intermediaryArr, ","))
- // FindHandle(partyASql, dataMap, "firstparty")
- // }
- }
- //供应商 //同甲异业
- // findInSetArr := []string{}
- // if supplier != "" {
- // for _, v := range strings.Split(supplier, ",") {
- // if v != "" {
- // findInSetArr = append(findInSetArr, fmt.Sprintf("'%s'", v))
- // }
- // }
- // }
- // if len(findInSetArr) > 0 {
- // sqlStr := fmt.Sprintf(`SELECT buyer_id,buyer FROM transaction_info_all where hasAny(winner_id,[%s]) `, strings.Join(findInSetArr, ","))
- // FindHandle(sqlStr, dataMap, "supplier")
- // }
- //同甲异业
- // findheterotophyArr := []string{}
- // if heterotophy != "" {
- // for _, v := range strings.Split(heterotophy, ",") {
- // if v != "" {
- // findheterotophyArr = append(findheterotophyArr, fmt.Sprintf("'%s'", v))
- // }
- // }
- // }
- // if len(findheterotophyArr) > 0 {
- // sqlStr := fmt.Sprintf(`SELECT buyer_id,buyer FROM transaction_info_all where hasAny(winner_id,[%s]) `, strings.Join(findheterotophyArr, ","))
- // FindHandle(sqlStr, dataMap, "adiffb")
- // }
- //中间人
- if intermediary != "" {
- wh, args := common.WhArgs(strings.Split(intermediary, ","))
- args = append(args, positionId)
- 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...)
- if intermediaryArr != nil && len(*intermediaryArr) > 0 {
- for _, m := range *intermediaryArr {
- buyerId := gconv.String(m["relate_id"])
- buyerName := gconv.String(m["relate_name"])
- (*dataMap)[buyerId] = map[string]interface{}{
- "buyerName": buyerName,
- "buyerType": "middleman",
- "cId": gconv.String(m["id"]),
- }
- }
- }
- }
- //招标代理
- // if agency != "" {
- // agencyArr := []string{}
- // for _, s := range strings.Split(agency, ",") {
- // agencyArr = append(agencyArr, fmt.Sprintf("'%s'", s))
- // }
- // sqlStr := fmt.Sprintf(`SELECT buyer_id,buyer FROM transaction_info_all where agency_id in (%s) `, strings.Join(agencyArr, ","))
- // FindHandle(sqlStr, dataMap, "agency")
- // }
- return dataMap
- }
- // 项目数量查
- func (t *OwnerService) ProjectHandle(buyerArr []string, businessStr string, monitorBuyers []string) (*map[string]map[string]interface{}, int64) {
- projectMap := &map[string]map[string]interface{}{}
- startIndex := (t.PageIndex - 1) * t.PageSize
- whBs, allArgs := common.WhArgs(strings.Split(businessStr, ","))
- allArgs = append(allArgs, allArgs...)
- sqlStr := `select b.buyer as buyer,b.buyer_id as buyer_id,groupUniqArray(b.area) as areaStr,count(if(hasAny(b.topscopeclass,[`
- sqlStr += whBs + `])=1 and b.project_bidstatus>1,1,null)) as ocount,SUM(if(hasAny(b.topscopeclass,[`
- sqlStr += whBs + `])=1 and b.project_bidstatus>1,b.project_money,0)) as project_money,MAX(b.zbtime) as zbtime from transaction_info_all a inner join transaction_info_all b on`
- ors1 := []string{}
- if len(buyerArr) > 0 {
- wh, args := common.WhArgs(buyerArr)
- ors1 = append(ors1, `(a.buyer_id in (`+wh+`) and a.project_id=b.project_id)`)
- allArgs = append(allArgs, args...)
- }
- ors2 := []string{}
- winner_ids := []string{}
- if t.Supplier != "" {
- winner_ids = append(winner_ids, strings.Split(t.Supplier, ",")...)
- }
- if t.Heterotophy != "" {
- winner_ids = append(winner_ids, strings.Split(t.Heterotophy, ",")...)
- }
- if len(winner_ids) > 0 {
- wh, args := common.WhArgs(winner_ids)
- ors2 = append(ors2, `hasAny(a.winner_id,[`+wh+`])`)
- allArgs = append(allArgs, args...)
- }
- if t.Agency != "" {
- wh, args := common.WhArgs(strings.Split(t.Agency, ","))
- ors2 = append(ors2, `a.agency_id in (`+wh+`)`)
- allArgs = append(allArgs, args...)
- }
- if len(ors2) > 0 {
- ors1 = append(ors1, "(("+strings.Join(ors2, " or ")+") and a.buyer_id<>'' and a.buyer_id=b.buyer_id)")
- }
- sqlStr += " (" + strings.Join(ors1, " or ") + ")"
- wheres := []string{}
- if len(monitorBuyers) > 0 {
- wh, args := common.WhArgs(monitorBuyers)
- wheres = append(wheres, `b.buyer in (`+wh+`)`)
- allArgs = append(allArgs, args...)
- }
- if t.Area != "" {
- wh, args := common.WhArgs(strings.Split(t.Area, ","))
- wheres = append(wheres, `b.area in (`+wh+`)`)
- allArgs = append(allArgs, args...)
- }
- if len(wheres) > 0 {
- sqlStr += " where " + strings.Join(wheres, " and ")
- }
- sqlStr += " GROUP by b.buyer,b.buyer_id"
- if t.SourceType == "1" {
- sqlStr += " HAVING ocount>2 "
- }
- countSql := fmt.Sprintf("select count(1) as count from (%s) b ", sqlStr)
- sqlStr += fmt.Sprintf(" order by zbtime desc limit %d,%d", startIndex, t.PageSize)
- //总数查询
- ass := []BuyerAggStruct{}
- rows, err1 := ClickhouseConn.Query(context.TODO(), sqlStr, allArgs...)
- if err1 != nil {
- logx.Error(err1)
- return projectMap, 0
- }
- defer rows.Close()
- for rows.Next() {
- as := BuyerAggStruct{}
- if err := rows.ScanStruct(&as); err != nil {
- logx.Error(err)
- continue
- }
- ass = append(ass, as)
- }
- for _, object := range ass {
- buyerName := object.Buyer
- buyerId := object.BuyerId
- projectMoney, _ := object.ProjectMoney.Float64()
- (*projectMap)[buyerName] = map[string]interface{}{
- "area": strings.Join(object.Area, ","),
- "zbtime": object.Zbtime,
- "money": common.RetainDecimal(projectMoney/10000, 2),
- "count": object.Ocount,
- "buyerId": buyerId,
- "buyerName": buyerName,
- }
- }
- return projectMap, T.NetworkCom.Count(countSql, allArgs...)
- }
- type BuyerAggStruct struct {
- Buyer string `ch:"buyer"`
- BuyerId string `ch:"buyer_id"`
- Area []string `ch:"areaStr"`
- ProjectMoney decimal.Decimal `ch:"project_money"`
- Zbtime int64 `ch:"zbtime"`
- Ocount uint64 `ch:"ocount"`
- }
- // 采购单位查询sql
- func FindHandle(sqlStr string, dataMap *map[string]map[string]interface{}, buyerType string) {
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
- if err != nil {
- logx.Error("查询失败", sqlStr, err)
- }
- for rows.Next() {
- data := Recommend{}
- rows.ScanStruct(&data)
- if data.BuyerId != "" {
- (*dataMap)[data.BuyerId] = map[string]interface{}{
- "buyerName": data.Buyer,
- "buyerType": buyerType,
- }
- }
- }
- return
- }
- type ConnectionsEntity struct {
- Aid string `ch:"a_id"`
- Bid string `ch:"b_id"`
- Aname string `ch:"a_name"`
- Bname string `ch:"b_name"`
- Status uint8 `ch:"status"`
- Code string `ch:"code"`
- Cperson string `ch:"c_person"`
- }
- type PersonEntity struct {
- Id string `ch:"id"`
- PersonName string `ch:"personName"`
- }
- type SupplierConnectionsEntity struct {
- Zbtime int64 `ch:"zbtime"`
- Project_id string `ch:"project_id"`
- Project_name string `ch:"project_name"`
- Buyer_id string `ch:"buyer_id"`
- Buyer string `ch:"buyer"`
- EntId string `ch:"entId"`
- EntType string `ch:"entType"`
- }
- type SupplierConnectionsEntitys struct {
- Zbtime int64
- Project_id string
- Project_name string
- Buyer_id string
- Buyer string
- EntId string
- EntType string
- }
- type TimeConnectionsEntity struct {
- Zbtime int64 `ch:"zbtime"`
- Project_id string `ch:"project_id"`
- Project_name string `ch:"project_name"`
- }
- // 采购单位人脉信息查询sql
- func BuyerFindConnectionsHandle(sqlStr string, args ...interface{}) []ConnectionsEntity {
- returnData := []ConnectionsEntity{}
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr, args...)
- if err != nil {
- logx.Error("查询失败", sqlStr, err)
- }
- for rows.Next() {
- data := ConnectionsEntity{}
- err := rows.ScanStruct(&data)
- logx.Info(err)
- returnData = append(returnData, data)
- }
- return returnData
- }
- func PersonFindConnectionsHandle(sqlStr string, args ...interface{}) []PersonEntity {
- returnData := []PersonEntity{}
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr, args...)
- if err != nil {
- logx.Error("查询失败", sqlStr, err)
- }
- for rows.Next() {
- data := PersonEntity{}
- rows.ScanStruct(&data)
- returnData = append(returnData, data)
- }
- return returnData
- }
- // 供应商
- func SupplierFindConnectionsHandle(sqlStr string, args ...interface{}) []SupplierConnectionsEntitys {
- returnData := []SupplierConnectionsEntitys{}
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr, args...)
- if err != nil {
- logx.Error("查询失败", sqlStr, err)
- }
- for rows.Next() {
- data := SupplierConnectionsEntity{}
- err = rows.ScanStruct(&data)
- if err != nil {
- logx.Error(err)
- }
- returnData = append(returnData, SupplierConnectionsEntitys{
- Zbtime: data.Zbtime,
- Project_id: data.Project_id,
- Project_name: data.Project_name,
- Buyer_id: data.Buyer_id,
- Buyer: data.Buyer,
- EntId: data.EntId,
- EntType: data.EntType,
- })
- }
- return returnData
- }
- // 合作时间查询
- func TimeFindConnectionsHandle(sqlStr string) []TimeConnectionsEntity {
- returnData := []TimeConnectionsEntity{}
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
- if err != nil {
- logx.Error("查询失败", sqlStr, err)
- }
- for rows.Next() {
- data := TimeConnectionsEntity{}
- rows.ScanStruct(&data)
- returnData = append(returnData, data)
- }
- return returnData
- }
- // 去重处理
- func Deduplication(a, b string) string {
- newArr := []string{}
- if b == "全国" {
- return a
- }
- if a != "" {
- newArr = append(newArr, a)
- }
- if b != "" {
- newArr = append(newArr, b)
- }
- return strings.Join(newArr, ",")
- }
- // 路径列表
- func (t *OwnerService) OwnerRoute() []map[string]interface{} {
- if t.BuyerId == "" {
- return []map[string]interface{}{}
- }
- routeList := ConnectionsHandle([]string{t.BuyerId}, t.PositionId, false)
- for i, routeMap := range routeList {
- sourceType := gconv.String(routeMap["sourceType"])
- if sourceType == "agency" || sourceType == "supplier" {
- data := routeList[i]
- data["cooperateCount"] = len(gconv.Maps(data["list"]))
- delete(data, "list")
- routeList[i] = data
- }
- }
- return routeList
- }
- // 合作列表
- func (t *OwnerService) OwnerCooperate() []map[string]interface{} {
- returData := []map[string]interface{}{}
- dataArr := []TimeConnectionsEntity{}
- if t.CooperateType == "supplier" {
- //供应商
- supplierSql := fmt.Sprintf(`SELECT
- zbtime,
- project_id,
- project_name
-
- FROM
- transaction_info_all
- ARRAY
- JOIN winner_id as winnerId
- where buyer_id= '%s' and winnerId='%s' order by zbtime DESC,project_id DESC `, t.BuyerId, t.WinnerId)
- dataArr = TimeFindConnectionsHandle(supplierSql)
- } else if t.CooperateType == "agency" {
- //代理机构
- 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)
- dataArr = TimeFindConnectionsHandle(agentSql)
- }
- if dataArr != nil && len(dataArr) > 0 {
- for _, m := range dataArr {
- zbtime := m.Zbtime
- returData = append(returData, map[string]interface{}{
- "zbtime": common.If(zbtime == 0, "", time.Unix(zbtime, 10).Format("2006.01.02")),
- "entName": t.WinnerName,
- "projectId": encrypt.EncodeArticleId2ByCheck(m.Project_id),
- "projectName": m.Project_name,
- })
- }
- }
- return returData
- }
- // 物业业务类型查询
- func FindBusiness(endId int64, userId string) string {
- businessStr := ""
- if endId != 0 {
- //企业
- entInfo := JianyuMysql.FindOne("entniche_info", map[string]interface{}{
- "id": endId,
- }, "", "")
- if (*entInfo)["identity_info"] != nil {
- entMap := gconv.Map((*entInfo)["identity_info"])
- businessStr = gconv.String(entMap["business"])
- }
- } else {
- //个人
- user, ok := Mgo.FindById("user", userId, "")
- if ok && user != nil && len(*user) > 0 {
- if _, ok1 := (*user)["identity_info"]; !ok1 {
- return ""
- }
- identityInfo := gconv.Map((*user)["identity_info"])
- if _, ok1 := identityInfo["is_init"]; !ok1 {
- return ""
- }
- if !gconv.Bool(identityInfo["is_init"]) {
- //设置过
- businessStr = gconv.String(identityInfo["ent_businessType"])
- }
- }
- }
- return businessStr
- }
|