stack_company_statistic.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. AscribeContent string `description:"归因标签说明"`
  94. IsShowNoRenewedNote bool `description:"是否展示未续约备注按钮"`
  95. Content string `description:"归因内容说明"`
  96. }
  97. func GetStackCompanyList(condition, orderBy string, pars []interface{}, startSize, pageSize int) (items []*StackCompanyStatisticList, err error) {
  98. o := orm.NewOrm()
  99. /*sql := `SELECT *,count(1) count from stack_company_statistic WHERE 1 = 1 `
  100. if condition != "" {
  101. sql += condition
  102. }
  103. sql += " group by company_id order by end_date asc LIMIT ?,? "*/
  104. //sql := `SELECT a.*,b.renewal_reason,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. 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
  108. left join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  109. join company c on a.company_id=c.company_id WHERE 1 = 1 `
  110. if condition != "" {
  111. sql += condition
  112. }
  113. if orderBy == "" {
  114. orderBy = ` end_date asc`
  115. }
  116. 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 ?,? "
  117. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  118. return
  119. }
  120. // GetStackCompanyListFromReportRecord 查找存量客户数据FromReportRecord
  121. func GetStackCompanyListFromReportRecord(condition, orderBy string, pars []interface{}, startSize, pageSize int) (items []*StackCompanyStatisticList, err error) {
  122. o := orm.NewOrm()
  123. 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
  124. left join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  125. join company c on a.company_id=c.company_id WHERE 1 = 1 `
  126. if condition != "" {
  127. sql += condition
  128. }
  129. if orderBy == "" {
  130. orderBy = ` end_date asc`
  131. }
  132. 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 ?,? "
  133. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  134. return
  135. }
  136. // 根据企业客户编号数据集、记录编号数据集获取不在里面的数据列表
  137. func GetStackCompanyListByCompanyIds(companyIds, statisticIds, condition string, pars []interface{}) (items []*StackCompanyStatisticList, err error) {
  138. o := orm.NewOrm()
  139. sql := `SELECT * from stack_company_statistic WHERE 1 = 1 AND company_id in (` + companyIds + `)
  140. AND statistic_id not in (` + statisticIds + `) `
  141. if condition != "" {
  142. sql += condition
  143. }
  144. sql += " order by end_date desc "
  145. _, err = o.Raw(sql, pars).QueryRows(&items)
  146. return
  147. }