From cfc9b0e38d630f15f570a7a366ee950030d39fce Mon Sep 17 00:00:00 2001
From: liujiandao <274878379@qq.com>
Date: 星期四, 21 九月 2023 09:52:05 +0800
Subject: [PATCH] swagger同步

---
 controllers/operation.go |  262 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 245 insertions(+), 17 deletions(-)

diff --git a/controllers/operation.go b/controllers/operation.go
index 9e1dc72..06b3c19 100644
--- a/controllers/operation.go
+++ b/controllers/operation.go
@@ -2,8 +2,11 @@
 
 import (
 	"errors"
-	"fmt"
 	"github.com/gin-gonic/gin"
+	"github.com/spf13/cast"
+	"gorm.io/gorm"
+	"strconv"
+	"wms/constvar"
 	"wms/extend/code"
 	"wms/extend/util"
 	"wms/models"
@@ -27,19 +30,64 @@
 	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 := slf.FormatLocation(&params); err != nil {
+		util.ResponseFormat(c, code.RequestParamError, err.Error())
+		return
+	}
+
+	params.Status = constvar.OperationStatus_Ready
 	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) FormatLocation(params *models.Operation) error {
+	operationType, err := models.NewOperationTypeSearch().SetID(uint(params.OperationTypeId)).First()
+	if err != nil {
+		return err
+	}
+	if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
+		if location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeVendor)).First(); err != nil {
+			return err
+		} else {
+			params.FromLocationId = location.Id
+		}
+		if params.ToLocationId == 0 {
+			return errors.New("璇烽�夋嫨鐩爣浣嶇疆")
+		}
+	}
+	if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing {
+		if location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeCustomer)).First(); err != nil {
+			return err
+		} else {
+			params.ToLocationId = location.Id
+		}
+		if params.FromLocationId == 0 {
+			return errors.New("璇烽�夋嫨婧愪綅缃�")
+		}
+	}
+	if operationType.BaseOperationType == constvar.BaseOperationTypeInternal {
+		if params.ToLocationId == 0 {
+			return errors.New("璇烽�夋嫨鐩爣浣嶇疆")
+		}
+		if params.FromLocationId == 0 {
+			return errors.New("璇烽�夋嫨婧愪綅缃�")
+		}
+	}
+	return nil
 }
 
 func (slf OperationController) CheckParams(params models.Operation) error {
@@ -51,15 +99,7 @@
 		return errors.New("operationTypeId涓�0")
 	}
 
-	if params.FromLocationId == 0 {
-		return errors.New("璇烽�夋嫨婧愪綅缃�")
-	}
-
-	if params.ToLocationId == 0 {
-		return errors.New("璇烽�夋嫨鐩爣浣嶇疆")
-	}
-
-	if params.OperationDate.IsZero() {
+	if params.OperationDate == "" {
 		return errors.New("璇烽�夋嫨瀹夋帓鏃ユ湡")
 	}
 
@@ -69,7 +109,7 @@
 
 	//妫�鏌ユ槑缁嗛儴鍒�
 	for _, v := range params.Details {
-		if v.ProductId == 0 {
+		if v.ProductId == "" {
 			return errors.New("productID涓�0")
 		}
 		if v.ProductName == "" {
@@ -78,11 +118,199 @@
 		if v.Quantity.IsNegative() {
 			return errors.New("浜у搧鏁伴噺鍑洪敊")
 		}
-		if v.FinishQuantity.IsNegative() {
-			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, "鍙傛暟瑙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 reqParams.Status != constvar.OperationStatus_Ready {
+		util.ResponseFormat(c, code.RequestParamError, "璇ュ叆搴撲俊鎭凡瀹屾垚锛屾棤娉曡繘琛屼慨鏀�")
+		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 := slf.FormatLocation(&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涓�0")
+		return
+	}
+	operation, err := models.NewOperationSearch().SetID(id).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏈壘鍒扮浉鍏冲嚭鍏ュ簱淇℃伅:"+err.Error())
+		return
+	}
+	if operation.Status != constvar.OperationStatus_Ready {
+		util.ResponseFormat(c, code.RequestError, "璇ュ叆搴撲俊鎭棤娉曡繘琛屽垹闄�")
+		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, "鍒犻櫎鎴愬姛")
+}
+
+// DeleteDevice
+//
+//	@Tags		鍏ュ簱/鍑哄簱
+//	@Summary	鏇存敼璁板綍鐘舵��
+//	@Produce	application/json
+//	@Param		id	path		int			true	"id"
+//	@Success	200	{object}	util.Response	"鎴愬姛"
+//	@Router		/api-wms/v1/operation/finish/{id} [put]
+func (slf OperationController) Finish(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涓�0")
+		return
+	}
+	operation, err := models.NewOperationSearch().SetID(id).First()
+	if err != nil {
+		util.ResponseFormat(c, code.RequestParamError, "鏈壘鍒扮浉鍏冲嚭鍏ュ簱淇℃伅:"+err.Error())
+		return
+	}
+	if operation.Status != constvar.OperationStatus_Ready {
+		util.ResponseFormat(c, code.RequestError, "璇ュ嚭鍏ュ簱淇℃伅鏃犳硶瀹屾垚")
+		return
+	}
+	//operationType, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).First()
+	//if err != nil {
+	//	util.ResponseFormat(c, code.RequestError, err.Error())
+	//	return
+	//}
+	//if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
+	//	if location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeVendor)).First(); err != nil {
+	//		return err
+	//	} else {
+	//		params.FromLocationId = location.Id
+	//	}
+	//	if params.ToLocationId == 0 {
+	//		return errors.New("璇烽�夋嫨鐩爣浣嶇疆")
+	//	}
+	//}
+	if err := models.WithTransaction(func(tx *gorm.DB) error {
+
+		if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Update(&models.Operation{Status: constvar.OperationStatus_Finish}); err != nil {
+			return err
+		}
+		if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount + wms_operation_details.quantity where wms_operation.id=?", id).Error; err != nil {
+			return err
+		}
+		return nil
+	}); err != nil {
+		util.ResponseFormat(c, code.RequestError, err.Error())
+		return
+	}
+	util.ResponseFormat(c, code.Success, "鎿嶄綔鎴愬姛")
+}

--
Gitblit v1.8.0