hotgo/server/internal/logic/sys/dict_data.go
2022-12-11 20:48:18 +08:00

131 lines
2.8 KiB
Go

// Package sys
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2022 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package sys
import (
"context"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gtime"
"hotgo/internal/consts"
"hotgo/internal/dao"
"hotgo/internal/model/input/sysin"
"hotgo/internal/service"
)
type sSysDictData struct{}
func NewSysDictData() *sSysDictData {
return &sSysDictData{}
}
func init() {
service.RegisterSysDictData(NewSysDictData())
}
// Delete 删除
func (s *sSysDictData) Delete(ctx context.Context, in sysin.DictDataDeleteInp) error {
_, err := dao.SysDictData.Ctx(ctx).Where("id", in.Id).Delete()
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return err
}
return nil
}
// Edit 修改/新增
func (s *sSysDictData) Edit(ctx context.Context, in sysin.DictDataEditInp) (err error) {
if in.Label == "" {
err = gerror.New("字典标签不能为空")
return err
}
// 修改
in.UpdatedAt = gtime.Now()
if in.Id > 0 {
_, err = dao.SysDictData.Ctx(ctx).Where("id", in.Id).Data(in).Update()
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return err
}
return nil
}
// 新增
in.CreatedAt = gtime.Now()
if in.TypeID <= 0 {
err = gerror.New("字典类型不能为空")
return err
}
in.Type, err = dao.SysDictType.GetType(ctx, in.TypeID)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return err
}
if in.Type == "" {
return gerror.Wrap(err, "类型选择无效,请检查")
}
_, err = dao.SysDictData.Ctx(ctx).Data(in).Insert()
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return err
}
return nil
}
// List 获取列表
func (s *sSysDictData) List(ctx context.Context, in sysin.DictDataListInp) (list []*sysin.DictDataListModel, totalCount int64, err error) {
mod := dao.SysDictData.Ctx(ctx)
// 类型ID
if in.TypeID > 0 {
types, err := dao.SysDictType.GetTypes(ctx, in.TypeID)
if err != nil {
return list, totalCount, err
}
mod = mod.WhereIn("type", types)
}
if in.Type != "" {
mod = mod.Where("type", in.Type)
}
// 访问路径
if in.Label != "" {
mod = mod.WhereLike("label", "%"+in.Label+"%")
}
// 请求方式
if in.Status > 0 {
mod = mod.Where("status", in.Status)
}
totalCount, err = mod.Count()
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return list, totalCount, err
}
if totalCount == 0 {
return list, totalCount, nil
}
if err = mod.Page(int(in.Page), int(in.PerPage)).Order("id desc").Scan(&list); err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return list, totalCount, err
}
for _, v := range list {
v.TypeID, _ = dao.SysDictType.GetId(ctx, v.Type)
}
return list, totalCount, err
}