CoopHistoryService.go 10 KB

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