plistService.go 12 KB

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