Files
EasyGoAdmin_GoFrame_EleVue/internal/logic/param.go
T
2026-10-20 11:37:25 +08:00

134 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package logic
import (
"context"
"easygoadmin/api/v1"
"easygoadmin/internal/dao"
"easygoadmin/internal/service"
)
// ============================== 系统参数业务实现 ==============================
// sParam 系统参数业务实现
// 实现 service.IParam 接口,接口定义见 api/v1/param.go
type sParam struct{}
func init() {
service.RegisterParam(&sParam{})
}
// paramSerializeMaps 枚举字段到字典编码的映射
var paramSerializeMaps = map[string]string{
"type": "param_type",
"status": "param_status",
}
// paramSpec 系统参数模块声明
var paramSpec = ModuleSpec[v1.ParamItem, v1.ParamItem, v1.ParamDetailRes]{
Model: dao.Param.Ctx,
LikeFields: []string{"name", "code"},
EqFields: []string{"type", "status"},
OrderBy: "sort, id",
SerializeMaps: paramSerializeMaps,
UniqueFields: []UniqueRule{{Field: "code"}}, // 参数编码唯一
FieldLabels: map[string]string{"code": "参数编码"}, // 唯一提示显式声明(原取自列注释)
ToData: paramToData,
}
// paramToData 全量表单语义转换
// 兼容新增与修改请求,将入参转换为落库字段
func paramToData(req any) map[string]any {
switch r := req.(type) {
case *v1.ParamAddReq:
return map[string]any{"name": r.Name, "code": r.Code, "value": r.Value,
"type": r.Type, "status": r.Status, "sort": r.Sort, "note": r.Note}
case *v1.ParamUpdateReq:
return map[string]any{"name": r.Name, "code": r.Code, "value": r.Value,
"type": r.Type, "status": r.Status, "sort": r.Sort, "note": r.Note}
}
panic("paramToData: 不支持的入参类型")
}
// Page 系统参数分页
// 按查询条件分页返回系统参数列表
func (s *sParam) Page(ctx context.Context, req *v1.ParamPageReq) (res *v1.ParamPageRes, err error) {
list, total, err := paramSpec.Page(ctx, req, req.GetPageNo(), req.GetPageSize())
if err != nil {
return nil, err
}
return v1.NewPageData(req.GetPageNo(), req.GetPageSize(), total, derefSlice(list)), nil
}
// List 系统参数全量列表
// 按关键词、类型、状态等条件返回系统参数列表
func (s *sParam) List(ctx context.Context, req *v1.ParamListReq) (res v1.ParamListRes, err error) {
list, err := paramSpec.List(ctx, req)
if err != nil {
return nil, err
}
return derefSlice(list), nil
}
// Detail 系统参数详情
// 按参数ID查询详情
func (s *sParam) Detail(ctx context.Context, req *v1.ParamDetailReq) (res *v1.ParamDetailRes, err error) {
return paramSpec.Detail(ctx, req.Id, req)
}
// Add 新增系统参数
// 校验参数编码唯一后新增,返回新记录ID
func (s *sParam) Add(ctx context.Context, req *v1.ParamAddReq) (res *v1.IdRes, err error) {
id, err := paramSpec.Add(ctx, req)
if err != nil {
return nil, err
}
return &v1.IdRes{Id: id}, nil
}
// Update 修改系统参数
// 按参数ID更新记录
func (s *sParam) Update(ctx context.Context, req *v1.ParamUpdateReq) (res *v1.ParamUpdateRes, err error) {
if err = paramSpec.Update(ctx, req.Id, req); err != nil {
return nil, err
}
return &v1.ParamUpdateRes{}, nil
}
// Delete 删除系统参数
// 按参数ID删除单条记录
func (s *sParam) Delete(ctx context.Context, req *v1.ParamDeleteReq) (res *v1.ParamDeleteRes, err error) {
if err = paramSpec.Delete(ctx, req.Id); err != nil {
return nil, err
}
return &v1.ParamDeleteRes{}, nil
}
// BatchDelete 批量删除系统参数
// 按参数ID列表批量删除记录
func (s *sParam) BatchDelete(ctx context.Context, req *v1.ParamBatchDeleteReq) (res *v1.ParamBatchDeleteRes, err error) {
if err = paramSpec.BatchDelete(ctx, req.Ids); err != nil {
return nil, err
}
return &v1.ParamBatchDeleteRes{}, nil
}
// paramGetString 按编码读取启用状态的系统参数值
//
// 软删除由全局钩子过滤,未命中时返回 fallback
func paramGetString(ctx context.Context, code, fallback string) string {
v, err := dao.Param.Ctx(ctx).Where("code", code).Where("status", 1).Fields("value").Value()
if err != nil || v.IsNil() || v.String() == "" {
return fallback
}
return v.String()
}
// getDefaultPassword 获取默认密码明文
//
// 取系统参数 DEFAULT_PASSWORD,未配置时回退 123456;
// 仅用于初始化密码的中间环节,禁止写入日志
func getDefaultPassword(ctx context.Context) string {
return paramGetString(ctx, "DEFAULT_PASSWORD", "123456")
}