From 73b6baf6af3d88cdcb0e2df7932a9bd96b0b85c5 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 01 七月 2024 22:32:34 +0800
Subject: [PATCH] 月度统计出入库按类型汇总报表定时任务和手动跑任务接口

---
 controllers/location_product.go |   94 +++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 82 insertions(+), 12 deletions(-)

diff --git a/controllers/location_product.go b/controllers/location_product.go
index 59cf228..6089724 100644
--- a/controllers/location_product.go
+++ b/controllers/location_product.go
@@ -1,8 +1,11 @@
 package controllers
 
 import (
+	"errors"
 	"github.com/gin-gonic/gin"
+	"gorm.io/gorm"
 	"strconv"
+	"wms/constvar"
 	"wms/extend/code"
 	"wms/extend/util"
 	"wms/models"
@@ -40,14 +43,44 @@
 		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浣嶇疆")
 		return
 	}
-	if params.ProductId == "" {
-		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧")
+
+	if params.RuleType != constvar.RuleType_Product && params.RuleType != constvar.RuleType_ProductCategory {
+		util.ResponseFormat(c, code.RequestParamError, "ruleType寮傚父")
 		return
 	}
-	if params.ProductCategoryID == 0 {
-		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧绫诲埆")
-		return
+
+	if params.RuleType == constvar.RuleType_Product {
+		if params.ProductId == "" {
+			util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧")
+			return
+		}
+		if _, err := models.NewLocationProductSearch().SetProductId(params.ProductId).SetLocationId(params.LocationId).SetAreaId(params.AreaId).First(); err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+		} else {
+			util.ResponseFormat(c, code.RequestParamError, "宸插瓨鍦ㄨ涓婂瑙勫垯锛岃鍕块噸澶嶆坊鍔�")
+			return
+		}
 	}
+
+	if params.RuleType == constvar.RuleType_ProductCategory {
+		if params.ProductCategoryID == 0 {
+			util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧绫诲埆")
+			return
+		}
+		if _, err := models.NewLocationProductSearch().SetProductCategoryId(params.ProductCategoryID).SetLocationId(params.LocationId).SetAreaId(params.AreaId).First(); err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+		} else {
+			util.ResponseFormat(c, code.RequestParamError, "宸插瓨鍦ㄨ涓婂瑙勫垯锛岃鍕块噸澶嶆坊鍔�")
+			return
+		}
+	}
+
 	if err := models.NewLocationProductSearch().Create(&params); err != nil {
 		logx.Errorf("Operation create err: %v", err)
 		util.ResponseFormat(c, code.SaveFail, "娣诲姞澶辫触锛�"+err.Error())
@@ -64,7 +97,7 @@
 // @Success   200 {object} util.Response	"鎴愬姛"
 // @Router    /api-wms/v1/locationProduct/list [post]
 func (slf LocationProductController) List(c *gin.Context) {
-	var params request.PageInfo
+	var params request.QueryLocationProductConditon
 	if err := c.BindJSON(&params); err != nil {
 		util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�:"+err.Error())
 		return
@@ -76,6 +109,14 @@
 
 	search := models.NewLocationProductSearch()
 	search.SetPage(params.Page, params.PageSize)
+
+	if params.ProductId != "" {
+		search = search.SetProductId(params.ProductId)
+	}
+
+	if params.ProductCategoryId != 0 {
+		search = search.SetProductCategoryId(params.ProductCategoryId)
+	}
 	list, total, err := search.SetPreload(true).SetOrder("created_at desc").FindByPage()
 	if err != nil {
 		util.ResponseFormat(c, code.RequestError, "鏌ユ壘澶辫触:"+err.Error())
@@ -115,14 +156,44 @@
 		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浣嶇疆")
 		return
 	}
-	if params.ProductId == "" {
-		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧")
+
+	if params.RuleType != constvar.RuleType_Product && params.RuleType != constvar.RuleType_ProductCategory {
+		util.ResponseFormat(c, code.RequestParamError, "ruleType寮傚父")
 		return
 	}
-	if params.ProductCategoryID == 0 {
-		util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧绫诲埆")
-		return
+
+	if params.RuleType == constvar.RuleType_Product {
+		if params.ProductId == "" {
+			util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧")
+			return
+		}
+		if rule, err := models.NewLocationProductSearch().SetProductId(params.ProductId).SetLocationId(params.LocationId).SetAreaId(params.AreaId).First(); err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+		} else if params.Id != rule.Id {
+			util.ResponseFormat(c, code.RequestParamError, "宸插瓨鍦ㄨ涓婂瑙勫垯锛岃鍕块噸澶嶆坊鍔�")
+			return
+		}
 	}
+
+	if params.RuleType == constvar.RuleType_ProductCategory {
+		if params.ProductCategoryID == 0 {
+			util.ResponseFormat(c, code.RequestParamError, "璇烽�夋嫨浜у搧绫诲埆")
+			return
+		}
+		if rule, err := models.NewLocationProductSearch().SetProductCategoryId(params.ProductCategoryID).SetLocationId(params.LocationId).SetAreaId(params.AreaId).First(); err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				util.ResponseFormat(c, code.RequestParamError, err.Error())
+				return
+			}
+		} else if params.Id != rule.Id {
+			util.ResponseFormat(c, code.RequestParamError, "宸插瓨鍦ㄨ涓婂瑙勫垯锛岃鍕块噸澶嶆坊鍔�")
+			return
+		}
+	}
+
 	if err := models.NewLocationProductSearch().SetID(params.Id).Update(&params); err != nil {
 		logx.Errorf("LocationProduct update err: %v", err)
 		util.ResponseFormat(c, code.SaveFail, "淇敼澶辫触锛�"+err.Error())
@@ -150,7 +221,6 @@
 		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())

--
Gitblit v1.8.0