165 lines
4.6 KiB
Go
165 lines
4.6 KiB
Go
// Package sys
|
||
// @Link https://github.com/bufanyun/hotgo
|
||
// @Copyright Copyright (c) 2025 HotGo CLI
|
||
// @Author Ms <133814250@qq.com>
|
||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||
// @AutoGenerate Version 2.17.8
|
||
package sys
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"hotgo/internal/dao"
|
||
"hotgo/internal/library/contexts"
|
||
"hotgo/internal/library/hgorm/handler"
|
||
"hotgo/internal/model/entity"
|
||
"hotgo/internal/model/input/form"
|
||
"hotgo/internal/model/input/sysin"
|
||
"hotgo/internal/service"
|
||
"hotgo/utility/convert"
|
||
"hotgo/utility/excel"
|
||
|
||
"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/gctx"
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
|
||
"hotgo/internal/library/dict"
|
||
"hotgo/internal/model"
|
||
)
|
||
|
||
type sSysLesson struct{}
|
||
|
||
func NewSysLesson() *sSysLesson {
|
||
return &sSysLesson{}
|
||
}
|
||
|
||
func init() {
|
||
service.RegisterSysLesson(NewSysLesson())
|
||
dict.RegisterFunc("lessonOption", "课程管理选项", service.SysLesson().Option)
|
||
}
|
||
|
||
// Model 课程管理ORM模型
|
||
func (s *sSysLesson) Model(ctx context.Context, option ...*handler.Option) *gdb.Model {
|
||
return handler.Model(dao.Lesson.Ctx(ctx), option...)
|
||
}
|
||
|
||
// List 获取课程管理列表
|
||
func (s *sSysLesson) List(ctx context.Context, in *sysin.LessonListInp) (list []*sysin.LessonListModel, totalCount int, err error) {
|
||
mod := s.Model(ctx)
|
||
|
||
// 字段过滤
|
||
mod = mod.Fields(sysin.LessonListModel{})
|
||
|
||
// 查询课程名
|
||
if in.Name != "" {
|
||
mod = mod.WhereLike(dao.Lesson.Columns().Name, in.Name)
|
||
}
|
||
|
||
// 查询所属学校
|
||
if in.School != "" {
|
||
mod = mod.WhereLike(dao.Lesson.Columns().School, in.School)
|
||
}
|
||
|
||
// 分页
|
||
mod = mod.Page(in.Page, in.PerPage)
|
||
|
||
// 排序
|
||
mod = mod.OrderDesc(dao.Lesson.Columns().Id)
|
||
|
||
// 查询数据
|
||
if err = mod.ScanAndCount(&list, &totalCount, false); err != nil {
|
||
err = gerror.Wrap(err, "获取课程管理列表失败,请稍后重试!")
|
||
return
|
||
}
|
||
return
|
||
}
|
||
|
||
// Export 导出课程管理
|
||
func (s *sSysLesson) Export(ctx context.Context, in *sysin.LessonListInp) (err error) {
|
||
list, totalCount, err := s.List(ctx, in)
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
// 字段的排序是依据tags的字段顺序,如果你不想使用默认的排序方式,可以直接定义 tags = []string{"字段名称", "字段名称2", ...}
|
||
tags, err := convert.GetEntityDescTags(sysin.LessonExportModel{})
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
var (
|
||
fileName = "导出课程管理-" + gctx.CtxId(ctx)
|
||
sheetName = fmt.Sprintf("索引条件共%v行,共%v页,当前导出是第%v页,本页共%v行", totalCount, form.CalPageCount(totalCount, in.PerPage), in.Page, len(list))
|
||
exports []sysin.LessonExportModel
|
||
)
|
||
|
||
if err = gconv.Scan(list, &exports); err != nil {
|
||
return
|
||
}
|
||
|
||
err = excel.ExportByStructs(ctx, tags, exports, fileName, sheetName)
|
||
return
|
||
}
|
||
|
||
// Edit 修改/新增课程管理
|
||
func (s *sSysLesson) Edit(ctx context.Context, in *sysin.LessonEditInp) (err error) {
|
||
return g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||
|
||
// 修改
|
||
if in.Id > 0 {
|
||
in.UpdatedBy = contexts.GetUserId(ctx)
|
||
if _, err = s.Model(ctx).
|
||
Fields(sysin.LessonUpdateFields{}).
|
||
WherePri(in.Id).Data(in).Update(); err != nil {
|
||
err = gerror.Wrap(err, "修改课程管理失败,请稍后重试!")
|
||
}
|
||
return
|
||
}
|
||
|
||
// 新增
|
||
in.CreatedBy = contexts.GetUserId(ctx)
|
||
if _, err = s.Model(ctx, &handler.Option{FilterAuth: false}).
|
||
Fields(sysin.LessonInsertFields{}).
|
||
Data(in).OmitEmptyData().Insert(); err != nil {
|
||
err = gerror.Wrap(err, "新增课程管理失败,请稍后重试!")
|
||
}
|
||
return
|
||
})
|
||
}
|
||
|
||
// Delete 删除课程管理
|
||
func (s *sSysLesson) Delete(ctx context.Context, in *sysin.LessonDeleteInp) (err error) {
|
||
|
||
if _, err = s.Model(ctx).WherePri(in.Id).Unscoped().Delete(); err != nil {
|
||
err = gerror.Wrap(err, "删除课程管理失败,请稍后重试!")
|
||
return
|
||
}
|
||
return
|
||
}
|
||
|
||
// View 获取课程管理指定信息
|
||
func (s *sSysLesson) View(ctx context.Context, in *sysin.LessonViewInp) (res *sysin.LessonViewModel, err error) {
|
||
if err = s.Model(ctx).WherePri(in.Id).Scan(&res); err != nil {
|
||
err = gerror.Wrap(err, "获取课程管理信息,请稍后重试!")
|
||
return
|
||
}
|
||
return
|
||
}
|
||
|
||
// Option 获取课程管理选项
|
||
func (s *sSysLesson) Option(ctx context.Context) (opts []*model.Option, err error) {
|
||
var models []*entity.Lesson
|
||
if err = s.Model(ctx).Fields(dao.Lesson.Columns().Id, dao.Lesson.Columns().Name).
|
||
OrderDesc(dao.Lesson.Columns().Id).Scan(&models); err != nil {
|
||
err = gerror.Wrap(err, "获取课程管理选项失败!")
|
||
return
|
||
}
|
||
|
||
opts = make([]*model.Option, len(models))
|
||
for k, v := range models {
|
||
opts[k] = dict.GenHashOption(v.Id, gconv.String(v.Name))
|
||
}
|
||
return
|
||
} |