plistService.go 13 KB

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