CoopHistoryService.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. "app.yhyue.com/moapp/jybase/redis"
  11. T "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
  12. "bp.jydev.jianyu360.cn/CRM/networkManage/api/internal/types"
  13. "github.com/ClickHouse/clickhouse-go/v2/lib/driver"
  14. "github.com/shopspring/decimal"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. const (
  18. NetworkManageCoopHistory = "networkManage_coopHistory_%d_%s"
  19. )
  20. var (
  21. INDEX_1 = "transaction_info_all"
  22. sql_2_0 = `SELECT buyer_id FROM information.transaction_info_all WHERE project_id = ?`
  23. sql_2_2 = `select id as buyer_id from ent_info where company_name=? order by id desc limit 1`
  24. sql_2_1 = `SELECT project_id, project_name, zbtime FROM information.transaction_info_all WHERE buyer_id = ? AND has(winner, ?) ORDER BY zbtime DESC`
  25. sql_2_1_1 = `SELECT project_id, project_name, zbtime FROM information.transaction_info_all WHERE buyer_id = ? AND agency = ? ORDER BY zbtime DESC`
  26. )
  27. type ResultData struct {
  28. SourceType string `json:"SourceType"`
  29. EntName string `json:"EntName"`
  30. EntId string `json:"EntId"`
  31. EntPerson string `json:"EntPerson"`
  32. Middleman string `json:"Middleman"`
  33. ProjectNum int `json:"ProjectNum"`
  34. TotalAmount float64 `json:"TotalAmount"`
  35. RecentTime int64 `json:"RecentTime"`
  36. NearlyYears bool `json:"NearlyYears"`
  37. SupEnt string `json:"SupEnt"` // 上级
  38. NextEnt string `json:"NextEnt"` // 下级
  39. Relationship string `json:"Relationship"` // 关系
  40. BuyerId string `json:"BuyerId"`
  41. IsIgnore bool `json:"isIgnore"`
  42. }
  43. type ResultDatas []*ResultData
  44. func (n *ResultDatas) Len() int {
  45. return len(*n)
  46. }
  47. func (n *ResultDatas) Less(i, j int) bool {
  48. if (*n)[i].RecentTime == (*n)[j].RecentTime {
  49. return (*n)[i].EntName < (*n)[j].EntName
  50. }
  51. return (*n)[i].RecentTime > (*n)[j].RecentTime
  52. }
  53. func (n *ResultDatas) Swap(i, j int) {
  54. (*n)[i], (*n)[j] = (*n)[j], (*n)[i]
  55. }
  56. type ProjectTmp struct {
  57. BuyerId string `ch:"buyer_id"`
  58. }
  59. type Cooperate struct {
  60. Near bool
  61. ZbTime int64
  62. }
  63. type AggStruct struct {
  64. Id string `ch:"id"`
  65. Name string `ch:"name"`
  66. Sum uint64 `ch:"sum"`
  67. ProjectMoney decimal.Decimal `ch:"project_money"`
  68. Zbtime int64 `ch:"zbtime"`
  69. }
  70. type PrListRes struct {
  71. Result []*ResultData
  72. Size_1 int64
  73. Size_2 int64
  74. Size_3 int64
  75. Size_4 int64
  76. }
  77. func GetPrList(req *types.CoopHistoryReq) *PrListRes {
  78. redisFlag := ""
  79. if req.Buyer != "" {
  80. redisFlag = req.Buyer
  81. } else if req.Pid != "" {
  82. redisFlag = req.Pid
  83. }
  84. redisKey := fmt.Sprintf(NetworkManageCoopHistory, req.PositionId, req.ChannelType+"_"+redisFlag)
  85. prListRes := &PrListRes{}
  86. if rBt, rErr := redis.GetNewBytes("newother", redisKey); rErr == nil && rBt != nil {
  87. json.Unmarshal(*rBt, &prListRes)
  88. return prListRes
  89. }
  90. pTmp := ProjectTmp{}
  91. var err error
  92. if req.Buyer != "" {
  93. err = T.ClickhouseConn.QueryRow(context.TODO(), sql_2_2, req.Buyer).ScanStruct(&pTmp)
  94. } else if req.Pid != "" {
  95. err = T.ClickhouseConn.QueryRow(context.TODO(), sql_2_0, req.Pid).ScanStruct(&pTmp)
  96. } else {
  97. return prListRes
  98. }
  99. if err != nil || pTmp.BuyerId == "" {
  100. return prListRes
  101. }
  102. // firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构 sup_sub: 上下级
  103. // 1、同甲异业数据/ 3、招标代理机构渠道
  104. // 中间人可介绍业主
  105. var r3 []map[string]interface{}
  106. r3 = FindMiddleman([]string{pTmp.BuyerId}, req.PositionId, r3)
  107. prListRes.Size_4 = int64(len(r3))
  108. if req.ChannelType == "0" || req.ChannelType == "4" {
  109. for _, m := range r3 {
  110. tmp := ResultData{
  111. SourceType: "middleman",
  112. EntName: common.ObjToString(m["b_name"]),
  113. EntId: common.ObjToString(m["b_id"]),
  114. EntPerson: common.ObjToString(m["personName"]),
  115. Relationship: "业主的关系人",
  116. BuyerId: pTmp.BuyerId,
  117. }
  118. prListRes.Result = append(prListRes.Result, &tmp)
  119. }
  120. }
  121. // 关联单位
  122. var r4 []map[string]interface{}
  123. r4 = Findfirstparty([]string{pTmp.BuyerId}, r4)
  124. prListRes.Size_2 = int64(len(r4))
  125. if req.ChannelType == "0" || req.ChannelType == "2" {
  126. for _, m := range r4 {
  127. tmp := ResultData{
  128. SourceType: "sup_sub",
  129. EntName: common.ObjToString(m["b_name"]),
  130. EntId: common.ObjToString(m["b_id"]),
  131. EntPerson: common.ObjToString(m["personName"]),
  132. Relationship: common.ObjToString(m["relationship"]),
  133. BuyerId: pTmp.BuyerId,
  134. }
  135. prListRes.Result = append(prListRes.Result, &tmp)
  136. }
  137. }
  138. scopeClass := FindBusiness(req.EntId, req.EntUserId)
  139. r1 := GetWinnerData(scopeClass, pTmp.BuyerId)
  140. prListRes.Size_1 = int64(len(r1))
  141. if req.ChannelType == "0" || req.ChannelType == "1" {
  142. for _, m := range r1 {
  143. tmp := ResultData{
  144. SourceType: "adiffb",
  145. EntName: m.Name,
  146. ProjectNum: int(m.Sum),
  147. BuyerId: pTmp.BuyerId,
  148. RecentTime: m.Zbtime,
  149. }
  150. tmp.TotalAmount, _ = m.ProjectMoney.Float64()
  151. if time.Now().AddDate(-3, 0, 0).Unix() <= tmp.RecentTime {
  152. tmp.NearlyYears = true
  153. }
  154. prListRes.Result = append(prListRes.Result, &tmp)
  155. }
  156. }
  157. r2 := GetAgencyData(pTmp.BuyerId)
  158. prListRes.Size_3 = int64(len(r2))
  159. if req.ChannelType == "0" || req.ChannelType == "3" {
  160. for _, m := range r2 {
  161. tmp := ResultData{
  162. SourceType: "agency",
  163. EntName: m.Name,
  164. ProjectNum: int(m.Sum),
  165. BuyerId: pTmp.BuyerId,
  166. RecentTime: m.Zbtime,
  167. }
  168. tmp.TotalAmount, _ = m.ProjectMoney.Float64()
  169. if time.Now().AddDate(-3, 0, 0).Unix() <= tmp.RecentTime {
  170. tmp.NearlyYears = true
  171. }
  172. prListRes.Result = append(prListRes.Result, &tmp)
  173. }
  174. }
  175. redis.Put("newother", redisKey, prListRes, T.NetworkCom.CacheTimeout())
  176. return prListRes
  177. }
  178. func GetWinnerData(scopeClass string, bid string) []AggStruct {
  179. ass := []AggStruct{}
  180. if scopeClass == "" {
  181. return ass
  182. }
  183. args := []interface{}{bid, bid}
  184. wh, newArgs := common.WhArgs(strings.Split(scopeClass, ","))
  185. args = append(args, newArgs...)
  186. rows, err1 := T.ClickhouseConn.Query(context.TODO(), `select distinct winner as name,winner_id as id from information.transaction_info_all ARRAY JOIN winner_id,winner
  187. WHERE buyer_id IN (SELECT buyer_id from information.transaction_info_all WHERE has(winner_id,?) and buyer_id<>'') and winner_id<>? AND hasAny(topscopeclass,[`+wh+`])=0`, args...)
  188. if err1 != nil {
  189. logx.Error(err1)
  190. return ass
  191. }
  192. winnerIds := []string{}
  193. for rows.Next() {
  194. as := AggStruct{}
  195. if err := rows.ScanStruct(&as); err != nil {
  196. logx.Error(err)
  197. continue
  198. }
  199. winnerIds = append(winnerIds, as.Id)
  200. ass = append(ass, as)
  201. }
  202. rows.Close()
  203. if err := rows.Err(); err != nil {
  204. logx.Error(err)
  205. }
  206. hz := GetWinnerDataHz(bid, winnerIds)
  207. for _, v := range ass {
  208. if _, ok := hz[v.Id]; !ok {
  209. continue
  210. }
  211. v.ProjectMoney = hz[v.Id].ProjectMoney
  212. v.Sum = hz[v.Id].Sum
  213. v.Zbtime = hz[v.Id].Zbtime
  214. }
  215. return ass
  216. }
  217. //
  218. func GetWinnerDataHz(buyerId string, winnerIds []string) map[string]AggStruct {
  219. res := map[string]AggStruct{}
  220. if len(winnerIds) == 0 {
  221. return res
  222. }
  223. args := []interface{}{buyerId}
  224. wh, newArgs := common.WhArgs(winnerIds)
  225. args = append(args, newArgs...)
  226. rows, err1 := T.ClickhouseConn.Query(context.TODO(), `select winner_id as id,sum(1) as sum,sum(project_money) as project_money,max(zbtime) as zbtime from information.transaction_info_all ARRAY JOIN winner_id
  227. WHERE winner_id in(`+wh+`) and buyer_id=? group by winner_id`, args...)
  228. if err1 != nil {
  229. logx.Error(err1)
  230. return res
  231. }
  232. for rows.Next() {
  233. as := AggStruct{}
  234. if err := rows.ScanStruct(&as); err != nil {
  235. logx.Error(err)
  236. continue
  237. }
  238. res[as.Id] = as
  239. }
  240. rows.Close()
  241. if err := rows.Err(); err != nil {
  242. logx.Error(err)
  243. }
  244. return res
  245. }
  246. //代理机构
  247. func GetAgencyData(bid string) []AggStruct {
  248. ass := []AggStruct{}
  249. rows, err1 := T.ClickhouseConn.Query(context.TODO(), `select agency as name,agency_id as id,sum(1) as sum,sum(project_money) as project_money,max(zbtime) as zbtime from information.transaction_info_all where buyer_id=? and agency<>'' and agency_id<>'' GROUP by agency,agency_id`, bid)
  250. if err1 != nil {
  251. logx.Error(err1)
  252. return ass
  253. }
  254. for rows.Next() {
  255. as := AggStruct{}
  256. if err := rows.ScanStruct(&as); err != nil {
  257. logx.Error(err)
  258. continue
  259. }
  260. ass = append(ass, as)
  261. }
  262. rows.Close()
  263. if err := rows.Err(); err != nil {
  264. logx.Error(err)
  265. }
  266. return ass
  267. }
  268. type P_History struct {
  269. ProjectId string `ch:"project_id"`
  270. ProjectName string `ch:"project_name"`
  271. ZbTime int64 `ch:"zbtime"`
  272. Href string `ch:"href"`
  273. }
  274. // @Author jianghan
  275. // @Description 合作历史
  276. // @Date 2024/4/20
  277. func GetData3(source string, buyerId, winnerId string) (result []*P_History) {
  278. var rows driver.Rows
  279. if source == "1" {
  280. rows, _ = T.ClickhouseConn.Query(context.TODO(), sql_2_1_1, buyerId, winnerId)
  281. } else if source == "2" {
  282. rows, _ = T.ClickhouseConn.Query(context.TODO(), sql_2_1, buyerId, winnerId)
  283. }
  284. for rows.Next() {
  285. pHis := P_History{}
  286. _ = rows.ScanStruct(&pHis)
  287. pHis.Href = fmt.Sprintf("/article/content/%s.html", encrypt.CommonEncodeArticle("content", pHis.ProjectId))
  288. result = append(result, &pHis)
  289. }
  290. return
  291. }
  292. // @Author jianghan
  293. // @Description 上次合作时间
  294. // @Date 2024/4/24
  295. func LastTimeCoop(buyerId, ent, stype string) (bool, int64) {
  296. zbtime := int64(0)
  297. near := false
  298. if stype == "adiffb" {
  299. sql := `SELECT zbtime FROM information.transaction_info_all WHERE buyer_id = ? AND has(winner, ?) ORDER BY zbtime DESC LIMIT 1`
  300. _ = T.ClickhouseConn.QueryRow(context.TODO(), sql, buyerId, ent).Scan(&zbtime)
  301. } else if stype == "agency" {
  302. sql := `SELECT zbtime FROM information.transaction_info_all WHERE buyer_id = ? AND agency = ? ORDER BY zbtime DESC LIMIT 1`
  303. _ = T.ClickhouseConn.QueryRow(context.TODO(), sql, buyerId, ent).Scan(&zbtime)
  304. }
  305. timestamp := time.Now().AddDate(-3, 0, 0).Unix()
  306. if timestamp <= zbtime {
  307. near = true
  308. }
  309. return near, zbtime
  310. }
  311. func LastTimeCoopBath(positionId int64, buyerId string, winners, agencys []string) (map[string]*Cooperate, map[string]*Cooperate) {
  312. adiffb, agency := map[string]*Cooperate{}, map[string]*Cooperate{}
  313. if buyerId == "" {
  314. return adiffb, agency
  315. }
  316. var toSearch = func(tp int, query string, ws []string) {
  317. logx.Info("LastTimeCoopBath once start ", positionId)
  318. defer logx.Info("LastTimeCoopBath once over ", positionId)
  319. wh, newArgs := common.WhArgs(ws)
  320. query = fmt.Sprintf(query, wh)
  321. args := []interface{}{buyerId}
  322. args = append(args, newArgs...)
  323. rows, err := T.ClickhouseConn.Query(context.Background(), query, args...)
  324. if err != nil {
  325. logx.Error(err)
  326. return
  327. }
  328. for rows.Next() {
  329. var (
  330. one string
  331. zbtime int64
  332. )
  333. if err := rows.Scan(&one, &zbtime); err != nil {
  334. logx.Error(err)
  335. continue
  336. }
  337. c := &Cooperate{
  338. ZbTime: zbtime,
  339. }
  340. timestamp := time.Now().AddDate(-3, 0, 0).Unix()
  341. if timestamp <= c.ZbTime {
  342. c.Near = true
  343. }
  344. if tp == 1 {
  345. adiffb[one] = c
  346. } else {
  347. agency[one] = c
  348. }
  349. }
  350. rows.Close()
  351. if err := rows.Err(); err != nil {
  352. logx.Error(err)
  353. }
  354. }
  355. if len(winners) > 0 {
  356. key := fmt.Sprintf("networkManage_lastTimeCoop_winner_%s", common.GetMd5String(buyerId+"_"+strings.Join(winners, ",")))
  357. wb, err := redis.GetBytes("newother", key)
  358. if err == nil {
  359. json.Unmarshal(*wb, &adiffb)
  360. } else {
  361. sql := `SELECT winner_one,max(zbtime) FROM information.transaction_info_all ARRAY JOIN winner AS winner_one WHERE buyer_id=? AND hasAny(winner,[%s])) group by winner_one`
  362. wns := []string{}
  363. for _, v := range winners {
  364. wns = append(wns, v)
  365. if len(wns) == 50 {
  366. toSearch(1, sql, wns)
  367. wns = []string{}
  368. }
  369. }
  370. if len(wns) > 0 {
  371. toSearch(1, sql, wns)
  372. }
  373. redis.Put("newother", key, adiffb, T.NetworkCom.CacheTimeout())
  374. }
  375. }
  376. if len(agencys) > 0 {
  377. key := fmt.Sprintf("networkManage_lastTimeCoop_agency_%s", common.GetMd5String(buyerId+"_"+strings.Join(agencys, ",")))
  378. wb, err := redis.GetBytes("newother", key)
  379. if err == nil {
  380. json.Unmarshal(*wb, &agency)
  381. } else {
  382. sql := `SELECT agency,max(zbtime) FROM information.transaction_info_all WHERE buyer_id=? AND agency in (%s) group by agency`
  383. acs := []string{}
  384. for _, v := range agencys {
  385. acs = append(acs, v)
  386. if len(acs) == 50 {
  387. toSearch(2, sql, acs)
  388. acs = []string{}
  389. }
  390. }
  391. if len(acs) > 0 {
  392. toSearch(2, sql, acs)
  393. }
  394. redis.Put("newother", key, agency, T.NetworkCom.CacheTimeout())
  395. }
  396. }
  397. return adiffb, agency
  398. }