签入脚本
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
v1 "easygoadmin/api/v1"
|
||||
"easygoadmin/internal/dao"
|
||||
"easygoadmin/internal/service"
|
||||
"easygoadmin/utility/excel"
|
||||
"easygoadmin/utility/upload"
|
||||
)
|
||||
|
||||
// ============================== 案例演示业务实现 ==============================
|
||||
|
||||
// sExample 案例演示业务实现
|
||||
// 实现 service.IExample 接口,接口定义见 api/v1/example.go
|
||||
type sExample struct{}
|
||||
|
||||
func init() {
|
||||
service.RegisterExample(&sExample{})
|
||||
}
|
||||
|
||||
// exampleSerializeMaps 枚举字段到字典编码的映射
|
||||
// {field}Text 由基座自动填充
|
||||
var exampleSerializeMaps = map[string]string{
|
||||
"status": "example_status",
|
||||
}
|
||||
|
||||
// exampleSpec 案例演示模块声明
|
||||
//
|
||||
// 采用声明式实现:查询、输出与规则均在声明中定义,方法仅做委托
|
||||
var exampleSpec = ModuleSpec[v1.ExampleItem, v1.ExampleItem, v1.ExampleDetailRes]{
|
||||
Model: dao.Example.Ctx,
|
||||
LikeFields: []string{"name"},
|
||||
EqFields: []string{"type", "status"},
|
||||
OrderBy: "sort, id",
|
||||
SerializeMaps: exampleSerializeMaps,
|
||||
FileFields: []string{
|
||||
"avatar",
|
||||
},
|
||||
FileSaveDir: "example",
|
||||
ToData: exampleToData,
|
||||
}
|
||||
|
||||
// exampleToData 全量表单语义转换
|
||||
// 新增与修改均提交全部字段,兼容两种入参类型
|
||||
func exampleToData(req any) map[string]any {
|
||||
switch r := req.(type) {
|
||||
case *v1.ExampleAddReq:
|
||||
return map[string]any{
|
||||
"name": r.Name,
|
||||
"avatar": r.Avatar,
|
||||
"type": r.Type,
|
||||
"status": r.Status,
|
||||
"sort": r.Sort,
|
||||
}
|
||||
case *v1.ExampleUpdateReq:
|
||||
return map[string]any{
|
||||
"name": r.Name,
|
||||
"avatar": r.Avatar,
|
||||
"type": r.Type,
|
||||
"status": r.Status,
|
||||
"sort": r.Sort,
|
||||
}
|
||||
}
|
||||
panic(fmt.Sprintf("exampleToData: 不支持的入参类型 %T", req))
|
||||
}
|
||||
|
||||
// ---------- 标准方法:委托基座 ----------
|
||||
|
||||
// Page 案例演示分页
|
||||
// 按查询条件分页返回案例演示列表
|
||||
func (s *sExample) Page(ctx context.Context, req *v1.ExamplePageReq) (res *v1.ExamplePageRes, err error) {
|
||||
list, total, err := exampleSpec.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 案例演示平铺列表
|
||||
// data 直接为数组,树形结构由前端 buildTree 构建
|
||||
func (s *sExample) List(ctx context.Context, req *v1.ExampleListReq) (res v1.ExampleListRes, err error) {
|
||||
list, err := exampleSpec.List(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return derefSlice(list), nil
|
||||
}
|
||||
|
||||
// Detail 案例演示详情
|
||||
// 按主键ID查询详情
|
||||
func (s *sExample) Detail(ctx context.Context, req *v1.ExampleDetailReq) (res *v1.ExampleDetailRes, err error) {
|
||||
return exampleSpec.Detail(ctx, req.Id, req)
|
||||
}
|
||||
|
||||
// Add 新增案例演示
|
||||
// 新增成功后返回新记录ID
|
||||
func (s *sExample) Add(ctx context.Context, req *v1.ExampleAddReq) (res *v1.ExampleIdRes, err error) {
|
||||
id, err := exampleSpec.Add(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &v1.ExampleIdRes{Id: id}, nil
|
||||
}
|
||||
|
||||
// Update 修改案例演示
|
||||
// 按主键ID更新记录
|
||||
func (s *sExample) Update(ctx context.Context, req *v1.ExampleUpdateReq) (res *v1.ExampleUpdateRes, err error) {
|
||||
if err = exampleSpec.Update(ctx, req.Id, req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &v1.ExampleUpdateRes{}, nil
|
||||
}
|
||||
|
||||
// Status 案例演示状态
|
||||
// 非标准方法,保留手写实现,设置记录的状态字段
|
||||
func (s *sExample) Status(ctx context.Context, req *v1.ExampleStatusReq) (res *v1.ExampleStatusRes, err error) {
|
||||
if _, err = dao.Example.Ctx(ctx).Where("id", req.Id).Data(map[string]any{"status": req.Status}).Update(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &v1.ExampleStatusRes{}, nil
|
||||
}
|
||||
|
||||
// Delete 删除案例演示
|
||||
// 按主键ID删除单条记录
|
||||
func (s *sExample) Delete(ctx context.Context, req *v1.ExampleDeleteReq) (res *v1.ExampleDeleteRes, err error) {
|
||||
if err = exampleSpec.Delete(ctx, req.Id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &v1.ExampleDeleteRes{}, nil
|
||||
}
|
||||
|
||||
// BatchDelete 批量删除案例演示
|
||||
// 按主键ID列表批量删除记录
|
||||
func (s *sExample) BatchDelete(ctx context.Context, req *v1.ExampleBatchDeleteReq) (res *v1.ExampleBatchDeleteRes, err error) {
|
||||
if err = exampleSpec.BatchDelete(ctx, req.Ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &v1.ExampleBatchDeleteRes{}, nil
|
||||
}
|
||||
|
||||
// Import 导入案例演示
|
||||
// 表头为表单字段名,按列序写入
|
||||
func (s *sExample) Import(ctx context.Context, req *v1.ExampleImportReq) (res *v1.ExampleImportRes, err error) {
|
||||
_, rows, err := excel.ReadSheet(req.File)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res = &v1.ExampleImportRes{Total: len(rows)}
|
||||
for i, row := range rows {
|
||||
if cellAt(row, 0) == "" {
|
||||
res.Failed++
|
||||
res.Reasons = append(res.Reasons, fmt.Sprintf("第%d行:案例名称为空", i+2))
|
||||
continue
|
||||
}
|
||||
if _, e := dao.Example.Ctx(ctx).Data(map[string]any{
|
||||
"name": cellAt(row, 0),
|
||||
"avatar": cellAt(row, 1),
|
||||
"type": cellInt(row, 2, 0),
|
||||
"status": cellInt(row, 3, 0),
|
||||
"sort": cellInt(row, 4, 0),
|
||||
}).Insert(); e != nil {
|
||||
res.Failed++
|
||||
res.Reasons = append(res.Reasons, fmt.Sprintf("第%d行:写入失败 %v", i+2, e))
|
||||
continue
|
||||
}
|
||||
res.Success++
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Export 导出案例演示
|
||||
// 与列表同套筛选条件,枚举列输出字典文本
|
||||
func (s *sExample) Export(ctx context.Context, req *v1.ExampleExportReq) (res *v1.ExampleExportRes, err error) {
|
||||
rows, err := exampleSpec.applyFilters(ctx, exampleSpec.Model(ctx), req).Order("sort, id").All()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
headers := []string{"案例名称", "案例图片", "案例类型", "案例状态", "案例排序"}
|
||||
data := make([][]any, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
data = append(data, []any{r["name"].String(), r["avatar"].String(), DictGetText(ctx, "example_type", r["type"]), DictGetText(ctx, "example_status", r["status"]), r["sort"].String()})
|
||||
}
|
||||
fileName, _, err := excel.WriteSheet(upload.BaseDir(ctx), "案例演示数据", headers, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
url := upload.GetFileUrl(ctx, fileName)
|
||||
return &url, nil
|
||||
}
|
||||
Reference in New Issue
Block a user