plistService.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "app.yhyue.com/moapp/jybase/common"
  7. P "app.yhyue.com/moapp/jybase/mapping"
  8. T "bp.jydev.jianyu360.cn/CRM/application/api/common"
  9. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  10. )
  11. const (
  12. sql_1 = `SELECT count(1) FROM information.transaction_info WHERE buyer_id = ?`
  13. sql_2 = `SELECT relate_id, is_handle, is_ignore, is_create FROM crm.connection_status WHERE position_id = ? AND itype = 2`
  14. )
  15. type ProjectData struct {
  16. count int64
  17. hasNextPage bool
  18. pList []*ProjectEntry
  19. }
  20. type ProjectEntry struct {
  21. ProjectId string `ch:"project_id"`
  22. ProjectName string `ch:"project_name"`
  23. BusinessType string `ch:"business_type"`
  24. Buyer string `ch:"buyer"`
  25. BuyerId string `ch:"buyer_id"`
  26. Winner []string `ch:"winner"`
  27. WinnerId []string `ch:"winner_id"`
  28. Area string `ch:"area"`
  29. City string `ch:"city"`
  30. District string `ch:"district"`
  31. ZbTime int64 `ch:"zbtime"`
  32. EndTime int64 `ch:"endtime"`
  33. IsHandle int `json:"IsHandle"`
  34. IsIgnore int `json:"IsIgnore"`
  35. IsCreate int `json:"IsCreate"`
  36. MyConn bool `json:"MyConn"`
  37. ConnType int `json:"ConnType"`
  38. HighSuccess bool `json:"HighSuccess"`
  39. NwRoute map[string]interface{} `json:"NwRoute"`
  40. }
  41. func GetProjectList(req *types.ProjectListReq) (resultList []*ProjectEntry, hasNextPage bool, total int) {
  42. buyerM := BuyerList(req.PartyA, req.Supplier, req.Heterotophy, req.Intermediary, req.Agency)
  43. MonitorStatusInit(req.PositionId, buyerM, "0")
  44. buyerArr := make([]string, len(*buyerM))
  45. for b := range *buyerM {
  46. buyerArr = append(buyerArr, b)
  47. }
  48. preSales := preSalesStatus(req.PositionId)
  49. isSqlPage := false
  50. if req.SaleStatus == "0" {
  51. isSqlPage = true // 是否sql分页
  52. }
  53. countSql, findSql := getQuerySql(req, isSqlPage, buyerArr)
  54. rows, err := T.ClickhouseConn.Query(context.TODO(), findSql)
  55. defer rows.Close()
  56. if err != nil {
  57. return nil, false, 0
  58. }
  59. for rows.Next() {
  60. project := ProjectEntry{}
  61. _ = rows.ScanStruct(&project)
  62. resultList = append(resultList, &project)
  63. }
  64. resultList = filterData(req, resultList, preSales, isSqlPage)
  65. if !isSqlPage {
  66. total = len(resultList)
  67. if total > req.PageSize {
  68. start := (req.PageNum - 1) * req.PageSize
  69. end := start + req.PageSize
  70. if req.PageNum > 1 {
  71. resultList = resultList[start:end]
  72. } else {
  73. resultList = resultList[:req.PageSize]
  74. }
  75. }
  76. } else {
  77. total = int(T.NetworkCom.Count(countSql))
  78. }
  79. if total > req.PageSize {
  80. hasNextPage = true
  81. } else {
  82. hasNextPage = false
  83. }
  84. moreInfo(req, resultList) // 补充信息
  85. return
  86. }
  87. // @Author jianghan
  88. // @Description 销售机会线索状态
  89. // @Date 2024/4/18
  90. func preSalesStatus(posid int64) (m1 map[string]interface{}) {
  91. m1 = make(map[string]interface{})
  92. info := T.CrmMysql.SelectBySql(sql_2, posid)
  93. if info != nil && len(*info) > 0 {
  94. for _, m := range *info {
  95. m1[common.ObjToString(m["relate_id"])] = m
  96. }
  97. }
  98. return m1
  99. }
  100. func getQuerySql(req *types.ProjectListReq, isPage bool, buyerArr []string) (countSql, findSql string) {
  101. querys := []string{}
  102. // 左侧选中的业主id
  103. if len(buyerArr) > 0 {
  104. querys = append(querys, fmt.Sprintf(" a.buyer_id in (%s) ", strings.Join(buyerArr, ",")))
  105. }
  106. // 商机类型
  107. if req.BusinessType != "" && req.BusinessType != "全部" {
  108. querys = append(querys, fmt.Sprintf(" a.business_type in ('%s') ", strings.Join(strings.Split(req.BusinessType, ","), "', '")))
  109. }
  110. if req.ProjectName != "" {
  111. querys = append(querys, " a.project_name like '%"+req.ProjectName+"%'")
  112. }
  113. if req.StartTime > 0 && req.EntTime > 0 {
  114. st := req.StartTime + 90*24*60*60
  115. et := req.StartTime + 90*24*60*60
  116. querys = append(querys, fmt.Sprintf(" a.endtime>=%d and a.endtime<=%d", st, et))
  117. } else if req.StartTime > 0 && req.EntTime == 0 {
  118. st := req.StartTime + 90*24*60*60
  119. querys = append(querys, fmt.Sprintf(" a.endtime>=%d", st))
  120. } else if req.StartTime == 0 && req.EntTime > 0 {
  121. et := req.StartTime + 90*24*60*60
  122. querys = append(querys, fmt.Sprintf(" a.endtime<=%d", et))
  123. }
  124. var regionArr = []string{}
  125. if req.Area != "" || req.City != "" || req.District != "" {
  126. //城市
  127. city := []string{}
  128. for _, v := range strings.Split(req.City, ",") {
  129. if P.BidCodeMapping.City[v] != "" {
  130. city = append(city, fmt.Sprint(P.BidCodeMapping.City[v]))
  131. }
  132. }
  133. if len(city) > 0 {
  134. regionArr = append(regionArr, fmt.Sprintf(" a.city in (%s) ", strings.Join(city, ",")))
  135. }
  136. //区域
  137. area := []string{}
  138. for _, v := range strings.Split(req.Area, ",") {
  139. if P.BidCodeMapping.Area[v] != "" {
  140. area = append(area, fmt.Sprint(P.BidCodeMapping.Area[v]))
  141. }
  142. }
  143. if len(area) > 0 {
  144. regionArr = append(regionArr, fmt.Sprintf(" a.area in (%s) ", strings.Join(area, ",")))
  145. }
  146. //区域
  147. district := []string{}
  148. if req.District != "" {
  149. for _, v := range strings.Split(req.District, ",") {
  150. cityName := strings.Split(v, "_")[0]
  151. districtName := strings.Split(v, "_")[1]
  152. if P.BidCodeMapping.District[cityName][districtName] != "" {
  153. district = append(district, fmt.Sprint(P.BidCodeMapping.District[cityName][districtName]))
  154. }
  155. }
  156. }
  157. if len(district) > 0 {
  158. regionArr = append(regionArr, fmt.Sprintf(" a.district in (%s) ", strings.Join(district, ",")))
  159. }
  160. if len(regionArr) > 0 {
  161. querys = append(querys, fmt.Sprintf("(%s)", strings.Join(regionArr, "or")))
  162. }
  163. }
  164. if req.SubClass != "" {
  165. arr := []string{}
  166. for _, v := range strings.Split(req.SubClass, ",") {
  167. arr = append(arr, fmt.Sprintf(`"物业_%s"`, v))
  168. }
  169. querys = append(querys, fmt.Sprintf(" a.subclass in (%s) ", strings.Join(regionArr, ",")))
  170. }
  171. // 项目金额
  172. if req.Amount != "" && strings.Contains(req.Amount, "-") {
  173. minPriceStr, maxPriceStr := strings.Split(req.Amount, "-")[0], strings.Split(req.Amount, "-")[1]
  174. minPrice := common.Int64All(common.Float64All(minPriceStr) * 10000) //换成元
  175. maxPrice := common.Int64All(common.Float64All(maxPriceStr) * 10000) //换成元
  176. if minPriceStr != "" && maxPriceStr != "" {
  177. querys = append(querys, fmt.Sprintf("((a.project_money>=%d and a.project_money<=%d))", minPrice, maxPrice))
  178. } else if minPriceStr != "" {
  179. querys = append(querys, fmt.Sprintf("(a.project_money>=%d)", minPrice))
  180. } else if maxPriceStr != "" {
  181. querys = append(querys, fmt.Sprintf("(a.project_money<=%d)", maxPrice))
  182. }
  183. }
  184. //物业业态
  185. if req.PropertyForm != "" {
  186. arr := []string{}
  187. for _, v := range strings.Split(req.PropertyForm, ",") {
  188. arr = append(arr, fmt.Sprintf(`"%s"`, v))
  189. }
  190. querys = append(querys, fmt.Sprintf(" a.property_form in (%s) ", strings.Join(arr, ",")))
  191. }
  192. findSql = "select a.project_id, a.project_name, a.business_type, a.buyer, a.buyer_id, a.winner, a.winner_id, a.area, a.city, a.district, a.zbtime, a.endtime "
  193. if len(querys) > 0 {
  194. countSql = fmt.Sprintf("select count(1) from %s a where %s ", "information.transaction_info", strings.Join(querys, " and "))
  195. findSql = fmt.Sprintf("%s from %s a where %s ", findSql, "information.transaction_info", strings.Join(querys, " and "))
  196. } else {
  197. countSql = fmt.Sprintf("select count(1) from %s a ", "information.transaction_info")
  198. findSql = fmt.Sprintf("%s from %s a ", findSql, "information.transaction_info")
  199. }
  200. if isPage {
  201. findSql += fmt.Sprintf(" limit %d,%d", (req.PageNum-1)*req.PageSize, req.PageSize)
  202. }
  203. return
  204. }
  205. // @Author jianghan
  206. // @Description 过滤数据/补充销售机会状态信息,返回分页结果数据
  207. // @Date 2024/4/18
  208. func filterData(req *types.ProjectListReq, resultList []*ProjectEntry, preSales map[string]interface{}, isSqlPage bool) []*ProjectEntry {
  209. var newList []*ProjectEntry
  210. f := make(map[string]int, 3)
  211. if strings.Contains(req.SaleStatus, "1") {
  212. f["is_handle"] = 0
  213. } else if strings.Contains(req.SaleStatus, "2") {
  214. f["is_ignore"] = 1
  215. } else if strings.Contains(req.SaleStatus, "3") {
  216. f["is_create"] = 1
  217. }
  218. for _, m := range resultList {
  219. if m1, ok := preSales[m.ProjectId].(map[string]interface{}); ok {
  220. m.IsHandle = common.IntAll(m1["is_handle"])
  221. m.IsIgnore = common.IntAll(m1["is_ignore"])
  222. m.IsCreate = common.IntAll(m1["is_create"])
  223. }
  224. if !isSqlPage {
  225. for k, v := range f {
  226. if k == "is_handle" && m.IsHandle == v {
  227. newList = append(newList, m)
  228. break
  229. } else if k == "is_ignore" && m.IsIgnore == v {
  230. newList = append(newList, m)
  231. break
  232. } else if k == "is_create" && m.IsCreate == v {
  233. newList = append(newList, m)
  234. break
  235. }
  236. }
  237. }
  238. }
  239. if !isSqlPage {
  240. if newList == nil {
  241. resultList = make([]*ProjectEntry, 0)
  242. } else {
  243. resultList = newList
  244. }
  245. }
  246. return resultList
  247. }
  248. // @Author jianghan
  249. // @Description 补充人脉 等信息
  250. // @Date 2024/4/17
  251. func moreInfo(req *types.ProjectListReq, list []*ProjectEntry) (result []*ProjectEntry) {
  252. for _, m := range list {
  253. // 人脉、人脉所在单位项目 conn_type: 1 人脉可转介绍项目; conn_type: 2 人脉所在单位项目
  254. field1 := ""
  255. query1 := map[string]interface{}{
  256. "position_id": req.PositionId,
  257. "company_id": m.BuyerId,
  258. "status": 1,
  259. }
  260. info1 := T.CrmMysql.FindOne("connection", query1, field1, "")
  261. if info1 != nil && len(*info1) > 0 {
  262. m.MyConn = true // 我的人脉
  263. m.ConnType = 1
  264. } else {
  265. m.MyConn = false
  266. }
  267. if m.ConnType == 0 {
  268. query2 := map[string]interface{}{
  269. "company_id": m.BuyerId,
  270. }
  271. info2 := T.CrmMysql.FindOne("connection", query2, field1, "")
  272. if info2 != nil && len(*info2) > 0 {
  273. m.ConnType = 1
  274. } else {
  275. m.ConnType = 2
  276. }
  277. }
  278. // 转介绍成功率高标签
  279. count := 0
  280. err := T.ClickhouseConn.QueryRow(context.TODO(), sql_1, m.BuyerId).Scan(&count)
  281. if err != nil && count > 2 {
  282. m.HighSuccess = true
  283. } else {
  284. m.HighSuccess = false
  285. }
  286. // 人脉路径
  287. // 有我的人脉标签时不需要查询人脉路径信息
  288. if m.MyConn == false && m.BuyerId != "" {
  289. companyList := ConnectionsHandle([]string{m.BuyerId}, req.PositionId, false)
  290. if len(companyList) > 0 {
  291. m.NwRoute = companyList[0]
  292. }
  293. }
  294. }
  295. return list
  296. }