编辑分类
This commit is contained in:
parent
872e028e0d
commit
c1758443ca
|
@ -12,7 +12,7 @@ import (
|
||||||
|
|
||||||
type CreateCategoryRequest struct {
|
type CreateCategoryRequest struct {
|
||||||
Name string `json:"name" validate:"required"` //名称
|
Name string `json:"name" validate:"required"` //名称
|
||||||
ParentId uint32 `json:"parent_id"` //父分类id
|
Pid uint32 `json:"pid"` //父分类id
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateCategory 创建分类
|
// CreateCategory 创建分类
|
||||||
|
@ -40,7 +40,7 @@ func CreateCategory(c *gin.Context) {
|
||||||
category := models.Category{
|
category := models.Category{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
Display: 1,
|
Display: 1,
|
||||||
Pid: req.ParentId,
|
Pid: req.Pid,
|
||||||
CooperativeBusinessId: middleware.GetCooperativeBusinessId(c),
|
CooperativeBusinessId: middleware.GetCooperativeBusinessId(c),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,3 +53,60 @@ func CreateCategory(c *gin.Context) {
|
||||||
|
|
||||||
app.OK(c, nil, "ok")
|
app.OK(c, nil, "ok")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateCategoryRequest struct {
|
||||||
|
CreateCategoryRequest
|
||||||
|
Id uint32 `json:"id" validate:"required"` //分类id
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateCategory 编辑分类
|
||||||
|
// @Summary 编辑分类
|
||||||
|
// @Tags 商品分类
|
||||||
|
// @Produce json
|
||||||
|
// @Accept json
|
||||||
|
// @Param request body UpdateCategoryRequest true "商品分类编辑模型"
|
||||||
|
// @Success 200 {object} app.Response
|
||||||
|
// @Router /api/v1/category/update [post]
|
||||||
|
func UpdateCategory(c *gin.Context) {
|
||||||
|
req := new(UpdateCategoryRequest)
|
||||||
|
|
||||||
|
if err := c.ShouldBindJSON(req); err != nil {
|
||||||
|
app.Error(c, 400, err, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := tools.Validate(req)
|
||||||
|
if err != nil {
|
||||||
|
app.Error(c, 400, err, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
old, err := models.GetCategoryById(req.Id)
|
||||||
|
if err != nil {
|
||||||
|
app.Error(c, 500, err, err.Error())
|
||||||
|
logger.Error("GetCategoryById err", logger.Field("req", req), logger.Field("err", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var category = models.Category{
|
||||||
|
Name: req.Name,
|
||||||
|
Pid: req.Pid,
|
||||||
|
}
|
||||||
|
if old.Pid != category.Pid {
|
||||||
|
category.Number, err = models.GenerateNumber(old.CooperativeBusinessId, req.Pid)
|
||||||
|
if err != nil {
|
||||||
|
app.Error(c, 500, err, err.Error())
|
||||||
|
logger.Error("GenerateNumber err", logger.Field("req", req), logger.Field("err", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = orm.Eloquent.Model(category).Updates(category).Error
|
||||||
|
if err != nil {
|
||||||
|
app.Error(c, 500, err, "更新失败")
|
||||||
|
logger.Error("GenerateNumber err", logger.Field("category", category), logger.Field("err", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
app.OK(c, nil, "ok")
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package models
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go-admin/app/admin/models/common"
|
"go-admin/app/admin/models/common"
|
||||||
|
orm "go-admin/common/global"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,39 +23,57 @@ func (c *Category) TableName() string {
|
||||||
|
|
||||||
func (c *Category) BeforeCreate(tx *gorm.DB) error {
|
func (c *Category) BeforeCreate(tx *gorm.DB) error {
|
||||||
if c.Number == "" {
|
if c.Number == "" {
|
||||||
var count int64
|
n, err := GenerateNumber(c.CooperativeBusinessId, c.Pid)
|
||||||
var err error
|
|
||||||
if c.Pid == 0 {
|
|
||||||
err = tx.Model(c).
|
|
||||||
Unscoped().
|
|
||||||
Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)).
|
|
||||||
Where("pid", "").
|
|
||||||
Count(&count).
|
|
||||||
Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.Number = fmt.Sprintf("%03d", count)
|
c.Number = n
|
||||||
} else {
|
|
||||||
var parent Category
|
|
||||||
err = tx.
|
|
||||||
Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)).
|
|
||||||
Where("id", c.Pid).
|
|
||||||
First(&parent).Error
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = tx.Model(c).
|
|
||||||
Unscoped().
|
|
||||||
Scopes(common.ScopeCooperativeBusiness(c.CooperativeBusinessId)).
|
|
||||||
Where("pid", c.Pid).
|
|
||||||
Count(&count).
|
|
||||||
Error
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c.Number = fmt.Sprintf("%s%03d", parent.Number, count)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateNumber 生成分类编码
|
||||||
|
func GenerateNumber(cid uint32, pid uint32) (string, error) {
|
||||||
|
m := orm.Eloquent.Model(Category{})
|
||||||
|
var count int64
|
||||||
|
var err error
|
||||||
|
if pid == 0 {
|
||||||
|
err = m.Scopes(common.ScopeBusiness(cid)).
|
||||||
|
Where("pid", 0).
|
||||||
|
Count(&count).
|
||||||
|
Error
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%03d", count), nil
|
||||||
|
} else {
|
||||||
|
var parent Category
|
||||||
|
err = m.
|
||||||
|
Scopes(common.ScopeBusiness(cid)).
|
||||||
|
Where("id", pid).
|
||||||
|
First(&parent).Error
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
err = m.
|
||||||
|
Unscoped().
|
||||||
|
Scopes(common.ScopeBusiness(cid)).
|
||||||
|
Where("pid", pid).
|
||||||
|
Count(&count).
|
||||||
|
Error
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s%03d", parent.Number, count), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCategoryById(id uint32) (*Category, error) {
|
||||||
|
var c *Category
|
||||||
|
err := orm.Eloquent.Model(c).Where("id", id).First(c).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package common
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import "gorm.io/gorm"
|
||||||
|
|
||||||
func ScopeCooperativeBusiness(id uint32) func(db *gorm.DB) *gorm.DB {
|
func ScopeBusiness(id uint32) func(db *gorm.DB) *gorm.DB {
|
||||||
return func(db *gorm.DB) *gorm.DB {
|
return func(db *gorm.DB) *gorm.DB {
|
||||||
return db.Where("cooperative_business_id = ?", id)
|
return db.Where("cooperative_business_id = ?", id)
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ func (s *Supplier) BeforeCreate(tx *gorm.DB) error {
|
||||||
var count int64
|
var count int64
|
||||||
err := tx.Table(s.TableName()).
|
err := tx.Table(s.TableName()).
|
||||||
Unscoped().
|
Unscoped().
|
||||||
Scopes(common.ScopeCooperativeBusiness(s.CooperativeBusinessId)).
|
Scopes(common.ScopeBusiness(s.CooperativeBusinessId)).
|
||||||
Count(&count).Error
|
Count(&count).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("get supplier count failed", logger.Field("err", err), logger.Field("s", s))
|
logger.Error("get supplier count failed", logger.Field("err", err), logger.Field("s", s))
|
||||||
|
@ -67,12 +67,9 @@ func GetSupplier(req GetSupplierRequest) ([]*Supplier, error) {
|
||||||
m = m.Where("number = ?", req.Number)
|
m = m.Where("number = ?", req.Number)
|
||||||
}
|
}
|
||||||
|
|
||||||
m = m.Scopes(common.ScopeCooperativeBusiness(req.CooperativeBusinessId))
|
m = m.Scopes(common.ScopeBusiness(req.CooperativeBusinessId))
|
||||||
|
|
||||||
err := m.
|
err := m.Order("created_at desc").Find(&list).Error
|
||||||
Order("created_at desc").
|
|
||||||
Find(&list).
|
|
||||||
Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("get supplier list err", logger.Field("err", err), logger.Field("req", req))
|
logger.Error("get supplier list err", logger.Field("err", err), logger.Field("req", req))
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue
Block a user