CoopHistoryService.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. type ProjectTmp struct {
  87. Buyer string `ch:"buyer"`
  88. BuyerId string `ch:"buyer_id"`
  89. Agency string `ch:"agency"`
  90. AgencyId string `ch:"agency_id"`
  91. PropertyForm []string `ch:"property_form"`
  92. }
  93. func GetPrList(req *types.CoopHistoryReq) (result []*ResultData) {
  94. pTmp := ProjectTmp{}
  95. err := T.ClickhouseConn.QueryRow(context.TODO(), sql_2_0, req.Pid).ScanStruct(&pTmp)
  96. if err != nil {
  97. return nil
  98. }
  99. propertyForm := ""
  100. m1 := T.CrmMysql.FindOne("config_tenant", map[string]interface{}{"account_id": req.EntAccountId}, "probusfor", "")
  101. if m1 != nil && len(*m1) > 0 {
  102. propertyForm = common.ObjToString((*m1)["probusfor"])
  103. }
  104. // 1、同甲异业数据/招标代理机构渠道
  105. if propertyForm != "" {
  106. r1, r2 := GetData(propertyForm, pTmp.BuyerId)
  107. if r1 != nil && len(r1) > 0 {
  108. tmp := &ResultData{
  109. channelType: 1,
  110. channel: "同甲异业渠道",
  111. size: len(r1),
  112. data: r1,
  113. }
  114. result = append(result, tmp)
  115. }
  116. if r2 != nil && len(r2) > 0 {
  117. tmp := &ResultData{
  118. channelType: 1,
  119. channel: "招标代理机构",
  120. size: len(r2),
  121. data: r2,
  122. }
  123. result = append(result, tmp)
  124. }
  125. }
  126. // 中间人可介绍业主
  127. if pTmp.BuyerId != "" {
  128. r3 := GetData1(req.PositionId, pTmp.BuyerId)
  129. if r3 != nil && len(r3) > 0 {
  130. tmp := &ResultData{
  131. channelType: 1,
  132. channel: "中间人",
  133. size: len(r3),
  134. data: r3,
  135. }
  136. result = append(result, tmp)
  137. }
  138. // 关联单位
  139. r4 := GetData2(pTmp.BuyerId)
  140. if r4 != nil && len(r4) > 0 {
  141. tmp := &ResultData{
  142. channelType: 1,
  143. channel: "业主关联单位",
  144. size: len(r4),
  145. data: r4,
  146. }
  147. result = append(result, tmp)
  148. }
  149. }
  150. return
  151. }
  152. func GetData(propertyForm, bid string) (result1, result2 []map[string]interface{}) {
  153. timestamp := time.Now().AddDate(-3, 0, 0).Unix()
  154. aggs, count, res := elastic.GetAggs(INDEX_1, INDEX_1, fmt.Sprintf(es_query, timestamp, propertyForm, bid))
  155. logx.Info("es聚合查询结果:", aggs, count, res)
  156. type AggStruct struct {
  157. Buckets []struct {
  158. Key string `json:"key,omitempty"`
  159. Doc_count int64 `json:"doc_count,omitempty"`
  160. Amount_all struct {
  161. Value string `json:"value,omitempty"`
  162. } `json:"amount_all"`
  163. } `json:"buckets"`
  164. }
  165. m1 := make(map[string]interface{}) //采购单位-中标单位
  166. m2 := make(map[string]interface{}) //采购单位-代理机构
  167. var m1Buckets = AggStruct{}
  168. if aggs != nil && aggs["winner_count"] != nil {
  169. bs, err := aggs["winner_count"].MarshalJSON()
  170. if err != nil {
  171. logx.Info(err)
  172. } else {
  173. if len(bs) == 0 {
  174. logx.Info(err)
  175. } else {
  176. err := json.Unmarshal(bs, &m1Buckets)
  177. logx.Info(err)
  178. if len(m1Buckets.Buckets) > 0 {
  179. for _, v := range m1Buckets.Buckets {
  180. m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  181. }
  182. }
  183. }
  184. }
  185. }
  186. var m2Buckets = AggStruct{}
  187. if aggs != nil && aggs["amount_all"] != nil {
  188. bs, err := aggs["amount_all"].MarshalJSON()
  189. if err != nil {
  190. logx.Info(err)
  191. } else {
  192. if len(bs) == 0 {
  193. logx.Info(err)
  194. } else {
  195. err := json.Unmarshal(bs, &m2Buckets)
  196. logx.Info(err)
  197. if len(m2Buckets.Buckets) > 0 {
  198. for _, v := range m2Buckets.Buckets {
  199. m1[v.Key] = map[string]interface{}{"name": v.Key, "amount": v.Amount_all.Value, "count": v.Doc_count}
  200. }
  201. }
  202. }
  203. }
  204. }
  205. for k, v := range m1 {
  206. if m2[k] != nil {
  207. tmp := make(map[string]interface{})
  208. tmp["name"] = v
  209. tmp["coop_size"] = v
  210. tmp["coop_amount"] = m2[k]
  211. result1 = append(result1, tmp)
  212. }
  213. }
  214. for k, v := range m2 {
  215. if m2[k] != nil {
  216. tmp := make(map[string]interface{})
  217. tmp["name"] = v
  218. tmp["coop_size"] = v
  219. tmp["coop_amount"] = m2[k]
  220. result2 = append(result2, tmp)
  221. }
  222. }
  223. return
  224. }
  225. // @Author jianghan
  226. // @Description 中间人
  227. // @Date 2024/4/20
  228. func GetData1(positionId int64, buyid string) (result []map[string]interface{}) {
  229. info := T.CrmMysql.SelectBySql(sql_2_2, positionId, buyid)
  230. if info != nil && len(*info) > 0 {
  231. for _, m := range *info {
  232. result = append(result, map[string]interface{}{"name": m["company_name"], "pserson": m["contact_name"]})
  233. }
  234. }
  235. return
  236. }
  237. // @Author jianghan
  238. // @Description 业主关联企业 上下级/股权控制
  239. // @Date 2024/4/20
  240. func GetData2(buyid string) (result []map[string]interface{}) {
  241. info := T.CrmMysql.SelectBySql(sql_2_3, buyid, buyid, buyid, buyid, buyid, buyid)
  242. if info != nil && len(*info) > 0 {
  243. for _, m := range *info {
  244. result = append(result, map[string]interface{}{"name": m["company_name"], "pserson": m["contact_name"]})
  245. }
  246. }
  247. return
  248. }
  249. type P_History struct {
  250. ProjectId string `ch:"project_id"`
  251. ProjectName string `ch:"project_name"`
  252. ZbTime int64 `ch:"zbtime"`
  253. }
  254. // @Author jianghan
  255. // @Description 合作历史
  256. // @Date 2024/4/20
  257. func GetData3(buyerId, winnerId string) (result []*P_History) {
  258. rows, _ := T.ClickhouseConn.Query(context.TODO(), sql_2_1, buyerId, winnerId)
  259. for rows.Next() {
  260. pHis := P_History{}
  261. _ = rows.ScanStruct(&pHis)
  262. result = append(result, &pHis)
  263. }
  264. return
  265. }