plistService.go 15 KB

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