package controllers
|
|
import (
|
"github.com/gin-gonic/gin"
|
"github.com/shopspring/decimal"
|
"strconv"
|
"time"
|
"wms/extend/code"
|
"wms/extend/util"
|
"wms/models"
|
"wms/pkg/logx"
|
"wms/pkg/structx"
|
"wms/request"
|
)
|
|
type LocationProductController struct {
|
}
|
|
// Add
|
// @Tags 上架规则
|
// @Summary 添加上架规则
|
// @Produce application/json
|
// @Param object body request.AddLocationProduct true "新增上架规则"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/locationProduct/add [post]
|
func (slf LocationProductController) Add(c *gin.Context) {
|
var reqParams request.AddLocationProduct
|
var params models.LocationProduct
|
if err := c.BindJSON(&reqParams); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
|
return
|
}
|
if err := structx.AssignTo(reqParams, ¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "数据转换错误"+err.Error())
|
return
|
}
|
if params.AreaId == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "请选择区域")
|
return
|
}
|
if params.LocationId == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "请选择位置")
|
return
|
}
|
if params.ProductId == "" {
|
util.ResponseFormat(c, code.RequestParamError, "请选择产品")
|
return
|
}
|
//if params.ProductCategoryID == 0 {
|
// util.ResponseFormat(c, code.RequestParamError, "请选择产品类别")
|
// return
|
//}
|
|
if err := models.NewLocationProductSearch().Create(¶ms); err != nil {
|
logx.Errorf("Operation create err: %v", err)
|
util.ResponseFormat(c, code.SaveFail, "添加失败:"+err.Error())
|
return
|
}
|
locationProductAmount := &models.LocationProductAmount{
|
LocationProductId: params.Id,
|
Amount: decimal.NewFromFloat(0),
|
CreateDate: time.Now().Format("2006-01-02 15:04:05"),
|
}
|
if err := models.NewLocationProductAmountSearch().Create(locationProductAmount); err != nil {
|
logx.Errorf("Operation create err: %v", err)
|
util.ResponseFormat(c, code.SaveFail, "添加失败:"+err.Error())
|
return
|
}
|
util.ResponseFormat(c, code.Success, "添加成功")
|
}
|
|
// List
|
// @Tags 上架规则
|
// @Summary 上架规则列表
|
// @Produce application/json
|
// @Param object body request.PageInfo true "查询参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/locationProduct/list [post]
|
func (slf LocationProductController) List(c *gin.Context) {
|
var params request.PageInfo
|
if err := c.BindJSON(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
|
return
|
}
|
if !params.Check() {
|
util.ResponseFormat(c, code.RequestParamError, "参数异常")
|
return
|
}
|
|
search := models.NewLocationProductSearch()
|
search.SetPage(params.Page, params.PageSize)
|
list, total, err := search.SetPreload(true).SetOrder("created_at desc").FindByPage()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestError, "查找失败:"+err.Error())
|
return
|
}
|
|
util.ResponseFormatListWithPage(c, code.Success, list, int(total), params.Page, params.PageSize)
|
}
|
|
// Update
|
// @Tags 上架规则
|
// @Summary 修改上架规则
|
// @Produce application/json
|
// @Param object body request.UpdateLocationProduct true "修改参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/locationProduct/update [post]
|
func (slf LocationProductController) Update(c *gin.Context) {
|
var reqParams request.UpdateLocationProduct
|
var params models.LocationProduct
|
if err := c.BindJSON(&reqParams); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
|
return
|
}
|
if reqParams.Id == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "id为0")
|
return
|
}
|
if err := structx.AssignTo(reqParams, ¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "数据转换错误"+err.Error())
|
return
|
}
|
if params.AreaId == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "请选择区域")
|
return
|
}
|
if params.LocationId == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "请选择位置")
|
return
|
}
|
if params.ProductId == "" {
|
util.ResponseFormat(c, code.RequestParamError, "请选择产品")
|
return
|
}
|
if params.ProductCategoryID == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "请选择产品类别")
|
return
|
}
|
if err := models.NewLocationProductSearch().SetID(params.Id).Update(¶ms); err != nil {
|
logx.Errorf("LocationProduct update err: %v", err)
|
util.ResponseFormat(c, code.SaveFail, "修改失败:"+err.Error())
|
return
|
}
|
|
util.ResponseFormat(c, code.Success, "修改成功")
|
}
|
|
// Delete
|
//
|
// @Tags 上架规则
|
// @Summary 删除上架规则
|
// @Produce application/json
|
// @Param id path int true "id"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/locationProduct/delete/{id} [delete]
|
func (slf LocationProductController) Delete(c *gin.Context) {
|
id, err := strconv.Atoi(c.Param("id"))
|
if err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "错误的id值")
|
return
|
}
|
if id == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "id为0")
|
return
|
}
|
//TODO:此处可能需要增加限制,如果该上架规则如果已经产生了库存数量,删除会造成库存查不到的影响
|
if err := models.NewLocationProductSearch().SetID(id).Delete(); err != nil {
|
logx.Errorf("LocationProduct delete err: %v", err)
|
util.ResponseFormat(c, code.SaveFail, "删除失败:"+err.Error())
|
return
|
}
|
util.ResponseFormat(c, code.Success, "删除成功")
|
}
|