From 660fba5b40303dd661afcc2e2bd54a18d4f53c5c Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Wed, 09 Dec 2020 19:14:44 +0800
Subject: [PATCH] add invalid time function

---
 zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java                              |  100 +++++++++++++------
 zq-erp/src/main/java/com/matrix/system/hive/bean/ShoppingGoods.java                   |  102 ++++++++++++++++++++
 zq-erp/src/main/java/com/matrix/system/hive/service/imp/ShoppingGoodsServiceImpl.java |   25 ++++
 zq-erp/src/main/resources/mybatis/mapper/hive/ShoppingGoodsDao.xml                    |   19 +++
 zq-erp/src/main/resources/templates/views/admin/hive/products/goods-form.html         |   40 +++++++
 5 files changed, 248 insertions(+), 38 deletions(-)

diff --git a/zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java b/zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java
index bb10294..4fdaef7 100644
--- a/zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java
+++ b/zq-erp/src/main/java/com/matrix/core/tools/DateUtil.java
@@ -5,6 +5,7 @@
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
@@ -12,7 +13,7 @@
 
 /**
  * 日期格式化,转换工具类
- * 
+ *
  * @author Ron
  * @createTime 2014.08.30
  */
@@ -90,7 +91,7 @@
 
 	/**
 	 * 返回年份
-	 * 
+	 *
 	 * @param date
 	 *            日期
 	 * @return 返回年份
@@ -137,7 +138,7 @@
 	}
 	/**
 	 * 返回月份
-	 * 
+	 *
 	 * @param date
 	 *            日期
 	 * @return 返回月份
@@ -149,14 +150,14 @@
 			c.setTime(date);
 			return c.get(Calendar.MONTH) + 1;
 		} catch (Exception e) {
-			
+
 		}
 		return 0;
 	}
 
 	/**
 	 * 日期转字符串
-	 * 
+	 *
 	 * @param date
 	 * @param format
 	 * @return
@@ -172,7 +173,7 @@
 
 	/**
 	 * 字符串转日期
-	 * 
+	 *
 	 * @param dateStr
 	 * @param format
 	 * @return
@@ -183,7 +184,7 @@
 		try {
 			return fmt.parse(dateStr);
 		} catch (ParseException e) {
-			
+
 		}
 		return null;
 	}
@@ -199,7 +200,7 @@
 
 	/**
 	 * 判断给定的日期是一周中的第几天,注意:按照中国的习惯,周日是第七天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -225,7 +226,7 @@
 
 	/**
 	 * 指定时间的下一天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -241,7 +242,7 @@
 			cal.add(Calendar.DATE, 1);
 			return cal.getTime();
 		} catch (Exception e) {
-			
+
 		}
 
 		return null;
@@ -249,7 +250,7 @@
 
 	/**
 	 * 指定时间的前一天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -265,7 +266,7 @@
 			cal.add(Calendar.DATE, -1);
 			return cal.getTime();
 		} catch (Exception e) {
-			
+
 		}
 
 		return null;
@@ -273,7 +274,7 @@
 
 	/**
 	 * 指定时间的下N天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -289,7 +290,7 @@
 			cal.add(Calendar.DATE, nDay);
 			return cal.getTime();
 		} catch (Exception e) {
-			
+
 		}
 
 		return null;
@@ -297,7 +298,7 @@
 
 	/**
 	 * 指定时间的前N天
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -313,7 +314,7 @@
 			cal.add(Calendar.DATE, -nDay);
 			return cal.getTime();
 		} catch (Exception e) {
-			
+
 		}
 
 		return null;
@@ -321,7 +322,7 @@
 
 	/**
 	 * 获取一天的起始时间
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -338,7 +339,7 @@
 		try {
 			return fmt.parse(dateStr);
 		} catch (ParseException e) {
-			
+
 		}
 
 		return date;
@@ -346,7 +347,7 @@
 
 	/**
 	 * 获取一天的结束时间
-	 * 
+	 *
 	 * @param date
 	 * @return
 	 */
@@ -363,7 +364,7 @@
 		try {
 			return fmt.parse(dateStr);
 		} catch (ParseException e) {
-			
+
 		}
 
 		return date;
