plistService.go 9.6 KB

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