CoopHistoryService.go 9.8 KB

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