@@ -371,7 +372,7 @@
 
 	/**
 	 * currentDat是否在referenceDate日期之前
-	 * 
+	 *
 	 * @param referenceDate
 	 * @param currentDat
 	 * @return
@@ -389,7 +390,7 @@
 
 	/**
 	 * currentDat是否在referenceDate日期之后
-	 * 
+	 *
 	 * @param referenceDate
 	 * @param currentDat
 	 * @return
@@ -436,7 +437,7 @@
 	}
 	/**
 	 * 判断currentDate是否在startDate和endDate之间,不包括startDate和endDate
-	 * 
+	 *
 	 * @param startDate
 	 * @param endDate
 	 * @param currentDate
@@ -454,11 +455,11 @@
 		return false;
 	}
 
-	 
+
 
 	/**
 	 * 获取startDate到endDate之间的星期day(中文星期)不包括startDate和endDate
-	 * 
+	 *
 	 * @param startDate
 	 * @param endDate
 	 * @param day
@@ -483,11 +484,11 @@
 		return listDate;
 	}
 
-	 
+
 
 	/**
 	 * date转换成Timestamp
-	 * 
+	 *
 	 * @param date
 	 * @param format
 	 * @return
@@ -507,7 +508,7 @@
 
 	/**
 	 * 获取早中晚
-	 * 
+	 *
 	 * @param time
 	 * @return
 	 */
@@ -530,7 +531,7 @@
 
 	/**
 	 * 获取早中晚的开始时间
-	 * 
+	 *
 	 * @param date
 	 * @param time
 	 * @return
@@ -554,7 +555,7 @@
 
 	/**
 	 * 获取早中晚的结束时间
-	 * 
+	 *
 	 * @param date
 	 * @param time
 	 * @return
@@ -578,7 +579,7 @@
 
 	/**
 	 * 得到几天前的时间
-	 * 
+	 *
 	 * @param d
 	 * @param day
 	 * @return
@@ -592,7 +593,7 @@
 
 	/**
 	 * 得到几天后的时间
-	 * 
+	 *
 	 * @param d
 	 * @param day
 	 * @return
@@ -606,7 +607,7 @@
 
 	/**
 	 * 将日期类型格式化成字符串
-	 * 
+	 *
 	 * @param date
 	 * @return 格式化后日期字符串
 	 * @throws ParseException
@@ -622,7 +623,7 @@
 
 	/**
 	 * 获取时间戳
-	 * 
+	 *
 	 * @return
 	 */
 	public static String getTimeMark() {
@@ -694,4 +695,35 @@
 		}
 		return um;
 	}
+
+	/**
+	 * 根据单位计算目标日期
+	 *
+	 * @param num 距离
+	 * @param unit 日期单位 Y/M/D
+	 * @return
+	 */
+	public static Date calDate(Integer num, String unit) {
+	    Calendar calendar = Calendar.getInstance();
+	    Date targetDate = null;
+	    switch (unit) {
+            case "Y":
+                calendar.add(Calendar.YEAR, num);
+                targetDate = calendar.getTime();
+                break;
+            case "M":
+                calendar.add(Calendar.MONTH, num);
+                targetDate = calendar.getTime();
+                break;
+            case "D":
+                calendar.add(Calendar.DAY_OF_MONTH, num);
+                targetDate = calendar.getTime();
+                break;
+            default:
+                targetDate = new Date();
+
+        }
+
+		return targetDate;
+	}
 }
diff --git a/zq-erp/src/main/java/com/matrix/system/hive/bean/ShoppingGoods.java b/zq-erp/src/main/java/com/matrix/system/hive/bean/ShoppingGoods.java
index e75be3b..32be44f 100644
--- a/zq-erp/src/main/java/com/matrix/system/hive/bean/ShoppingGoods.java
+++ b/zq-erp/src/main/java/com/matrix/system/hive/bean/ShoppingGoods.java
@@ -2,6 +2,7 @@
 
 import com.matrix.core.anotations.Extend;
 import com.matrix.core.tools.DateUtil;
+import com.matrix.core.tools.StringUtils;
 import org.springframework.format.annotation.DateTimeFormat;
 
 import java.io.Serializable;
@@ -393,7 +394,108 @@
      */
     private Integer isDel;
 
+    /**
+     * 购买有效期
+     */
+    private String buyValid;
 
