plistService.go 13 KB

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