plistService.go 12 KB

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