CoopHistoryService.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. elastic "app.yhyue.com/moapp/jybase/es"
  5. T "bp.jydev.jianyu360.cn/CRM/application/api/common"
  6. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  7. "context"
  8. "encoding/json"
  9. "fmt"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. "time"
  12. )
  13. var (
  14. INDEX_1 = "transaction_info"
  15. sql_2_0 = `SELECT buyer, buyer_id, agency, agency_id, property_form FROM information.transaction_info WHERE project_id = ?`
  16. sql_2_1 = `SELECT project_id, project_name, zbtime FROM information.transaction_info WHERE buyer_id = ? AND has(winner_id, ?) ORDER BY zbtime DESC`
  17. sql_2_2 = `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 = ? and a.itype =1 and b.itype=4 and b.status=1 `
  18. 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 crm.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')`
  19. es_query = `{"query": {"bool": {"must": [{"term": {"buyer": "%s"}}],"must_not": {"term": {"property_form": "%s"}}}},"aggs": {"winner_count": {"terms": {"field": "winner","size": 1000,"order": {"_count": "desc"}},"aggs": {"amount_all": {"sum": {"field": "bidamount"}}}}},"size": 1}`
  20. es_query1 = `{"query": {"bool": {"must": [{"term": {"buyer": "%s"}}]}},"aggs": {"agency_count": {"terms": {"field": "agency","size": 1000,"order": {"_count": "desc"}},"aggs": {"amount_all": {"sum": {"field": "bidamount"}}}}},"size": 1}`
  21. )
  22. type ResultData struct {
  23. SourceType string `json:"SourceType"`
  24. EntName string `json:"EntName"`
  25. EntPerson string `json:"EntPerson"`
  26. Middleman string `json:"Middleman"`
  27. ProjectNum int `json:"ProjectNum"`
  28. TotalAmount float64 `json:"TotalAmount"`
  29. RecentTime int64 `json:"RecentTime"`
  30. NearlyYears bool `json:"NearlyYears"`
  31. SupEnt string `json:"SupEnt"` // 上级
  32. NextEnt string `json:"NextEnt"` // 下级
  33. Relationship string `json:"Relationship"` // 关系
  34. }
  35. type ProjectTmp struct {
  36. Buyer string `ch:"buyer"`
  37. BuyerId string `ch:"buyer_id"`
  38. Agency string `ch:"agency"`
  39. AgencyId string `ch:"agency_id"`
  40. PropertyForm []string `ch:"property_form"`
  41. }
  42. func GetPrList(req *types.CoopHistoryReq) (result []*ResultData) {
  43. pTmp := ProjectTmp{}
  44. err := T.ClickhouseConn.QueryRow(context.TODO(), sql_2_0, req.Pid).ScanStruct(&pTmp)
  45. if err != nil {
  46. return nil
  47. }
  48. propertyForm := ""
  49. m1 := T.CrmMysql.FindOne("config_tenant", map[string]interface{}{"account_id": req.EntAccountId}, "probusfor", "")
  50. if m1 != nil && len(*m1) > 0 {
  51. propertyForm = common.ObjToString((*m1)["probusfor"])
  52. }
  53. // firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构 sup_sub: 上下级
  54. // 1、同甲异业数据/招标代理机构渠道
  55. r1, r2 := GetData(propertyForm, pTmp.BuyerId)
  56. if r1 != nil && len(r1) > 0 {
  57. for _, m := range r1 {
  58. near, zbtime := LastTimeCoop(pTmp.BuyerId, common.ObjToString(m["name"]), "adiffb")
  59. tmp := ResultData{
  60. SourceType: "adiffb",
  61. EntName: common.ObjToString(m["name"]),
  62. ProjectNum: common.IntAll(m["coop_size"]),
  63. TotalAmount: common.Float64All(m["coop_amount"]),
  64. RecentTime: zbtime,
  65. NearlyYears: near,
  66. }
  67. result = append(result, &tmp)
  68. }
  69. }
  70. if r2 != nil && len(r2) > 0 {
  71. for _, m := range r2 {
  72. near, zbtime := LastTimeCoop(pTmp.BuyerId, common.ObjToString(m["name"]), "agency")
  73. tmp := ResultData{
  74. SourceType: "agency",
  75. EntName: common.ObjToString(m["name"]),
  76. ProjectNum: common.IntAll(m["coop_size"]),
  77. TotalAmount: common.Float64All(m["coop_amount"]),
  78. RecentTime: zbtime,
  79. NearlyYears: near,
  80. }
  81. result = append(result, &tmp)
  82. }
  83. }
  84. // 中间人可介绍业主
  85. //if pTmp.BuyerId != "" {
  86. // r3 := GetData1(req.PositionId, pTmp.BuyerId)
  87. // if r3 != nil && len(r3) > 0 {
  88. // tmp := ResultData{
  89. // SourceType: "middleman",
  90. // EntName: common.ObjToString(m["name"]),
  91. // ProjectNum: common.IntAll(m["coop_size"]),
  92. // TotalAmount: common.Float64All(m["coop_amount"]),
  93. // RecentTime: zbtime,
  94. // NearlyYears: near,
  95. // }
  96. // result = append(result, &tmp)
  97. // }
  98. //// 关联单位
  99. //r4 := GetData2(pTmp.BuyerId)
  100. //if r4 != nil && len(r4) > 0 {
  101. // tmp := &ResultData{
  102. // channelType: 1,
  103. // channel: "业主关联单位",
  104. // size: len(r4),
  105. // data: r4,
  106. // }
  107. // result = append(result, tmp)
  108. //}
  109. //}
  110. return
  111. }
  112. func GetData(propertyForm, bid string) (result1, result2 []map[string]interface{}) {
  113. type AggStruct struct {
  114. Buckets []struct {
  115. Key string `json:"key,omitempty"`
  116. Doc_count int64 `json:"doc_count,omitempty"`
  117. Amount_all struct {
  118. Value float64 `json:"value,omitempty"`
  119. } `json:"amount_all"`
  120. } `json:"buckets"`
  121. }
  122. m1 := make(map[string]interface{}) //采购单位-中标单位
  123. m2 := make(map[string]interface{}) //采购单位-代理机构
  124. if propertyForm != "" {
  125. //同甲异态 中标企业
  126. aggs, count, res := elastic.GetAggs("bidding", "bidding", fmt.Sprintf(es_query, propertyForm, "中国计量大学"))
  127. logx.Info("es聚合查询结果:", aggs, count, res)
  128. var m1Buckets = AggStruct{}
  129. if aggs != nil && aggs["winner_count"] != nil {
  130. bs, err := aggs["winner_count"].MarshalJSON()
  131. if err != nil {
  132. logx.Info(err)
  133. } else {
  134. if len(bs) == 0 {
  135. logx.Info(err)
  136. } else {
  137. err := json.Unmarshal(bs, &m1Buckets)
  138. logx.Info(err)
  139. if len(m1Buckets.Buckets) > 0 {
  140. for _, v := range m1Buckets.Buckets {
  141. m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  142. }
  143. }
  144. }
  145. }
  146. }
  147. for k, v := range m1 {
  148. v1 := v.(map[string]interface{})
  149. tmp := make(map[string]interface{})
  150. tmp["name"] = k
  151. tmp["coop_size"] = v1["count"]
  152. tmp["coop_amount"] = v1["amount"]
  153. result1 = append(result1, tmp)
  154. }
  155. }
  156. //代理机构
  157. aggs1, count1, res1 := elastic.GetAggs("bidding", "bidding", fmt.Sprintf(es_query1, "中国计量大学"))
  158. logx.Info("es聚合查询结果:", aggs1, count1, res1)
  159. var m2Buckets = AggStruct{}
  160. if aggs1 != nil && aggs1["agency_count"] != nil {
  161. bs, err := aggs1["agency_count"].MarshalJSON()
  162. if err != nil {
  163. logx.Info(err)
  164. } else {
  165. if len(bs) == 0 {
  166. logx.Info(err)
  167. } else {
  168. err = json.Unmarshal(bs, &m2Buckets)
  169. logx.Info(err)
  170. if len(m2Buckets.Buckets) > 0 {
  171. for _, v := range m2Buckets.Buckets {
  172. m2[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  173. }
  174. }
  175. }
  176. }
  177. }
  178. for k, v := range m2 {
  179. v1 := v.(map[string]interface{})
  180. tmp := make(map[string]interface{})
  181. tmp["name"] = k
  182. tmp["coop_size"] = v1["count"]
  183. tmp["coop_amount"] = v1["amount"]
  184. result2 = append(result2, tmp)
  185. }
  186. return
  187. }
  188. // @Author jianghan
  189. // @Description 中间人
  190. // @Date 2024/4/20
  191. func GetData1(positionId int64, buyid string) (result []map[string]interface{}) {
  192. info := T.CrmMysql.SelectBySql(sql_2_2, positionId, buyid)
  193. if info != nil && len(*info) > 0 {
  194. for _, m := range *info {
  195. result = append(result, map[string]interface{}{"name": m["company_name"], "pserson": m["contact_name"]})
  196. }
  197. }
  198. return
  199. }
  200. // @Author jianghan
  201. // @Description 业主关联企业 上下级/股权控制
  202. // @Date 2024/4/20
  203. func GetData2(buyid string) (result []map[string]interface{}) {
  204. info := T.CrmMysql.SelectBySql(sql_2_3, buyid, buyid, buyid, buyid, buyid, buyid)
  205. if info != nil && len(*info) > 0 {
  206. for _, m := range *info {
  207. result = append(result, map[string]interface{}{"name": m["company_name"], "pserson": m["contact_name"]})
  208. }
  209. }
  210. return
  211. }
  212. type P_History struct {
  213. ProjectId string `ch:"project_id"`
  214. ProjectName string `ch:"project_name"`
  215. ZbTime int64 `ch:"zbtime"`
  216. }
  217. // @Author jianghan
  218. // @Description 合作历史
  219. // @Date 2024/4/20
  220. func GetData3(buyerId, winnerId string) (result []*P_History) {
  221. rows, _ := T.ClickhouseConn.Query(context.TODO(), sql_2_1, buyerId, winnerId)
  222. for rows.Next() {
  223. pHis := P_History{}
  224. _ = rows.ScanStruct(&pHis)
  225. result = append(result, &pHis)
  226. }
  227. return
  228. }
  229. // @Author jianghan
  230. // @Description 上次合作时间
  231. // @Date 2024/4/24
  232. func LastTimeCoop(buyerId, ent, stype string) (bool, int64) {
  233. zbtime := int64(0)
  234. near := false
  235. if stype == "adiffb" {
  236. sql := `SELECT zbtime FROM information.transaction_info WHERE buyer_id = ? AND has(winner, ?) ORDER BY zbtime DESC LIMIT 1`
  237. _ = T.ClickhouseConn.QueryRow(context.TODO(), sql, buyerId, ent).Scan(&zbtime)
  238. } else if stype == "agency" {
  239. sql := `SELECT zbtime FROM information.transaction_info WHERE buyer_id = ? AND agency = ? ORDER BY zbtime DESC LIMIT 1`
  240. _ = T.ClickhouseConn.QueryRow(context.TODO(), sql, buyerId, ent).Scan(&zbtime)
  241. }
  242. timestamp := time.Now().AddDate(-3, 0, 0).Unix()
  243. if timestamp <= zbtime {
  244. near = true
  245. }
  246. return near, zbtime
  247. }