stack_company_statistic.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. ShareSeller string `description:"共享销售员"`
  81. GroupId int `description:"所属销售分组id"`
  82. DepartmentId int `description:"所属销售部门id"`
  83. Date string `description:"记录日期"`
  84. StartDate string `description:"服务起始时间"`
  85. EndDate string `description:"服务截止时间"`
  86. RegionType string `description:"所属区域,国内,海外"`
  87. CreateTime time.Time `description:"记录添加时间"`
  88. CreateTimeStr string `description:"记录添加时间,字符串形式"`
  89. ExpireDay string `description:"剩余可用天数"`
  90. Count int `json:"-"`
  91. RenewalReason string `description:"未续约说明"`
  92. RenewalTodo string `description:"未续约说明中的待办事项说明"`
  93. PackageDifference string `description:"和上一份合同的区别"`
  94. AscribeContent string `description:"归因标签说明"`
  95. IsShowNoRenewedNote bool `description:"是否展示未续约备注按钮"`
  96. Content string `description:"归因内容说明"`
  97. PermissionName string `description:"权限名"`
  98. Money float64 `description:"合同金额"`
  99. IsUserMaker int `description:"近四周之内是否包含决策人互动过 ,0否,1是"`
  100. }
  101. func GetStackCompanyList(condition, orderBy string, pars []interface{}, startSize, pageSize int) (items []*StackCompanyStatisticList, err error) {
  102. o := orm.NewOrm()
  103. /*sql := `SELECT *,count(1) count from stack_company_statistic WHERE 1 = 1 `
  104. if condition != "" {
  105. sql += condition
  106. }
  107. sql += " group by company_id order by end_date asc LIMIT ?,? "*/
  108. //sql := `SELECT a.*,b.renewal_reason,b.status from stack_company_statistic a
  109. //left join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  110. //join company c on a.company_id=c.company_id WHERE 1 = 1 `
  111. 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
  112. left join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  113. join company c on a.company_id=c.company_id WHERE 1 = 1 `
  114. if condition != "" {
  115. sql += condition
  116. }
  117. if orderBy == "" {
  118. orderBy = ` end_date asc`
  119. }
  120. 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 ?,? "
  121. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  122. return
  123. }
  124. // GetStackCompanyListFromReportRecord 查找存量客户数据FromReportRecord
  125. func GetStackCompanyListFromReportRecord(condition, orderBy string, pars []interface{}, startSize, pageSize int) (items []*StackCompanyStatisticList, err error) {
  126. o := orm.NewOrm()
  127. 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
  128. left join company_product b on a.company_id=b.company_id and a.product_id=b.product_id
  129. join company c on a.company_id=c.company_id WHERE 1 = 1 `
  130. if condition != "" {
  131. sql += condition
  132. }
  133. if orderBy == "" {
  134. orderBy = ` end_date asc`
  135. }
  136. 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 ?,? "
  137. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  138. return
  139. }
  140. // 根据企业客户编号数据集、记录编号数据集获取不在里面的数据列表
  141. func GetStackCompanyListByCompanyIds(companyIds, statisticIds, condition string, pars []interface{}) (items []*StackCompanyStatisticList, err error) {
  142. o := orm.NewOrm()
  143. sql := `SELECT * from stack_company_statistic WHERE 1 = 1 AND company_id in (` + companyIds + `)
  144. AND statistic_id not in (` + statisticIds + `) `
  145. if condition != "" {
  146. sql += condition
  147. }
  148. sql += " order by end_date desc "
  149. _, err = o.Raw(sql, pars).QueryRows(&items)
  150. return
  151. }