CoopHistoryService.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. )
  12. var (
  13. INDEX_1 = "transaction_info"
  14. sql_2_0 = `SELECT buyer, buyer_id, agency, agency_id, property_form FROM information.transaction_info WHERE project_id = ?`
  15. sql_2_1 = `SELECT project_id, project_name, zbtime FROM information.transaction_info WHERE buyer_id = ? AND winner winner IN ? ORDER BY zbtime DESC`
  16. es_query = `{
  17. "query": {
  18. "bool": {
  19. "must": [
  20. {
  21. "range": {
  22. "zbtime": {
  23. "gte": 123
  24. }
  25. }
  26. },
  27. {
  28. "term": {
  29. "buyer_id": "123"
  30. }
  31. }
  32. ],
  33. "must_not": {
  34. "term": {
  35. "property_form": "123123"
  36. }
  37. }
  38. }
  39. },
  40. "aggs": {
  41. "winner_count": {
  42. "terms": {
  43. "field": "winner",
  44. "size": 100,
  45. "order": {
  46. "_count": "desc"
  47. }
  48. },
  49. "aggs": {
  50. "amount_all": {
  51. "sum": {
  52. "field": "bidamount"
  53. }
  54. }
  55. }
  56. },
  57. "agency_count": {
  58. "terms": {
  59. "field": "agency",
  60. "size": 100,
  61. "order": {
  62. "_count": "desc"
  63. }
  64. },
  65. "aggs": {
  66. "amount_all": {
  67. "sum": {
  68. "field": "bidamount"
  69. }
  70. }
  71. }
  72. }
  73. },
  74. "size": 1
  75. }`
  76. )
  77. type ResultData struct {
  78. channelType int `json:"channel_type"`
  79. channel string `json:"channel"`
  80. size int `json:"size"`
  81. data []map[string]interface{} `json:"data"`
  82. }
  83. func GetPrList(req *types.CoopHistoryReq) (result []*ResultData) {
  84. pMap := make(map[string]interface{})
  85. err := T.ClickhouseConn.QueryRow(context.TODO(), sql_2_0, req.Pid).Scan(&pMap)
  86. if err != nil {
  87. return nil
  88. }
  89. propertyForm := ""
  90. m1 := T.CrmMysql.FindOne("config_tenant", map[string]interface{}{"account_id": req.EntAccountId}, "probusfor", "")
  91. if m1 != nil && len(*m1) > 0 {
  92. propertyForm = common.ObjToString((*m1)["probusfor"])
  93. }
  94. // 1、同甲异业数据/招标代理机构渠道
  95. if propertyForm != "" {
  96. r1, r2 := getData(propertyForm, common.ObjToString(pMap["buyer_id"]))
  97. if len(r1) > 0 {
  98. tmp := &ResultData{
  99. channelType: 1,
  100. channel: "同甲异业渠道",
  101. size: len(r1),
  102. data: r1,
  103. }
  104. result = append(result, tmp)
  105. }
  106. if len(r2) > 0 {
  107. tmp := &ResultData{
  108. channelType: 1,
  109. channel: "招标代理机构",
  110. size: len(r1),
  111. data: r2,
  112. }
  113. result = append(result, tmp)
  114. }
  115. }
  116. return
  117. }
  118. func getData(propertyForm, bid string) (result1, result2 []map[string]interface{}) {
  119. aggs, count, res := elastic.GetAggs(INDEX_1, INDEX_1, fmt.Sprintf(es_query, 1, propertyForm, bid))
  120. logx.Info("es聚合查询结果:", aggs, count, res)
  121. type AggStruct struct {
  122. Buckets []struct {
  123. Key string `json:"key,omitempty"`
  124. Doc_count int64 `json:"doc_count,omitempty"`
  125. Amount_all struct {
  126. Value string `json:"value,omitempty"`
  127. } `json:"amount_all"`
  128. } `json:"buckets"`
  129. }
  130. m1 := make(map[string]interface{}) //采购单位-中标单位
  131. m2 := make(map[string]interface{}) //采购单位-代理机构
  132. var m1Buckets = AggStruct{}
  133. if aggs != nil && aggs["winner_count"] != nil {
  134. bs, err := aggs["winner_count"].MarshalJSON()
  135. if err != nil {
  136. logx.Info(err)
  137. } else {
  138. if len(bs) == 0 {
  139. logx.Info(err)
  140. } else {
  141. err := json.Unmarshal(bs, &m1Buckets)
  142. logx.Info(err)
  143. if len(m1Buckets.Buckets) > 0 {
  144. for _, v := range m1Buckets.Buckets {
  145. m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  146. }
  147. }
  148. }
  149. }
  150. }
  151. var m2Buckets = AggStruct{}
  152. if aggs != nil && aggs["amount_all"] != nil {
  153. bs, err := aggs["amount_all"].MarshalJSON()
  154. if err != nil {
  155. logx.Info(err)
  156. } else {
  157. if len(bs) == 0 {
  158. logx.Info(err)
  159. } else {
  160. err := json.Unmarshal(bs, &m2Buckets)
  161. logx.Info(err)
  162. if len(m2Buckets.Buckets) > 0 {
  163. for _, v := range m2Buckets.Buckets {
  164. m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  165. }
  166. }
  167. }
  168. }
  169. }
  170. for k, v := range m1 {
  171. if m2[k] != nil {
  172. tmp := make(map[string]interface{})
  173. tmp["name"] = v
  174. tmp["coop_size"] = v
  175. tmp["coop_amount"] = m2[k]
  176. result1 = append(result1, tmp)
  177. }
  178. }
  179. for k, v := range m2 {
  180. if m2[k] != nil {
  181. tmp := make(map[string]interface{})
  182. tmp["name"] = v
  183. tmp["coop_size"] = v
  184. tmp["coop_amount"] = m2[k]
  185. result2 = append(result2, tmp)
  186. }
  187. }
  188. // 合作项目, 业主与中标单位
  189. for _, v := range result1 {
  190. rows, err := T.ClickhouseConn.Query(context.TODO(), sql_2_1, bid, v["name"])
  191. var mlist []map[string]interface{}
  192. for err != nil && rows.Next() {
  193. m := make(map[string]interface{})
  194. rows.Scan(&m)
  195. mlist = append(mlist, m)
  196. }
  197. v["data"] = mlist
  198. }
  199. // 业主与代理机构
  200. for _, v := range result2 {
  201. rows, err := T.ClickhouseConn.Query(context.TODO(), sql_2_1, bid, v["name"])
  202. var mlist []map[string]interface{}
  203. for err != nil && rows.Next() {
  204. m := make(map[string]interface{})
  205. rows.Scan(&m)
  206. mlist = append(mlist, m)
  207. }
  208. v["data"] = mlist
  209. }
  210. return
  211. }