plistService.go 14 KB

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