forked from free-lancers/electricity_bill_calc_service
feat(withdraw):完成公示撤回及审核相关的功能。
This commit is contained in:
111
service/withdraw.go
Normal file
111
service/withdraw.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/config"
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/model"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
type _WithdrawService struct{}
|
||||
|
||||
var WithdrawService _WithdrawService
|
||||
|
||||
func (_WithdrawService) ApplyWithdraw(reportId string) (bool, error) {
|
||||
var report = new(model.Report)
|
||||
has, err := global.DBConn.ID(reportId).Get(report)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !has {
|
||||
return false, exceptions.NewNotFoundError("指定报表未能找到")
|
||||
}
|
||||
if !report.Published {
|
||||
return false, exceptions.NewImproperOperateError("指定报表尚未发布。")
|
||||
}
|
||||
reports := make([]model.Report, 0)
|
||||
err = global.DBConn.
|
||||
Where(builder.Eq{"park_id": report.ParkId}).
|
||||
Find(&reports)
|
||||
if err != nil {
|
||||
return false, exceptions.NewNotFoundError("未能找到匹配的系列报表。")
|
||||
}
|
||||
maxPublished := lo.Reduce(
|
||||
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("申请撤回的报表必须是最新已发布的报表。")
|
||||
}
|
||||
|
||||
report.Withdraw = model.REPORT_WITHDRAW_APPLIED
|
||||
report.LastWithdrawAppliedAt = lo.ToPtr(time.Now())
|
||||
_, err = global.DBConn.ID(report.Id).Cols("withdraw", "last_withdraw_applied_at").Update(report)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]model.JoinedReportForWithdraw, int64, error) {
|
||||
cond := builder.NewCond()
|
||||
cond = cond.And(builder.Eq{"r.withdraw": model.REPORT_WITHDRAW_APPLIED})
|
||||
if len(keyword) > 0 {
|
||||
cond = cond.And(
|
||||
builder.Like{"p.name", keyword}.
|
||||
Or(
|
||||
builder.Like{"p.abbr", keyword},
|
||||
builder.Like{"u.name", keyword},
|
||||
builder.Like{"u.abbr", keyword},
|
||||
),
|
||||
)
|
||||
}
|
||||
var reports = make([]model.JoinedReportForWithdraw, 0)
|
||||
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
|
||||
}
|
||||
startItem := (page - 1) * config.ServiceSettings.ItemsPageSize
|
||||
err = global.DBConn.
|
||||
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).
|
||||
Limit(config.ServiceSettings.ItemsPageSize, startItem).
|
||||
Find(&reports)
|
||||
return reports, total, err
|
||||
}
|
||||
func (_WithdrawService) AuditWithdraw(reportId string, granted bool) error {
|
||||
var report = new(model.Report)
|
||||
has, err := global.DBConn.ID(reportId).NoAutoCondition().Get(report)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return exceptions.NewNotFoundError("指定公示报表未找到。")
|
||||
}
|
||||
report.Withdraw = lo.If(granted, model.REPORT_WITHDRAW_GRANTED).Else(model.REPORT_WITHDRAW_DENIED)
|
||||
report.LastWithdrawAuditAt = lo.ToPtr(time.Now())
|
||||
if granted {
|
||||
report.Published = false
|
||||
}
|
||||
_, err = global.DBConn.ID(report.Id).Cols("withdraw", "last_withdraw_audit_at", "published").Update(report)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user