package controllers
|
|
import (
|
"errors"
|
"github.com/gin-gonic/gin"
|
"github.com/spf13/cast"
|
"gorm.io/gorm"
|
"strconv"
|
"wms/extend/code"
|
"wms/extend/util"
|
"wms/models"
|
"wms/pkg/logx"
|
"wms/pkg/structx"
|
"wms/request"
|
)
|
|
type OperationController struct {
|
}
|
|
// Add
|
// @Tags 入库/出库
|
// @Summary 添加入库/出库
|
// @Produce application/json
|
// @Param object body request.AddOperation true "入库/出库信息"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/operation/operation [post]
|
func (slf OperationController) Add(c *gin.Context) {
|
var reqParams request.AddOperation
|
var params models.Operation
|
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 err := slf.CheckParams(params); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err.Error())
|
return
|
}
|
if err := models.NewOperationSearch().Create(¶ms); err != nil {
|
logx.Errorf("Operation create err: %v", err)
|
util.ResponseFormat(c, code.SaveFail, "添加失败:"+err.Error())
|
return
|
}
|
util.ResponseFormat(c, code.Success, "添加成功")
|
}
|
|
func (slf OperationController) CheckParams(params models.Operation) error {
|
if params.SourceNumber == "" {
|
return errors.New("请填入源单号")
|
}
|
|
if params.OperationTypeId == 0 {
|
return errors.New("operationTypeId为0")
|
}
|
|
if params.FromLocationId == 0 {
|
return errors.New("请选择源位置")
|
}
|
|
if params.ToLocationId == 0 {
|
return errors.New("请选择目标位置")
|
}
|
|
if params.OperationDate == "" {
|
return errors.New("请选择安排日期")
|
}
|
|
if len(params.Details) <= 0 {
|
return errors.New("请添加明细信息")
|
}
|
|
//检查明细部分
|
for _, v := range params.Details {
|
if v.ProductId == 0 {
|
return errors.New("productID为0")
|
}
|
if v.ProductName == "" {
|
return errors.New("产品名称异常")
|
}
|
if v.Quantity.IsNegative() {
|
return errors.New("产品数量出错")
|
}
|
if v.FinishQuantity.IsNegative() {
|
return errors.New("产品数量出错")
|
}
|
}
|
|
return nil
|
}
|
|
// List
|
// @Tags 入库/出库
|
// @Summary 入库/出库列表
|
// @Produce application/json
|
// @Accept json
|
// @Param object query request.OperationList true "参数"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/operation/operation [get]
|
func (slf OperationController) List(c *gin.Context) {
|
var params request.OperationList
|
if err := c.ShouldBindQuery(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
|
return
|
}
|
if err := slf.CheckListParams(¶ms); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err.Error())
|
return
|
}
|
search := models.NewOperationSearch()
|
search.SetPage(params.Page, params.PageSize)
|
list, total, err := search.SetOperationTypeId(params.OperationTypeId).SetPreload(true).SetOrder("created_at desc").Find()
|
if err != nil {
|
util.ResponseFormat(c, code.RequestError, "查找失败:"+err.Error())
|
return
|
}
|
|
util.ResponseFormatList(c, code.Success, list, int(total))
|
|
}
|
|
func (slf OperationController) CheckListParams(params *request.OperationList) error {
|
if !params.PageInfo.Check() {
|
return errors.New("数据分页信息错误")
|
}
|
if params.OperationTypeId == 0 {
|
return errors.New("operationTypeId为0")
|
}
|
return nil
|
}
|
|
// Update
|
// @Tags 入库/出库
|
// @Summary 修改入库/出库信息
|
// @Produce application/json
|
// @Param object body request.UpdateOperation true "入库信息"
|
// @Param id path int true "入库信息id"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/operation/operation/{id} [put]
|
func (slf OperationController) Update(c *gin.Context) {
|
id := cast.ToUint(c.Param("id"))
|
if id == 0 {
|
util.ResponseFormat(c, code.RequestParamError, "空的记录id")
|
return
|
}
|
var reqParams request.UpdateOperation
|
var params models.Operation
|
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, "数据转换错误"+err.Error())
|
return
|
}
|
if err := slf.CheckParams(params); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, err.Error())
|
return
|
}
|
if err := models.WithTransaction(func(tx *gorm.DB) error {
|
if err := models.NewOperationDetailsSearch().SetOrm(tx).SetOperationId(params.Id).Delete(); err != nil {
|
return err
|
}
|
if err := models.NewOperationSearch().SetOrm(tx).SetID(params.Id).Save(¶ms); err != nil {
|
return err
|
}
|
return nil
|
}); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "修改失败:"+err.Error())
|
return
|
}
|
|
util.ResponseFormat(c, code.Success, "修改成功")
|
}
|
|
// DeleteDevice
|
//
|
// @Tags 入库/出库
|
// @Summary 删除入库/出库信息
|
// @Produce application/json
|
// @Param id path int true "id"
|
// @Success 200 {object} util.Response "成功"
|
// @Router /api-wms/v1/operation/operation/{id} [delete]
|
func (slf OperationController) 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")
|
return
|
}
|
if err := models.WithTransaction(func(tx *gorm.DB) error {
|
if err := models.NewOperationDetailsSearch().SetOrm(tx).SetOperationId(id).Delete(); err != nil {
|
return err
|
}
|
if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Delete(); err != nil {
|
return err
|
}
|
return nil
|
}); err != nil {
|
util.ResponseFormat(c, code.RequestParamError, "修改失败:"+err.Error())
|
return
|
}
|
util.ResponseFormat(c, code.Success, "删除成功")
|
}
|