添加category的模板以及相关的sql文件
This commit is contained in:
1 parent
598d5549c8
commit
fc259ba274
11 files changed
+1000
No files matched your search
@@ -0,0 +1,109 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/go-admin-team/go-admin-core/sdk/service"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-admin/app/jbxh/models"
|
||||
"go-admin/app/jbxh/service/dto"
|
||||
"go-admin/common/actions"
|
||||
cDto "go-admin/common/dto"
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
service.Service
|
||||
}
|
||||
|
||||
// GetPage 获取Category列表
|
||||
func (e *Category) GetPage(c *dto.CategoryGetPageReq, p *actions.DataPermission, list *[]models.Category, count *int64) error {
|
||||
var err error
|
||||
var data models.Category
|
||||
|
||||
err = e.Orm.Model(&data).
|
||||
Scopes(
|
||||
cDto.MakeCondition(c.GetNeedSearch()),
|
||||
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
|
||||
actions.Permission(data.TableName(), p),
|
||||
).
|
||||
Find(list).Limit(-1).Offset(-1).
|
||||
Count(count).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("CategoryService GetPage error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get 获取Category对象
|
||||
func (e *Category) Get(d *dto.CategoryGetReq, p *actions.DataPermission, model *models.Category) error {
|
||||
var data models.Category
|
||||
|
||||
err := e.Orm.Model(&data).
|
||||
Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).
|
||||
First(model, d.GetId()).Error
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = errors.New("查看对象不存在或无权查看")
|
||||
e.Log.Errorf("Service GetCategory error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
e.Log.Errorf("db error:%s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Insert 创建Category对象
|
||||
func (e *Category) Insert(c *dto.CategoryInsertReq) error {
|
||||
var err error
|
||||
var data models.Category
|
||||
c.Generate(&data)
|
||||
err = e.Orm.Create(&data).Error
|
||||
if err != nil {
|
||||
e.Log.Errorf("CategoryService Insert error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update 修改Category对象
|
||||
func (e *Category) Update(c *dto.CategoryUpdateReq, p *actions.DataPermission) error {
|
||||
var err error
|
||||
var data = models.Category{}
|
||||
e.Orm.Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).First(&data, c.GetId())
|
||||
c.Generate(&data)
|
||||
|
||||
db := e.Orm.Save(&data)
|
||||
if err = db.Error; err != nil {
|
||||
e.Log.Errorf("CategoryService Save error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
if db.RowsAffected == 0 {
|
||||
return errors.New("无权更新该数据")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove 删除Category
|
||||
func (e *Category) Remove(d *dto.CategoryDeleteReq, p *actions.DataPermission) error {
|
||||
var data models.Category
|
||||
|
||||
db := e.Orm.Model(&data).
|
||||
Scopes(
|
||||
actions.Permission(data.TableName(), p),
|
||||
).Delete(&data, d.GetId())
|
||||
if err := db.Error; err != nil {
|
||||
e.Log.Errorf("Service RemoveCategory error:%s \r\n", err)
|
||||
return err
|
||||
}
|
||||
if db.RowsAffected == 0 {
|
||||
return errors.New("无权删除该数据")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"go-admin/app/jbxh/models"
|
||||
"go-admin/common/dto"
|
||||
common "go-admin/common/models"
|
||||
)
|
||||
|
||||
type CategoryGetPageReq struct {
|
||||
dto.Pagination `search:"-"`
|
||||
CategoryOrder
|
||||
}
|
||||
|
||||
type CategoryOrder struct {
|
||||
Id string `form:"idOrder" search:"type:order;column:id;table:category"`
|
||||
Title string `form:"titleOrder" search:"type:order;column:title;table:category"`
|
||||
Slug string `form:"slugOrder" search:"type:order;column:slug;table:category"`
|
||||
Desc string `form:"descOrder" search:"type:order;column:desc;table:category"`
|
||||
Parent string `form:"parentOrder" search:"type:order;column:parent;table:category"`
|
||||
Order string `form:"orderOrder" search:"type:order;column:order;table:category"`
|
||||
Status string `form:"statusOrder" search:"type:order;column:status;table:category"`
|
||||
CreateBy string `form:"createByOrder" search:"type:order;column:create_by;table:category"`
|
||||
UpdateBy string `form:"updateByOrder" search:"type:order;column:update_by;table:category"`
|
||||
CreatedAt string `form:"createdAtOrder" search:"type:order;column:created_at;table:category"`
|
||||
UpdatedAt string `form:"updatedAtOrder" search:"type:order;column:updated_at;table:category"`
|
||||
DeletedAt string `form:"deletedAtOrder" search:"type:order;column:deleted_at;table:category"`
|
||||
}
|
||||
|
||||
func (m *CategoryGetPageReq) GetNeedSearch() interface{} {
|
||||
return *m
|
||||
}
|
||||
|
||||
type CategoryInsertReq struct {
|
||||
Id int `json:"-" comment:"编码"` // 编码
|
||||
Title string `json:"title" comment:"标题"`
|
||||
Slug string `json:"slug" comment:"id"`
|
||||
Desc string `json:"desc" comment:"描述"`
|
||||
Parent string `json:"parent" comment:"父"`
|
||||
Order string `json:"order" comment:"排序"`
|
||||
Status string `json:"status" comment:"状态"`
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *CategoryInsertReq) Generate(model *models.Category) {
|
||||
if s.Id == 0 {
|
||||
model.Model = common.Model{Id: s.Id}
|
||||
}
|
||||
model.Title = s.Title
|
||||
model.Slug = s.Slug
|
||||
model.Desc = s.Desc
|
||||
model.Parent = s.Parent
|
||||
model.Order = s.Order
|
||||
model.Status = s.Status
|
||||
model.CreateBy = s.CreateBy // 添加这而,需要记录是被谁创建的
|
||||
}
|
||||
|
||||
func (s *CategoryInsertReq) GetId() interface{} {
|
||||
return s.Id
|
||||
}
|
||||
|
||||
type CategoryUpdateReq struct {
|
||||
Id int `uri:"id" comment:"编码"` // 编码
|
||||
Title string `json:"title" comment:"标题"`
|
||||
Slug string `json:"slug" comment:"id"`
|
||||
Desc string `json:"desc" comment:"描述"`
|
||||
Parent string `json:"parent" comment:"父"`
|
||||
Order string `json:"order" comment:"排序"`
|
||||
Status string `json:"status" comment:"状态"`
|
||||
common.ControlBy
|
||||
}
|
||||
|
||||
func (s *CategoryUpdateReq) Generate(model *models.Category) {
|
||||
if s.Id == 0 {
|
||||
model.Model = common.Model{Id: s.Id}
|
||||
}
|
||||
model.Title = s.Title
|
||||
model.Slug = s.Slug
|
||||
model.Desc = s.Desc
|
||||
model.Parent = s.Parent
|
||||
model.Order = s.Order
|
||||
model.Status = s.Status
|
||||
model.UpdateBy = s.UpdateBy // 添加这而,需要记录是被谁更新的
|
||||
}
|
||||
|
||||
func (s *CategoryUpdateReq) GetId() interface{} {
|
||||
return s.Id
|
||||
}
|
||||
|
||||
// CategoryGetReq 功能获取请求参数
|
||||
type CategoryGetReq struct {
|
||||
Id int `uri:"id"`
|
||||
}
|
||||
|
||||
func (s *CategoryGetReq) GetId() interface{} {
|
||||
return s.Id
|
||||
}
|
||||
|
||||
// CategoryDeleteReq 功能删除请求参数
|
||||
type CategoryDeleteReq struct {
|
||||
Ids []int `json:"ids"`
|
||||
}
|
||||
|
||||
func (s *CategoryDeleteReq) GetId() interface{} {
|
||||
return s.Ids
|
||||
}
|
||||
Reference in new issue
Block a user