255 lines
9.1 KiB
Go
255 lines
9.1 KiB
Go
// Package gen
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2026 LiXiujie
|
|
// @Author Lxj <xxx@example.com>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
// @AutoGenerate Version
|
|
package common
|
|
|
|
import (
|
|
"context"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/contexts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/exportcenter"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/handler"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/library/xgorm/hook"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/convert"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
)
|
|
|
|
type sCommonTBalanceDetail struct{}
|
|
|
|
func NewCommonTBalanceDetail() *sCommonTBalanceDetail {
|
|
return &sCommonTBalanceDetail{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterCommonTBalanceDetail(NewCommonTBalanceDetail())
|
|
}
|
|
|
|
// Model 余额明细表ORM模型
|
|
func (s *sCommonTBalanceDetail) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
|
|
if len(option) == 0 {
|
|
option = append(option, &handler.Option{
|
|
FilterTenant: true,
|
|
FilterAuth: true,
|
|
})
|
|
}
|
|
return handler.Model(dao.TBalanceDetail.Ctx(ctx), option...)
|
|
}
|
|
|
|
// List 获取余额明细表列表
|
|
func (s *sCommonTBalanceDetail) List(ctx context.Context, in *model.TBalanceDetailListParam) (list []*model.TBalanceDetailListModel, totalCount int, err error) {
|
|
mod := s.Model(ctx)
|
|
sysDeptTable := dao.SysDept.Table()
|
|
orderTable := dao.TOrder.Table()
|
|
|
|
// 字段过滤
|
|
mod = mod.FieldsPrefix(dao.TBalanceDetail.Table(), model.TBalanceDetailListModel{})
|
|
mod = mod.Fields(sysDeptTable + ".dept_name as deptName")
|
|
mod = mod.Fields(orderTable + ".order_type as orderType")
|
|
mod = mod.Fields(orderTable + ".pay_type as payType")
|
|
deptId := contexts.GetDeptId(ctx)
|
|
|
|
// 字段过滤
|
|
mod = mod.Where(dao.TBalanceDetail.Columns().DeptId, deptId)
|
|
|
|
// 查询明细ID
|
|
if in.Id > 0 {
|
|
mod = mod.Where(dao.TBalanceDetail.Columns().Id, in.Id)
|
|
}
|
|
g.Log().Info(ctx, "month==:", in.Month, " len:", len(in.Month))
|
|
// 查询收支类型
|
|
if in.IncomeExpenseType != nil && *in.IncomeExpenseType >= 0 {
|
|
mod = mod.Where(dao.TBalanceDetail.Columns().IncomeExpenseType, *in.IncomeExpenseType)
|
|
}
|
|
// 查询交易类型
|
|
if in.TransactionType != nil && *in.TransactionType >= 0 {
|
|
mod = mod.Where(dao.TBalanceDetail.Columns().TransactionType, *in.TransactionType)
|
|
}
|
|
// 查询交易渠道
|
|
if in.TransactionChannel != nil && *in.TransactionChannel >= 0 {
|
|
mod = mod.Where(dao.TBalanceDetail.Columns().TransactionChannel, *in.TransactionChannel)
|
|
}
|
|
// // 查询支付类型
|
|
// if in.PayType != nil && *in.PayType >= 0 {
|
|
// mod = mod.Where(dao.TBalanceDetail.Columns().PayType, *in.PayType)
|
|
// }
|
|
|
|
// 查询交易时间范围
|
|
if len(in.Month) == 2 {
|
|
t1, err := time.ParseInLocation("2006-01", in.Month[0], time.Local)
|
|
t2, err2 := time.ParseInLocation("2006-01", in.Month[1], time.Local)
|
|
if err == nil && err2 == nil {
|
|
startOfMonth := gtime.NewFromTime(t1).StartOfMonth()
|
|
endOfMonth := gtime.NewFromTime(t2).EndOfMonth()
|
|
mod = mod.WhereGTE(dao.TBalanceDetail.Columns().TransactionTime, startOfMonth)
|
|
mod = mod.WhereLTE(dao.TBalanceDetail.Columns().TransactionTime, endOfMonth)
|
|
}
|
|
}
|
|
// 查询账期(根据 transaction_time 筛选月份)
|
|
if in.AccountPeriod != "" {
|
|
// 统一转换为 "2026-06" 格式后解析
|
|
accountPeriodStr := in.AccountPeriod
|
|
if len(accountPeriodStr) == 6 {
|
|
accountPeriodStr = accountPeriodStr[:4] + "-" + accountPeriodStr[4:]
|
|
}
|
|
t := gtime.NewFromStr(accountPeriodStr + "-01")
|
|
startOfMonth := t.StartOfDay()
|
|
endOfMonth := startOfMonth.EndOfMonth()
|
|
mod = mod.WhereBetween(dao.TBalanceDetail.Columns().TransactionTime, startOfMonth, endOfMonth)
|
|
}
|
|
|
|
mod = mod.LeftJoin(sysDeptTable, dao.TBalanceDetail.Table()+"."+dao.TBalanceDetail.Columns().DeptId+" COLLATE utf8mb4_general_ci="+sysDeptTable+"."+dao.SysDept.Columns().DeptId)
|
|
mod = mod.LeftJoin(orderTable, dao.TBalanceDetail.Table()+"."+dao.TBalanceDetail.Columns().OrderSn+" COLLATE utf8mb4_general_ci="+orderTable+"."+dao.TOrder.Columns().OrderSn)
|
|
|
|
// 分页
|
|
mod = mod.Page(in.Page, in.PageSize)
|
|
|
|
// 排序
|
|
mod = mod.OrderDesc(dao.TBalanceDetail.Columns().Id)
|
|
|
|
// 操作人摘要信息
|
|
mod = mod.Hook(hook.MemberSummary)
|
|
|
|
// 查询数据
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
err = gerror.Wrap(err, "获取余额明细表列表失败,请稍后重试!")
|
|
return
|
|
}
|
|
|
|
// 根据 TransactionTime 赋值账期
|
|
for i := range list {
|
|
if list[i].TransactionTime != nil {
|
|
list[i].TradingMonth = list[i].TransactionTime.Format("Y-m")
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// Export 导出余额明细(异步:提交任务后到下载中心取文件)
|
|
func (s *sCommonTBalanceDetail) Export(ctx context.Context, in *model.TBalanceDetailListParam) (err error) {
|
|
var req exportcenter.Request
|
|
req = exportcenter.Request{
|
|
BizKey: "tBalanceDetail",
|
|
BizName: "余额明细",
|
|
PermCode: "cpm:common:tBalanceDetail:export",
|
|
QueryParam: in,
|
|
Tags: func() ([]string, error) {
|
|
return convert.GetEntityDescTags(model.TBalanceDetailListModel{})
|
|
},
|
|
Fetch: func(ctx context.Context, page, pageSize int) (rows []interface{}, total int, err error) {
|
|
query := *in
|
|
query.Page = page
|
|
query.PageSize = pageSize
|
|
list, count, err := s.List(ctx, &query)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
rows = make([]interface{}, 0, len(list))
|
|
for _, item := range list {
|
|
// 与列表 getDisplayTransactionChannel 保持一致,先归一化历史渠道值。
|
|
if len(req.Columns) > 0 && (item.TransactionChannel == 4 || (item.TransactionChannel == 0 && item.IncomeExpenseType == 1 && item.TransactionType == 1)) {
|
|
item.TransactionChannel = 3
|
|
}
|
|
rows = append(rows, item)
|
|
}
|
|
return rows, count, nil
|
|
},
|
|
}
|
|
_, err = exportcenter.Submit(ctx, &req)
|
|
return err
|
|
}
|
|
|
|
// Edit 修改/新增余额明细表
|
|
func (s *sCommonTBalanceDetail) Edit(ctx context.Context, in *model.TBalanceDetailEditParam) (err error) {
|
|
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
|
|
|
// 修改
|
|
if in.Id > 0 {
|
|
updatedBy := contexts.GetUserId(ctx)
|
|
in.UpdatedBy = &updatedBy
|
|
in.UpdatedAt = gtime.Now()
|
|
if _, err = s.Model(ctx).
|
|
Fields(model.TBalanceDetailUpdateFields{}).
|
|
WherePri(in.Id).Data(in).Update(); err != nil {
|
|
err = gerror.Wrap(err, "修改余额明细表失败,请稍后重试!")
|
|
}
|
|
return
|
|
}
|
|
|
|
// 新增
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
in.TenantId = &tenantId
|
|
createdDept := contexts.GetDeptId(ctx)
|
|
in.CreatedDept = &createdDept
|
|
createdBy := contexts.GetUserId(ctx)
|
|
in.CreatedBy = &createdBy
|
|
in.CreatedAt = gtime.Now()
|
|
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
|
|
Fields(model.TBalanceDetailInsertFields{}).
|
|
Data(in).Insert(); err != nil {
|
|
err = gerror.Wrap(err, "新增余额明细表失败,请稍后重试!")
|
|
}
|
|
return
|
|
})
|
|
}
|
|
|
|
// Delete 删除余额明细表
|
|
func (s *sCommonTBalanceDetail) Delete(ctx context.Context, in *model.TBalanceDetailDeleteParam) (err error) {
|
|
|
|
if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
|
|
err = gerror.Wrap(err, "删除余额明细表失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// View 获取余额明细表指定信息
|
|
func (s *sCommonTBalanceDetail) View(ctx context.Context, in *model.TBalanceDetailViewParam) (res *model.TBalanceDetailViewModel, err error) {
|
|
if err = s.Model(ctx).WherePri(in.Id).Hook(hook.MemberSummary).Scan(&res); err != nil {
|
|
err = gerror.Wrap(err, "获取余额明细表信息,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// CreateBalanceTransferDetail 写入余额转可提现相关的余额明细。
|
|
func (s *sCommonTBalanceDetail) CreateBalanceTransferDetail(ctx context.Context, tx gdb.TX, in *model.TBalanceTransferDetailCreateParam) error {
|
|
if in == nil {
|
|
return gerror.New("余额明细参数不能为空")
|
|
}
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
userId := contexts.GetUserId(ctx)
|
|
deptId := contexts.GetDeptId(ctx)
|
|
createdAt := gtime.Now()
|
|
columns := dao.TBalanceDetail.Columns()
|
|
if _, err := tx.Ctx(ctx).Model(dao.TBalanceDetail.Table()).Data(g.Map{
|
|
columns.DeptId: in.DeptId,
|
|
columns.SerialNo: in.SerialNo,
|
|
columns.TransactionTime: in.TransactionTime,
|
|
columns.IncomeExpenseType: in.IncomeExpenseType,
|
|
columns.TransactionType: in.TransactionType,
|
|
columns.TransactionChannel: in.TransactionChannel,
|
|
columns.Amount: in.Amount,
|
|
columns.BeforeBalance: in.BeforeBalance,
|
|
columns.BalanceAfter: in.BalanceAfter,
|
|
columns.OrderSn: in.OrderSn,
|
|
columns.Remark: in.Remark,
|
|
columns.TenantId: tenantId,
|
|
columns.CreatedDept: deptId,
|
|
columns.CreatedBy: userId,
|
|
columns.CreatedAt: createdAt,
|
|
}).Insert(); err != nil {
|
|
return gerror.Wrap(err, "新增余额转可提现余额明细失败,请稍后重试!")
|
|
}
|
|
return nil
|
|
}
|