plistService.go 15 KB

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