123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935 |
- package service
- import (
- "app.yhyue.com/moapp/jybase/encrypt"
- "context"
- "encoding/json"
- "fmt"
- "sort"
- "strings"
- "app.yhyue.com/moapp/jybase/common"
- elastic "app.yhyue.com/moapp/jybase/es"
- . "bp.jydev.jianyu360.cn/CRM/application/api/common"
- "bp.jydev.jianyu360.cn/CRM/application/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"
- )
- type OwnerService struct {
- PartyA string
- Supplier string
- Heterotophy string
- Intermediary string
- Agency string
- SearchEntName string
- SourceType string
- ProcessingStatus string
- Area string
- PositionId int64
- PageIndex int64
- PageSize int64
- BuyerId string
- BuyerName string
- WinnerId string
- WinnerName string
- CooperateType string
- EntAccountId int64
- }
- type BuyerProject struct {
- BuyerId string
- BuyerName string
- Project ProjectEntity
- IsMonitor bool
- IsCreateCustomer bool
- IsIgnore bool
- Area string
- Zbtime int64
- }
- 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"`
- }
- func (t *OwnerService) OwnerlList() map[string]interface{} {
- //先查询采购单位列表
- dataMap := &map[string]map[string]interface{}{}
- projectMap := &map[string]map[string]interface{}{}
- if t.PartyA != "" || t.Supplier != "" || t.Heterotophy != "" || t.Intermediary != "" || t.Agency != "" {
- dataMap = BuyerList(t.PartyA, t.Supplier, t.Heterotophy, t.Intermediary, t.Agency)
- //处理状态初始化
- if t.ProcessingStatus != "" {
- ProcessingStatusInit(t.PositionId, dataMap, t.ProcessingStatus)
- }
- //监控状态处理
- MonitorStatusInit(t.PositionId, dataMap, t.SourceType)
- //项目数量查询
- buyerArr := []string{}
- for _, value := range *dataMap {
- buyerArr = append(buyerArr, fmt.Sprintf(`"%s"`, gconv.String(value["buyerName"])))
- }
- //项目数量处理
- projectMap = ProjectHandle(buyerArr, "", t.Area)
- logx.Info(projectMap)
- } else if t.SearchEntName != "" {
- //1 只看转介绍成功率高2只看已监控的
- if t.SourceType == "2" {
- buyerArr := FindMonitor(t.PositionId, t.SearchEntName)
- if len(buyerArr) > 0 {
- projectMap = ProjectHandle(buyerArr, "", t.Area)
- }
- } else {
- projectMap = ProjectHandle([]string{}, t.SearchEntName, t.Area)
- }
- dataMap = FindBuyer(t.SearchEntName)
- }
- if len(*dataMap) == 0 {
- return map[string]interface{}{}
- }
- //采购单位和项目合并
- returnData, connectionsNumber, highSuccessNumber, monitorNumber := BuyerProjectMerge(dataMap, projectMap, t.SourceType)
- //数组排序
- //分页数据处理
- if t.PageSize == 0 {
- t.PageSize = 10
- }
- if t.PageIndex == 0 {
- t.PageIndex = 1
- }
- startIndex := (t.PageIndex - 1) * t.PageSize
- endIndex := t.PageIndex * t.PageSize
- sort.Slice(returnData, func(i, j int) bool {
- return returnData[i].Zbtime < returnData[j].Zbtime
- })
- if startIndex < gconv.Int64(len(returnData)) {
- if endIndex > gconv.Int64(len(returnData)) {
- endIndex = gconv.Int64(len(returnData))
- }
- returnData = returnData[startIndex:endIndex]
- } else {
- return map[string]interface{}{}
- }
- buyerIdArr := []string{}
- for _, value := range returnData {
- buyerIdArr = append(buyerIdArr, fmt.Sprintf("'%s'", value.BuyerId))
- }
- //可介绍业主人脉处理
- companyList := ConnectionsHandle(buyerIdArr, t.PositionId, false)
- for _, v := range companyList {
- 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)
- }
- }
- }
- //返回数据组装
- return map[string]interface{}{
- "connectionsNumber": connectionsNumber,
- "highSuccessNumber": highSuccessNumber,
- "monitorNumber": monitorNumber,
- "list": returnData,
- }
- }
- // 可介绍业主人脉列表
- func ConnectionsHandle(buyerArr []string, positionId int64, isAll bool) []map[string]interface{} {
- returnData := []map[string]interface{}{}
- //中间人 作为可介绍业主 企业名称
- companyArr := CrmMysql.SelectBySql(
- 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, ",")))
- 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"])
- //key := fmt.Sprintf("%s_%s", relateId, companyId)
- 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"]),
- })
- }
- if isAll && len(returnData) > 0 {
- return returnData
- }
- //采购单位 投资关系 管辖关系
- //buyerSql := fmt.Sprintf("select a_id,b_id,a_name,b_name,code from ent_map_code a where ( a_name in (%s) or b_name in (%s) ) and code in('0101','0201')", strings.Join(buyerArr, ","), strings.Join(buyerArr, ","))
- buyerSql := fmt.Sprintf(`select a.a_id as a_id,a.b_id as b_id,a.a_name as a_name,a.b_name as b_name,a.code as code,c.legal_person as c_person,d.legal_person d_person from ent_map_code a
- left join ent_info c on (a.a_id in (%s) or a.b_id in (%s) ) and a.code in('0101','0201') and a.a_id =c.id
- left join ent_info d on (a.a_id in (%s) or a.b_id in (%s) ) and a.code in('0101','0201') and a.b_id =d.id
- where (a.a_id in (%s) or a.b_id in (%s) ) and a.code in('0101','0201')`,
- strings.Join(buyerArr, ","),
- strings.Join(buyerArr, ","),
- strings.Join(buyerArr, ","),
- strings.Join(buyerArr, ","),
- strings.Join(buyerArr, ","),
- strings.Join(buyerArr, ","))
- relationshipArr := BuyerFindConnectionsHandle(buyerSql)
- if relationshipArr != nil && len(relationshipArr) > 0 {
- for _, v := range relationshipArr {
- for _, v1 := range buyerArr {
- code := v.Code
- if fmt.Sprintf("'%s'", v.Aid) == v1 {
- //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": v.Dperson,
- })
- 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": v.Dperson,
- })
- }
- }
- if fmt.Sprintf("'%s'", v.Bid) == v1 {
- //key := fmt.Sprintf("%s_%s", b_id, a_id)
- if code == "0101" {
- //"0101":管辖关系
- returnData = append(returnData, map[string]interface{}{
- "b_id": v.Aid,
- "b_name": v.Aname,
- "a_name": v.Bname,
- "a_id": v.Bid,
- "sourceType": "firstparty",
- "relationship": "业主的下级机构",
- "personName": v.Cperson,
- })
- }
- }
- }
- }
- }
- //供应商 采购单位 供应商 招标代理机构 采购单位 招标代理机构 需要计算合作次数
- winnerSql := fmt.Sprintf(`SELECT DISTINCT a.project_name as project_name,a.project_id as project_id , a.zbtime as zbtime ,a.buyer_id as buyer_id,a.buyer as buyer,a.agency_id as agency_id,a.agency as agency,c.company_name as company_name ,b.name as personName ,a.winnerId as awinnerId
- FROM
- (select zbtime,project_id,project_name,project_id,buyer_id,buyer,agency_id,agency,winner_id as winnerId from transaction_info
- ARRAY JOIN winner_id ) a
- inner join ent_contact b on a.buyer_id in (%s) and a.winnerId=b.id
- inner join ent_info c on a.winnerId=c.id order by a.zbtime,a.winnerId`, strings.Join(buyerArr, ","))
- winnerArr := SupplierFindConnectionsHandle(winnerSql)
- if winnerArr != nil && len(winnerArr) > 0 {
- winnerMap := map[string]map[string]interface{}{}
- agencyMap := map[string]map[string]interface{}{}
- for _, m := range winnerArr {
- buyerId := m.Buyer_id
- winner := m.Company_name
- buyer := m.Buyer
- winnerId := m.AwinnerId
- agency := m.Agency
- agencyId := m.Agency_id
- projectId := encrypt.EncodeArticleId2ByCheck(m.Project_id)
- projectName := m.Project_name
- zbtime := m.Zbtime
- personName := m.PersonName
- key := fmt.Sprintf("%s_%s", buyerId, winnerId)
- returnData = append(returnData, map[string]interface{}{
- "b_id": winnerId,
- "b_name": winner,
- "a_name": buyer,
- "a_id": buyerId,
- "sourceType": "supplier",
- "personName": personName,
- })
- if _, ok := winnerMap[key]; ok {
- data := winnerMap[key]
- data["count"] = gconv.Int64(data["data"]) + 1
- dataList := gconv.Maps(data["list"])
- dataList = append(dataList, map[string]interface{}{
- "zbtime": zbtime,
- "entName": winner,
- "projectId": projectId,
- "projectName": projectName,
- })
- data["list"] = dataList
- winnerMap[key] = data
- } else {
- winnerMap[key] = map[string]interface{}{
- "b_id": winnerId,
- "b_name": winner,
- "a_name": buyer,
- "a_id": buyerId,
- "sourceType": "supplier",
- "personName": personName,
- "count": 1,
- "list": []map[string]interface{}{
- {
- "zbtime": zbtime,
- "entName": winner,
- "projectId": projectId,
- "projectName": projectName,
- },
- },
- }
- }
- key = fmt.Sprintf("%s_%s_%s", buyerId, agencyId, projectId)
- if _, ok := agencyMap[key]; ok {
- data := agencyMap[key]
- data["count"] = gconv.Int64(data["data"]) + 1
- dataList := gconv.Maps(data["list"])
- dataList = append(dataList, map[string]interface{}{
- "zbtime": zbtime,
- "entName": winner,
- "projectId": projectId,
- "projectName": projectName,
- })
- data["list"] = dataList
- agencyMap[key] = data
- } else {
- agencyMap[key] = map[string]interface{}{
- "b_id": agencyId,
- "b_name": agency,
- "a_name": buyer,
- "a_id": buyerId,
- "sourceType": "agency",
- "count": 1,
- "personName": personName,
- "list": []map[string]interface{}{
- {
- "zbtime": zbtime,
- "entName": agency,
- "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"]),
- "list": m["list"],
- })
- }
- 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"]),
- "count": gconv.Int64(m["count"]),
- "list": m["list"],
- })
- }
- }
- return returnData
- }
- // 采购单位和项目合并
- func BuyerProjectMerge(dataMap, projectMap *map[string]map[string]interface{}, sourceType string) ([]BuyerProject, int64, int64, int64) {
- returnData := []BuyerProject{}
- connectionsNumber := int64(0)
- highSuccessNumber := int64(0)
- monitorNumber := int64(0)
- for buyerId, buyerMap := range *dataMap {
- buyerName := gconv.String(buyerMap["buyerName"])
- if _, ok := (*projectMap)[buyerId]; ok {
- projectMap := (*projectMap)[buyerId]
- if isMonitor := gconv.Bool(buyerMap["isMonitor"]); isMonitor {
- monitorNumber++
- }
- count := gconv.Int64(projectMap["count"])
- money := gconv.Int64(projectMap["money"])
- zbtime := gconv.Int64(projectMap["zbtime"])
- if sourceType == "1" {
- if count < 2 {
- continue
- }
- highSuccessNumber++
- returnData = append(returnData, BuyerProject{
- BuyerId: buyerId,
- BuyerName: buyerName,
- Project: ProjectEntity{
- Number: count,
- EstimatedAmount: money,
- Connections: []map[string]interface{}{},
- },
- Area: gconv.String(projectMap["area"]),
- Zbtime: zbtime,
- })
- } else {
- if count > 2 {
- highSuccessNumber++
- }
- returnData = append(returnData, BuyerProject{
- BuyerId: buyerId,
- BuyerName: buyerName,
- Project: ProjectEntity{
- Number: count,
- EstimatedAmount: money,
- Connections: []map[string]interface{}{},
- },
- Area: gconv.String(projectMap["area"]),
- Zbtime: zbtime,
- })
- }
- } else {
- returnData = append(returnData, BuyerProject{
- BuyerId: buyerId,
- BuyerName: buyerName,
- Project: ProjectEntity{
- Number: 0,
- EstimatedAmount: 0,
- Connections: []map[string]interface{}{},
- },
- Area: "",
- Zbtime: 0,
- })
- }
- connectionsNumber++
- }
- return returnData, connectionsNumber, highSuccessNumber, monitorNumber
- }
- // 已监控数据处理
- func MonitorStatusInit(positionId int64, dataMap *map[string]map[string]interface{}, sourceType string) {
- monitorMap := map[string]bool{}
- query := map[string]interface{}{
- "userId": positionId,
- }
- clist, ok := Mgo.Find("follow_customer", query, ``, `{"name":1}`, false, -1, -1)
- if ok && clist != nil && len(*clist) > 0 {
- for _, v := range *clist {
- monitorMap[gconv.String(v["name"])] = true
- }
- }
- //采购单位和自己监控的对比
- newMap := &map[string]map[string]interface{}{}
- for k, v := range *dataMap {
- buyerName := gconv.String(v["buyerName"])
- if _, ok1 := monitorMap[gconv.String(buyerName)]; ok1 {
- v["isMonitor"] = true
- (*newMap)[k] = v
- } else {
- if sourceType != "2" && sourceType != "1" {
- v["isMonitor"] = false
- (*newMap)[k] = v
- }
- }
- }
- if newMap != nil {
- dataMap = newMap
- }
- }
- // 监控数据查询初始化
- func FindMonitor(positionId int64, entName string) []string {
- nameArr := []string{}
- clist, ok := Mgo.Find("", map[string]interface{}{
- "userId": positionId,
- "name": fmt.Sprintf(" /.*%s.*/i", entName),
- }, "", `{"name":1}`, false, -1, -1)
- if ok && clist != nil && len(*clist) > 0 {
- for _, v := range *clist {
- nameArr = append(nameArr, fmt.Sprintf(`"%s"`, gconv.String(v["name"])))
- }
- }
- return nameArr
- }
- // 处理状态初始化
- func ProcessingStatusInit(positionId int64, dataMap *map[string]map[string]interface{}, processingStatus string) {
- //未处理
- untreatedMap := &map[string]bool{}
- //已忽略
- ignoredMap := &map[string]bool{}
- //已创建
- createdMap := &map[string]bool{}
- processingArr := CrmMysql.Find(entity.CONNECTION_STATUS, map[string]interface{}{
- "type": 1,
- "position id": positionId,
- }, "relate_id,is_handle,is_ignore,is_create", "", -1, -1)
- if 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
- }
- }
- }
- newMap := &map[string]map[string]interface{}{}
- //所有采购单位和处理状态对比
- for buyerId, value := range *dataMap {
- for _, v := range strings.Split(processingStatus, ",") {
- switch v {
- case "1":
- if _, ok := (*untreatedMap)[buyerId]; !ok {
- (*newMap)[buyerId] = value
- }
- case "2":
- if _, ok := (*ignoredMap)[buyerId]; ok {
- (*newMap)[buyerId] = value
- }
- case "3":
- if _, ok := (*createdMap)[buyerId]; ok {
- (*newMap)[buyerId] = value
- }
- }
- }
- }
- if newMap != nil {
- dataMap = newMap
- }
- }
- // 采购单位查询
- func BuyerList(partyA, supplier, heterotophy, intermediary, agency string) *map[string]map[string]interface{} {
- dataMap := &map[string]map[string]interface{}{}
- //甲方
- if partyA != "" {
- for _, v := range strings.Split(partyA, ",") {
- intermediaryArr := strings.Split(v, "_")
- if len(intermediaryArr) == 2 {
- (*dataMap)[intermediaryArr[0]] = map[string]interface{}{
- "buyerName": intermediaryArr[1],
- }
- }
- }
- }
- //供应商 //同甲异业
- if supplier != "" || heterotophy != "" {
- findInSetArr := []string{}
- for _, v := range strings.Split(supplier, ",") {
- findInSetArr = append(findInSetArr, fmt.Sprintf("'%s'", v))
- }
- for _, v := range strings.Split(heterotophy, ",") {
- findInSetArr = append(findInSetArr, fmt.Sprintf("'%s'", v))
- }
- sqlStr := fmt.Sprintf(`SELECT buyer, buyer_id FROM transaction_info where hasAny(winner_id,[%s]) `, strings.Join(findInSetArr, ","))
- FindHandle(sqlStr, dataMap)
- }
- //中间人
- if intermediary != "" {
- for _, v := range strings.Split(intermediary, ",") {
- intermediaryArr := strings.Split(v, "_")
- if len(intermediaryArr) == 2 {
- (*dataMap)[intermediaryArr[0]] = map[string]interface{}{
- "buyerName": intermediaryArr[1],
- }
- }
- }
- }
- //招标代理
- if agency != "" {
- agencyArr := []string{}
- for _, s := range strings.Split(agency, ",") {
- agencyArr = append(agencyArr, fmt.Sprintf("'%s'", s))
- }
- sqlStr := fmt.Sprintf(`SELECT buyer, buyer_id FROM transaction_info where agency_id in (%s) order by userid`, strings.Join(agencyArr, ","))
- FindHandle(sqlStr, dataMap)
- }
- return dataMap
- }
- // 项目数量查询
- func ProjectHandle(buyerArr []string, entName, area string) *map[string]map[string]interface{} {
- projectMap := &map[string]map[string]interface{}{}
- sql := ""
- sqlStr := ""
- if entName != "" {
- sql = `{"query":{"bool":{"must":[{"terms":{"buyer":[%s]%s}}]}},"aggs":{"buyer_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"project_count":{"terms":{"field":"winner"}}}},"area_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"area_count":{"terms":{"field":"area"}}}},"money_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"money_count":{"sum":{"field":"bidendtime"}}}},"zbtime_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"zbtime_count":{"max":{"field":"bidendtime"}}}}},"size":%d}`
- sqlStr = fmt.Sprintf(sql, strings.Join(buyerArr, ","), common.If(area != "", fmt.Sprintf(`,{"term":{"area":"%s"}}`, area), ""), len(buyerArr))
- } else {
- sql = `{"query":{"bool":{"must":[{"terms":{"buyer":[%s]%s}}]}},"aggs":{"buyer_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"project_count":{"terms":{"field":"winner"}}}},"area_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"area_count":{"terms":{"field":"area"}}}},"money_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"money_count":{"sum":{"field":"bidendtime"}}}},"zbtime_count":{"terms":{"field":"buyer","size":1000,"order":{"_count":"desc"}},"aggs":{"zbtime_count":{"max":{"field":"bidendtime"}}}}},"size":%d}`
- sqlStr = fmt.Sprintf(sql, strings.Join(buyerArr, ","), common.If(area != "", fmt.Sprintf(`,{"term":{"area":"%s"}}`, area), ""), len(buyerArr))
- }
- data, _, _ := elastic.GetAggs(INDEX, TYPE, sqlStr)
- for name, object := range data {
- switch name {
- case "buyer_count":
- buyerArr := BuyerAggStruct{}
- bs, ok := object.MarshalJSON()
- if ok != nil {
- logx.Error("解析项目数据失败", object)
- continue
- }
- json.Unmarshal(bs, &buyerArr)
- for _, v := range buyerArr.Buckets {
- key := v.Key
- if (*projectMap)[key] != nil {
- value := (*projectMap)[key]
- value["count"] = v.Doc_count
- (*projectMap)[key] = value
- } else {
- (*projectMap)[key] = map[string]interface{}{
- "count": v.Doc_count,
- }
- }
- }
- case "area_count":
- areaArr := AreaAggStruct{}
- bs, ok := object.MarshalJSON()
- if ok != nil {
- logx.Error("解析项目数据失败", object)
- continue
- }
- json.Unmarshal(bs, &areaArr)
- logx.Info(areaArr)
- for _, v := range areaArr.Buckets {
- key := v.Key
- area := ""
- for _, v1 := range v.Area_count.Buckets {
- area = Deduplication(area, v1.Key)
- }
- if (*projectMap)[key] != nil {
- value := (*projectMap)[key]
- value["area"] = area
- (*projectMap)[key] = value
- } else {
- (*projectMap)[key] = map[string]interface{}{
- "area": area,
- }
- }
- }
- case "money_count":
- moneyArr := MoneyAggStruct{}
- bs, ok := object.MarshalJSON()
- if ok != nil {
- logx.Error("解析项目数据失败", object)
- continue
- }
- json.Unmarshal(bs, &moneyArr)
- logx.Info(moneyArr)
- for _, v := range moneyArr.Buckets {
- key := v.Key
- if (*projectMap)[key] != nil {
- value := (*projectMap)[key]
- value["money"] = v.Money_count.Value
- (*projectMap)[key] = value
- } else {
- (*projectMap)[key] = map[string]interface{}{
- "money": v.Money_count.Value,
- }
- }
- }
- case "zbtime_count":
- timeArr := TimeAggStruct{}
- bs, ok := object.MarshalJSON()
- if ok != nil {
- logx.Error("解析项目数据失败", object)
- continue
- }
- a := json.Unmarshal(bs, &timeArr)
- logx.Info(timeArr, a)
- for _, v := range timeArr.Buckets {
- key := v.Key
- if (*projectMap)[key] != nil {
- value := (*projectMap)[key]
- value["zbtime"] = v.Zbtime_count.Value
- (*projectMap)[key] = value
- } else {
- (*projectMap)[key] = map[string]interface{}{
- "zbtime": v.Zbtime_count.Value,
- }
- }
- }
- }
- }
- return projectMap
- }
- type TimeAggStruct struct {
- Buckets []struct {
- Key string `json:"key,omitempty"`
- Zbtime_count struct {
- Value interface{} `json:"value,omitempty"`
- } `json:"zbtime_count,omitempty"`
- }
- }
- type MoneyAggStruct struct {
- Buckets []struct {
- Key string `json:"key,omitempty"`
- Money_count struct {
- Value interface{} `json:"value,omitempty"`
- } `json:"money_count,omitempty"`
- }
- }
- type BuyerAggStruct struct {
- Buckets []struct {
- Key string `json:"key,omitempty"`
- Doc_count int64 `json:"doc_count,omitempty"`
- Buyer_count struct {
- value string `json:"value,omitempty"`
- } `json:"buyer_count,omitempty"`
- }
- }
- type AreaAggStruct struct {
- Buckets []struct {
- Key string `json:"key,omitempty"`
- Area_count struct {
- Buckets []struct {
- Key string `json:"key,omitempty"`
- Doc_count int64 `json:"doc_count,omitempty"`
- }
- } `json:"area_count"`
- }
- }
- // 采购单位查询sql
- func FindHandle(sqlStr string, dataMap *map[string]map[string]interface{}) {
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
- if err != nil {
- logx.Error("查询失败", sqlStr, err)
- }
- for rows.Next() {
- data := Recommend{}
- rows.ScanStruct(&data)
- (*dataMap)[data.buyerId] = map[string]interface{}{
- "buyerName": data.buyer,
- }
- }
- return
- }
- type ConnectionsEntity struct {
- Aid string `ch:"a_id"`
- Bid string `ch:"b_id"`
- Aname string `ch:"a_name"`
- Bname string `ch:"b_name"`
- Code string `ch:"code"`
- Cperson string `ch:"c_person"`
- Dperson string `ch:"d_person"`
- }
- type SupplierConnectionsEntity struct {
- Project_name string `ch:"project_name"`
- Project_id string `ch:"project_id"`
- Zbtime int64 `ch:"zbtime"`
- Buyer_id string `ch:"buyer_id"`
- Buyer string `ch:"buyer"`
- Agency_id string `ch:"agency_id"`
- Agency string `ch:"agency"`
- Company_name string `ch:"company_name"`
- PersonName string `ch:"personName"`
- AwinnerId string `ch:"awinnerId"`
- }
- type TimeConnectionsEntity struct {
- Zbtime int64 `ch:"zbtime"`
- Project_id string `ch:"project_id"`
- Project_name string `ch:"project_name"`
- }
- // 采购单位人脉信息查询sql
- func BuyerFindConnectionsHandle(sqlStr string) []ConnectionsEntity {
- returnData := []ConnectionsEntity{}
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
- if err != nil {
- logx.Error("查询失败", sqlStr, err)
- }
- for rows.Next() {
- data := ConnectionsEntity{}
- rows.ScanStruct(&data)
- returnData = append(returnData, data)
- }
- return returnData
- }
- // 供应商
- func SupplierFindConnectionsHandle(sqlStr string) []SupplierConnectionsEntity {
- returnData := []SupplierConnectionsEntity{}
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
- if err != nil {
- logx.Error("查询失败", sqlStr, err)
- }
- for rows.Next() {
- data := SupplierConnectionsEntity{}
- err = rows.ScanStruct(&data)
- logx.Info(err)
- returnData = append(returnData, data)
- }
- 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 FindBuyer(searchName string) *map[string]map[string]interface{} {
- sql := `{"query":{"bool":{"must":[{"match":{"company_name":"%s"}}]}},"sort":[{"_id":"asc"}],"_source":["id","company_name"],"from":0,"size":10}`
- sqlStr := fmt.Sprintf(sql, searchName)
- data := elastic.Get(EntINDEX, EntTYPE, sqlStr)
- dataMap := &map[string]map[string]interface{}{}
- for _, m := range *data {
- id := gconv.String(m["id"])
- name := gconv.String(m["company_name"])
- (*dataMap)[id] = map[string]interface{}{
- "buyerName": name,
- }
- }
- return dataMap
- }
- // 路径列表
- func (t *OwnerService) OwnerRoute() []map[string]interface{} {
- companyList := ConnectionsHandle([]string{
- fmt.Sprintf("'%s'", t.BuyerId),
- }, t.PositionId, false)
- return companyList
- }
- // 合作列表
- func (t *OwnerService) OwnerCooperate() []map[string]interface{} {
- returData := []map[string]interface{}{}
- dataArr := []TimeConnectionsEntity{}
- if t.CooperateType == "supplier" {
- //供应商
- supplierSql := fmt.Sprintf(`SELECT DISTINCT a.zbtime as zbtime,a.project_id as project_id,a.project_name as project_name
- FROM
- (select zbtime,project_id,project_name,winner_id as winnerId from transaction_info
- ARRAY JOIN winner_id ) a
- inner join ent_contact b on a.buyer_id = '%s' and a.winnerId='%s' and a.winnerId=b.id
- inner join ent_info c on a.winnerId=c.id order by a.zbtime,a.winnerId`, t.BuyerId, t.WinnerId)
- dataArr = TimeFindConnectionsHandle(supplierSql)
- } else if t.CooperateType == "agency" {
- //代理机构
- agentSql := fmt.Sprintf(`select zbtime,project_id,project_name from transaction_info where buyer_id ='%s' and agency_id ='%s' order by zbtime,winner_id `, t.BuyerId, t.WinnerId)
- dataArr = TimeFindConnectionsHandle(agentSql)
- }
- if dataArr != nil && len(dataArr) > 0 {
- for _, m := range dataArr {
- zbtime := m.Zbtime
- returData = append(returData, map[string]interface{}{
- "zbtime": zbtime,
- "entName": t.WinnerName,
- "project_id": encrypt.EncodeArticleId2ByCheck(m.Project_id),
- "project_name": m.Project_name,
- })
- }
- }
- return returData
- }
- func (t *OwnerService) CandidateChannel() []*ResultData {
- returnData := []*ResultData{}
- dataMap := &map[string]map[string]interface{}{}
- if t.PartyA != "" || t.Supplier != "" || t.Heterotophy != "" || t.Intermediary != "" || t.Agency != "" {
- dataMap = BuyerList(t.PartyA, t.Supplier, t.Heterotophy, t.Intermediary, t.Agency)
- }
- if len(*dataMap) > 0 && dataMap != nil {
- propertyForm := ""
- m1 := CrmMysql.FindOne("config_tenant", map[string]interface{}{"account_id": t.EntAccountId}, "probusfor", "")
- if m1 != nil && len(*m1) > 0 {
- propertyForm = common.ObjToString((*m1)["probusfor"])
- }
- for buyerId := range *dataMap {
- logx.Info(buyerId)
- r1, r2 := GetData(propertyForm, buyerId)
- if r1 != nil && len(r1) > 0 {
- tmp := &ResultData{
- channelType: 1,
- channel: "同甲异业渠道",
- size: len(r1),
- data: r1,
- }
- returnData = append(returnData, tmp)
- }
- if r2 != nil && len(r2) > 0 {
- tmp := &ResultData{
- channelType: 1,
- channel: "招标代理机构",
- size: len(r2),
- data: r2,
- }
- returnData = append(returnData, tmp)
- }
- r3 := GetData1(t.PositionId, buyerId)
- if r3 != nil && len(r3) > 0 {
- tmp := &ResultData{
- channelType: 1,
- channel: "中间人",
- size: len(r3),
- data: r3,
- }
- returnData = append(returnData, tmp)
- }
- GetData2(buyerId)
- }
- }
- return returnData
- }
|