CoopHistoryService.go 10 KB

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