company_area_statistics.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package statistic_report
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. )
  5. type CompanyAreaGroup struct {
  6. Province string `description:"所属省份"`
  7. City string `description:"所属城市"`
  8. AdminName string `description:"所属销售名称"`
  9. Num int `description:"汇总次数"`
  10. CompanyIds string `description:"客户id字符串"`
  11. TryStage int `description:"试用客户标签:1未分类、2 推进、3 跟踪、4 预备"`
  12. }
  13. // GetGroupCompanyList 获取城市分组数据
  14. func GetGroupCompanyAreaList(condition string, pars []interface{}) (list []*CompanyAreaGroup, err error) {
  15. o := orm.NewOrm()
  16. sql := ` SELECT c.province,c.city,count(1) num, GROUP_CONCAT(DISTINCT c.company_id SEPARATOR ',') AS company_ids
  17. FROM company AS c
  18. INNER JOIN company_product AS p
  19. WHERE 1=1
  20. AND c.company_id = p.company_id
  21. AND c.enabled = 1
  22. `
  23. if condition != "" {
  24. sql += condition
  25. }
  26. sql += ` GROUP BY c.city `
  27. _, err = o.Raw(sql, pars).QueryRows(&list)
  28. return
  29. }
  30. // GetTryGroupCompanyAreaList 获取城市试用分组数据
  31. func GetTryGroupCompanyAreaList(productId int) (list []*CompanyAreaGroup, err error) {
  32. o := orm.NewOrm()
  33. sql := ` SELECT c.province,c.city,count(1) num,b.try_stage, GROUP_CONCAT(DISTINCT c.company_id SEPARATOR ',') AS company_ids
  34. FROM company AS c
  35. left JOIN (select * from company_product where product_id=? AND status = "试用") b on b.company_id= c.company_id
  36. WHERE 1=1
  37. AND c.enabled = 1 and b.status = "试用" and b.product_id = ? GROUP BY c.city, b.try_stage`
  38. _, err = o.Raw(sql, productId, productId).QueryRows(&list)
  39. return
  40. }
  41. // CompanyViewTotalAreaSlice 获取客户的浏览次数
  42. type CompanyViewTotalAreaSlice struct {
  43. CompanyIds string `description:"客户id字符串"`
  44. Num int `description:"用户浏览次数"`
  45. Province string `description:"所属省份"`
  46. City string `description:"所属城市"`
  47. }
  48. // GetCompanyViewTotalAreaList 获取客户的浏览数
  49. func GetCompanyViewTotalAreaList(condition string, pars []interface{}, viewNum int, sumNumType string) (items []*CompanyViewTotalAreaSlice, err error) {
  50. o := orm.NewOrm()
  51. sql := `SELECT
  52. count(
  53. DISTINCT ( company_id )) num,
  54. GROUP_CONCAT( DISTINCT company_id SEPARATOR ',' ) AS company_ids,
  55. city,
  56. province
  57. FROM
  58. (
  59. SELECT
  60. sum( a.view_num ) sum_num,
  61. a.company_id,
  62. c.city,
  63. c.province,
  64. b.product_id
  65. FROM
  66. company_view_statistics a
  67. JOIN company c ON a.company_id = c.company_id
  68. JOIN company_product b ON a.company_id = b.company_id
  69. WHERE
  70. 1 =1
  71. AND c.enabled = 1`
  72. if condition != "" {
  73. sql += condition
  74. }
  75. childSql := ` sum_num >= `
  76. if sumNumType == "<" {
  77. childSql = ` sum_num ` + sumNumType + ` `
  78. }
  79. sql += ` GROUP BY company_id) b where 1 = 1 and ` + childSql + `? GROUP BY city`
  80. _, err = o.Raw(sql, pars, viewNum).QueryRows(&items)
  81. return
  82. }
  83. func GetCompanyCountGroupByCity(condition string) (list []*CompanyAreaGroup, err error) {
  84. o := orm.NewOrm()
  85. sqlCount := ` SELECT
  86. c.province,
  87. c.city,
  88. count(
  89. DISTINCT ( c.company_id )) num,
  90. GROUP_CONCAT( DISTINCT c.company_id SEPARATOR ',' ) AS company_ids
  91. FROM company_product AS p
  92. INNER JOIN company AS c
  93. WHERE 1=1 AND p.status="试用" AND p.company_id = c.company_id" ` + condition
  94. if condition != "" {
  95. sqlCount += condition
  96. }
  97. sqlCount += ` GROUP BY c.city `
  98. _, err = o.Raw(sqlCount).QueryRows(&list)
  99. return
  100. }
  101. func GetAllCompanyArea() (list []*CompanyAreaGroup, err error) {
  102. o := orm.NewOrm()
  103. sql := ` SELECT province,city FROM company GROUP BY city `
  104. _, err = o.Raw(sql).QueryRows(&list)
  105. return
  106. }