plistService.go 15 KB

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