CoopHistoryService.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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、同甲异业数据/ 3、招标代理机构渠道
  55. if req.ChannelType == "0" || req.ChannelType == "1" || req.ChannelType == "3" {
  56. r1, r2 := GetData(propertyForm, pTmp.BuyerId)
  57. if req.ChannelType != "3" && r1 != nil && len(r1) > 0 {
  58. for _, m := range r1 {
  59. near, zbtime := LastTimeCoop(pTmp.BuyerId, common.ObjToString(m["name"]), "adiffb")
  60. tmp := ResultData{
  61. SourceType: "adiffb",
  62. EntName: common.ObjToString(m["name"]),
  63. ProjectNum: common.IntAll(m["coop_size"]),
  64. TotalAmount: common.Float64All(m["coop_amount"]),
  65. RecentTime: zbtime,
  66. NearlyYears: near,
  67. }
  68. result = append(result, &tmp)
  69. }
  70. }
  71. if req.ChannelType != "2" && r2 != nil && len(r2) > 0 {
  72. for _, m := range r2 {
  73. near, zbtime := LastTimeCoop(pTmp.BuyerId, common.ObjToString(m["name"]), "agency")
  74. tmp := ResultData{
  75. SourceType: "agency",
  76. EntName: common.ObjToString(m["name"]),
  77. ProjectNum: common.IntAll(m["coop_size"]),
  78. TotalAmount: common.Float64All(m["coop_amount"]),
  79. RecentTime: zbtime,
  80. NearlyYears: near,
  81. }
  82. result = append(result, &tmp)
  83. }
  84. }
  85. }
  86. if pTmp.BuyerId != "" {
  87. if req.ChannelType == "0" || req.ChannelType == "4" {
  88. // 中间人可介绍业主
  89. var r3 []map[string]interface{}
  90. r3 = FindMiddleman([]string{pTmp.BuyerId}, req.PositionId, r3)
  91. if r3 != nil && len(r3) > 0 {
  92. for _, m := range r3 {
  93. tmp := ResultData{
  94. SourceType: "middleman",
  95. EntName: common.ObjToString(m["b_name"]),
  96. EntPerson: common.ObjToString(m["personName"]),
  97. Relationship: "业主的关系人",
  98. }
  99. result = append(result, &tmp)
  100. }
  101. }
  102. }
  103. if req.ChannelType == "0" || req.ChannelType == "2" {
  104. // 关联单位
  105. var r4 []map[string]interface{}
  106. r4 = Findfirstparty([]string{pTmp.BuyerId}, r4)
  107. if r4 != nil && len(r4) > 0 {
  108. for _, m := range r4 {
  109. tmp := ResultData{
  110. SourceType: "sup_sub",
  111. EntName: common.ObjToString(m["b_name"]),
  112. EntPerson: common.ObjToString(m["personName"]),
  113. Relationship: common.ObjToString(m["relationship"]),
  114. }
  115. result = append(result, &tmp)
  116. }
  117. }
  118. }
  119. }
  120. return
  121. }
  122. func GetData(propertyForm, bid string) (result1, result2 []map[string]interface{}) {
  123. type AggStruct struct {
  124. Buckets []struct {
  125. Key string `json:"key,omitempty"`
  126. Doc_count int64 `json:"doc_count,omitempty"`
  127. Amount_all struct {
  128. Value float64 `json:"value,omitempty"`
  129. } `json:"amount_all"`
  130. } `json:"buckets"`
  131. }
  132. m1 := make(map[string]interface{}) //采购单位-中标单位
  133. m2 := make(map[string]interface{}) //采购单位-代理机构
  134. if propertyForm != "" {
  135. //同甲异态 中标企业
  136. aggs, count, res := elastic.GetAggs("bidding", "bidding", fmt.Sprintf(es_query, propertyForm, "中国计量大学"))
  137. logx.Info("es聚合查询结果:", aggs, count, res)
  138. var m1Buckets = AggStruct{}
  139. if aggs != nil && aggs["winner_count"] != nil {
  140. bs, err := aggs["winner_count"].MarshalJSON()
  141. if err != nil {
  142. logx.Info(err)
  143. } else {
  144. if len(bs) == 0 {
  145. logx.Info(err)
  146. } else {
  147. err := json.Unmarshal(bs, &m1Buckets)
  148. logx.Info(err)
  149. if len(m1Buckets.Buckets) > 0 {
  150. for _, v := range m1Buckets.Buckets {
  151. m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  152. }
  153. }
  154. }
  155. }
  156. }
  157. for k, v := range m1 {
  158. v1 := v.(map[string]interface{})
  159. tmp := make(map[string]interface{})
  160. tmp["name"] = k
  161. tmp["coop_size"] = v1["count"]
  162. tmp["coop_amount"] = v1["amount"]
  163. result1 = append(result1, tmp)
  164. }
  165. }
  166. //代理机构
  167. aggs1, count1, res1 := elastic.GetAggs("bidding", "bidding", fmt.Sprintf(es_query1, "中国计量大学"))
  168. logx.Info("es聚合查询结果:", aggs1, count1, res1)
  169. var m2Buckets = AggStruct{}
  170. if aggs1 != nil && aggs1["agency_count"] != nil {
  171. bs, err := aggs1["agency_count"].MarshalJSON()
  172. if err != nil {
  173. logx.Info(err)
  174. } else {
  175. if len(bs) == 0 {
  176. logx.Info(err)
  177. } else {
  178. err = json.Unmarshal(bs, &m2Buckets)
  179. logx.Info(err)
  180. if len(m2Buckets.Buckets) > 0 {
  181. for _, v := range m2Buckets.Buckets {
  182. m2[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  183. }
  184. }
  185. }
  186. }
  187. }
  188. for k, v := range m2 {
  189. v1 := v.(map[string]interface{})
  190. tmp := make(map[string]interface{})
  191. tmp["name"] = k
  192. tmp["coop_size"] = v1["count"]
  193. tmp["coop_amount"] = v1["amount"]
  194. result2 = append(result2, tmp)
  195. }
  196. return
  197. }
  198. type P_History struct {
  199. ProjectId string `ch:"project_id"`
  200. ProjectName string `ch:"project_name"`
  201. ZbTime int64 `ch:"zbtime"`
  202. }
  203. // @Author jianghan
  204. // @Description 合作历史
  205. // @Date 2024/4/20
  206. func GetData3(buyerId, winnerId string) (result []*P_History) {
  207. rows, _ := T.ClickhouseConn.Query(context.TODO(), sql_2_1, buyerId, winnerId)
  208. for rows.Next() {
  209. pHis := P_History{}
  210. _ = rows.ScanStruct(&pHis)
  211. result = append(result, &pHis)
  212. }
  213. return
  214. }
  215. // @Author jianghan
  216. // @Description 上次合作时间
  217. // @Date 2024/4/24
  218. func LastTimeCoop(buyerId, ent, stype string) (bool, int64) {
  219. zbtime := int64(0)
  220. near := false
  221. if stype == "adiffb" {
  222. sql := `SELECT zbtime FROM information.transaction_info WHERE buyer_id = ? AND has(winner, ?) ORDER BY zbtime DESC LIMIT 1`
  223. _ = T.ClickhouseConn.QueryRow(context.TODO(), sql, buyerId, ent).Scan(&zbtime)
  224. } else if stype == "agency" {
  225. sql := `SELECT zbtime FROM information.transaction_info WHERE buyer_id = ? AND agency = ? ORDER BY zbtime DESC LIMIT 1`
  226. _ = T.ClickhouseConn.QueryRow(context.TODO(), sql, buyerId, ent).Scan(&zbtime)
  227. }
  228. timestamp := time.Now().AddDate(-3, 0, 0).Unix()
  229. if timestamp <= zbtime {
  230. near = true
  231. }
  232. return near, zbtime
  233. }