91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"cid/model/entity"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
// statReportDao 统计报表DAO
|
|
type statReportDao struct{}
|
|
|
|
var StatReport = &statReportDao{}
|
|
|
|
// Ctx 获取数据库上下文
|
|
func (d *statReportDao) Ctx(ctx context.Context) *gdb.Model {
|
|
return g.DB().Model("stat_report").Ctx(ctx)
|
|
}
|
|
|
|
// Create 创建统计报表
|
|
func (d *statReportDao) Create(ctx context.Context, report *entity.StatReport) (int64, error) {
|
|
result, err := d.Ctx(ctx).Insert(report)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
id, _ := result.LastInsertId()
|
|
return id, nil
|
|
}
|
|
|
|
// GetByID 根据ID获取统计报表
|
|
func (d *statReportDao) GetByID(ctx context.Context, id int64) (*entity.StatReport, error) {
|
|
var report *entity.StatReport
|
|
err := d.Ctx(ctx).Where("id", id).Scan(&report)
|
|
return report, err
|
|
}
|
|
|
|
// GetByTenantAndDate 根据租户和日期获取统计报表
|
|
func (d *statReportDao) GetByTenantAndDate(ctx context.Context, tenantID int64, reportType, date string) (*entity.StatReport, error) {
|
|
var report *entity.StatReport
|
|
err := d.Ctx(ctx).Where("tenant_id", tenantID).Where("report_type", reportType).Where("report_date", date).Scan(&report)
|
|
return report, err
|
|
}
|
|
|
|
// Update 更新统计报表
|
|
func (d *statReportDao) Update(ctx context.Context, report *entity.StatReport) error {
|
|
_, err := d.Ctx(ctx).Where("id", report.Id).Update(report)
|
|
return err
|
|
}
|
|
|
|
// Delete 删除统计报表
|
|
func (d *statReportDao) Delete(ctx context.Context, id int64) error {
|
|
_, err := d.Ctx(ctx).Where("id", id).Delete()
|
|
return err
|
|
}
|
|
|
|
// List 统计报表列表
|
|
func (d *statReportDao) List(ctx context.Context, tenantID, appID int64, reportType, startDate, endDate string, page, pageSize int) ([]*entity.StatReport, int, error) {
|
|
model := d.Ctx(ctx)
|
|
|
|
if tenantID > 0 {
|
|
model = model.Where("tenant_id", tenantID)
|
|
}
|
|
if appID > 0 {
|
|
model = model.Where("app_id", appID)
|
|
}
|
|
if reportType != "" {
|
|
model = model.Where("report_type", reportType)
|
|
}
|
|
if startDate != "" {
|
|
model = model.WhereGTE("report_date", startDate)
|
|
}
|
|
if endDate != "" {
|
|
model = model.WhereLTE("report_date", endDate)
|
|
}
|
|
|
|
var reports []*entity.StatReport
|
|
err := model.Page(page, pageSize).OrderDesc("generated_at").Scan(&reports)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
total, err := model.Count()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return reports, total, nil
|
|
}
|