123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- package service
- import (
- "app.yhyue.com/moapp/jybase/common"
- elastic "app.yhyue.com/moapp/jybase/es"
- T "bp.jydev.jianyu360.cn/CRM/application/api/common"
- "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
- "context"
- "encoding/json"
- "fmt"
- "github.com/zeromicro/go-zero/core/logx"
- "time"
- )
- var (
- INDEX_1 = "transaction_info"
- sql_2_0 = `SELECT buyer, buyer_id, agency, agency_id, property_form FROM information.transaction_info WHERE project_id = ?`
- sql_2_1 = `SELECT project_id, project_name, zbtime FROM information.transaction_info WHERE buyer_id = ? AND winner winner_id IN ? ORDER BY zbtime DESC`
- sql_2_2 = `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 b.position_id =%d AND a.connection_id = b.id AND b.state = 1 AND a.itype = 1 AND b.itype = 4 AND a.relate_Id = %s`
- sql_2_3 = `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 = ? or a.b_id = ? ) and a.code in('0101', '0201') and a.a_id = c.id left join ent_info d on (a.a_id = ? or a.b_id = ? ) and a.code in('0101', '0201') and a.b_id = d.id where (a.a_id = ? or a.b_id = ? ) and a.code in('0101', '0201')`
- es_query = `{
- "query": {
- "bool": {
- "must": [
- {
- "range": {
- "zbtime": {
- "gte": %d
- }
- }
- },
- {
- "term": {
- "buyer_id": %s
- }
- }
- ],
- "must_not": {
- "term": {
- "property_form": %s
- }
- }
- }
- },
- "aggs": {
- "winner_count": {
- "terms": {
- "field": "winner",
- "size": 100,
- "order": {
- "_count": "desc"
- }
- },
- "aggs": {
- "amount_all": {
- "sum": {
- "field": "bidamount"
- }
- }
- }
- },
- "agency_count": {
- "terms": {
- "field": "agency",
- "size": 100,
- "order": {
- "_count": "desc"
- }
- },
- "aggs": {
- "amount_all": {
- "sum": {
- "field": "bidamount"
- }
- }
- }
- }
- },
- "size": 1
- }`
- )
- type ResultData struct {
- channelType int `json:"channel_type"`
- channel string `json:"channel"`
- size int `json:"size"`
- data []map[string]interface{} `json:"data"`
- }
- func GetPrList(req *types.CoopHistoryReq) (result []*ResultData) {
- pMap := make(map[string]interface{})
- err := T.ClickhouseConn.QueryRow(context.TODO(), sql_2_0, req.Pid).Scan(&pMap)
- if err != nil {
- return nil
- }
- propertyForm := ""
- m1 := T.CrmMysql.FindOne("config_tenant", map[string]interface{}{"account_id": req.EntAccountId}, "probusfor", "")
- if m1 != nil && len(*m1) > 0 {
- propertyForm = common.ObjToString((*m1)["probusfor"])
- }
- // 1、同甲异业数据/招标代理机构渠道
- if propertyForm != "" {
- r1, r2 := GetData(propertyForm, common.ObjToString(pMap["buyer_id"]))
- if r1 != nil && len(r1) > 0 {
- tmp := &ResultData{
- channelType: 1,
- channel: "同甲异业渠道",
- size: len(r1),
- data: r1,
- }
- result = append(result, tmp)
- }
- if r2 != nil && len(r2) > 0 {
- tmp := &ResultData{
- channelType: 1,
- channel: "招标代理机构",
- size: len(r2),
- data: r2,
- }
- result = append(result, tmp)
- }
- }
- // 中间人可介绍业主
- r3 := GetData1(req.PositionId, common.ObjToString(pMap["buyer_id"]))
- if r3 != nil && len(r3) > 0 {
- tmp := &ResultData{
- channelType: 1,
- channel: "中间人",
- size: len(r3),
- data: r3,
- }
- result = append(result, tmp)
- }
- // 关联单位
- GetData2(common.ObjToString(pMap["buyer_id"]))
- return
- }
- func GetData(propertyForm, bid string) (result1, result2 []map[string]interface{}) {
- timestamp := time.Now().AddDate(-3, 0, 0).Unix()
- aggs, count, res := elastic.GetAggs(INDEX_1, INDEX_1, fmt.Sprintf(es_query, timestamp, propertyForm, bid))
- logx.Info("es聚合查询结果:", aggs, count, res)
- type AggStruct struct {
- Buckets []struct {
- Key string `json:"key,omitempty"`
- Doc_count int64 `json:"doc_count,omitempty"`
- Amount_all struct {
- Value string `json:"value,omitempty"`
- } `json:"amount_all"`
- } `json:"buckets"`
- }
- m1 := make(map[string]interface{}) //采购单位-中标单位
- m2 := make(map[string]interface{}) //采购单位-代理机构
- var m1Buckets = AggStruct{}
- if aggs != nil && aggs["winner_count"] != nil {
- bs, err := aggs["winner_count"].MarshalJSON()
- if err != nil {
- logx.Info(err)
- } else {
- if len(bs) == 0 {
- logx.Info(err)
- } else {
- err := json.Unmarshal(bs, &m1Buckets)
- logx.Info(err)
- if len(m1Buckets.Buckets) > 0 {
- for _, v := range m1Buckets.Buckets {
- m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
- }
- }
- }
- }
- }
- var m2Buckets = AggStruct{}
- if aggs != nil && aggs["amount_all"] != nil {
- bs, err := aggs["amount_all"].MarshalJSON()
- if err != nil {
- logx.Info(err)
- } else {
- if len(bs) == 0 {
- logx.Info(err)
- } else {
- err := json.Unmarshal(bs, &m2Buckets)
- logx.Info(err)
- if len(m2Buckets.Buckets) > 0 {
- for _, v := range m2Buckets.Buckets {
- m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
- }
- }
- }
- }
- }
- for k, v := range m1 {
- if m2[k] != nil {
- tmp := make(map[string]interface{})
- tmp["name"] = v
- tmp["coop_size"] = v
- tmp["coop_amount"] = m2[k]
- result1 = append(result1, tmp)
- }
- }
- for k, v := range m2 {
- if m2[k] != nil {
- tmp := make(map[string]interface{})
- tmp["name"] = v
- tmp["coop_size"] = v
- tmp["coop_amount"] = m2[k]
- result2 = append(result2, tmp)
- }
- }
- return
- }
- // @Author jianghan
- // @Description 中间人
- // @Date 2024/4/20
- func GetData1(positionId int64, buyid string) (result []map[string]interface{}) {
- info := T.CrmMysql.SelectBySql(sql_2_2, positionId, buyid)
- if info != nil && len(*info) > 0 {
- for _, m := range *info {
- result = append(result, map[string]interface{}{"name": m["company_name"], "pserson": m["contact_name"]})
- }
- }
- return
- }
- // @Author jianghan
- // @Description 业主关联企业 上下级/股权控制
- // @Date 2024/4/20
- func GetData2(buyid string) (result []map[string]interface{}) {
- info := T.CrmMysql.SelectBySql(sql_2_3, buyid, buyid, buyid, buyid, buyid, buyid)
- if info != nil && len(*info) > 0 {
- for _, m := range *info {
- result = append(result, map[string]interface{}{"name": m["company_name"], "pserson": m["contact_name"]})
- }
- }
- return
- }
- // @Author jianghan
- // @Description 合作历史
- // @Date 2024/4/20
- func GetData3(buyerId, winnerId string) (result []map[string]interface{}) {
- rows, err := T.ClickhouseConn.Query(context.TODO(), sql_2_1, buyerId, winnerId)
- for err != nil && rows.Next() {
- m := make(map[string]interface{})
- rows.Scan(&m)
- result = append(result, m)
- }
- return
- }
|