plistService.go 9.0 KB

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