266 lines
8.4 KiB
Go
266 lines
8.4 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"strings"
|
||
|
||
"easygoadmin/api/v1"
|
||
"easygoadmin/internal/dao"
|
||
"easygoadmin/internal/service"
|
||
"easygoadmin/utility/upload"
|
||
)
|
||
|
||
// ============================== 配置项业务实现 ==============================
|
||
|
||
// sConfigItem 配置项业务实现
|
||
// 实现 service.IConfigItem 接口,承载配置项模块的业务逻辑
|
||
type sConfigItem struct{}
|
||
|
||
// configItemSerializeMaps 枚举字段到字典编码的映射
|
||
// {field}Text 由 dict_item 解析,字典管理页可修改文案
|
||
var configItemSerializeMaps = map[string]string{
|
||
"status": "config_item_status",
|
||
}
|
||
|
||
// configItemSpec 配置项模块声明
|
||
var configItemSpec = ModuleSpec[v1.ConfigItemItem, v1.ConfigItemItem, v1.ConfigItemDetailRes]{
|
||
Model: dao.ConfigItem.Ctx,
|
||
LikeFields: []string{"name", "code"},
|
||
EqFields: []string{"status", "config_id"},
|
||
OrderBy: "sort, id",
|
||
SerializeMaps: configItemSerializeMaps,
|
||
AfterPageList: configItemExpandName, // 行内所属配置名批量补全
|
||
AfterDetail: configItemAfterDetail, // 所属配置编码 + 文件类字段拼接访问URL
|
||
}
|
||
|
||
// configItemExpandName 分页行所属配置名批量补全
|
||
// 将配置ID映射为配置名称,避免逐行查询
|
||
func configItemExpandName(ctx context.Context, list []*v1.ConfigItemItem, req any) error {
|
||
cfgNames, err := (&sConfigItem{}).configNames(ctx)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
for _, item := range list {
|
||
item.ConfigName = cfgNames[item.ConfigId]
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// configItemAfterDetail 详情装配
|
||
//
|
||
// 补充所属配置编码,并按控件类型拼接文件类字段的访问URL:
|
||
// - image:value 直接拼域名
|
||
// - file:value 为 "原文件名|路径",路径部分拼域名
|
||
// - images:value 保持相对路径逗号串,valueList 为逐项拼域名后的数组
|
||
// - files:value 保持 "原文件名|路径" 逗号串,valueList 为逐项拼域名后的数组
|
||
func configItemAfterDetail(ctx context.Context, item *v1.ConfigItemDetailRes, req any) error {
|
||
code, err := (&sConfigItem{}).configCode(ctx, item.ConfigId)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
item.ConfigCode = code
|
||
switch item.Type {
|
||
case "image":
|
||
item.Value = upload.GetFileUrl(ctx, item.Value)
|
||
case "file":
|
||
item.Value = configItemFileURL(ctx, item.Value)
|
||
case "images":
|
||
item.ValueList = configItemListURLs(ctx, item.Value, false)
|
||
case "files":
|
||
item.ValueList = configItemListURLs(ctx, item.Value, true)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// configItemSaveValue 保存前按控件类型归一化 value
|
||
//
|
||
// 文件类字段执行临时转正并剥离域名前缀,入库形态为相对路径:
|
||
// - image:单文件 URL
|
||
// - images:逗号分隔多文件 URL
|
||
// - file:值为 "原文件名|URL",仅处理 URL 部分
|
||
// - files:逗号分隔 "原文件名|URL" 串,逐项处理 URL 部分
|
||
// - ueditor:富文本,临时转正并将本站URL写为 [IMG_URL] 占位符
|
||
// - 其余类型:原样返回
|
||
func configItemSaveValue(ctx context.Context, typ, value string) string {
|
||
switch typ {
|
||
case "image":
|
||
return SaveFileField(ctx, value, "config")
|
||
case "images":
|
||
return SaveMultiFileField(ctx, value, "config")
|
||
case "file":
|
||
return configItemSaveFilePair(ctx, value)
|
||
case "files":
|
||
if value == "" {
|
||
return ""
|
||
}
|
||
parts := strings.Split(value, ",")
|
||
for i, p := range parts {
|
||
parts[i] = configItemSaveFilePair(ctx, strings.TrimSpace(p))
|
||
}
|
||
return strings.Join(parts, ",")
|
||
case "ueditor":
|
||
return LocalizeContentFields(ctx, value, "config")
|
||
}
|
||
return value
|
||
}
|
||
|
||
// configItemSaveFilePair 处理单个 "原文件名|URL" 对
|
||
// URL 部分转正并剥离域名,文件名前缀保留
|
||
func configItemSaveFilePair(ctx context.Context, value string) string {
|
||
name, url, found := strings.Cut(value, "|")
|
||
if !found {
|
||
return SaveFileField(ctx, value, "config")
|
||
}
|
||
return name + "|" + SaveFileField(ctx, url, "config")
|
||
}
|
||
|
||
// configItemFileURL 单文件值转访问URL
|
||
// 将 "原文件名|相对路径" 中的路径部分拼接访问URL
|
||
func configItemFileURL(ctx context.Context, value string) string {
|
||
name, path, found := strings.Cut(value, "|")
|
||
if !found {
|
||
return upload.GetFileUrl(ctx, value)
|
||
}
|
||
return name + "|" + upload.GetFileUrl(ctx, path)
|
||
}
|
||
|
||
// configItemListURLs 逗号串逐项拼接访问URL
|
||
// keepName 为 true 时保留 "原文件名|" 前缀
|
||
func configItemListURLs(ctx context.Context, value string, keepName bool) []string {
|
||
if value == "" {
|
||
return nil
|
||
}
|
||
parts := strings.Split(value, ",")
|
||
list := make([]string, 0, len(parts))
|
||
for _, p := range parts {
|
||
p = strings.TrimSpace(p)
|
||
if p == "" {
|
||
continue
|
||
}
|
||
if keepName {
|
||
list = append(list, configItemFileURL(ctx, p))
|
||
continue
|
||
}
|
||
list = append(list, upload.GetFileUrl(ctx, p))
|
||
}
|
||
return list
|
||
}
|
||
|
||
func init() {
|
||
service.RegisterConfigItem(&sConfigItem{})
|
||
}
|
||
|
||
// Page 配置项分页
|
||
// 按查询条件分页返回配置项列表
|
||
func (s *sConfigItem) Page(ctx context.Context, req *v1.ConfigItemPageReq) (res *v1.ConfigItemPageRes, err error) {
|
||
list, total, err := configItemSpec.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 *sConfigItem) List(ctx context.Context, req *v1.ConfigItemListReq) (res v1.ConfigItemListRes, err error) {
|
||
list, err := configItemSpec.List(ctx, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
cfgNames, err := s.configNames(ctx)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
rows := derefSlice(list)
|
||
for i := range rows {
|
||
rows[i].ConfigName = cfgNames[rows[i].ConfigId]
|
||
}
|
||
return rows, nil
|
||
}
|
||
|
||
// Detail 配置项详情
|
||
// 按配置项ID查询详情及其值列表
|
||
func (s *sConfigItem) Detail(ctx context.Context, req *v1.ConfigItemDetailReq) (res *v1.ConfigItemDetailRes, err error) {
|
||
return configItemSpec.Detail(ctx, req.Id, req)
|
||
}
|
||
|
||
// Add 新增配置项
|
||
// 按控件类型归一化值后落库,返回新记录ID
|
||
func (s *sConfigItem) Add(ctx context.Context, req *v1.ConfigItemAddReq) (res *v1.IdRes, err error) {
|
||
result, err := dao.ConfigItem.Ctx(ctx).Data(map[string]any{
|
||
"config_id": req.ConfigId,
|
||
"name": req.Name,
|
||
"code": req.Code,
|
||
"value": configItemSaveValue(ctx, req.Type, req.Value),
|
||
"options": req.Options,
|
||
"type": req.Type,
|
||
"status": req.Status,
|
||
"sort": req.Sort,
|
||
"note": req.Note,
|
||
}).Insert()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
idv, _ := result.LastInsertId()
|
||
return &v1.IdRes{Id: idv}, nil
|
||
}
|
||
|
||
// Update 修改配置项
|
||
// 按配置项ID更新记录,值按控件类型归一化
|
||
func (s *sConfigItem) Update(ctx context.Context, req *v1.ConfigItemUpdateReq) (res *v1.ConfigItemUpdateRes, err error) {
|
||
if _, err = dao.ConfigItem.Ctx(ctx).Where("id", req.Id).Data(map[string]any{
|
||
"config_id": req.ConfigId,
|
||
"name": req.Name,
|
||
"code": req.Code,
|
||
"value": configItemSaveValue(ctx, req.Type, req.Value),
|
||
"options": req.Options,
|
||
"type": req.Type,
|
||
"status": req.Status,
|
||
"sort": req.Sort,
|
||
"note": req.Note,
|
||
}).Update(); err != nil {
|
||
return nil, err
|
||
}
|
||
return &v1.ConfigItemUpdateRes{}, nil
|
||
}
|
||
|
||
// Delete 删除配置项
|
||
// 按配置项ID删除单条记录
|
||
func (s *sConfigItem) Delete(ctx context.Context, req *v1.ConfigItemDeleteReq) (res *v1.ConfigItemDeleteRes, err error) {
|
||
if err = configItemSpec.Delete(ctx, req.Id); err != nil {
|
||
return nil, err
|
||
}
|
||
return &v1.ConfigItemDeleteRes{}, nil
|
||
}
|
||
|
||
// BatchDelete 批量删除配置项
|
||
// 按配置项ID列表批量删除记录
|
||
func (s *sConfigItem) BatchDelete(ctx context.Context, req *v1.ConfigItemBatchDeleteReq) (res *v1.ConfigItemBatchDeleteRes, err error) {
|
||
if err = configItemSpec.BatchDelete(ctx, req.Ids); err != nil {
|
||
return nil, err
|
||
}
|
||
return &v1.ConfigItemBatchDeleteRes{}, nil
|
||
}
|
||
|
||
// configCode 查询配置ID对应的编码
|
||
func (s *sConfigItem) configCode(ctx context.Context, configId int64) (string, error) {
|
||
code, err := dao.Config.Ctx(ctx).Where("id", configId).Fields("code").Value()
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
return code.String(), nil
|
||
}
|
||
|
||
// configNames 查询配置ID到名称的映射
|
||
func (s *sConfigItem) configNames(ctx context.Context) (map[int64]string, error) {
|
||
result := make(map[int64]string)
|
||
rows, err := dao.Config.Ctx(ctx).Fields("id, name").All()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
for _, r := range rows {
|
||
result[r["id"].Int64()] = r["name"].String()
|
||
}
|
||
return result, nil
|
||
}
|