plistService.go 15 KB

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