plistService.go 8.7 KB

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