forked from free-lancers/electricity_bill_calc_service
enhance(report):
This commit is contained in:
@@ -32,6 +32,35 @@ var CalculateRepository = _CalculateRepository{
|
||||
ds: goqu.Dialect("postgres"),
|
||||
}
|
||||
|
||||
//更新当前报表的核算状态
|
||||
func (cr _CalculateRepository) UpdateReportCalculateStatus(rid string, status string,
|
||||
message string) (bool, error) {
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
currentTime := time.Now()
|
||||
|
||||
updateResultSql, updateResultArgs, _ := cr.ds.
|
||||
Update(goqu.T("report_task")).
|
||||
Set(goqu.Record{
|
||||
"status": status,
|
||||
"last_modified_at": currentTime,
|
||||
"message": message,
|
||||
}).Where(goqu.I("id").Eq(rid)).
|
||||
ToSQL()
|
||||
|
||||
res, err := global.DB.Exec(ctx, updateResultSql, updateResultArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("未能更新当前报表的核算状态", zap.Error(err))
|
||||
return false, err
|
||||
}
|
||||
if res.RowsAffected() == 0 {
|
||||
cr.log.Warn("未能保存当前报表的核算状态", zap.String("Report", rid))
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// 获取当前正在等待计算的核算任务ID列表
|
||||
func (cr _CalculateRepository) ListPendingTasks() ([]string, error) {
|
||||
cr.log.Info("获取当前正在等待计算的核算任务ID列表")
|
||||
|
@@ -100,7 +100,7 @@ func (rr _ReportRepository) GetReportIndex(rid string) (*model.ReportIndex, erro
|
||||
}
|
||||
|
||||
// 为指园区创建一个新的核算报表
|
||||
func (rr _ReportRepository) CreateReport(form *vo.ReportCreationForm) (bool, error) {
|
||||
func (rr _ReportRepository) CreateReport(form *vo.ReportCreationForm) (bool, string, error) {
|
||||
rr.log.Info("为指定园区创建一个新的核算报表", zap.String("Park", form.Park))
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
@@ -108,13 +108,13 @@ func (rr _ReportRepository) CreateReport(form *vo.ReportCreationForm) (bool, err
|
||||
tx, err := global.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
rr.log.Error("未能开始一个数据库事务", zap.Error(err))
|
||||
return false, err
|
||||
return false, "", err
|
||||
}
|
||||
park, err := ParkRepository.RetrieveParkDetail(form.Park)
|
||||
if err != nil || park == nil {
|
||||
rr.log.Error("未能获取指定园区的详细信息", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return false, exceptions.NewNotFoundErrorFromError("未能获取指定园区的详细信息", err)
|
||||
return false, "", exceptions.NewNotFoundErrorFromError("未能获取指定园区的详细信息", err)
|
||||
}
|
||||
createTime := types.Now()
|
||||
periodRange := types.NewDateRange(&form.PeriodBegin, &form.PeriodEnd)
|
||||
@@ -176,42 +176,42 @@ func (rr _ReportRepository) CreateReport(form *vo.ReportCreationForm) (bool, err
|
||||
if err != nil {
|
||||
rr.log.Error("创建核算报表索引时出现错误", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return false, err
|
||||
return false, "", err
|
||||
}
|
||||
if resIndex.RowsAffected() == 0 {
|
||||
rr.log.Error("保存核算报表索引时出现错误", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return false, exceptions.NewUnsuccessCreateError("创建核算报表索引时出现错误")
|
||||
return false, "", exceptions.NewUnsuccessCreateError("创建核算报表索引时出现错误")
|
||||
}
|
||||
resSummary, err := tx.Exec(ctx, summarySql, summaryArgs...)
|
||||
if err != nil {
|
||||
rr.log.Error("创建核算报表汇总时出现错误", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return false, err
|
||||
return false, "", err
|
||||
}
|
||||
if resSummary.RowsAffected() == 0 {
|
||||
rr.log.Error("保存核算报表汇总时出现错误", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return false, exceptions.NewUnsuccessCreateError("创建核算报表汇总时出现错误")
|
||||
return false, "", exceptions.NewUnsuccessCreateError("创建核算报表汇总时出现错误")
|
||||
}
|
||||
resTask, err := tx.Exec(ctx, taskSql, taskArgs...)
|
||||
if err != nil {
|
||||
rr.log.Error("创建核算报表任务时出现错误", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return false, err
|
||||
return false, "", err
|
||||
}
|
||||
if resTask.RowsAffected() == 0 {
|
||||
rr.log.Error("保存核算报表任务时出现错误", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return false, exceptions.NewUnsuccessCreateError("创建核算报表任务时出现错误")
|
||||
return false, "", exceptions.NewUnsuccessCreateError("创建核算报表任务时出现错误")
|
||||
}
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
rr.log.Error("提交核算报表创建事务时出现错误", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return false, err
|
||||
return false, "", err
|
||||
}
|
||||
return resIndex.RowsAffected() > 0 && resSummary.RowsAffected() > 0 && resTask.RowsAffected() > 0, nil
|
||||
return resIndex.RowsAffected() > 0 && resSummary.RowsAffected() > 0 && resTask.RowsAffected() > 0, reportId, nil
|
||||
}
|
||||
|
||||
// 更新报表的基本信息
|
||||
|
Reference in New Issue
Block a user