plistService.go 13 KB

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