plistService.go 13 KB

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