Files
easygoadmin/app/service/config.go
yaoyilin 1b36bd8fbe feat: 初始化项目
初始化项目
2022-10-31 22:29:16 +08:00

101 lines
2.0 KiB
Go

/**
* 配置-服务类
* @author
* @since 2021/11/13
* @File : config
*/
package service
import (
"easygoadmin/app/dto"
"easygoadmin/app/model"
"easygoadmin/utils"
"easygoadmin/utils/gconv"
"errors"
"strconv"
"strings"
"time"
)
var Config = new(configService)
type configService struct{}
func (s *configService) GetList() []model.Config {
// 创建查询对象
query := utils.XormDb.
Where("mark=1").
OrderBy("sort asc")
// 对象转换
var list []model.Config
query.FindAndCount(&list)
return list
}
func (s *configService) Add(req *dto.ConfigAddReq, userId int) (int64, error) {
if utils.AppDebug() {
return 0, errors.New("演示环境,暂无权限操作")
}
// 实例化对象
var entity model.Config
entity.Name = req.Name
entity.Sort = req.Sort
entity.CreateUser = userId
entity.CreateTime = time.Now().Unix()
entity.Mark = 1
// 插入记录
return entity.Insert()
}
func (s *configService) Update(req *dto.ConfigUpdateReq, userId int) (int64, error) {
if utils.AppDebug() {
return 0, errors.New("演示环境,暂无权限操作")
}
// 查询记录
entity := &model.Config{Id: req.Id}
has, err := entity.Get()
if err != nil || !has {
return 0, err
}
// 设置对象
entity.Name = req.Name
entity.Sort = req.Sort
entity.UpdateUser = userId
entity.UpdateTime = time.Now().Unix()
// 更新数据
return entity.Update()
}
func (s *configService) Delete(ids string) (int64, error) {
if utils.AppDebug() {
return 0, errors.New("演示环境,暂无权限操作")
}
// 记录ID
idsArr := strings.Split(ids, ",")
if len(idsArr) == 1 {
// 单个删除
entity := &model.Config{Id: gconv.Int(ids)}
rows, err := entity.Delete()
if err != nil || rows == 0 {
return 0, errors.New("删除失败")
}
return rows, nil
} else {
// 批量删除
count := 0
for _, v := range idsArr {
id, _ := strconv.Atoi(v)
entity := &model.Config{Id: id}
rows, err := entity.Delete()
if rows == 0 || err != nil {
continue
}
count++
}
return int64(count), nil
}
}