From 28d2d010f0dda4c11c024d516287aeee6094548b Mon Sep 17 00:00:00 2001
From: wzy <wzy19931122ai@163.com>
Date: Sun, 13 Dec 2020 19:04:45 +0800
Subject: [PATCH] finish daily-list-new

---
 zq-erp/src/main/java/com/matrix/system/hive/service/imp/ShoppingGoodsServiceImpl.java |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/zq-erp/src/main/java/com/matrix/system/hive/service/imp/ShoppingGoodsServiceImpl.java b/zq-erp/src/main/java/com/matrix/system/hive/service/imp/ShoppingGoodsServiceImpl.java
index c89bc78..bcc6590 100644
--- a/zq-erp/src/main/java/com/matrix/system/hive/service/imp/ShoppingGoodsServiceImpl.java
+++ b/zq-erp/src/main/java/com/matrix/system/hive/service/imp/ShoppingGoodsServiceImpl.java
@@ -26,6 +26,7 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.validation.constraints.NotNull;
 import java.awt.event.WindowStateListener;
 import java.time.LocalDate;
 import java.util.ArrayList;
@@ -421,4 +422,49 @@
 
         return shoppingGoodsDao.selectByModelData(shoppingGoods);
     }
+
+    /**
+     * 计算失效时间
+     *
+     * @param shoppingGoods
+     * @param type 1 - 购买时  2 - 消耗时
+     * @param buyDate 购买日期, 当计算消耗日期时,不能为空
+     * @return
+     */
+    @Override
+    public Date calInvalidTime(ShoppingGoods shoppingGoods, @NotNull Integer type, Date buyDate) {
+        if (StringUtils.isBlank(shoppingGoods.getBuyValid()) && StringUtils.isBlank(shoppingGoods.getUseValid()) && shoppingGoods.getInvalidTime() == null) {
+            return null;
+        }
+
+        if (type == 2 && buyDate == null) {
+            throw new GlobleException("购买日期不能为空");
+        }
+
+        Date target = null;
+        Date buyValidDate = null;
+        if (type == 1) {
+            buyValidDate = DateUtil.calDate(shoppingGoods.getBuyDateNum(), shoppingGoods.getBuyDateUnit());
+        } else {
+            buyValidDate = buyDate;
+        }
+
+        Date useValidDate = DateUtil.stringToDate("9999-12-31", DateUtil.DATE_FORMAT_DD);
+        if (type == 2) {
+            useValidDate = DateUtil.calDate(shoppingGoods.getUseDateNum(), shoppingGoods.getUseDateUnit());
+        }
+
+        if (buyValidDate.after(useValidDate)) {
+            target = useValidDate;
+        } else {
+            target = buyValidDate;
+        }
+
+        if (shoppingGoods.getInvalidTime() != null) {
+            if (target.after(shoppingGoods.getInvalidTime())) {
+                target = shoppingGoods.getInvalidTime();
+            }
+        }
+        return target;
+    }
 }

--
Gitblit v1.9.1