plistService.go 8.5 KB

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