liyg пре 2 година
родитељ
комит
85c9516b7e
1 измењених фајлова са 145 додато и 35 уклоњено
  1. 145 35
      src/main/java/com/steerinfo/dil/util/DataChange.java

+ 145 - 35
src/main/java/com/steerinfo/dil/util/DataChange.java

@@ -2,11 +2,11 @@ package com.steerinfo.dil.util;
 
 import java.math.BigDecimal;
 import java.text.DecimalFormat;
-import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 
 /**
  * @ author    :TXF
@@ -17,24 +17,55 @@ public class DataChange {
 
 
     /**
-     * 解析前端传来的日期字符串
+     * 时间转换类
+     * 处理了三种类型 yyyy-MM-dd HH:mm:ss  yyyy/MM/dd HH:mm:ss  时间戳类型(带毫秒数时间戳13位)
      * @param vueDate
      * @return
      */
     public static Date dataToDate(Object vueDate){
-        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
-        Date parseDate = null;
-        if (vueDate != null){
+        if(vueDate instanceof Date){
+            return (Date) vueDate;
+        } else if(vueDate instanceof Long){
+            return  new Date((Long)vueDate);
+        } else {
             try {
-                String date = (String) vueDate;
-                parseDate = sdf.parse(date);
-            } catch (ParseException e) {
-                e.printStackTrace();
+                String str = String.valueOf(vueDate);
+                if(judgeNumber(str) && str.length() == 13){
+                    return new Date(Long.parseLong(str));
+                }else if(str.contains("-")){
+                    if(str.length() == "yyyy-MM-dd".length()){
+                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+                        return sdf.parse(str);
+                    }
+                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                    return sdf.parse(str);
+                }else if(str.contains("/")){
+                    if(str.length() == "yyyy/MM/dd".length()){
+                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
+                        return sdf.parse(str);
+                    }
+                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
+                    return sdf.parse(str);
+                }
+            }catch (Exception e){
+                System.out.println("时间解析错误!返回null");
+                return null;
             }
         }
-        return parseDate;
+        return null;
+    }
+
+    /**
+     * 判断是否纯数字(不带小数点)仅供上面方法使用
+     * @param str
+     * @return
+     */
+    public static boolean judgeNumber(String str){
+        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
+        return pattern.matcher(str).matches();
     }
 
+
     /**
      * 数据转换成BigDecimal
      * @param data
@@ -46,19 +77,19 @@ public class DataChange {
                 return (BigDecimal) data;
             }else{
                 String str = String.valueOf(data);
-                BigDecimal decimal = null;
+                BigDecimal decimal = BigDecimal.ZERO;
                 if(!"".equals(str)){
                     try {
                         decimal = new BigDecimal(str);
                     } catch (Exception e) {
-                        e.printStackTrace();
-                        return new BigDecimal(0);
+                        System.out.println(data + ":数据解析失败!返回0");
+                        return BigDecimal.ZERO;
                     }
                 }
                 return decimal;
             }
         }
-        return new BigDecimal(0);
+        return BigDecimal.ZERO;
     }
 
     /**
@@ -66,49 +97,57 @@ public class DataChange {
      * @param date 传入时间
      * @return
      */
-    public static String dateToDayDate(Object date){
-        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
-        Date changeDate = null;
-        if(date == null)
-            return null;
+    public static Date dataToDayDate(Object date) {
         try{
-             changeDate = (Date) date;
+            Date origin = dataToDate(date);
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+            if(origin == null) {
+                return null;
+            }
+            return sdf.parse(sdf.format(origin));
         }catch (Exception e){
             e.printStackTrace();
+            return null;
         }
-        return sdf.format(changeDate);
     }
 
     /**
-     * 遍历从数据库中传进来的数据
-     * @param list 从数据库查询出来的list数据列表
-     * @param key map中多个需要转换的date参数
+     * 查询日期是否过期,过期则返回 true
+     * @param day 传入时间
+     * @return
      */
-    public static void changeDateToDayDate(List<Map<String, Object>> list, String ...key){
-        //遍历List
-        for (Map<String, Object> map : list) {
-            for (String s : key) {
-                //从map中取 date的值 并转换成字符串类型的日期
-                String stringDate = dateToDayDate(map.get(s));
-                map.put(s, stringDate);
+    public static boolean isExpireDay(Object day) {
+        try{
+            if(dataToDayDate(day).getTime() < dataToDayDate(new Date()).getTime()){
+                return true;
+            }else{
+                return false;
             }
+        }catch (Exception e){
+            e.printStackTrace();
+            return true;
         }
     }
 
+
     /**
      * 遍历列表使只显示两位小数
      * @param list
      * @param key
      */
     public static void dataTo2Number(List<Map<String, Object>> list, String ...key){
+        DecimalFormat df =  new DecimalFormat("0.00");
         //遍历List
         for (Map<String, Object> map : list) {
             for (String s : key) {
                 //修改数据为带两位小数
-                BigDecimal oldDate = (BigDecimal) map.get(s);
-                DecimalFormat df =  new DecimalFormat("0.00");
-                String resultDeduction = df.format(oldDate.doubleValue());
-                map.put(s, resultDeduction);
+                try {
+                    BigDecimal oldDate = dataToBigDecimal(map.get(s));
+                    String resultDeduction = df.format(oldDate.doubleValue());
+                    map.put(s, resultDeduction);
+                } catch (Exception e) {
+                    System.out.println("原料扣减量数据有误");
+                }
             }
         }
     }
@@ -141,6 +180,7 @@ public class DataChange {
      * @return
      */
     public static String generateEightDigitsNumber(String start, Integer id){
+        id = id % 10000000; //保证不超过
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
         StringBuilder sb = new StringBuilder(start + sdf.format(new Date()));
         sb.append(
@@ -156,4 +196,74 @@ public class DataChange {
         return sb.toString();
     }
 
+    /**
+     * 根据时间段查询数据 支持只选择单个时间
+     * @Author TXF
+     * @Date 2022/1/10 23:21
+     * @param startTime
+     * @param endTime
+     * @param map
+     * @param sdf
+     * @return
+     **/
+    public static void queryDataByDate(String startTime, String endTime, Map<String, Object> map, SimpleDateFormat sdf){
+        if (startTime != null && !"null".equals(startTime) && endTime != null && !"null".equals(endTime)) {
+            map.put("startDate", sdf.format(new Date(Long.parseLong(startTime))));
+            map.put("endDate", sdf.format(new Date(Long.parseLong(endTime) + 86400000)));
+        } else if (startTime != null && !"null".equals(startTime)) {
+            map.put("oneDate", sdf.format(new Date(Long.parseLong(startTime))));
+        } else if (endTime != null && !"null".equals(endTime)) {
+            map.put("oneDate", sdf.format(new Date(Long.parseLong(endTime))));
+        } else {
+            map.put("oneDate", sdf.format(new Date()));
+        }
+    }
+
+    /**
+     * 只支持两个时间查询
+     * @Author TXF
+     * @Date 2022/1/15 9:08
+     * @param startTime
+     * @param endTime
+     * @param sdf
+     * @return
+     **/
+    public static void queryDataByDateTime(String startTime, String endTime, Map<String, Object> map,SimpleDateFormat sdf){
+        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
+        if (startTime != null && !"null".equals(startTime) && endTime != null && !"null".equals(endTime)) {
+            map.put("startDate", sdf.format(new Date(Long.parseLong(startTime))));
+            map.put("endDate", sdf.format(new Date(Long.parseLong(endTime))));
+        }
+        //如果开始时间和结束时间有且只有一个为空 则只查那天的数据
+        else if((startTime != null && !"null".equals(startTime)) || (endTime != null && !"null".equals(endTime))){
+            if(startTime != null && !"null".equals(startTime)){
+                queryDataByTwoDateSon(map, startTime, sdfDate);
+            }
+            if(endTime != null && !"null".equals(endTime)){
+                queryDataByTwoDateSon(map, endTime, sdfDate);
+            }
+        }else {
+            //如果两者时间都为空,则查询当天数据
+            String nowDate = sdfDate.format(new Date());
+            map.put("oneDate", nowDate + " 00:00:00");
+        }
+    }
+
+    /**
+     * 上面方法的儿子方法 如果只传入了一个时间 则查询那天的数据
+     * @Author TXF
+     * @Date 2022/1/17 16:17
+     * @param map
+     * @param time
+     * @param sdfDate
+     * @return
+     **/
+    private static void queryDataByTwoDateSon(Map<String, Object> map, String time, SimpleDateFormat sdfDate){
+        Date date1 = new Date(Long.parseLong(time));
+        Date date2 = new Date(Long.parseLong(time) + 86400000);
+        String dayStartTime = sdfDate.format(date1);
+        String dayEndTime = sdfDate.format(date2);
+        map.put("startDate", dayStartTime + " 00:00:00");
+        map.put("endDate", dayEndTime + " 00:00:00");
+    }
 }