plistService.go 12 KB

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