plistService.go 12 KB

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