plistService.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. P "app.yhyue.com/moapp/jybase/mapping"
  5. T "bp.jydev.jianyu360.cn/CRM/application/api/common"
  6. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  7. "context"
  8. "fmt"
  9. "github.com/shopspring/decimal"
  10. "strings"
  11. )
  12. const (
  13. sql_1 = `SELECT buyer_id, count(1) as count FROM information.transaction_info WHERE buyer_id in (%s) GROUP BY 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. sql_3 = `SELECT * FROM crm.connection WHERE company_id in (%s) AND status = 1`
  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. BusinessType string `ch:"business_type"`
  26. Buyer string `ch:"buyer"`
  27. BuyerId string `ch:"buyer_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. ProjectMoney decimal.Decimal `ch:"project_money"`
  34. IsHandle int `json:"IsHandle"`
  35. IsIgnore int `json:"IsIgnore"`
  36. IsCreate int `json:"IsCreate"`
  37. MyConn bool `json:"MyConn"`
  38. ConnType int `json:"ConnType"`
  39. HighSuccess bool `json:"HighSuccess"`
  40. BId string `json:"BId"`
  41. BName string `json:"BName"`
  42. RelationShip string `json:"RelationShip"`
  43. SourceType string `json:"SourceType"` // firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  44. Person string `json:"Person"`
  45. Num string `json:"Num"`
  46. }
  47. func GetProjectList(req *types.ProjectListReq) (resultList []*ProjectEntry, hasNextPage bool, total int) {
  48. buyerM := BuyerList(req.PartyA, req.Supplier, req.Heterotophy, req.Intermediary, req.Agency)
  49. MonitorStatusInit(req.PositionId, buyerM, "0")
  50. buyerArr := make([]string, len(*buyerM))
  51. for b := range *buyerM {
  52. buyerArr = append(buyerArr, b)
  53. }
  54. preSales := preSalesStatus(req.PositionId)
  55. isSqlPage := false
  56. if req.SaleStatus == "0" {
  57. isSqlPage = true // 是否sql分页
  58. }
  59. countSql, findSql := getQuerySql(req, isSqlPage, buyerArr)
  60. rows, err := T.ClickhouseConn.Query(context.TODO(), findSql)
  61. defer rows.Close()
  62. if err != nil {
  63. return nil, false, 0
  64. }
  65. for rows.Next() {
  66. project := ProjectEntry{}
  67. _ = rows.ScanStruct(&project)
  68. resultList = append(resultList, &project)
  69. }
  70. resultList = filterData(req, resultList, preSales, isSqlPage)
  71. if !isSqlPage {
  72. total = len(resultList)
  73. if total > req.PageSize {
  74. start := (req.PageNum - 1) * req.PageSize
  75. end := start + req.PageSize
  76. if req.PageNum > 1 {
  77. resultList = resultList[start:end]
  78. } else {
  79. resultList = resultList[:req.PageSize]
  80. }
  81. }
  82. } else {
  83. total = int(T.NetworkCom.Count(countSql))
  84. }
  85. if total > req.PageSize {
  86. hasNextPage = true
  87. } else {
  88. hasNextPage = false
  89. }
  90. moreInfo(req, resultList) // 补充信息
  91. return
  92. }
  93. // @Author jianghan
  94. // @Description 销售机会线索状态
  95. // @Date 2024/4/18
  96. func preSalesStatus(posid int64) (m1 map[string]interface{}) {
  97. m1 = make(map[string]interface{})
  98. info := T.CrmMysql.SelectBySql(sql_2, posid)
  99. if info != nil && len(*info) > 0 {
  100. for _, m := range *info {
  101. m1[common.ObjToString(m["relate_id"])] = m
  102. }
  103. }
  104. return m1
  105. }
  106. func getQuerySql(req *types.ProjectListReq, isPage bool, buyerArr []string) (countSql, findSql string) {
  107. querys := []string{}
  108. // 左侧选中的业主id
  109. if len(buyerArr) > 0 {
  110. querys = append(querys, fmt.Sprintf(" a.buyer_id in (%s) ", strings.Join(buyerArr, ",")))
  111. }
  112. // 商机类型
  113. if req.BusinessType != "" && req.BusinessType != "全部" {
  114. querys = append(querys, fmt.Sprintf(" a.business_type in ('%s') ", strings.Join(strings.Split(req.BusinessType, ","), "', '")))
  115. }
  116. if req.ProjectName != "" {
  117. querys = append(querys, " a.project_name like '%"+req.ProjectName+"%'")
  118. }
  119. if req.StartTime > 0 && req.EntTime > 0 {
  120. st := req.StartTime + 90*24*60*60
  121. et := req.StartTime + 90*24*60*60
  122. querys = append(querys, fmt.Sprintf(" a.endtime>=%d and a.endtime<=%d", st, et))
  123. } else if req.StartTime > 0 && req.EntTime == 0 {
  124. st := req.StartTime + 90*24*60*60
  125. querys = append(querys, fmt.Sprintf(" a.endtime>=%d", st))
  126. } else if req.StartTime == 0 && req.EntTime > 0 {
  127. et := req.StartTime + 90*24*60*60
  128. querys = append(querys, fmt.Sprintf(" a.endtime<=%d", et))
  129. }
  130. var regionArr = []string{}
  131. if req.Area != "" || req.City != "" || req.District != "" {
  132. //城市
  133. city := []string{}
  134. for _, v := range strings.Split(req.City, ",") {
  135. if P.BidCodeMapping.City[v] != "" {
  136. city = append(city, fmt.Sprint(P.BidCodeMapping.City[v]))
  137. }
  138. }
  139. if len(city) > 0 {
  140. regionArr = append(regionArr, fmt.Sprintf(" a.city in (%s) ", strings.Join(city, ",")))
  141. }
  142. //区域
  143. area := []string{}
  144. for _, v := range strings.Split(req.Area, ",") {
  145. if P.BidCodeMapping.Area[v] != "" {
  146. area = append(area, fmt.Sprint(P.BidCodeMapping.Area[v]))
  147. }
  148. }
  149. if len(area) > 0 {
  150. regionArr = append(regionArr, fmt.Sprintf(" a.area in (%s) ", strings.Join(area, ",")))
  151. }
  152. //区域
  153. district := []string{}
  154. if req.District != "" {
  155. for _, v := range strings.Split(req.District, ",") {
  156. cityName := strings.Split(v, "_")[0]
  157. districtName := strings.Split(v, "_")[1]
  158. if P.BidCodeMapping.District[cityName][districtName] != "" {
  159. district = append(district, fmt.Sprint(P.BidCodeMapping.District[cityName][districtName]))
  160. }
  161. }
  162. }
  163. if len(district) > 0 {
  164. regionArr = append(regionArr, fmt.Sprintf(" a.district in (%s) ", strings.Join(district, ",")))
  165. }
  166. if len(regionArr) > 0 {
  167. querys = append(querys, fmt.Sprintf("(%s)", strings.Join(regionArr, "or")))
  168. }
  169. }
  170. if req.SubClass != "" {
  171. arr := []string{}
  172. for _, v := range strings.Split(req.SubClass, ",") {
  173. arr = append(arr, fmt.Sprintf(`"物业_%s"`, v))
  174. }
  175. querys = append(querys, fmt.Sprintf(" a.subclass in (%s) ", strings.Join(regionArr, ",")))
  176. }
  177. // 项目金额
  178. if req.Amount != "" && strings.Contains(req.Amount, "-") {
  179. minPriceStr, maxPriceStr := strings.Split(req.Amount, "-")[0], strings.Split(req.Amount, "-")[1]
  180. minPrice := common.Int64All(common.Float64All(minPriceStr) * 10000) //换成元
  181. maxPrice := common.Int64All(common.Float64All(maxPriceStr) * 10000) //换成元
  182. if minPriceStr != "" && maxPriceStr != "" {
  183. querys = append(querys, fmt.Sprintf("((a.project_money>=%d and a.project_money<=%d))", minPrice, maxPrice))
  184. } else if minPriceStr != "" {
  185. querys = append(querys, fmt.Sprintf("(a.project_money>=%d)", minPrice))
  186. } else if maxPriceStr != "" {
  187. querys = append(querys, fmt.Sprintf("(a.project_money<=%d)", maxPrice))
  188. }
  189. }
  190. //物业业态
  191. if req.PropertyForm != "" {
  192. arr := []string{}
  193. for _, v := range strings.Split(req.PropertyForm, ",") {
  194. arr = append(arr, fmt.Sprintf(`"%s"`, v))
  195. }
  196. querys = append(querys, fmt.Sprintf(" a.property_form in (%s) ", strings.Join(arr, ",")))
  197. }
  198. findSql = "select a.project_id, a.project_name, a.business_type, a.buyer, a.buyer_id, a.area, a.city, a.district, a.zbtime, a.endtime, a.project_money "
  199. if len(querys) > 0 {
  200. countSql = fmt.Sprintf("select count(1) from %s a where %s ", "information.transaction_info", strings.Join(querys, " and "))
  201. findSql = fmt.Sprintf("%s from %s a where %s order by zbtime", findSql, "information.transaction_info", strings.Join(querys, " and "))
  202. } else {
  203. countSql = fmt.Sprintf("select count(1) from %s a ", "information.transaction_info")
  204. findSql = fmt.Sprintf("%s from %s a order by zbtime", findSql, "information.transaction_info")
  205. }
  206. if isPage {
  207. findSql += fmt.Sprintf(" limit %d,%d", (req.PageNum-1)*req.PageSize, req.PageSize)
  208. }
  209. return
  210. }
  211. // @Author jianghan
  212. // @Description 过滤数据/补充销售机会状态信息,返回分页结果数据
  213. // @Date 2024/4/18
  214. func filterData(req *types.ProjectListReq, resultList []*ProjectEntry, preSales map[string]interface{}, isSqlPage bool) []*ProjectEntry {
  215. var newList []*ProjectEntry
  216. f := make(map[string]int, 3)
  217. if strings.Contains(req.SaleStatus, "1") {
  218. f["is_handle"] = 0
  219. } else if strings.Contains(req.SaleStatus, "2") {
  220. f["is_ignore"] = 1
  221. } else if strings.Contains(req.SaleStatus, "3") {
  222. f["is_create"] = 1
  223. }
  224. for _, m := range resultList {
  225. if m1, ok := preSales[m.ProjectId].(map[string]interface{}); ok {
  226. m.IsHandle = common.IntAll(m1["is_handle"])
  227. m.IsIgnore = common.IntAll(m1["is_ignore"])
  228. m.IsCreate = common.IntAll(m1["is_create"])
  229. }
  230. if !isSqlPage {
  231. for k, v := range f {
  232. if k == "is_handle" && m.IsHandle == v {
  233. newList = append(newList, m)
  234. break
  235. } else if k == "is_ignore" && m.IsIgnore == v {
  236. newList = append(newList, m)
  237. break
  238. } else if k == "is_create" && m.IsCreate == v {
  239. newList = append(newList, m)
  240. break
  241. }
  242. }
  243. }
  244. }
  245. if !isSqlPage {
  246. if newList == nil {
  247. resultList = make([]*ProjectEntry, 0)
  248. } else {
  249. resultList = newList
  250. }
  251. }
  252. return resultList
  253. }
  254. // @Author jianghan
  255. // @Description 补充人脉 等信息
  256. // @Date 2024/4/17
  257. func moreInfo(req *types.ProjectListReq, list []*ProjectEntry) (result []*ProjectEntry) {
  258. var buyerIds []string
  259. for _, m := range list {
  260. if m.BuyerId != "" {
  261. buyerIds = append(buyerIds, m.BuyerId)
  262. }
  263. }
  264. countMap := make(map[string]int)
  265. str1, arr1 := common.WhArgs(buyerIds)
  266. info1, err := T.ClickhouseConn.Query(context.TODO(), fmt.Sprintf(sql_1, str1), arr1...)
  267. if err == nil {
  268. for info1.Next() {
  269. var buyerId string
  270. var count uint64
  271. _ = info1.Scan(&buyerId, &count)
  272. countMap[buyerId] = int(count)
  273. }
  274. }
  275. info2 := T.CrmMysql.SelectBySql(fmt.Sprintf(sql_3, str1), arr1...)
  276. connMap := make(map[string]int)
  277. if info2 != nil && len(*info2) > 0 {
  278. for _, m := range *info2 {
  279. if req.PositionId == common.Int64All(m["position_id"]) {
  280. connMap[common.ObjToString(m["company_id"])] = 1 // 我的人脉
  281. } else {
  282. connMap[common.ObjToString(m["company_id"])] = 2
  283. }
  284. }
  285. }
  286. for _, m := range list {
  287. // 人脉、人脉所在单位项目 conn_type: 1 人脉可转介绍项目; conn_type: 2 人脉所在单位项目
  288. if connMap[m.BuyerId] == 1 {
  289. m.MyConn = true // 我的人脉
  290. m.ConnType = 1
  291. } else {
  292. m.MyConn = false
  293. }
  294. if m.ConnType == 0 {
  295. if connMap[m.BuyerId] != 0 {
  296. m.ConnType = 1
  297. } else {
  298. m.ConnType = 2
  299. }
  300. }
  301. // 转介绍成功率高标签
  302. if countMap[m.BuyerId] > 2 {
  303. m.HighSuccess = true
  304. } else {
  305. m.HighSuccess = false
  306. }
  307. }
  308. // 人脉路径
  309. var bArr []string
  310. for _, m := range list {
  311. // 有我的人脉标签时不需要查询人脉路径信息
  312. if m.MyConn == false && m.BuyerId != "" {
  313. bArr = append(bArr, fmt.Sprintf("'%s'", m.BuyerId))
  314. }
  315. }
  316. companyList := Findfirstparty(bArr, nil)
  317. if companyList != nil && len(companyList) > 0 {
  318. for _, m := range list {
  319. if m.MyConn == false {
  320. for _, m1 := range companyList {
  321. if m.BuyerId == common.ObjToString(m1["b_id"]) {
  322. m.BId = common.ObjToString(m1["b_id"])
  323. m.BName = common.ObjToString(m1["b_name"])
  324. m.RelationShip = common.ObjToString(m1["relationship"])
  325. m.SourceType = common.ObjToString(m1["sourceType"])
  326. m.Person = common.ObjToString(m1["person"])
  327. m.Num = common.ObjToString(m1["count"])
  328. break
  329. }
  330. }
  331. }
  332. }
  333. } else {
  334. companyList = Findwinner(bArr, nil)
  335. if companyList != nil && len(companyList) > 0 {
  336. for _, m := range list {
  337. if m.MyConn == false {
  338. for _, m1 := range companyList {
  339. if m.BuyerId == common.ObjToString(m1["b_id"]) {
  340. m.BId = common.ObjToString(m1["b_id"])
  341. m.BName = common.ObjToString(m1["b_name"])
  342. m.RelationShip = common.ObjToString(m1["relationship"])
  343. m.SourceType = common.ObjToString(m1["sourceType"])
  344. m.Person = common.ObjToString(m1["person"])
  345. m.Num = common.ObjToString(m1["count"])
  346. break
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. return list
  354. }