New file |
| | |
| | | package controllers |
| | | |
| | | import ( |
| | | "github.com/gin-gonic/gin" |
| | | "strconv" |
| | | "wms/extend/code" |
| | | "wms/extend/util" |
| | | "wms/models" |
| | | "wms/pkg/logx" |
| | | "wms/pkg/structx" |
| | | "wms/request" |
| | | ) |
| | | |
| | | type AttributeValueController struct { |
| | | } |
| | | |
| | | // AddAttributeValue |
| | | // @Tags 属性值和对象 |
| | | // @Summary 添加属性值和对象 |
| | | // @Produce application/json |
| | | // @Param object body request.AttributeValue true "属性值和对象信息" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/attributeValue/add [post] |
| | | func (slf *AttributeValueController) AddAttributeValue(c *gin.Context) { |
| | | var params request.AddAttributeValue |
| | | var attributeValue models.AttributeValue |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | if err := structx.AssignTo(params, &attributeValue); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "数据转换错误"+err.Error()) |
| | | return |
| | | } |
| | | attributeValueSearch := models.NewAttributeValueSearch() |
| | | attributeValues, err := attributeValueSearch.FindByQueryNotTotal("entity_id = ? AND attribute_id = ?", []interface{}{attributeValue.EntityID, attributeValue.AttributeID}) |
| | | if len(attributeValues) > 0 { |
| | | util.ResponseFormat(c, code.SaveFail, "添加失败:属性值和对象已存在") |
| | | return |
| | | } |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.SaveFail, "添加失败:"+err.Error()) |
| | | return |
| | | } |
| | | err = attributeValueSearch.Create(&attributeValue) |
| | | if err != nil { |
| | | logx.Errorf("AttributeValue create err: %v", err) |
| | | util.ResponseFormat(c, code.SaveFail, "添加失败:"+err.Error()) |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "添加成功") |
| | | } |
| | | |
| | | // UpdateAttributeValue |
| | | // @Tags 属性值和对象 |
| | | // @Summary 更新属性值和对象 |
| | | // @Produce application/json |
| | | // @Param object body request.AttributeValue true "属性值和对象信息" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/attributeValue/update [post] |
| | | func (slf *AttributeValueController) UpdateAttributeValue(c *gin.Context) { |
| | | var params request.UpdateAttributeValue |
| | | if err := c.BindJSON(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | |
| | | attributeValueSearch := models.NewAttributeValueSearch() |
| | | attributeValue, err := attributeValueSearch.SetID(params.ID).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "属性值和对象不存在") |
| | | return |
| | | } |
| | | attributeValues, err := attributeValueSearch.FindByQueryNotTotal("entity_id = ? AND attribute_id = ? AND id != ?", []interface{}{params.EntityID, params.AttributeID, params.ID}) |
| | | if len(attributeValues) > 0 { |
| | | util.ResponseFormat(c, code.SaveFail, "修改失败:属性值和对象已存在") |
| | | return |
| | | } |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.SaveFail, "修改失败:"+err.Error()) |
| | | return |
| | | } |
| | | attributeValue.EntityID = params.EntityID |
| | | attributeValue.AttributeID = params.AttributeID |
| | | attributeValue.Value = params.Value |
| | | err = attributeValueSearch.Save(attributeValue) |
| | | if err != nil { |
| | | logx.Errorf("AttributeValue update err: %v", err) |
| | | util.ResponseFormat(c, code.SaveFail, "修改失败:"+err.Error()) |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "修改成功") |
| | | } |
| | | |
| | | // DeleteAttributeValue |
| | | // @Tags 属性值和对象 |
| | | // @Summary 删除属性值和对象 |
| | | // @Produce application/json |
| | | // @Param id path unit true "id" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/attributeValue/delete/{id} [delete] |
| | | func (slf *AttributeValueController) DeleteAttributeValue(c *gin.Context) { |
| | | id, _ := strconv.ParseUint(c.Param("id"), 10, 64) |
| | | if id == 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "无效id") |
| | | return |
| | | } |
| | | err := models.NewAttributeValueSearch().SetID(uint(id)).Delete() |
| | | if err != nil { |
| | | logx.Errorf("AttributeValue delete err: %v", err) |
| | | util.ResponseFormat(c, code.RequestParamError, "删除失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "删除成功") |
| | | } |