151 lines
4.6 KiB
Go
151 lines
4.6 KiB
Go
package service
|
|
|
|
import (
|
|
"electricity_bill_calc/cache"
|
|
"electricity_bill_calc/config"
|
|
"electricity_bill_calc/exceptions"
|
|
"electricity_bill_calc/global"
|
|
"electricity_bill_calc/model"
|
|
"fmt"
|
|
"strconv"
|
|
"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
|
|
}
|
|
cache.AbolishRelation("report")
|
|
cache.AbolishRelation(fmt.Sprintf("publicity_%s", reportId))
|
|
return true, nil
|
|
}
|
|
|
|
func (_WithdrawService) FetchPagedWithdrawApplies(page int, keyword string) ([]model.JoinedReportForWithdraw, int64, error) {
|
|
var conditions = make([]string, 0)
|
|
cond := builder.NewCond()
|
|
conditions = append(conditions, strconv.Itoa(int(model.REPORT_WITHDRAW_APPLIED)), strconv.Itoa(page))
|
|
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},
|
|
),
|
|
)
|
|
conditions = append(conditions, keyword)
|
|
}
|
|
var reports = make([]model.JoinedReportForWithdraw, 0)
|
|
var (
|
|
total int64
|
|
err error
|
|
)
|
|
if cachedTotal, _ := cache.RetreiveCount("join_report_for_withdraw", conditions...); cachedTotal != -1 {
|
|
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 {
|
|
return *cachedReports, total, err
|
|
}
|
|
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)
|
|
cache.CacheSearch(reports, []string{"report", "park"}, "join_report_for_withdraw", conditions...)
|
|
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)
|
|
cache.AbolishRelation("report")
|
|
return err
|
|
}
|
|
|
|
func (_WithdrawService) AuditWaits() (int64, error) {
|
|
if cachedWaits, _ := cache.RetreiveCount("withdraw_waits"); cachedWaits != -1 {
|
|
return cachedWaits, nil
|
|
}
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"withdraw": model.REPORT_WITHDRAW_APPLIED})
|
|
total, err := global.DBConn.
|
|
Table(new(model.JoinedReportForWithdraw)).
|
|
Where(cond).
|
|
Count()
|
|
if err == nil {
|
|
cache.CacheCount([]string{"report", "park"}, "withdraw_waits", total)
|
|
}
|
|
return total, err
|
|
}
|