company_report_permission.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_task/utils"
  5. "time"
  6. )
  7. type CompanyReportPermission struct {
  8. CompanyReportPermissionId int64 `orm:"column(company_report_permission_id);pk"`
  9. CompanyId int `description:"公司id"`
  10. ProductId int `description:"产品id"`
  11. ProductName string `description:"产品名称"`
  12. ReportPermissionId int `description:"权限id(作废)"`
  13. Status string `description:"状态"`
  14. CreatedTime time.Time `description:"创建时间"`
  15. LastUpdatedTime time.Time `description:"更新时间"`
  16. ChartPermissionId int `description:"大分类ID"`
  17. StartDate string `description:"合同开始日期"`
  18. EndDate string `description:"合同结束日期"`
  19. ModifyTime string `description:"更新时间"`
  20. IsUpgrade int `description:"是否升级,1是,0否"`
  21. ExpensiveYx int `description:"权益研选: 0-3w; 1-5w"`
  22. }
  23. // 客户授权产品结构体(包含产品名称)
  24. type CompanyReportPermissionAndName struct {
  25. CompanyReportPermissionId int `description:"客户授权产品id"`
  26. CompanyId int
  27. ReportPermissionId int
  28. CreatedTime time.Time
  29. LastUpdatedTime time.Time
  30. ChartPermissionId int
  31. StartDate string `description:"权限开始日期"`
  32. EndDate string `description:"权限结束日期"`
  33. ProductId int `description:"产品id"`
  34. ProductName string `description:"产品名称"`
  35. CompanyContractId int `description:"合同id"`
  36. PermissionName string `description:"客户授权产品的名称"`
  37. ClassifyName string `description:"客户授权产品的分类名称"`
  38. Status string `description:"'正式','试用','关闭'"`
  39. ModifyTime time.Time `description:"修改时间"`
  40. }
  41. // Update 更新客户产品权限
  42. func (item *CompanyReportPermission) Update(cols []string) (err error) {
  43. o := orm.NewOrm()
  44. _, err = o.Update(item, cols...)
  45. return
  46. }
  47. func GetCompanyReportPermission(companyId int) (items []*CompanyReportPermission, err error) {
  48. sql := `SELECT * FROM company_report_permission WHERE company_id=? `
  49. _, err = orm.NewOrm().Raw(sql, companyId).QueryRows(&items)
  50. return
  51. }
  52. func AddCompanyReportPermission(item *CompanyReportPermission) (err error) {
  53. o := orm.NewOrm()
  54. _, err = o.Insert(item)
  55. return
  56. }
  57. func ModifyCompanyOldPermission(startDate, endDate, productName string, companyId, productId int) (err error) {
  58. o := orm.NewOrm()
  59. sql := `UPDATE company_report_permission SET start_date=?,end_date=?,product_id=?,product_name=?,modify_time=NOW() WHERE company_id=? AND start_date IS NULL `
  60. _, err = o.Raw(sql, startDate, endDate, productId, productName, companyId).Exec()
  61. return
  62. }
  63. // GetCompanyReportPermissionNeedTryOut 获取需要正式转关闭的产品权益
  64. func GetCompanyReportPermissionNeedTryOut(endDate string) (items []*CompanyReportPermission, err error) {
  65. o := orm.NewOrm()
  66. sql := `SELECT *
  67. FROM company_report_permission
  68. WHERE status='正式' AND end_date<=?`
  69. _, err = o.Raw(sql, endDate).QueryRows(&items)
  70. return
  71. }
  72. // GetCompanyReportPermissionNeedClose 获取需要关闭的产品权限
  73. func GetCompanyReportPermissionNeedClose(endDate string) (items []*CompanyReportPermission, err error) {
  74. o := orm.NewOrm()
  75. sql := `SELECT *
  76. FROM company_report_permission
  77. WHERE status='试用' AND end_date<=?`
  78. _, err = o.Raw(sql, endDate).QueryRows(&items)
  79. return
  80. }
  81. // CompanyReportPermissionTryOut 正式转试用用户权限
  82. func CompanyReportPermissionTryOut(companyReportPermissionId int64, companyId, productId int) (err error) {
  83. startDate := time.Now().Format(utils.FormatDate)
  84. endDate := time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  85. o := orm.NewOrm()
  86. //修改产品权限状态为关闭
  87. //sql := `UPDATE company_report_permission SET status='试用',modify_time=NOW() WHERE company_report_permission_id = ? AND company_id= ? AND product_id = ? `
  88. sql := `UPDATE company_report_permission SET status='试用',start_date=?,end_date=?,modify_time=NOW() WHERE company_report_permission_id = ? AND company_id= ? AND product_id = ? `
  89. _, err = o.Raw(sql, startDate, endDate, companyReportPermissionId, companyId, productId).Exec()
  90. //_, err = o.Raw(sql, companyReportPermissionId, companyId, productId).Exec()
  91. if err != nil {
  92. return err
  93. }
  94. return
  95. }
  96. // CompanyReportPermissionClose 关闭用户权限
  97. func CompanyReportPermissionClose(companyReportPermissionId int64, companyId, productId int) (err error) {
  98. o := orm.NewOrm()
  99. //修改产品权限状态为关闭
  100. sql := `UPDATE company_report_permission SET status='关闭',modify_time=NOW() WHERE company_report_permission_id = ? AND company_id= ? AND product_id = ? `
  101. _, err = o.Raw(sql, companyReportPermissionId, companyId, productId).Exec()
  102. if err != nil {
  103. return err
  104. }
  105. return
  106. }
  107. // 查询是否存在还未过期的产品权限
  108. func GetCompanyReportPermissionCount(companyId, productId int) (count int, err error) {
  109. o := orm.NewOrm()
  110. startDate := time.Now().Format(utils.FormatDate)
  111. countSql := `SELECT
  112. COUNT(company_report_permission_id ) AS COUNT
  113. FROM company_report_permission WHERE status in ('试用','正式') AND end_date >= ? AND company_id= ? AND product_id = ? `
  114. err = o.Raw(countSql, startDate, companyId, productId).QueryRow(&count)
  115. return
  116. }
  117. func GetCompanyReportPermissionByStatus(companyId, productId int, status string) (items []*CompanyReportPermission, err error) {
  118. o := orm.NewOrm()
  119. sql := `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? AND status = ? `
  120. _, err = o.Raw(sql, companyId, productId, status).QueryRows(&items)
  121. return
  122. }
  123. // 根据企业用户id和产品id获取所有正式的权限
  124. func GetCompanyProductReportPermissionList(companyId []int, productId int) (items []*CompanyReportPermissionAndName, err error) {
  125. o := orm.NewOrm()
  126. sql := `SELECT a.*,b.permission_name,b.classify_name FROM company_report_permission a left join chart_permission b on a.chart_permission_id=b.chart_permission_id WHERE a.company_id IN (` + utils.GetOrmInReplace(len(companyId)) + `) and a.product_id=? AND a.status IN ('正式','试用','永续') `
  127. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  128. return
  129. }