refactor(withdraw):报表撤回系列功能基本完成迁移。
This commit is contained in:
parent
d778c4d3f3
commit
e250ef6792
|
@ -5,131 +5,143 @@ import (
|
||||||
"electricity_bill_calc/config"
|
"electricity_bill_calc/config"
|
||||||
"electricity_bill_calc/exceptions"
|
"electricity_bill_calc/exceptions"
|
||||||
"electricity_bill_calc/global"
|
"electricity_bill_calc/global"
|
||||||
|
"electricity_bill_calc/logger"
|
||||||
"electricity_bill_calc/model"
|
"electricity_bill_calc/model"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
"xorm.io/builder"
|
"github.com/uptrace/bun"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type _WithdrawService struct{}
|
type _WithdrawService struct {
|
||||||
|
l *zap.Logger
|
||||||
|
}
|
||||||
|
|
||||||
var WithdrawService _WithdrawService
|
var WithdrawService = _WithdrawService{
|
||||||
|
l: logger.Named("Service", "Withdraw"),
|
||||||
|
}
|
||||||
|
|
||||||
func (_WithdrawService) ApplyWithdraw(reportId string) (bool, error) {
|
func (_WithdrawService) ApplyWithdraw(reportId string) (bool, error) {
|
||||||
var report = new(model.Report)
|
ctx, cancel := global.TimeoutContext()
|
||||||
has, err := global.DBConn.ID(reportId).Get(report)
|
defer cancel()
|
||||||
if err != nil {
|
|
||||||
return false, err
|
var report *model.Report
|
||||||
}
|
err := global.DB.NewSelect().Model(report).
|
||||||
if !has {
|
Where("id = ?", reportId).
|
||||||
return false, exceptions.NewNotFoundError("指定报表未能找到")
|
Scan(ctx)
|
||||||
|
if err != nil || report == nil {
|
||||||
|
return false, exceptions.NewNotFoundErrorFromError("指定报表未能找到", err)
|
||||||
}
|
}
|
||||||
if !report.Published {
|
if !report.Published {
|
||||||
return false, exceptions.NewImproperOperateError("指定报表尚未发布。")
|
return false, exceptions.NewImproperOperateError("指定报表尚未发布。")
|
||||||
}
|
}
|
||||||
reports := make([]model.Report, 0)
|
var maxPublished *time.Time
|
||||||
err = global.DBConn.
|
err = global.DB.NewSelect().Model((*model.Report)(nil)).
|
||||||
Where(builder.Eq{"park_id": report.ParkId}).
|
ColumnExpr("max(period)").
|
||||||
Find(&reports)
|
Where("park_id = ?", report.ParkId).
|
||||||
|
Where("published = ?", true).
|
||||||
|
Scan(ctx, maxPublished)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, exceptions.NewNotFoundError("未能找到匹配的系列报表。")
|
return false, exceptions.NewNotFoundError("未能找到匹配的系列报表。")
|
||||||
}
|
}
|
||||||
maxPublished := lo.Reduce(
|
if maxPublished != nil && !report.Period.Equal(*maxPublished) {
|
||||||
reports,
|
|
||||||
func(acc *time.Time, elem model.Report, index int) *time.Time {
|
|
||||||
if elem.Published {
|
|
||||||
if acc == nil || (acc != nil && elem.Period.After(*acc)) {
|
|
||||||
return &elem.Period
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return acc
|
|
||||||
},
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
if !report.Period.Equal(*maxPublished) {
|
|
||||||
return false, exceptions.NewImproperOperateError("申请撤回的报表必须是最新已发布的报表。")
|
return false, exceptions.NewImproperOperateError("申请撤回的报表必须是最新已发布的报表。")
|
||||||
}
|
}
|
||||||
|
|
||||||
report.Withdraw = model.REPORT_WITHDRAW_APPLIED
|
report.Withdraw = model.REPORT_WITHDRAW_APPLIED
|
||||||
report.LastWithdrawAppliedAt = lo.ToPtr(time.Now())
|
report.LastWithdrawAppliedAt = lo.ToPtr(time.Now())
|
||||||
_, err = global.DBConn.ID(report.Id).Cols("withdraw", "last_withdraw_applied_at").Update(report)
|
_, err = global.DB.NewUpdate().Model(report).
|
||||||
|
WherePK().
|
||||||
|
Column("withdraw", "last_withdraw_applied_at").
|
||||||
|
Exec(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
cache.AbolishRelation("report")
|
cache.AbolishRelation("withdraw_stat")
|
||||||
cache.AbolishRelation(fmt.Sprintf("publicity_%s", reportId))
|
cache.AbolishRelation(fmt.Sprintf("report:%s", reportId))
|
||||||
|
cache.AbolishRelation(fmt.Sprintf("publicity:%s", reportId))
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]model.JoinedReportForWithdraw, int64, error) {
|
func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]model.JoinedReportForWithdraw, int64, error) {
|
||||||
var conditions = make([]string, 0)
|
var (
|
||||||
cond := builder.NewCond()
|
conditions = make([]string, 0)
|
||||||
conditions = append(conditions, strconv.Itoa(int(model.REPORT_WITHDRAW_APPLIED)), strconv.Itoa(page))
|
reports = make([]model.Report, 0)
|
||||||
cond = cond.And(builder.Eq{"r.withdraw": model.REPORT_WITHDRAW_APPLIED})
|
cond = global.DB.NewSelect().Model(&reports).
|
||||||
if len(keyword) > 0 {
|
Relation("Park", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||||
cond = cond.And(
|
return q.Relation("Enterprise")
|
||||||
builder.Like{"p.name", keyword}.
|
})
|
||||||
Or(
|
|
||||||
builder.Like{"p.abbr", keyword},
|
|
||||||
builder.Like{"u.name", keyword},
|
|
||||||
builder.Like{"u.abbr", keyword},
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
conditions = append(conditions, strconv.Itoa(int(model.REPORT_WITHDRAW_APPLIED)), strconv.Itoa(page))
|
||||||
|
cond = cond.Where("r.withdraw = ?", model.REPORT_WITHDRAW_APPLIED)
|
||||||
|
if len(keyword) > 0 {
|
||||||
|
keywordCond := "%" + keyword + "%"
|
||||||
|
cond = cond.WhereGroup(" and ", func(q *bun.SelectQuery) *bun.SelectQuery {
|
||||||
|
return q.Where("p.name like ?", keywordCond).
|
||||||
|
WhereOr("p.abbr like ?", keywordCond).
|
||||||
|
WhereOr("d.name like ?", keywordCond).
|
||||||
|
WhereOr("d.abbr like ?", keywordCond)
|
||||||
|
})
|
||||||
conditions = append(conditions, keyword)
|
conditions = append(conditions, keyword)
|
||||||
}
|
}
|
||||||
var reports = make([]model.JoinedReportForWithdraw, 0)
|
|
||||||
var (
|
|
||||||
total int64
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
if cachedTotal, err := cache.RetreiveCount("join_report_for_withdraw", conditions...); cachedTotal != -1 && err == nil {
|
if cachedTotal, err := cache.RetreiveCount("join_report_for_withdraw", conditions...); cachedTotal != -1 && err == nil {
|
||||||
total = cachedTotal
|
|
||||||
} else {
|
|
||||||
total, err = global.DBConn.
|
|
||||||
Table(new(model.JoinedReportForWithdraw)).Alias("r").
|
|
||||||
Join("INNER", []string{"park", "p"}, "r.park_id=p.id").
|
|
||||||
Join("INNER", []string{"user_detail", "u"}, "p.user_id=u.id").
|
|
||||||
Where(cond).
|
|
||||||
Count()
|
|
||||||
if err != nil {
|
|
||||||
return nil, -1, err
|
|
||||||
}
|
|
||||||
cache.CacheCount([]string{"report", "park"}, "join_report_for_withdraw", total, conditions...)
|
|
||||||
}
|
|
||||||
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
|
|
||||||
if cachedReports, _ := cache.RetreiveSearch[[]model.JoinedReportForWithdraw]("join_user_detail", conditions...); cachedReports != nil {
|
if cachedReports, _ := cache.RetreiveSearch[[]model.JoinedReportForWithdraw]("join_user_detail", conditions...); cachedReports != nil {
|
||||||
return *cachedReports, total, err
|
return *cachedReports, cachedTotal, err
|
||||||
}
|
}
|
||||||
err = global.DBConn.
|
}
|
||||||
Alias("r").
|
|
||||||
Join("INNER", []string{"park", "p"}, "r.park_id=p.id").
|
ctx, cancel := global.TimeoutContext()
|
||||||
Join("INNER", []string{"user_detail", "u"}, "p.user_id=u.id").
|
defer cancel()
|
||||||
Where(cond).
|
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
|
||||||
Limit(config.ServiceSettings.ItemsPageSize, startItem).
|
total, err := cond.Limit(config.ServiceSettings.ItemsPageSize).
|
||||||
Find(&reports)
|
Offset(startItem).
|
||||||
cache.CacheSearch(reports, []string{"report", "park"}, "join_report_for_withdraw", conditions...)
|
ScanAndCount(ctx)
|
||||||
return reports, total, err
|
|
||||||
|
var (
|
||||||
|
joinedReports = make([]model.JoinedReportForWithdraw, 0)
|
||||||
|
relations = []string{"report", "park"}
|
||||||
|
)
|
||||||
|
for _, r := range reports {
|
||||||
|
joinedReports = append(joinedReports, model.JoinedReportForWithdraw{
|
||||||
|
Report: r,
|
||||||
|
Park: model.FromPark(*r.Park),
|
||||||
|
User: model.FromUserDetail(*r.Park.Enterprise),
|
||||||
|
})
|
||||||
|
relations = append(relations, fmt.Sprintf("report:%s", r.Id), fmt.Sprintf("publicity:%s", r.Id))
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.CacheCount(relations, "join_report_for_withdraw", int64(total), conditions...)
|
||||||
|
cache.CacheSearch(joinedReports, relations, "join_report_for_withdraw", conditions...)
|
||||||
|
return joinedReports, int64(total), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_WithdrawService) AuditWithdraw(reportId string, granted bool) error {
|
func (_WithdrawService) AuditWithdraw(reportId string, granted bool) error {
|
||||||
|
ctx, cancel := global.TimeoutContext()
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
var report = new(model.Report)
|
var report = new(model.Report)
|
||||||
has, err := global.DBConn.ID(reportId).NoAutoCondition().Get(report)
|
err := global.DB.NewSelect().Model(report).
|
||||||
|
Where("id = ?", reportId).
|
||||||
|
Scan(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return exceptions.NewNotFoundErrorFromError("指定公示报表未找到。", err)
|
||||||
}
|
|
||||||
if !has {
|
|
||||||
return exceptions.NewNotFoundError("指定公示报表未找到。")
|
|
||||||
}
|
}
|
||||||
report.Withdraw = lo.If(granted, model.REPORT_WITHDRAW_GRANTED).Else(model.REPORT_WITHDRAW_DENIED)
|
report.Withdraw = lo.If(granted, model.REPORT_WITHDRAW_GRANTED).Else(model.REPORT_WITHDRAW_DENIED)
|
||||||
report.LastWithdrawAuditAt = lo.ToPtr(time.Now())
|
report.LastWithdrawAuditAt = lo.ToPtr(time.Now())
|
||||||
if granted {
|
if granted {
|
||||||
report.Published = false
|
report.Published = false
|
||||||
}
|
}
|
||||||
_, err = global.DBConn.ID(report.Id).Cols("withdraw", "last_withdraw_audit_at", "published").Update(report)
|
_, err = global.DB.NewUpdate().Model(report).
|
||||||
cache.AbolishRelation("report")
|
WherePK().
|
||||||
|
Column("withdraw", "last_withdraw_audit_at", "published").
|
||||||
|
Exec(ctx)
|
||||||
|
if err == nil {
|
||||||
|
cache.AbolishRelation("withdraw_stat")
|
||||||
|
cache.AbolishRelation(fmt.Sprintf("report:%s", reportId))
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,14 +149,14 @@ func (_WithdrawService) AuditWaits() (int64, error) {
|
||||||
if cachedWaits, err := cache.RetreiveCount("withdraw_waits"); cachedWaits != -1 && err == nil {
|
if cachedWaits, err := cache.RetreiveCount("withdraw_waits"); cachedWaits != -1 && err == nil {
|
||||||
return cachedWaits, nil
|
return cachedWaits, nil
|
||||||
}
|
}
|
||||||
cond := builder.NewCond()
|
ctx, cancel := global.TimeoutContext()
|
||||||
cond = cond.And(builder.Eq{"withdraw": model.REPORT_WITHDRAW_APPLIED})
|
defer cancel()
|
||||||
total, err := global.DBConn.
|
|
||||||
Table(new(model.JoinedReportForWithdraw)).
|
total, err := global.DB.NewSelect().Model((*model.Report)(nil)).
|
||||||
Where(cond).
|
Where("withdraw = ?", model.REPORT_WITHDRAW_APPLIED).
|
||||||
Count()
|
Count(ctx)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cache.CacheCount([]string{"report", "park"}, "withdraw_waits", total)
|
cache.CacheCount([]string{"withdraw_stat"}, "withdraw_waits", int64(total))
|
||||||
}
|
}
|
||||||
return total, err
|
return int64(total), err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user