New file |
| | |
| | | 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, ¶ms); 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(¶ms); 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, ¶ms); 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(¶ms); 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(¶ms); 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) |
| | | //} |