- 新增 CommunityFund / FundLog 模型(公户余额单行表 + 6种流水类型) - 新增 FundRepo 仓储(GetBalance / IncrBalance / CreateLog / ListLogs / WithTx) - 改造 EnergyService 五个方法:Energize 税收入公户、DeductOnRename/DeductOnDeletePost 入公户、RefundOnRenameReject 从公户支出、AdminAdjust 拆分为 admin_transfer/system_operation - 新增 ErrInsufficientFund 错误哨兵 - 管理后台新增公户余额展示 + 公户流水页面 + 域能调整资金来源选择 - 新增 energy.css / energy.js 前端交互 - 侧边栏新增公户流水入口
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"metazone.cc/metalab/internal/model"
|
|
"metazone.cc/metalab/internal/service"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// FundRepo 公户数据访问
|
|
type FundRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewFundRepo 构造函数
|
|
func NewFundRepo(db *gorm.DB) *FundRepo {
|
|
return &FundRepo{db: db}
|
|
}
|
|
|
|
// WithTx 基于给定事务连接创建新的 FundRepo
|
|
func (r *FundRepo) WithTx(tx *gorm.DB) service.FundStore {
|
|
return &FundRepo{db: tx}
|
|
}
|
|
|
|
// GetBalance 查询公户余额
|
|
func (r *FundRepo) GetBalance() (int, error) {
|
|
var fund model.CommunityFund
|
|
err := r.db.First(&fund, 1).Error
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return fund.Balance, nil
|
|
}
|
|
|
|
// IncrBalance 原子增减公户余额
|
|
func (r *FundRepo) IncrBalance(amount int) error {
|
|
return r.db.Model(&model.CommunityFund{}).
|
|
Where("id = 1").
|
|
UpdateColumn("balance", gorm.Expr("balance + ?", amount)).Error
|
|
}
|
|
|
|
// CreateLog 创建公户流水
|
|
func (r *FundRepo) CreateLog(log *model.FundLog) error {
|
|
return r.db.Create(log).Error
|
|
}
|
|
|
|
// ListLogs 分页查询公户流水
|
|
func (r *FundRepo) ListLogs(logType string, offset, limit int) ([]model.FundLog, int64, error) {
|
|
query := r.db.Model(&model.FundLog{})
|
|
if logType != "" {
|
|
query = query.Where("type = ?", logType)
|
|
}
|
|
var total int64
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var logs []model.FundLog
|
|
err := query.Order("created_at DESC").Offset(offset).Limit(limit).Find(&logs).Error
|
|
return logs, total, err
|
|
}
|