CoopHistoryService.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 b.company_id, b.company_name, b.contact_name, a.relate_id, a.relate_name FROM connection_introduce a INNER JOIN connection b ON b.position_id =%d AND a.connection_id = b.id AND b.state = 1 AND a.itype = 1 AND b.itype = 4 AND a.relate_Id = %s`
  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 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 = `{
  20. "query": {
  21. "bool": {
  22. "must": [
  23. {
  24. "range": {
  25. "zbtime": {
  26. "gte": %d
  27. }
  28. }
  29. },
  30. {
  31. "term": {
  32. "buyer_id": %s
  33. }
  34. }
  35. ],
  36. "must_not": {
  37. "term": {
  38. "property_form": %s
  39. }
  40. }
  41. }
  42. },
  43. "aggs": {
  44. "winner_count": {
  45. "terms": {
  46. "field": "winner",
  47. "size": 100,
  48. "order": {
  49. "_count": "desc"
  50. }
  51. },
  52. "aggs": {
  53. "amount_all": {
  54. "sum": {
  55. "field": "bidamount"
  56. }
  57. }
  58. }
  59. },
  60. "agency_count": {
  61. "terms": {
  62. "field": "agency",
  63. "size": 100,
  64. "order": {
  65. "_count": "desc"
  66. }
  67. },
  68. "aggs": {
  69. "amount_all": {
  70. "sum": {
  71. "field": "bidamount"
  72. }
  73. }
  74. }
  75. }
  76. },
  77. "size": 1
  78. }`
  79. )
  80. type ResultData struct {
  81. channelType int `json:"channel_type"`
  82. channel string `json:"channel"`
  83. size int `json:"size"`
  84. data []map[string]interface{} `json:"data"`
  85. }
  86. func GetPrList(req *types.CoopHistoryReq) (result []*ResultData) {
  87. pMap := make(map[string]interface{})
  88. err := T.ClickhouseConn.QueryRow(context.TODO(), sql_2_0, req.Pid).Scan(&pMap)
  89. if err != nil {
  90. return nil
  91. }
  92. propertyForm := ""
  93. m1 := T.CrmMysql.FindOne("config_tenant", map[string]interface{}{"account_id": req.EntAccountId}, "probusfor", "")
  94. if m1 != nil && len(*m1) > 0 {
  95. propertyForm = common.ObjToString((*m1)["probusfor"])
  96. }
  97. // 1、同甲异业数据/招标代理机构渠道
  98. if propertyForm != "" {
  99. r1, r2 := GetData(propertyForm, common.ObjToString(pMap["buyer_id"]))
  100. if r1 != nil && len(r1) > 0 {
  101. tmp := &ResultData{
  102. channelType: 1,
  103. channel: "同甲异业渠道",
  104. size: len(r1),
  105. data: r1,
  106. }
  107. result = append(result, tmp)
  108. }
  109. if r2 != nil && len(r2) > 0 {
  110. tmp := &ResultData{
  111. channelType: 1,
  112. channel: "招标代理机构",
  113. size: len(r2),
  114. data: r2,
  115. }
  116. result = append(result, tmp)
  117. }
  118. }
  119. // 中间人可介绍业主
  120. r3 := GetData1(req.PositionId, common.ObjToString(pMap["buyer_id"]))
  121. if r3 != nil && len(r3) > 0 {
  122. tmp := &ResultData{
  123. channelType: 1,
  124. channel: "中间人",
  125. size: len(r3),
  126. data: r3,
  127. }
  128. result = append(result, tmp)
  129. }
  130. // 关联单位
  131. GetData2(common.ObjToString(pMap["buyer_id"]))
  132. return
  133. }
  134. func GetData(propertyForm, bid string) (result1, result2 []map[string]interface{}) {
  135. timestamp := time.Now().AddDate(-3, 0, 0).Unix()
  136. aggs, count, res := elastic.GetAggs(INDEX_1, INDEX_1, fmt.Sprintf(es_query, timestamp, propertyForm, bid))
  137. logx.Info("es聚合查询结果:", aggs, count, res)
  138. type AggStruct struct {
  139. Buckets []struct {
  140. Key string `json:"key,omitempty"`
  141. Doc_count int64 `json:"doc_count,omitempty"`
  142. Amount_all struct {
  143. Value string `json:"value,omitempty"`
  144. } `json:"amount_all"`
  145. } `json:"buckets"`
  146. }
  147. m1 := make(map[string]interface{}) //采购单位-中标单位
  148. m2 := make(map[string]interface{}) //采购单位-代理机构
  149. var m1Buckets = AggStruct{}
  150. if aggs != nil && aggs["winner_count"] != nil {
  151. bs, err := aggs["winner_count"].MarshalJSON()
  152. if err != nil {
  153. logx.Info(err)
  154. } else {
  155. if len(bs) == 0 {
  156. logx.Info(err)
  157. } else {
  158. err := json.Unmarshal(bs, &m1Buckets)
  159. logx.Info(err)
  160. if len(m1Buckets.Buckets) > 0 {
  161. for _, v := range m1Buckets.Buckets {
  162. m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  163. }
  164. }
  165. }
  166. }
  167. }
  168. var m2Buckets = AggStruct{}
  169. if aggs != nil && aggs["amount_all"] != nil {
  170. bs, err := aggs["amount_all"].MarshalJSON()
  171. if err != nil {
  172. logx.Info(err)
  173. } else {
  174. if len(bs) == 0 {
  175. logx.Info(err)
  176. } else {
  177. err := json.Unmarshal(bs, &m2Buckets)
  178. logx.Info(err)
  179. if len(m2Buckets.Buckets) > 0 {
  180. for _, v := range m2Buckets.Buckets {
  181. m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  182. }
  183. }
  184. }
  185. }
  186. }
  187. for k, v := range m1 {
  188. if m2[k] != nil {
  189. tmp := make(map[string]interface{})
  190. tmp["name"] = v
  191. tmp["coop_size"] = v
  192. tmp["coop_amount"] = m2[k]
  193. result1 = append(result1, tmp)
  194. }
  195. }
  196. for k, v := range m2 {
  197. if m2[k] != nil {
  198. tmp := make(map[string]interface{})
  199. tmp["name"] = v
  200. tmp["coop_size"] = v
  201. tmp["coop_amount"] = m2[k]
  202. result2 = append(result2, tmp)
  203. }
  204. }
  205. return
  206. }
  207. // @Author jianghan
  208. // @Description 中间人
  209. // @Date 2024/4/20
  210. func GetData1(positionId int64, buyid string) (result []map[string]interface{}) {
  211. info := T.CrmMysql.SelectBySql(sql_2_2, positionId, buyid)
  212. if info != nil && len(*info) > 0 {
  213. for _, m := range *info {
  214. result = append(result, map[string]interface{}{"name": m["company_name"], "pserson": m["contact_name"]})
  215. }
  216. }
  217. return
  218. }
  219. // @Author jianghan
  220. // @Description 业主关联企业 上下级/股权控制
  221. // @Date 2024/4/20
  222. func GetData2(buyid string) (result []map[string]interface{}) {
  223. info := T.CrmMysql.SelectBySql(sql_2_3, buyid, buyid, buyid, buyid, buyid, buyid)
  224. if info != nil && len(*info) > 0 {
  225. for _, m := range *info {
  226. result = append(result, map[string]interface{}{"name": m["company_name"], "pserson": m["contact_name"]})
  227. }
  228. }
  229. return
  230. }
  231. type P_History struct {
  232. ProjectId string `ch:"project_id"`
  233. ProjectName string `ch:"project_name"`
  234. ZbTime int64 `ch:"zbtime"`
  235. }
  236. // @Author jianghan
  237. // @Description 合作历史
  238. // @Date 2024/4/20
  239. func GetData3(buyerId, winnerId string) (result []*P_History) {
  240. rows, _ := T.ClickhouseConn.Query(context.TODO(), sql_2_1, buyerId, winnerId)
  241. for rows.Next() {
  242. pHis := P_History{}
  243. _ = rows.ScanStruct(&pHis)
  244. result = append(result, &pHis)
  245. }
  246. return
  247. }