plistService.go 8.9 KB

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