plistService.go 15 KB

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