dsmzx
2024-06-14 c14c24cad0f8ec362cacc1368c115bb5b4de9744
属性信息(增删改查)操作
2个文件已添加
3个文件已修改
233 ■■■■■ 已修改文件
controllers/attribute.go 186 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/attribute_value.go 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/attribute.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
request/attribute.go 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/router.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
controllers/attribute.go
New file
@@ -0,0 +1,186 @@
package controllers
import (
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "gorm.io/gorm"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/structx"
    "wms/request"
)
type AttributeController struct{}
// Add
// @Tags      属性
// @Summary   添加属性
// @Produce   application/json
// @Param     object  body  request.AddAttribute true  "属性信息"
// @Param     Authorization    header string true "token"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attribute/attribute [post]
func (slf *AttributeController) Add(c *gin.Context) {
    var reqParams request.AddAttribute
    var params models.Attribute
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "数据转换错误")
        return
    }
    if err := slf.CheckAttribute(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err)
        return
    }
    err := models.WithTransaction(func(tx *gorm.DB) error {
        if err := models.NewAttributeSearch().BeforeCreate(tx); err != nil {
            return err
        }
        if err := models.NewAttributeSearch().Create(&params); err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "创建失败")
        return
    }
    util.ResponseFormat(c, code.Success, "创建成功")
}
func (slf AttributeController) CheckAttribute(params models.Attribute) error {
    if params.Name == "" {
        return errors.New("名称不能为空")
    }
    if params.DataType == 0 {
        return errors.New("产品不能为空")
    }
    if params.DataType == 3 && len(params.SelectValues) == 0 {
        return errors.New("下拉框属性值不能为空")
    }
    return nil
}
// Update
// @Tags      属性
// @Summary   编辑属性
// @Produce   application/json
// @Param     object  body request.UpdateAttribute true  "属性信息"
// @Param     id  path string true  "属性id"
// @Param     Authorization    header string true "token"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attribute/updateAttribute/{id} [put]
func (slf AttributeController) Update(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    var (
        reqParams request.UpdateAttribute
        params    models.Attribute
    )
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("参数解析失败: %v"+err.Error()))
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("数据转换错误: %v", err.Error()))
        return
    }
    params.ID = id
    if _, err := models.NewAttributeSearch().SetID(params.ID).First(); errors.Is(err, gorm.ErrRecordNotFound) {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("记录不存在: %v", err))
        return
    }
    err := models.WithTransaction(func(tx *gorm.DB) error {
        if err := models.NewAttributeSearch().BeforeUpdate(tx); err != nil {
            return err
        }
        if err := models.NewAttributeSearch().SetID(params.ID).Save(&params); err != nil {
            return err
        }
        return nil
    })
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "更新成功")
}
// Delete
// @Tags      属性
// @Summary   删除属性
// @Produce   application/json
// @Param     Authorization    header string true "token"
// @Param     id  path string true  "属性id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attribute/delete/{id} [delete]
func (slf AttributeController) Delete(c *gin.Context) {
    id := cast.ToUint(c.Param("id"))
    if id == 0 {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    if _, err := models.NewAttributeSearch().SetID(id).First(); errors.Is(err, gorm.ErrRecordNotFound) {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("记录不存在: %v", err))
        return
    }
    err := models.NewAttributeSearch().SetID(id).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "删除成功")
}
// List
// @Tags      属性
// @Summary   查询属性列表
// @Produce   application/json
// @Param     object  query    request.GetAttributeList true  "查询参数"
// @Param     Authorization    header string true "token"
// @Success   200   {object}  util.ResponseList{data=[]models.Attribute}  "成功"
// @Router    /api-wms/v1/attribute/attribute [get]
func (slf AttributeController) List(c *gin.Context) {
    var params request.GetAttributeList
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    list, count, err := models.NewAttributeSearch().SetPage(params.Page, params.PageSize).SetOrder("id desc").Find()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查询失败")
        return
    }
    util.ResponseFormatList(c, code.Success, list, cast.ToInt(count))
}
// GetAttributeDetail
// @Tags      属性
// @Summary   获取属性详情
// @Produce   application/json
// @Param     id  path string true  "属性id"
// @Success   200   {object}  util.ResponseList{data=models.Attribute}  "成功"
// @Router    /api-wms/v1/attribute/attribute/{id} [get]
//func (slf AttributeController) GetAttributeDetail(c *gin.Context) {
//    id := cast.ToUint(c.Param("id"))
//    if id == 0 {
//        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
//        return
//    }
//    first, err := models.NewAttributeSearch().SetID(id).First()
//    if err != nil {
//        util.ResponseFormat(c, code.RequestParamError, "获取属性信息失败")
//        return
//    }
//
//    util.ResponseFormat(c, code.UpdateSuccess, first)
//}
controllers/attribute_value.go
@@ -18,7 +18,7 @@
// @Tags      属性值和对象
// @Summary   添加属性值和对象
// @Produce   application/json
// @Param     object  body  request.AttributeValue true  "属性值和对象信息"
// @Param     object  body  request.AddAttributeValue true  "属性值和对象信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attributeValue/add [post]
func (slf *AttributeValueController) AddAttributeValue(c *gin.Context) {
@@ -55,7 +55,7 @@
// @Tags      属性值和对象
// @Summary   更新属性值和对象
// @Produce   application/json
// @Param     object  body  request.AttributeValue true  "属性值和对象信息"
// @Param     object  body  request.UpdateAttributeValue true  "属性值和对象信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attributeValue/update [post]
func (slf *AttributeValueController) UpdateAttributeValue(c *gin.Context) {
@@ -95,7 +95,7 @@
// DeleteAttributeValue
// @Tags      属性值和对象
// @Summary   删除属性值和对象
// @Param        id    path        unit            true    "id"
// @Param        id    path        string            true    "id"
// @Success   200 {object} util.Response "成功"
// @Router    /api-wms/v1/attributeValue/delete/{id} [delete]
func (slf *AttributeValueController) DeleteAttributeValue(c *gin.Context) {
models/attribute.go
@@ -57,6 +57,17 @@
    return nil
}
func (slf *Attribute) BeforeUpdate(tx *gorm.DB) (err error) {
    if len(slf.SelectValues) != 0 {
        bts, err := json.Marshal(slf.SelectValues)
        if err != nil {
            return err
        }
        slf.SelectValue = string(bts)
    }
    return nil
}
func (slf *Attribute) AfterFind(tx *gorm.DB) (err error) {
    if slf.SelectValue != "" {
        var list []string
request/attribute.go
New file
@@ -0,0 +1,19 @@
package request
type GetAttributeList struct {
    PageInfo
    Keyword string `json:"keyword"`
}
type AddAttribute struct {
    Name         string   `gorm:"type:varchar(100);not null;default:''" json:"name"` //属性名称
    DataType     int      `gorm:"type:tinyint;not null;default:0" json:"dataType"`   //值类型(1字符串 2 int 3 下拉框 )
    EntityType   int      `gorm:"type:tinyint;not null;default:0" json:"entityType"` //给谁用的 1 物料(产品)
    SelectValues []string `json:"selectValues" gorm:"_"`
    SelectValue  string   `json:"-" gorm:"type:text;"`
}
type UpdateAttribute struct {
    ID uint `gorm:"comment:主键ID;primaryKey;" json:"id"`
    AddAttribute
}
router/router.go
@@ -66,6 +66,17 @@
        locationAPI.GET("getLocationTreeList", locationController.GetLocationTreeList)   //获取位置列表树
    }
    // 属性信息
    attributeController := new(controllers.AttributeController)
    attributeAPI := r.Group(urlPrefix + "/attribute")
    {
        attributeAPI.POST("attribute", attributeController.Add)             // 添加属性
        attributeAPI.PUT("updateAttribute/:id", attributeController.Update) // 修改属性
        attributeAPI.DELETE("delete/:id", attributeController.Delete)       // 删除属性
        attributeAPI.GET("attribute", attributeController.List)             // 获取属性
        //attributeAPI.GET("attribute/:id", attributeController.GetAttributeDetail) // 获取属性
    }
    // 业务类型
    operationTypeController := new(controllers.OperationTypeController)
    operationTypeAPI := r.Group(urlPrefix + "/operationType")