plistService.go 13 KB

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