stack_company_statistic.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. //存量客户数据表
  7. type StackCompanyStatistic struct {
  8. StatisticId int `orm:"column(statistic_id);pk"`
  9. Type string `description:"数据类型,取值范围:新签客户,续约客户,未续约客户"`
  10. CompanyId int `description:"企业客户id"`
  11. CompanyName string `description:"企业客户名称"`
  12. ProductId int `description:"客户产品id"`
  13. ProductName string `description:"客户产品名称"`
  14. ContractNum int `description:"第几份合同,默认是:1"`
  15. SellerId int `description:"所属销售id"`
  16. SellerName string `description:"所属销售名称"`
  17. GroupId int `description:"所属销售分组id"`
  18. DepartmentId int `description:"所属销售部门id"`
  19. Date string `description:"记录日期"`
  20. StartDate string `description:"服务起始时间"`
  21. EndDate string `description:"服务截止时间"`
  22. RegionType string `description:"所属区域,国内,海外"`
  23. CreateTime time.Time `description:"记录添加时间"`
  24. }
  25. //获取客户数量
  26. func GetStackCompanyCount(condition string, pars []interface{}) (count int, err error) {
  27. o := orm.NewOrm()
  28. //o.Using("rddp")
  29. //产品权限
  30. sql := `SELECT count(1) count from stack_company_statistic a
  31. join company c on a.company_id=c.company_id WHERE 1 = 1 `
  32. if condition != "" {
  33. sql += condition
  34. }
  35. sql = `SELECT count(1) count from ( ` + sql + ` group by a.company_id) b `
  36. err = o.Raw(sql, pars).QueryRow(&count)
  37. return
  38. }
  39. //获取客户产品数量
  40. func GetStackCompanyProductCount(condition string, pars []interface{}) (count int, err error) {
  41. o := orm.NewOrm()
  42. //o.Using("rddp")
  43. //产品权限
  44. sql := `SELECT statistic_id from stack_company_statistic a
  45. join company c on a.company_id=c.company_id WHERE 1 = 1 `
  46. if condition != "" {
  47. sql += condition
  48. }
  49. sql = `SELECT count(1) count from ( ` + sql + ` group by a.company_id,a.product_id) b `
  50. err = o.Raw(sql, pars).QueryRow(&count)
  51. return
  52. }
  53. //GetNotRenewalStackCompanyProductCount 获取未续约客户产品数量
  54. func GetNotRenewalStackCompanyProductCount(condition string, pars []interface{}) (count int, err error) {
  55. o := orm.NewOrm()
  56. //o.Using("rddp")
  57. //产品权限
  58. sql := `SELECT statistic_id from stack_company_statistic a
  59. left join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  60. join company c on a.company_id=c.company_id WHERE 1 = 1 `
  61. if condition != "" {
  62. sql += condition
  63. }
  64. sql = `SELECT count(1) count from ( ` + sql + ` group by a.company_id,a.product_id) b `
  65. err = o.Raw(sql, pars).QueryRow(&count)
  66. return
  67. }
  68. //获取收入统计报表列表数据(根据合同来展示)
  69. type StackCompanyStatisticList struct {
  70. StatisticId int `orm:"column(statistic_id);pk"`
  71. Type string `description:"数据类型,取值范围:新签客户,续约客户,未续约客户"`
  72. Status string `description:"当前状态"`
  73. CompanyId int `description:"企业客户id"`
  74. CompanyName string `description:"企业客户名称"`
  75. ProductId int `description:"客户产品id"`
  76. ProductName string `description:"客户产品名称"`
  77. ContractNum int `description:"第几份合同,默认是:1"`
  78. SellerId int `description:"所属销售id"`
  79. SellerName string `description:"所属销售名称"`
  80. GroupId int `description:"所属销售分组id"`
  81. DepartmentId int `description:"所属销售部门id"`
  82. Date string `description:"记录日期"`
  83. StartDate string `description:"服务起始时间"`
  84. EndDate string `description:"服务截止时间"`
  85. RegionType string `description:"所属区域,国内,海外"`
  86. CreateTime time.Time `description:"记录添加时间"`
  87. CreateTimeStr string `description:"记录添加时间,字符串形式"`
  88. ExpireDay string `description:"剩余可用天数"`
  89. Count int `json:"-"`
  90. RenewalReason string `description:"未续约说明"`
  91. RenewalTodo string `description:"未续约说明中的待办事项说明"`
  92. PackageDifference string `description:"和上一份合同的区别"`
  93. }
  94. func GetStackCompanyList(condition, orderBy string, pars []interface{}, startSize, pageSize int) (items []*StackCompanyStatisticList, err error) {
  95. o := orm.NewOrm()
  96. /*sql := `SELECT *,count(1) count from stack_company_statistic WHERE 1 = 1 `
  97. if condition != "" {
  98. sql += condition
  99. }
  100. sql += " group by company_id order by end_date asc LIMIT ?,? "*/
  101. //sql := `SELECT a.*,b.renewal_reason,b.status from stack_company_statistic a
  102. //left join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  103. //join company c on a.company_id=c.company_id WHERE 1 = 1 `
  104. sql := `SELECT a.statistic_id,a.type,a.company_id,c.company_name,a.product_id,a.product_name,a.contract_id,a.contract_num,a.seller_id,a.seller_name,a.group_id,a.department_id,a.date,a.start_date,a.end_date,a.region_type,a.create_time,b.renewal_reason,b.renewal_todo,b.status from stack_company_statistic a
  105. left join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  106. join company c on a.company_id=c.company_id WHERE 1 = 1 `
  107. if condition != "" {
  108. sql += condition
  109. }
  110. if orderBy == "" {
  111. orderBy = ` end_date asc`
  112. }
  113. sql = "select *,count(1) count from (" + sql + " order by a.end_date asc) c group by company_id,product_id order by " + orderBy + ",company_id desc LIMIT ?,? "
  114. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  115. return
  116. }
  117. //GetStackCompanyListFromReportRecord 查找存量客户数据FromReportRecord
  118. func GetStackCompanyListFromReportRecord(condition, orderBy string, pars []interface{}, startSize, pageSize int) (items []*StackCompanyStatisticList, err error) {
  119. o := orm.NewOrm()
  120. sql := `SELECT a.company_report_record_id,a.status,a.company_id,c.company_name,a.product_id,a.product_name,a.contract_id,a.contract_num,a.seller_id,a.seller_name,a.group_id,a.department_id,a.date,a.start_date,a.end_date,a.region_type,a.create_time,b.renewal_reason,b.status from company_report_record a
  121. left join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  122. join company c on a.company_id=c.company_id WHERE 1 = 1 `
  123. if condition != "" {
  124. sql += condition
  125. }
  126. if orderBy == "" {
  127. orderBy = ` end_date asc`
  128. }
  129. sql = "select *,count(1) count from (" + sql + " order by a.end_date asc) c group by company_id,product_id order by " + orderBy + ",company_id desc LIMIT ?,? "
  130. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  131. return
  132. }
  133. //根据企业客户编号数据集、记录编号数据集获取不在里面的数据列表
  134. func GetStackCompanyListByCompanyIds(companyIds, statisticIds, condition string, pars []interface{}) (items []*StackCompanyStatisticList, err error) {
  135. o := orm.NewOrm()
  136. sql := `SELECT * from stack_company_statistic WHERE 1 = 1 AND company_id in (` + companyIds + `)
  137. AND statistic_id not in (` + statisticIds + `) `
  138. if condition != "" {
  139. sql += condition
  140. }
  141. sql += " order by end_date desc "
  142. _, err = o.Raw(sql, pars).QueryRows(&items)
  143. return
  144. }