plistService.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "math"
  7. "strconv"
  8. "strings"
  9. "app.yhyue.com/moapp/jybase/common"
  10. "app.yhyue.com/moapp/jybase/encrypt"
  11. "app.yhyue.com/moapp/jybase/redis"
  12. "app.yhyue.com/moapp/jypkg/ent/util"
  13. T "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
  14. "bp.jydev.jianyu360.cn/CRM/networkManage/api/internal/types"
  15. "github.com/gogf/gf/v2/util/gconv"
  16. "github.com/shopspring/decimal"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. )
  19. const (
  20. sql_1 = `SELECT buyer_id, count(1) as count FROM information.transaction_info_all WHERE buyer_id in (%s) GROUP BY buyer_id`
  21. sql_2 = `SELECT relate_id, is_handle, is_ignore, is_create FROM crm.connection_status WHERE position_id = ? AND itype = 2`
  22. sql_3 = `SELECT * FROM crm.connection WHERE company_id in (%s) AND status = 1`
  23. sql_4 = `SELECT id, s_id FROM base_service.follow_project_monitor WHERE s_userid = ?`
  24. NetworkManageProjectList = "networkManage_projectList_%d_%s"
  25. )
  26. type ProjectData struct {
  27. Count int64
  28. HasNextPage bool
  29. PList []*ProjectEntry
  30. }
  31. type ProjectEntry struct {
  32. ProjectId string `ch:"project_id"`
  33. ProjectName string `ch:"project_name"`
  34. BusinessType string `ch:"business_type"`
  35. Buyer string `ch:"buyer"`
  36. BuyerId string `ch:"buyer_id"`
  37. Area string `ch:"area"`
  38. City string `ch:"city"`
  39. District string `ch:"district"`
  40. ZbTime int64 `ch:"zbtime"`
  41. EndTime int64 `ch:"endtime"`
  42. ProjectMoney decimal.Decimal `ch:"project_money"`
  43. InfoId string `ch:"info_id"`
  44. InformationId string `ch:"information_id"`
  45. InfoIds []string `ch:"info_ids"`
  46. Href string `json:"Href"`
  47. IsHandle int `json:"IsHandle"`
  48. IsIgnore int `json:"IsIgnore"`
  49. IsCreate int `json:"IsCreate"`
  50. MyConn bool `json:"MyConn"`
  51. ConnType int `json:"ConnType"`
  52. HighSuccess bool `json:"HighSuccess"`
  53. BId string `json:"BId"`
  54. BName string `json:"BName"`
  55. RelationShip string `json:"RelationShip"`
  56. SourceType string `json:"SourceType"` // firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  57. Person string `json:"Person"`
  58. Num int `json:"Num"`
  59. FocusId string `json:"FocusId"` // 监控主键
  60. IsFocus int `json:"IsFocus"`
  61. }
  62. func GetProjectList(req *types.ProjectListReq) *ProjectData {
  63. // 初始化分页参数
  64. if req.PageSize == 0 {
  65. req.PageSize = 10
  66. }
  67. if req.PageNum == 0 {
  68. req.PageNum = 1
  69. }
  70. projectData := &ProjectData{
  71. PList: []*ProjectEntry{},
  72. }
  73. businessStr := FindBusiness(req.EntId, req.UserId)
  74. if businessStr == "" {
  75. return projectData
  76. }
  77. logx.Info(req.PositionId, " GetProjectList start 。。。")
  78. dataMap := BuyerList(req.PartyA, req.Intermediary, req.PositionId)
  79. if len(*dataMap) == 0 && req.Supplier == "" && req.Heterotophy == "" && req.Agency == "" {
  80. return projectData
  81. }
  82. logx.Info(req.PositionId, " GetProjectList BuyerList over 。。。")
  83. // 项目数量查询
  84. buyerArr := getBuyerArr(dataMap)
  85. //preSales := preSalesStatus(req.PositionId)
  86. //isSqlPage := true
  87. //if req.SaleStatus == "0" {
  88. // isSqlPage = true // 是否sql分页
  89. //}
  90. countSql, findSql, args := getQuerySql(req, buyerArr, businessStr)
  91. // 缓存
  92. rkey := fmt.Sprintf(NetworkManageProjectList, req.PositionId, common.GetMd5String(fmt.Sprintf("%+v", req)))
  93. if ret, err := redis.GetNewBytes("newother", rkey); err == nil && ret != nil {
  94. err = json.Unmarshal(*ret, &projectData)
  95. } else {
  96. logx.Info(req.PositionId, " GetProjectList findSql ", findSql, args)
  97. logx.Info(req.PositionId, " GetProjectList countSql ", countSql, args)
  98. rows, err := T.ClickhouseConn.Query(context.TODO(), findSql, args...)
  99. if err != nil {
  100. return projectData
  101. }
  102. defer rows.Close()
  103. resultList := []*ProjectEntry{}
  104. for rows.Next() {
  105. project := ProjectEntry{}
  106. ssc := rows.ScanStruct(&project)
  107. if ssc != nil {
  108. logx.Error(ssc)
  109. continue
  110. }
  111. resultList = append(resultList, &project)
  112. }
  113. projectData.PList = resultList
  114. logx.Info(req.PositionId, " GetProjectList list over 。。。")
  115. projectData.Count = T.NetworkCom.Count(countSql, args...)
  116. logx.Info(req.PositionId, " GetProjectList count over 。。。")
  117. moreInfo(req, getIyProList(req.Intermediary, req.PositionId), projectData.PList) // 补充信息
  118. redis.Put("newother", rkey, projectData, T.NetworkCom.CacheTimeout())
  119. }
  120. mmp := MonitorStatus(req.UserId) // 项目监控
  121. projectData.PList = filterData(req, projectData.PList, nil, mmp, true)
  122. logx.Info(req.PositionId, " GetProjectList over 。。。")
  123. totalPage := int(math.Ceil(float64(projectData.Count) / float64(req.PageSize)))
  124. if totalPage > req.PageNum {
  125. projectData.HasNextPage = true
  126. } else {
  127. projectData.HasNextPage = false
  128. }
  129. return projectData
  130. }
  131. // @Author jianghan
  132. // @Description 销售机会线索状态
  133. // @Date 2024/4/18
  134. func preSalesStatus(posid int64) (m1 map[string]interface{}) {
  135. m1 = make(map[string]interface{})
  136. info := T.CrmMysql.SelectBySql(sql_2, posid)
  137. if info != nil && len(*info) > 0 {
  138. for _, m := range *info {
  139. m1[common.ObjToString(m["relate_id"])] = m
  140. }
  141. }
  142. return m1
  143. }
  144. // 中间人 直接查询可介绍项目
  145. func getIyProList(iy string, positionId int64) (array []string) {
  146. if iy == "" {
  147. return
  148. }
  149. wh, args := common.WhArgs(strings.Split(iy, ","))
  150. args = append(args, positionId)
  151. intermediaryArr := T.CrmMysql.SelectBySql(`select b.relate_id as relate_id,b.relate_name as relate_name from crm.connection a inner join crm.connection_introduce b on ( a.id in (`+wh+`) and a.position_id=? and a.id=b.connection_id and b.itype=2)`, args...)
  152. for _, m := range *intermediaryArr {
  153. pid := gconv.String(m["relate_id"])
  154. array = append(array, pid)
  155. }
  156. return
  157. }
  158. func getQuerySql(req *types.ProjectListReq, plist []string, businessStr string) (countSql, findSql string, allArgs []interface{}) {
  159. countSql = "select count(1) from information.transaction_info_all"
  160. findSql = "select project_id,project_name,business_type,buyer,buyer_id,area,city,district,zbtime,endtime,project_money,info_id,information_id,info_ids from information.transaction_info_all"
  161. allArgs = []interface{}{}
  162. ors1 := []string{}
  163. if len(plist) > 0 {
  164. wh, args := common.WhArgs(plist)
  165. ors1 = append(ors1, `buyer_id in (`+wh+`)`)
  166. allArgs = append(allArgs, args...)
  167. }
  168. wm := map[string]bool{}
  169. winner_ids := []string{}
  170. yearLimit := T.NetworkCom.ProjectYearLimit()
  171. if req.Supplier != "" {
  172. for _, v := range strings.Split(req.Supplier, ",") {
  173. if wm[v] {
  174. continue
  175. }
  176. winner_ids = append(winner_ids, v)
  177. wm[v] = true
  178. }
  179. }
  180. if req.Heterotophy != "" {
  181. for _, v := range strings.Split(req.Heterotophy, ",") {
  182. if wm[v] {
  183. continue
  184. }
  185. winner_ids = append(winner_ids, v)
  186. wm[v] = true
  187. }
  188. }
  189. ors2 := []string{}
  190. if len(winner_ids) > 0 {
  191. wh, args := common.WhArgs(winner_ids)
  192. ors2 = append(ors2, `hasAny(winner_id,[`+wh+`])`)
  193. allArgs = append(allArgs, args...)
  194. }
  195. if req.Agency != "" {
  196. wh, args := common.WhArgs(strings.Split(req.Agency, ","))
  197. ors2 = append(ors2, `agency_id in (`+wh+`)`)
  198. allArgs = append(allArgs, args...)
  199. }
  200. if len(ors2) > 0 {
  201. ors1 = append(ors1, `(buyer_id in (select DISTINCT buyer_id from transaction_info_all where (`+strings.Join(ors2, " or ")+`) and buyer_id<>'' and zbtime>?))`)
  202. allArgs = append(allArgs, yearLimit)
  203. }
  204. allArgs = append(allArgs, yearLimit)
  205. ands := []string{}
  206. //业务类型
  207. if businessStr != "" {
  208. wh, args := common.WhArgs(strings.Split(businessStr, ","))
  209. ands = append(ands, `hasAny(topscopeclass,[`+wh+`])`)
  210. allArgs = append(allArgs, args...)
  211. }
  212. //区域
  213. if req.Area != "" {
  214. ands = append(ands, "area=?")
  215. allArgs = append(allArgs, req.Area)
  216. }
  217. // 项目金额 0: 全部,1: 50万以下,2: 50-100万, 3: 100-200万, 4: 200-500万, 5:500万以上
  218. if req.Amount != "" {
  219. if !strings.Contains(req.Amount, "0") {
  220. var tempArr []string
  221. for _, s := range strings.Split(req.Amount, ",") {
  222. if s == "1" {
  223. tempArr = append(tempArr, "project_money<=?")
  224. allArgs = append(allArgs, 500000)
  225. } else if s == "2" {
  226. tempArr = append(tempArr, "(project_money>=? and project_money<=?)")
  227. allArgs = append(allArgs, 500000, 1000000)
  228. } else if s == "3" {
  229. tempArr = append(tempArr, "(project_money>=? and project_money<=?)")
  230. allArgs = append(allArgs, 1000000, 2000000)
  231. } else if s == "4" {
  232. tempArr = append(tempArr, "(project_money>=? and project_money<=?)")
  233. allArgs = append(allArgs, 2000000, 5000000)
  234. } else if s == "5" {
  235. tempArr = append(tempArr, "project_money>=?")
  236. allArgs = append(allArgs, 5000000)
  237. }
  238. }
  239. if len(tempArr) > 0 {
  240. ands = append(ands, fmt.Sprintf("(%s)", strings.Join(tempArr, " or ")))
  241. }
  242. }
  243. }
  244. //过滤掉已中标的招标项目数据
  245. sqlAppend := " where (" + strings.Join(ors1, " or ") + ") and project_bidstatus>1 and zbtime>? and " + strings.Join(ands, " and ")
  246. countSql += sqlAppend
  247. findSql += fmt.Sprintf(sqlAppend+" order by zbtime desc limit %d,%d", (req.PageNum-1)*req.PageSize, req.PageSize)
  248. return
  249. }
  250. // @Author jianghan
  251. // @Description 过滤数据/补充销售机会状态信息,返回分页结果数据
  252. // @Date 2024/4/18
  253. func filterData(req *types.ProjectListReq, resultList []*ProjectEntry, preSales, mmp map[string]interface{}, isSqlPage bool) []*ProjectEntry {
  254. var newList []*ProjectEntry
  255. f := make(map[string]int, 1)
  256. if req.SaleStatus == "1" {
  257. f["is_handle"] = 0
  258. } else if req.SaleStatus == "2" {
  259. f["is_ignore"] = 1
  260. } else if req.SaleStatus == "3" {
  261. f["is_create"] = 1
  262. }
  263. for _, m := range resultList {
  264. // 处理/忽略/销售机会
  265. //if m1, ok := preSales[m.ProjectId].(map[string]interface{}); ok {
  266. // m.IsIgnore = common.IntAll(m1["is_handle"])
  267. // m.IsIgnore = common.IntAll(m1["is_ignore"])
  268. // m.IsCreate = common.IntAll(m1["is_create"])
  269. //}
  270. // 监控
  271. for _, s := range strings.Split(m.InfoId, ",") {
  272. if mmp[s] != nil {
  273. m.IsFocus = 1
  274. m.FocusId = common.ObjToString(mmp[s])
  275. break
  276. }
  277. }
  278. if !isSqlPage {
  279. for k, v := range f {
  280. if k == "is_handle" && m.IsHandle == v {
  281. newList = append(newList, m)
  282. } else if k == "is_ignore" && m.IsIgnore == v {
  283. newList = append(newList, m)
  284. } else if k == "is_create" && m.IsCreate == v {
  285. newList = append(newList, m)
  286. }
  287. }
  288. }
  289. }
  290. if !isSqlPage {
  291. if newList == nil {
  292. resultList = make([]*ProjectEntry, 0)
  293. } else {
  294. resultList = newList
  295. }
  296. }
  297. return resultList
  298. }
  299. // @Author jianghan
  300. // @Description 补充人脉 等信息
  301. // @Date 2024/4/17
  302. func moreInfo(req *types.ProjectListReq, plist []string, list []*ProjectEntry) (result []*ProjectEntry) {
  303. var buyerIds []string
  304. for _, m := range list {
  305. if m.BuyerId != "" {
  306. buyerIds = append(buyerIds, m.BuyerId)
  307. }
  308. }
  309. countMap := make(map[string]int)
  310. connMap := make(map[string]int)
  311. if buyerIds != nil && len(buyerIds) > 0 {
  312. str1, arr1 := common.WhArgs(buyerIds)
  313. info1, err := T.ClickhouseConn.Query(context.TODO(), fmt.Sprintf(sql_1, str1), arr1...)
  314. if err == nil {
  315. for info1.Next() {
  316. var buyerId string
  317. var count uint64
  318. _ = info1.Scan(&buyerId, &count)
  319. countMap[buyerId] = int(count)
  320. }
  321. }
  322. info2 := T.CrmMysql.SelectBySql(fmt.Sprintf(sql_3, str1), arr1...)
  323. if info2 != nil && len(*info2) > 0 {
  324. for _, m := range *info2 {
  325. if req.PositionId == common.Int64All(m["position_id"]) {
  326. connMap[common.ObjToString(m["company_id"])] = 1 // 我的人脉
  327. } else {
  328. connMap[common.ObjToString(m["company_id"])] = 2
  329. }
  330. }
  331. }
  332. }
  333. for _, m := range list {
  334. // 中间人的项目 默认 我的人脉
  335. if len(plist) > 0 {
  336. for _, pid := range plist {
  337. if m.ProjectId == pid {
  338. m.MyConn = true
  339. m.ConnType = 1
  340. break
  341. }
  342. }
  343. }
  344. // 补充跳转链接
  345. //if m.BusinessType == "采购意向" || m.BusinessType == "招标项目" {
  346. m.Href = fmt.Sprintf("/article/content/%s.html", encrypt.CommonEncodeArticle("content", m.InfoId))
  347. //}
  348. m.ProjectId = util.EncodeId(m.ProjectId)
  349. // 人脉、人脉所在单位项目 conn_type: 1 人脉可转介绍项目; conn_type: 2 人脉所在单位项目
  350. if !m.MyConn {
  351. if connMap[m.BuyerId] == 1 {
  352. m.MyConn = true // 我的人脉
  353. m.ConnType = 1
  354. } else {
  355. m.MyConn = false
  356. }
  357. }
  358. if m.ConnType == 0 {
  359. if connMap[m.BuyerId] != 0 {
  360. m.ConnType = 1
  361. } else {
  362. m.ConnType = 2
  363. }
  364. }
  365. // 转介绍成功率高标签
  366. if countMap[m.BuyerId] > 2 {
  367. m.HighSuccess = true
  368. } else {
  369. m.HighSuccess = false
  370. }
  371. }
  372. // 人脉路径
  373. var bArr []string
  374. for _, m := range list {
  375. // 有我的人脉标签时不需要查询人脉路径信息
  376. if m.MyConn == false && m.BuyerId != "" {
  377. bArr = append(bArr, fmt.Sprintf("'%s'", m.BuyerId))
  378. }
  379. }
  380. companyList := Findfirstparty(bArr, nil)
  381. if companyList != nil && len(companyList) > 0 {
  382. for _, m := range list {
  383. if m.MyConn == false {
  384. for _, m1 := range companyList {
  385. if m.BuyerId == common.ObjToString(m1["a_id"]) {
  386. m.BId = common.ObjToString(m1["b_id"])
  387. m.BName = common.ObjToString(m1["b_name"])
  388. m.RelationShip = common.ObjToString(m1["relationship"])
  389. m.SourceType = common.ObjToString(m1["sourceType"])
  390. m.Person = common.ObjToString(m1["person"])
  391. m.Num = common.IntAll(m1["count"])
  392. break
  393. }
  394. }
  395. }
  396. }
  397. } else {
  398. companyList = Findwinner(bArr)
  399. if companyList != nil && len(companyList) > 0 {
  400. for _, m := range list {
  401. if m.MyConn == false {
  402. for _, m1 := range companyList {
  403. if m.BuyerId == common.ObjToString(m1["a_id"]) {
  404. m.BId = common.ObjToString(m1["b_id"])
  405. m.BName = common.ObjToString(m1["b_name"])
  406. m.RelationShip = common.ObjToString(m1["relationship"])
  407. m.SourceType = common.ObjToString(m1["sourceType"])
  408. m.Person = common.ObjToString(m1["person"])
  409. m.Num = common.IntAll(m1["count"])
  410. break
  411. }
  412. }
  413. }
  414. }
  415. }
  416. }
  417. return list
  418. }
  419. func MonitorStatus(uid string) map[string]interface{} {
  420. m1 := make(map[string]interface{})
  421. info := T.BaseMysql.SelectBySql(sql_4, uid)
  422. for _, m := range *info {
  423. m1[common.ObjToString(m["s_id"])] = util.EncodeId(strconv.Itoa(common.IntAll(m["id"])))
  424. }
  425. return m1
  426. }