+    /**
+     * 购买有效期单位 - 扩展字段用于参数接收  y/m/d
+     */
+    private String buyDateUnit;
+
+    /**
+     * 购买有效期时长
+     */
+    private Integer buyDateNum;
+
+    /**
+     * 消耗有效期
+     */
+    private String useValid;
+
+    /**
+     * 消耗有效期单位 - 扩展字段用于参数接收  y/m/d
+     */
+    private String useDateUnit;
+
+    /**
+     * 消耗有效期时长
+     */
+    private Integer useDateNum;
+
+    /**
+     * 失效时间
+     */
+    private Date invalidTime;
+
+    public Integer getBuyDateNum() {
+        return buyDateNum;
+    }
+
+    public void setBuyDateNum(Integer buyDateNum) {
+        this.buyDateNum = buyDateNum;
+    }
+
+    public Integer getUseDateNum() {
+        return useDateNum;
+    }
+
+    public void setUseDateNum(Integer useDateNum) {
+        this.useDateNum = useDateNum;
+    }
+
+    public String getBuyValid() {
+        return buyValid;
+    }
+
+    public void setBuyValid(String buyValid) {
+        if (StringUtils.isNotBlank(buyValid)) {
+            if (buyValid.contains("Y") || buyValid.contains("M") || buyValid.contains("D")) {
+                this.buyDateUnit = buyValid.substring(buyValid.length() - 1);
+                this.buyDateNum = Integer.parseInt(buyValid.substring(0, buyValid.length() - 1));
+            }
+        }
+        this.buyValid = buyValid;
+    }
+
+    public String getBuyDateUnit() {
+        return buyDateUnit;
+    }
+
+    public void setBuyDateUnit(String buyDateUnit) {
+        this.buyDateUnit = buyDateUnit;
+    }
+
+    public String getUseValid() {
+        return useValid;
+    }
+
+    public void setUseValid(String useValid) {
+        if (StringUtils.isNotBlank(useValid)) {
+            if (useValid.contains("Y") || useValid.contains("M") || useValid.contains("D")) {
+                this.useDateUnit = useValid.substring(useValid.length() - 1);
+                this.useDateNum = Integer.parseInt(useValid.substring(0, useValid.length() - 1));
+            }
+        }
+        this.useValid = useValid;
+    }
+
+    public String getUseDateUnit() {
+        return useDateUnit;
+    }
+
+    public void setUseDateUnit(String useDateUnit) {
+        this.useDateUnit = useDateUnit;
+    }
+
+    public Date getInvalidTime() {
+        return invalidTime;
+    }
+
+    public void setInvalidTime(Date invalidTime) {
+        this.invalidTime = invalidTime;
+    }
 
     public Integer getIsDel() {
         return isDel;
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 fc8b5d5..dc8cba5 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
@@ -3,6 +3,7 @@
 import com.matrix.core.constance.MatrixConstance;
 import com.matrix.core.exception.GlobleException;
 import com.matrix.core.pojo.PaginationVO;
+import com.matrix.core.tools.DateUtil;
 import com.matrix.core.tools.StringUtils;
 import com.matrix.core.tools.WebUtil;
 import com.matrix.system.common.bean.SysUsers;
@@ -26,6 +27,7 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import java.awt.event.WindowStateListener;
+import java.time.LocalDate;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -88,6 +90,9 @@
 
         shoppingGoods.setCreateTime(new Date());
         setPublicAttr(shoppingGoods);
+
+        setShoppingGoodsInvalidTime(shoppingGoods);
+
         if (shoppingGoods.getReferencePice() == null) {
             //赠送金额
             shoppingGoods.setReferencePice(0D);
@@ -116,6 +121,24 @@
 
 
         return i;
+    }
+
+    private void setShoppingGoodsInvalidTime(ShoppingGoods shoppingGoods) {
+        // 若未设置购买有效期和消耗有效期,则默认永久有效
+        if (shoppingGoods.getBuyDateNum() == null && shoppingGoods.getUseDateNum() == null) {
+            shoppingGoods.setInvalidTime(DateUtil.stringToDate("9999-12-31", DateUtil.DATE_FORMAT_DD));
+        } else {
+            // 计算失效日期,判断购买有效期和消耗有效期哪个先失效,则为失效日期
+            Date buyValidDate = DateUtil.calDate(shoppingGoods.getBuyDateNum(), shoppingGoods.getBuyDateUnit());
+            Date useValidDate = DateUtil.calDate(shoppingGoods.getUseDateNum(), shoppingGoods.getUseDateUnit());
+            if (buyValidDate.after(useValidDate)) {
+                shoppingGoods.setInvalidTime(useValidDate);
+            } else {
+                shoppingGoods.setInvalidTime(buyValidDate);
+            }
+            shoppingGoods.setBuyValid(shoppingGoods.getBuyDateNum() + shoppingGoods.getBuyDateUnit());
+            shoppingGoods.setUseValid(shoppingGoods.getUseDateNum() + shoppingGoods.getUseDateUnit());
+        }
     }
 
     private void setGoodsAssembles(ShoppingGoods shoppingGoods) {
@@ -190,7 +213,7 @@
             }
 
         }
