262 lines
9.2 KiB
Go
262 lines
9.2 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"
|
|
"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/model/entity"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/internal/service"
|
|
"git.5iot.com/iotlcm-public/iotlcm-admin-svr/utility/convert"
|
|
|
|
"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 sCommonTFinanceApply struct{}
|
|
|
|
func NewCommonTFinanceApply() *sCommonTFinanceApply {
|
|
return &sCommonTFinanceApply{}
|
|
}
|
|
|
|
func init() {
|
|
service.RegisterCommonTFinanceApply(NewCommonTFinanceApply())
|
|
}
|
|
|
|
// Model 财务申请表ORM模型
|
|
func (s *sCommonTFinanceApply) 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.TFinanceApply.Ctx(ctx), option...)
|
|
}
|
|
|
|
// List 获取财务申请表列表
|
|
func (s *sCommonTFinanceApply) List(ctx context.Context, in *model.TFinanceApplyListParam) (list []*model.TFinanceApplyListModel, totalCount int, err error) {
|
|
mod := s.Model(ctx)
|
|
applyTable := dao.TFinanceApply.Table()
|
|
applyDeptAlias := "apply_dept"
|
|
auditorDeptAlias := "auditor_dept"
|
|
sysDeptTable := dao.SysDept.Table()
|
|
mod = mod.LeftJoin(sysDeptTable+" as "+applyDeptAlias, applyDeptAlias+"."+dao.SysDept.Columns().DeptId+"="+applyTable+"."+dao.TFinanceApply.Columns().ApplyDeptId)
|
|
mod = mod.LeftJoin(sysDeptTable+" as "+auditorDeptAlias, auditorDeptAlias+"."+dao.SysDept.Columns().DeptId+"="+applyTable+"."+dao.TFinanceApply.Columns().AuditDeptId)
|
|
|
|
// 字段过滤
|
|
mod = mod.FieldsPrefix(applyTable, model.TFinanceApplyListModel{})
|
|
mod = mod.Fields(applyDeptAlias + ".dept_name as applyDeptName")
|
|
mod = mod.Fields(auditorDeptAlias + ".dept_name as auditDeptName")
|
|
|
|
// 查询主键ID
|
|
if in.Id > 0 {
|
|
mod = mod.Where(dao.TFinanceApply.Columns().Id, in.Id)
|
|
}
|
|
if in.ApplyNo != nil {
|
|
mod = mod.Where(dao.TFinanceApply.Columns().ApplyNo, *in.ApplyNo)
|
|
}
|
|
|
|
// 查询状态:0待审核,1已通过,2已驳回,3已打款,4已取消
|
|
if in.ApplyStatus != nil {
|
|
mod = mod.Where(dao.TFinanceApply.Columns().ApplyStatus, *in.ApplyStatus)
|
|
}
|
|
if in.ApplyType != nil {
|
|
mod = mod.Where(dao.TFinanceApply.Columns().ApplyType, *in.ApplyType)
|
|
}
|
|
|
|
// 查询创建时间
|
|
if len(in.CreatedAt) == 2 {
|
|
in.CreatedAt[1] = in.CreatedAt[1].EndOfDay()
|
|
}
|
|
if len(in.CreatedAt) == 2 {
|
|
mod = mod.WhereBetween(dao.TFinanceApply.Columns().CreatedAt, in.CreatedAt[0], in.CreatedAt[1])
|
|
}
|
|
|
|
// 分页
|
|
mod = mod.Page(in.Page, in.PageSize)
|
|
|
|
// 排序
|
|
mod = mod.OrderDesc(dao.TFinanceApply.Columns().Id)
|
|
|
|
// 操作人摘要信息
|
|
mod = mod.Hook(hook.MemberSummary)
|
|
|
|
// 查询数据
|
|
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
|
err = gerror.Wrap(err, "获取财务申请表列表失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// Export 导出财务申请(异步:提交任务后到下载中心取文件)
|
|
func (s *sCommonTFinanceApply) Export(ctx context.Context, in *model.TFinanceApplyListParam) (err error) {
|
|
_, err = exportcenter.Submit(ctx, &exportcenter.Request{
|
|
BizKey: "tFinanceApply",
|
|
BizName: "财务申请",
|
|
PermCode: "cpm:common:tFinanceApply:export",
|
|
QueryParam: in,
|
|
Tags: func() ([]string, error) {
|
|
return convert.GetEntityDescTags(model.TFinanceApplyListModel{})
|
|
},
|
|
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 {
|
|
rows = append(rows, item)
|
|
}
|
|
return rows, count, nil
|
|
},
|
|
})
|
|
return err
|
|
}
|
|
|
|
// Edit 修改/新增财务申请表
|
|
func (s *sCommonTFinanceApply) Edit(ctx context.Context, in *model.TFinanceApplyEditParam) (err error) {
|
|
// 验证'ApplyNo'唯一
|
|
if err = xgorm.IsUnique(ctx, &dao.TFinanceApply, g.Map{dao.TFinanceApply.Columns().ApplyNo: in.ApplyNo}, "申请单号已存在", in.Id); err != nil {
|
|
return
|
|
}
|
|
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()
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
in.TenantId = &tenantId
|
|
if _, err = s.Model(ctx).
|
|
Fields(model.TFinanceApplyUpdateFields{}).
|
|
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.TFinanceApplyInsertFields{}).
|
|
Data(in).Insert(); err != nil {
|
|
err = gerror.Wrap(err, "新增财务申请表失败,请稍后重试!")
|
|
}
|
|
return
|
|
})
|
|
}
|
|
|
|
// Delete 删除财务申请表
|
|
func (s *sCommonTFinanceApply) Delete(ctx context.Context, in *model.TFinanceApplyDeleteParam) (err error) {
|
|
|
|
if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
|
|
err = gerror.Wrap(err, "删除财务申请表失败,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// View 获取财务申请表指定信息
|
|
func (s *sCommonTFinanceApply) View(ctx context.Context, in *model.TFinanceApplyViewParam) (res *model.TFinanceApplyViewModel, err error) {
|
|
if err = s.Model(ctx).WherePri(in.Id).Hook(hook.MemberSummary).Scan(&res); err != nil {
|
|
err = gerror.Wrap(err, "获取财务申请表信息,请稍后重试!")
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// CreateBalanceTransferApply 创建余额转可提现的待审核申请单。
|
|
func (s *sCommonTFinanceApply) CreateBalanceTransferApply(ctx context.Context, tx gdb.TX, in *model.TFinanceApplyCreateBalanceTransferParam) error {
|
|
if in == nil {
|
|
return gerror.New("财务申请参数不能为空")
|
|
}
|
|
tenantId := contexts.GetTenantId(ctx)
|
|
userId := contexts.GetUserId(ctx)
|
|
deptId := contexts.GetDeptId(ctx)
|
|
createdAt := gtime.Now()
|
|
columns := dao.TFinanceApply.Columns()
|
|
if _, err := tx.Ctx(ctx).Model(dao.TFinanceApply.Table()).Data(g.Map{
|
|
columns.ApplyNo: in.ApplyNo,
|
|
columns.ApplyType: financeApplyTypeBalanceToWithdraw,
|
|
columns.AuditType: financeAuditTypeFirst,
|
|
columns.ApplyDeptId: in.ApplyDeptId,
|
|
columns.AuditDeptId: in.AuditDeptId,
|
|
columns.AvailableAmountSnapshot: in.AvailableAmountSnapshot,
|
|
columns.ApplyAmount: in.ApplyAmount,
|
|
columns.AccountType: 0,
|
|
columns.ApplyStatus: financeApplyStatusPending,
|
|
columns.ApplyTime: createdAt,
|
|
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
|
|
}
|
|
|
|
// GetPendingByApplyNo 按申请单号查询待审核申请。
|
|
func (s *sCommonTFinanceApply) GetPendingByApplyNo(ctx context.Context, tx gdb.TX, applyNo string, tenantId string) (*entity.TFinanceApply, error) {
|
|
apply := &entity.TFinanceApply{}
|
|
columns := dao.TFinanceApply.Columns()
|
|
if err := tx.Ctx(ctx).Model(dao.TFinanceApply.Table()).
|
|
Where(columns.ApplyNo, applyNo).
|
|
Where(columns.ApplyStatus, financeApplyStatusPending).
|
|
Where(columns.TenantId, tenantId).
|
|
Scan(apply); err != nil {
|
|
return nil, gerror.Wrap(err, "查询财务申请失败,请稍后重试!")
|
|
}
|
|
if apply.Id == 0 {
|
|
return nil, gerror.New("财务申请不存在或已审核")
|
|
}
|
|
return apply, nil
|
|
}
|
|
|
|
// UpdateAuditStatus 更新财务申请的审核状态。
|
|
func (s *sCommonTFinanceApply) UpdateAuditStatus(ctx context.Context, tx gdb.TX, in *model.TFinanceApplyUpdateAuditStatusParam) error {
|
|
if in == nil {
|
|
return gerror.New("财务申请审核状态参数不能为空")
|
|
}
|
|
userId := contexts.GetUserId(ctx)
|
|
updatedAt := gtime.Now()
|
|
columns := dao.TFinanceApply.Columns()
|
|
if _, err := tx.Ctx(ctx).Model(dao.TFinanceApply.Table()).
|
|
Where(columns.ApplyNo, in.ApplyNo).
|
|
Data(g.Map{
|
|
columns.ApplyStatus: in.ApplyStatus,
|
|
columns.AuditTime: in.AuditTime,
|
|
columns.UpdatedBy: userId,
|
|
columns.UpdatedAt: updatedAt,
|
|
}).
|
|
Update(); err != nil {
|
|
return gerror.Wrap(err, "更新财务申请审核状态失败,请稍后重试!")
|
|
}
|
|
return nil
|
|
}
|