plistService.go 13 KB

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