-
+        setShoppingGoodsInvalidTime(shoppingGoods);
 
         return shoppingGoodsDao.update(shoppingGoods);
 
diff --git a/zq-erp/src/main/resources/mybatis/mapper/hive/ShoppingGoodsDao.xml b/zq-erp/src/main/resources/mybatis/mapper/hive/ShoppingGoodsDao.xml
index 09060f2..fac724e 100644
--- a/zq-erp/src/main/resources/mybatis/mapper/hive/ShoppingGoodsDao.xml
+++ b/zq-erp/src/main/resources/mybatis/mapper/hive/ShoppingGoodsDao.xml
@@ -290,7 +290,10 @@
 		use_shop,
 		headquarters,
 		is_del,
-		zjm
+		zjm,
+		buy_valid,
+		use_valid,
+		invalid_time
 		)
 		VALUES (
 		#{id},
@@ -346,7 +349,10 @@
 			#{useShop},
 			#{headquarters},
 			#{isDel},
-			#{zjm}
+			#{zjm},
+			#{buyValid},
+			#{useValid},
+			#{invalidTime}
 		)
 	</insert>
 
@@ -519,6 +525,15 @@
 			<if test="isDel != null and isDel !='' ">
 				is_del = #{isDel},
 			</if>
+			<if test="invalidTime != null">
+				invalid_time = #{invalidTime},
+			</if>
+			<if test="buyValid != null and buyValid !='' ">
+				buy_valid = #{buyValid},
+			</if>
+			<if test="useValid != null and useValid !='' ">
+				use_valid = #{useValid},
+			</if>
 
 
 		</set>
diff --git a/zq-erp/src/main/resources/templates/views/admin/hive/products/goods-form.html b/zq-erp/src/main/resources/templates/views/admin/hive/products/goods-form.html
index a4a4c29..1052739 100644
--- a/zq-erp/src/main/resources/templates/views/admin/hive/products/goods-form.html
+++ b/zq-erp/src/main/resources/templates/views/admin/hive/products/goods-form.html
@@ -892,6 +892,44 @@
                             </el-date-picker>
                         </el-form-item>
                     </el-col>
+                    <el-col :offset="2" :span="5">
+                        <el-form-item label="购买有效期">
+                            <el-col  :span="6">
+                                <el-form-item label="">
+                                    <el-input v-model="form.buyDateNum"></el-input>
+                                </el-form-item>
+                            </el-col>
+                            <el-col :span="12">
+                                <el-form-item lable="">
+                                    <el-select v-model="form.buyDateUnit" placeholder="请选择">
+                                        <el-option
+                                                v-for="item in dateList"
+                                                :key="item.value"
+                                                :label="item.label"
+                                                :value="item.value">
+                                        </el-option>
+                                    </el-select>
+                                </el-form-item>
+                            </el-col>
+                        </el-form-item>
+                    </el-col>
+                    <el-col :offset="1" :span="5">
+                        <el-form-item label="消耗有效期">
+                            <el-col  :span="6">
+                                <el-input v-model="form.useDateNum"></el-input>
+                            </el-col>
+                            <el-col  :span="12">
+                                <el-select v-model="form.useDateUnit" placeholder="请选择">
+                                    <el-option
+                                            v-for="item in dateList"
+                                            :key="item.value"
+                                            :label="item.label"
+                                            :value="item.value">
+                                    </el-option>
+                                </el-select>
+                            </el-col>
+                        </el-form-item>
+                    </el-col>
                 </el-row>
                 <el-row v-if="shopInfo.shopType==1">
                     <el-col :span="10">
@@ -944,7 +982,7 @@
     var app = new Vue({
         el: '#app',
         data: {
-
+            dateList : [{value : 'Y', label : '年'}, {value : 'M', label : '月'}, { value : 'D' , label : '日' }],
             selectTreeParam: {
                 holder: '请选择商品类型',
                 treeData: [],

--
Gitblit v1.9.1