plistService.go 15 KB

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