plistService.go 8.7 KB

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