package com.jwipc.order.utils;

import cn.hutool.core.date.DateUtil;
import com.jwipc.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * author: 邓京术
 * date : 2025-03-13 11:00:30
 */
public class DataCheckUtils {

    // @描述：是否是2003的excel，返回true是2003 
    public static boolean isExcel2003(String filePath)  {  
         return filePath.matches("^.+\\.(?i)(xls)$");  
     }  
   
    //@描述：是否是2007的excel，返回true是2007 
    public static boolean isExcel2007(String filePath)  {  
         return filePath.matches("^.+\\.(?i)(xlsx)$");  
     }  


    public static String checkIsDate(String dateStr, int i) {
        if (StringUtils.isNotEmpty(dateStr)) {
            dateStr = dateStr.replaceAll("/", "-");
            try {
                LocalDateTime localDateTime = DateUtils.formatYYYYMMDDDash(dateStr);
            } catch (Exception e) {
                return "第" + i + "行:" + dateStr + " 日期格式不对,请检查。\n\r";
            }

            Date d1 = DateUtil.parse(dateStr,"yyyy-MM-dd");
            int d2 = DateUtil.compare(new Date(),d1 ,"yyyy-MM-dd");
            if (d2 > 0) {
                return "第" + i + "行:" + dateStr + " 日期必须大于或等于当前日期,请检查。\n\r";
            }
        }
        return "";
    }

    public static String checkIsNumber(String numStr, int i) {
        if (StringUtils.isNotEmpty(numStr)) {
            try {
                Integer number = Integer.parseInt(numStr);
            } catch (Exception e) {
                return "第" + i + "行:" + numStr + " 数据格式不对,请检查。\n\r";
            }
        }
        return "";
    }

    public static String checkIsVersion(String versionStr) {
        if (StringUtils.isNotEmpty(versionStr)) {
            String checkStr = versionStr.substring(0,8);
            String now = DateUtils.formatYYYYMMDD(new Date());
            if (!checkStr.equals(now)) {
                return "回复版本不是当天的版本,请检查。\n\r";
            }
        }
        return "";
    }

    public static String checkIsVersion(String versionStr, int i) {
        if (StringUtils.isEmpty(versionStr)) {
            return "第" + i + "行: 回复版本不是当天的版本,请检查。\n\r";
        }
        if (StringUtils.isNotEmpty(versionStr)) {
            String backStr = checkIsNumber(versionStr, i);
            if (!"".equals(backStr)) {
                return backStr;
            }
            String checkStr = versionStr.substring(0,8);
            String now = DateUtils.formatYYYYMMDD(new Date());
            if (!checkStr.equals(now)) {
                return "第" + i + "行:" + versionStr + " 回复版本不是当天的版本,请检查。\n\r";
            }
        }
        return "";
    }

    public static String checkIsDateTwo(String dateStr, int i) {
        if (StringUtils.isNotEmpty(dateStr)) {
            String[] strArr = dateStr.split(";");
            for (String ss : strArr) {
                String[] arr = ss.split("\\*");
                if (arr.length != 2) {
                    return "第" + i + "行:" + dateStr + " 数据格式不对,请检查。\n\r";
                }
                String dStr = arr[0];
                dStr = dStr.replaceAll("/", "-");
                String num = arr[1];
                try {
                    LocalDateTime localDateTime = DateUtils.formatYYYYMMDDDash(dStr);
                    Integer number = Integer.parseInt(num);
                } catch (Exception e) {
                    return "第" + i + "行: " + dateStr + "数据格式不对,请检查。\n\r";
                }
                Date d1 = DateUtil.parse(dStr,"yyyy-MM-dd");
                int d2 = DateUtil.compare(new Date(),d1 ,"yyyy-MM-dd");
                if (d2 > 0) {
                    return "第" + i + "行:" + dateStr + " 日期必须大于或等于当前日期,请检查。\n\r";
                }
            }
        }
        return "";
    }

    public static void main(String[] args) {
        String date1 = "20250326";

        String d2 = "2025-03-25*0";

        String d3 = "2025/04/10";
        System.out.println(checkIsDate(d3, 0));

//        System.out.println(checkIsDate(date1, 0));
//        System.out.println(checkIsDateTwo(d2, 2));



    }
}