From cfb6fbce3687230ccb4704dbc0c87fd411b39af1 Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期三, 20 九月 2023 17:37:22 +0800
Subject: [PATCH] 仓库字段添加与功能修改

---
 controllers/operation.go |  131 ++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 127 insertions(+), 4 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index 18a26f7..581f085 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -3,6 +3,9 @@
 import (
 	"errors"
 	"github.com/gin-gonic/gin"
+	"github.com/spf13/cast"
+	"gorm.io/gorm"
+	"strconv"
 	"wms/extend/code"
 	"wms/extend/util"
 	"wms/models"
@@ -26,19 +29,22 @@
 	var params models.Operation
 	if err := c.BindJSON(&reqParams); err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�")
+		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 {
 		util.ResponseFormat(c, code.RequestParamError, err.Error())
+		return
 	}
 	if err := models.NewOperationSearch().Create(&params); 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 {
@@ -58,7 +64,7 @@
 		return errors.New("璇烽�夋嫨鐩爣浣嶇疆")
 	}
 
-	if params.OperationDate.IsZero() {
+	if params.OperationDate == "" {
 		return errors.New("璇烽�夋嫨瀹夋帓鏃ユ湡")
 	}
 
@@ -84,3 +90,120 @@
 
 	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, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�:"+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, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇細"+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, "閿欒鐨刬d鍊�")
+		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, "鍒犻櫎鎴愬姛")
+}

--
Gitblit v1.8.0