liujiandao
2023-09-20 2baa7bad0482613829e217997c44a51d8ec0ec66
controllers/operation.go
@@ -2,8 +2,10 @@
import (
   "errors"
   "fmt"
   "github.com/gin-gonic/gin"
   "github.com/spf13/cast"
   "gorm.io/gorm"
   "strconv"
   "wms/extend/code"
   "wms/extend/util"
   "wms/models"
@@ -26,11 +28,11 @@
   var reqParams request.AddOperation
   var params models.Operation
   if err := c.BindJSON(&reqParams); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误"+err.Error())
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   if err := structx.AssignTo(reqParams, params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "数据转换错误")
   if err := structx.AssignTo(reqParams, &params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "数据转换错误"+err.Error())
      return
   }
   if err := slf.CheckParams(params); err != nil {
@@ -85,7 +87,123 @@
         return errors.New("产品数量出错")
      }
   }
   fmt.Println(111111)
   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(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误:"+err.Error())
      return
   }
   if err := slf.CheckListParams(&params); 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, &params); 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(&params); 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, "删除成功")
}