123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- package service
- import (
- . "bp.jydev.jianyu360.cn/CRM/application/api/common"
- "bp.jydev.jianyu360.cn/CRM/application/entity"
- "context"
- "fmt"
- "github.com/gogf/gf/v2/util/gconv"
- "github.com/zeromicro/go-zero/core/logx"
- "strings"
- )
- 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
- }
- 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 int64 `ch:"group_count"`
- Money int64 `ch:"money"`
- Zbtime int64 `ch:"zbtime"`
- }
- func (t *OwnerService) OwnerlList() map[string]interface{} {
- //先查询采购单位列表
- 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 t.SearchEntName != "" {
- //es查询
- }
- //处理状态初始化
- if t.ProcessingStatus != "" {
- ProcessingStatusInit(t.PositionId, dataMap, t.ProcessingStatus)
- }
- //监控状态处理
- MonitorStatusInit(t.PositionId, dataMap, t.SourceType)
- //项目数量查询
- buyerArr := []string{}
- for key := range *dataMap {
- buyerArr = append(buyerArr, fmt.Sprintf(`"%s"`, gconv.String(key)))
- }
- //项目数量处理
- projectMap := ProjectHandle(buyerArr, t.Area)
- //采购单位和项目合并
- returnData, monitorNumber, highSuccessNumber, connectionsNumber := BuyerProjectMerge(dataMap, projectMap, t.SourceType)
- //分页数据处理
- if t.PageSize == 0 {
- t.PageSize = 10
- }
- if t.PageIndex == 0 {
- t.PageIndex = 10
- }
- startIndex := (t.PageIndex - 1) * t.PageSize
- endIndex := t.PageIndex * t.PageSize
- if startIndex < connectionsNumber {
- if endIndex > connectionsNumber {
- endIndex = connectionsNumber
- }
- returnData = returnData[startIndex:endIndex]
- } else {
- return map[string]interface{}{}
- }
- buyerIdArr := []string{}
- for _, value := range returnData {
- buyerIdArr = append(buyerIdArr, fmt.Sprintf("'%s'", value.BuyerId))
- }
- //业主人脉处理
- projectList := ConnectionsHandle(buyerIdArr, t.PositionId, false)
- for _, v := range projectList {
- 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 b.company_id,b.company_name,b.contact_name,a.relate_id,a.relate_name 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.type =1 and b.type=4 and b.state=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": "中间人",
- "relationship": "业主的关系人",
- })
- }
- if isAll && len(returnData) > 0 {
- return returnData
- }
- //采购单位 投资关系 管辖关系
- buyerSql := fmt.Sprintf("select a_id,b_id,a_name,b_name,code from ent_map_code where ( a_name in (%s) or b_name in (%s) ) and code in('0101','0201')", strings.Join(buyerArr, ","), strings.Join(buyerArr, ","))
- relationshipArr := FindConnectionsHandle(buyerSql)
- if relationshipArr != nil && len(relationshipArr) > 0 {
- for _, v := range relationshipArr {
- a_id := gconv.String(v["a_id"])
- b_id := gconv.String(v["b_id"])
- a_name := gconv.String(v["a_name"])
- b_name := gconv.String(v["b_name"])
- for _, v1 := range buyerArr {
- code := gconv.String(v["a_id"])
- if a_id == v1 {
- //key := fmt.Sprintf("%s_%s", a_id, b_id)
- switch code {
- case "0101": //管辖关系
- returnData = append(returnData, map[string]interface{}{
- "b_id": b_id,
- "b_name": b_name,
- "a_name": a_name,
- "a_id": a_id,
- "sourceType": "甲方",
- "relationship": "业主的上级机构",
- })
- case "0201": //投资关系
- returnData = append(returnData, map[string]interface{}{
- "b_id": b_id,
- "b_name": b_name,
- "a_name": a_name,
- "a_id": a_id,
- "sourceType": "业主的股东",
- })
- }
- }
- if b_id == v1 {
- //key := fmt.Sprintf("%s_%s", b_id, a_id)
- if code == "0101" {
- //"0101":管辖关系
- returnData = append(returnData, map[string]interface{}{
- "b_id": a_id,
- "b_name": a_name,
- "a_name": b_name,
- "a_id": b_id,
- "sourceType": "甲方",
- "relationship": "业主的下级机构",
- })
- }
- }
- }
- }
- }
- //供应商 采购单位 供应商 招标代理机构 采购单位 招标代理机构 需要计算合作次数
- winnerSql := fmt.Sprintf("SELECT zbtime,project_id,buyer_id,buyer,winner,winner_id,agency_id,agency FROM transaction_info WHERE buyer_id in (%s)", strings.Join(buyerArr, ","))
- winnerArr := FindConnectionsHandle(winnerSql)
- if winnerArr != nil && len(winnerArr) > 0 {
- winnerMap := map[string]map[string]interface{}{}
- agencyMap := map[string]map[string]interface{}{}
- for _, m := range winnerArr {
- buyerId := gconv.String(m["buyer_id"])
- winner := gconv.String(m["winner"])
- buyer := gconv.String(m["buyer"])
- winnerId := gconv.String(m["winner_id"])
- agency := gconv.String(m["agency"])
- agencyId := gconv.String(m["agency_id"])
- projectId := gconv.String(m["project_id"])
- zbtime := gconv.String(m["zbtime"])
- 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": "供应商",
- })
- 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,
- })
- 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": "供应商",
- "count": 1,
- "list": []map[string]interface{}{
- {
- "zbtime": zbtime,
- "entName": winner,
- },
- },
- }
- }
- 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,
- })
- 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": "招标代理",
- "count": 1,
- "list": []map[string]interface{}{
- {
- "zbtime": zbtime,
- "entName": agency,
- },
- },
- }
- }
- }
- 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"]),
- })
- }
- 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"]),
- })
- }
- }
- 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 {
- if _, ok := (*projectMap)[buyerId]; ok {
- projectMap := (*projectMap)[buyerId]
- buyerName := gconv.String(buyerMap["buyerName"])
- 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: nil,
- },
- 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: nil,
- },
- Area: gconv.String(projectMap["area"]),
- Zbtime: zbtime,
- })
- }
- }
- 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 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(intermediary, ",") {
- intermediaryArr := strings.Split(v, "_")
- if len(intermediaryArr) == 2 {
- (*dataMap)[intermediaryArr[1]] = map[string]interface{}{
- "buyerName": intermediaryArr[0],
- }
- }
- }
- }
- //供应商 //同甲异业
- if supplier != "" || heterotophy != "" {
- findInSetArr := []string{}
- for _, v := range strings.Split(supplier, ",") {
- findInSetArr = append(findInSetArr, fmt.Sprintf(" (%s) in winner_id", v))
- }
- for _, v := range strings.Split(heterotophy, ",") {
- findInSetArr = append(findInSetArr, fmt.Sprintf(" (%s) in winner_id", v))
- }
- sqlStr := fmt.Sprintf(`SELECT buyer, buyer_id FROM transaction_info where (%s) `, strings.Join(findInSetArr, " or "))
- FindHandle(sqlStr, dataMap)
- }
- //中间人
- if intermediary != "" {
- for _, v := range strings.Split(intermediary, ",") {
- intermediaryArr := strings.Split(v, "_")
- if len(intermediaryArr) == 2 {
- (*dataMap)[intermediaryArr[1]] = map[string]interface{}{
- "buyerName": intermediaryArr[0],
- }
- }
- }
- }
- //招标代理
- if agency != "" {
- sqlStr := fmt.Sprintf(`SELECT buyer, buyer_id FROM transaction_info where agency_id in (%s) order by userid`, agency)
- FindHandle(sqlStr, dataMap)
- }
- return dataMap
- }
- // 项目数量查询
- func ProjectHandle(buyerArr []string, area string) *map[string]map[string]interface{} {
- sqlStr := ""
- if area != "" {
- sqlStr = fmt.Sprintf(`SELECT buyer_id,area, count(1) AS group_count FROM information.transaction_info WHERE buyer_id IN (%s) and area ="%s" GROUP BY buyer_id,area `, strings.Join(buyerArr, ","), area)
- } else {
- sqlStr = fmt.Sprintf(`SELECT buyer_id,area, count(1) AS group_count FROM information.transaction_info WHERE buyer_id IN (%s) GROUP BY buyer_id,area `, strings.Join(buyerArr, ","))
- }
- logx.Info(sqlStr)
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
- projectMap := &map[string]map[string]interface{}{}
- if err != nil {
- return projectMap
- }
- for rows.Next() {
- data := Project{}
- rows.ScanStruct(&data)
- if (*projectMap)[data.BuyerId] != nil {
- dataMap := (*projectMap)[data.BuyerId]
- dataMap[data.BuyerId] = map[string]interface{}{
- "area": Deduplication(gconv.String(dataMap["area"]), data.Area),
- "count": gconv.Int64(dataMap["count"]) + data.GroupCount,
- "money": gconv.Int64(dataMap["money"]) + data.Money,
- "zbtime": gconv.Int64(dataMap["Zbtime"]),
- }
- } else {
- (*projectMap)[data.BuyerId] = map[string]interface{}{
- "area": gconv.String(data.Area),
- "count": data.GroupCount,
- "money": data.Money,
- "zbtime": data.Zbtime,
- }
- }
- }
- return projectMap
- }
- // 采购单位查询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
- }
- // 人脉信息查询sql
- func FindConnectionsHandle(sqlStr string) []map[string]interface{} {
- returnData := []map[string]interface{}{}
- rows, err := ClickhouseConn.Query(context.Background(), sqlStr)
- if err != nil {
- logx.Error("查询失败", sqlStr, err)
- }
- for rows.Next() {
- data := map[string]interface{}{}
- rows.ScanStruct(&data)
- returnData = append(returnData, data)
- }
- return returnData
- }
- // 去重处理
- func Deduplication(a, b string) string {
- newArr := []string{}
- if a != "" || b != "" {
- foo := false
- newArr := strings.Split(a, ",")
- for _, v := range newArr {
- if v == b {
- foo = true
- }
- }
- if !foo {
- newArr = append(newArr, b)
- }
- }
- return strings.Join(newArr, ",")
- }
|