forked from free-lancers/electricity_bill_calc_service
enhance(report):基本完成报表查询部分的综合服务部分。
This commit is contained in:
70
repository/calculate.go
Normal file
70
repository/calculate.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/types"
|
||||
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
|
||||
"github.com/georgysavva/scany/v2/pgxscan"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type _CalculateRepository struct {
|
||||
log *zap.Logger
|
||||
ds goqu.DialectWrapper
|
||||
}
|
||||
|
||||
var CalculateRepository = _CalculateRepository{
|
||||
log: logger.Named("Repository", "Calculate"),
|
||||
ds: goqu.Dialect("postgres"),
|
||||
}
|
||||
|
||||
// 获取当前正在等待计算的核算任务ID列表
|
||||
func (cr _CalculateRepository) ListPendingTasks() ([]string, error) {
|
||||
cr.log.Info("获取当前正在等待计算的核算任务ID列表")
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
var ids []string
|
||||
querySql, queryArgs, _ := cr.ds.
|
||||
From("report_task").
|
||||
Select("id").
|
||||
Where(goqu.C("status").Eq(model.REPORT_CALCULATE_TASK_STATUS_PENDING)).
|
||||
Prepared(true).ToSQL()
|
||||
if err := pgxscan.Select(ctx, global.DB, &ids, querySql, queryArgs...); err != nil {
|
||||
cr.log.Error("未能获取到当前正在等待计算的核算任务ID列表", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// 更新指定报表的核算状态
|
||||
func (cr _CalculateRepository) UpdateReportTaskStatus(rid string, status int16, message *string) (bool, error) {
|
||||
cr.log.Info("更新指定报表的核算状态", zap.String("Report", rid), zap.Int16("Status", status))
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
currentTime := types.Now()
|
||||
updateSql, updateArgs, _ := cr.ds.
|
||||
Update("report_task").
|
||||
Set(goqu.Record{
|
||||
"status": status,
|
||||
"last_modified_at": currentTime,
|
||||
"message": message,
|
||||
}).
|
||||
Where(goqu.C("id").Eq(rid)).
|
||||
Prepared(true).ToSQL()
|
||||
res, err := global.DB.Exec(ctx, updateSql, updateArgs...)
|
||||
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 res.RowsAffected() > 0, nil
|
||||
}
|
Reference in New Issue
Block a user