CoopHistoryService.go 9.0 KB

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