All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m20s
- 新增 6 张表与成对迁移 000212,扩展企业微信审批场景业务类型白名单 - 后台线下套餐订单与两条代理线下充值入账路径在来源成功事务内建账,来源唯一键幂等 - 核销申请、审批尝试记录、账单分摊预占与驳回重提,审批业务类型 employee_collection_approval - 企业微信终态消费幂等:通过转已核销、驳回释放预占、通过后撤销不回滚并转异常终态 - 退款成功事务内按 bill_id+refund_id 幂等冲销账单或仅写退款关联提示 - 线下收款方式字典、账单查询/统计/关闭、申请查询与代办权限,均写入事务内审计 OpenSpec Change: add-employee-collection-bills
119 lines
4.2 KiB
Go
119 lines
4.2 KiB
Go
package employeecollection
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
|
|
employeecollectiondomain "github.com/break/junhong_cmp_fiber/internal/domain/employeecollection"
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
)
|
|
|
|
// BillCloseService 关闭员工代收款账单。
|
|
// 仅超级管理员可关闭,且只允许关闭仍待核销或部分核销、且不存在审批中分摊的账单。
|
|
type BillCloseService struct {
|
|
db *gorm.DB
|
|
audit BillAuditWriter
|
|
}
|
|
|
|
// NewBillCloseService 创建账单关闭事务脚本。
|
|
func NewBillCloseService(db *gorm.DB, audit BillAuditWriter) *BillCloseService {
|
|
return &BillCloseService{db: db, audit: audit}
|
|
}
|
|
|
|
// Close 关闭账单:作废未核销余额、保留已核销金额,并在同一事务内写关闭审计。
|
|
// 并发关闭通过行锁加 expected-status 条件更新兜底,状态已变化时返回冲突。
|
|
func (s *BillCloseService) Close(ctx context.Context, id uint, reason string) (*model.EmployeeCollectionBill, error) {
|
|
operatorID, err := requireSuperAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if s == nil || s.db == nil || s.audit == nil {
|
|
return nil, errors.New(errors.CodeServiceUnavailable, "账单关闭能力尚未配置")
|
|
}
|
|
if id == 0 {
|
|
return nil, errors.New(errors.CodeEmployeeCollectionBillNotFound)
|
|
}
|
|
closeReason := strings.TrimSpace(reason)
|
|
var closed *model.EmployeeCollectionBill
|
|
err = s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
|
bill, err := lockBill(ctx, tx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var pendingAllocations int64
|
|
if err := tx.WithContext(ctx).Model(&model.EmployeeCollectionApplicationAllocation{}).
|
|
Where("bill_id = ? AND status = ?", id, constants.EmployeeCollectionAllocationStatusPending).
|
|
Count(&pendingAllocations).Error; err != nil {
|
|
return errors.Wrap(errors.CodeDatabaseError, err, "统计账单审批中分摊失败")
|
|
}
|
|
if err := employeecollectiondomain.ValidateBillClose(employeecollectiondomain.BillCloseInput{
|
|
Status: bill.Status, PendingAllocations: pendingAllocations, Reason: closeReason,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
before := *bill
|
|
closedAt := time.Now().UTC()
|
|
result := tx.WithContext(ctx).Model(&model.EmployeeCollectionBill{}).
|
|
Where("id = ? AND status = ?", bill.ID, bill.Status).
|
|
Updates(map[string]any{
|
|
"status": constants.EmployeeCollectionBillStatusClosed,
|
|
"closed_reason": closeReason,
|
|
"closed_at": closedAt,
|
|
"updater": operatorID,
|
|
})
|
|
if result.Error != nil {
|
|
return errors.Wrap(errors.CodeDatabaseError, result.Error, "关闭员工代收款账单失败")
|
|
}
|
|
if result.RowsAffected != 1 {
|
|
return errors.New(errors.CodeConflict, "账单状态已变化,请刷新后重试")
|
|
}
|
|
bill.Status = constants.EmployeeCollectionBillStatusClosed
|
|
bill.ClosedReason = closeReason
|
|
bill.ClosedAt = &closedAt
|
|
bill.Updater = operatorID
|
|
closeEventID, err := composeAuditEventID("employee_collection", "bill", uintText(bill.ID), "close")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := s.audit.WriteEmployeeCollectionBill(ctx, tx, BillAudit{
|
|
EventID: closeEventID,
|
|
ActionCode: constants.AuditActionEmployeeCollectionBillClosed, Summary: "关闭员工代收款账单",
|
|
Bill: bill,
|
|
BeforeData: map[string]any{
|
|
"status": before.Status, "closed_reason": before.ClosedReason,
|
|
},
|
|
AfterData: map[string]any{
|
|
"status": bill.Status, "closed_reason": bill.ClosedReason,
|
|
},
|
|
CorrelationID: bill.SourceNo,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
closed = bill
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return closed, nil
|
|
}
|
|
|
|
// lockBill 以行锁读取账单,未找到返回稳定不存在错误。
|
|
func lockBill(ctx context.Context, tx *gorm.DB, id uint) (*model.EmployeeCollectionBill, error) {
|
|
var bill model.EmployeeCollectionBill
|
|
if err := tx.WithContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).
|
|
First(&bill, id).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.New(errors.CodeEmployeeCollectionBillNotFound)
|
|
}
|
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "锁定员工代收款账单失败")
|
|
}
|
|
return &bill, nil
|
|
}
|