package com.dxhy.erp.utils;

import com.alibaba.fastjson.JSON;
import com.dxhy.common.util.JsonUtils;
import com.dxhy.common.util.pojo.GlobalInfo;
import com.dxhy.common.utils.Base64Encoding;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.erp.enums.GlobalReturnEnums;
import com.dxhy.erp.model.ReturnStateInfo;
import com.dxhy.erp.model.auth.InterfaceCode;
import com.dxhy.pojo.obj.erp.RedisBatchInfo;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author Allen
 * @version 1.0.0
 * @ClassName CommonUtils.java
 * @Description 通用工具类
 * @createTime 2023年02月22日 20:31:00
 */
public class CommonUtils {

    /**yyyyMM**/
    private static final String MONTH_REGEX = "^([1-9]\\d{3})(([0][1-9])|([1][0-2]))$";
    /**YYYY-MM-DD**/
    private static final String MONTH_REGEX_YYYY_MM_DD = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$";
    /**YYYY-MM-DD HH:mm:ss**/
    private static final String MONTH_REGEX_YYYY_MM_DD_HHMMSS = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$";

    /**YYYYMMDDHHmmss**/
    private static final String MONTH_REGEX_YYYYMMDDHHMMSS = "^[1-9]\\d{3}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])(20|21|22|23|[0-1]\\d)[0-5]\\d[0-5]\\d$";

    /**YYYYMM**/
    private static final String FORMAT_YYYYMM = "yyyyMM";

    /**YYYYMMDD**/
    private static final String FORMAT_YYYYMMDD = "yyyyMMd";

    /**YYYY-MM-DD**/
    private static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    /**YYYY-MM-DD HH:mm:ss**/
    private static final String FORMAT_YYYY_MM_DD_HHMMSS = "yyyy-MM-dd HH:mm:ss";

    /**YYYYMMDDHHmmss**/
    private static final String FORMAT_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    /**
     * 封装返回参数
     * @param globalInfo
     * @param data json字符串
     * @return
     */
    public static String packageReturnMsg(GlobalInfo globalInfo, String data, GlobalReturnEnums gre) {
        ReturnStateInfo runInfo = new ReturnStateInfo();
        runInfo.setReturnCode(gre.getCode());
        runInfo.setReturnMessage(gre.getMsg());
        InterfaceCode interfaceCode = new InterfaceCode();
        interfaceCode.setData(Base64Encoding.encode(StringUtils.isBlank(data)?"":data));
        interfaceCode.setGlobalInfo(globalInfo);
        interfaceCode.setReturnStateInfo(runInfo);
        return JSON.toJSONString(interfaceCode);
    }

    /**
     * 封装返回参数
     * @param globalInfo
     * @param dataObj 数据实体
     * @return
     */
    public static String packageReturnMsg(GlobalInfo globalInfo, Object dataObj, GlobalReturnEnums gre) {
        String data = com.dxhy.erp.utils.JsonUtils.getInstance().toJsonString(dataObj);
        ReturnStateInfo runInfo = new ReturnStateInfo();
        runInfo.setReturnCode(gre.getCode());
        runInfo.setReturnMessage(gre.getMsg());
        InterfaceCode interfaceCode = new InterfaceCode();
        interfaceCode.setData(Base64Encoding.encode(StringUtils.isBlank(data)?"":data));
        interfaceCode.setGlobalInfo(globalInfo);
        interfaceCode.setReturnStateInfo(runInfo);
        return JSON.toJSONString(interfaceCode);
    }


    /**
     * 封装返回参数
     * @param globalInfo
     * @param data json格式字符串
     * @return
     */
    public static String packageReturnMsg(GlobalInfo globalInfo, String data, String code , String msg) {
        ReturnStateInfo runInfo = new ReturnStateInfo();
        runInfo.setReturnCode(code);
        runInfo.setReturnMessage(msg);
        InterfaceCode interfaceCode = new InterfaceCode();
        interfaceCode.setData(Base64Encoding.encode(StringUtils.isBlank(data)?"":data));
        interfaceCode.setGlobalInfo(globalInfo);
        interfaceCode.setReturnStateInfo(runInfo);
        return JSON.toJSONString(interfaceCode);
    }

    /**
     * 封装返回参数
     * @param globalInfo
     * @param dataObj 数据实体
     * @return
     */
    public static String packageReturnMsg(GlobalInfo globalInfo, Object dataObj, String code , String msg) {
        String data = com.dxhy.erp.utils.JsonUtils.getInstance().toJsonString(dataObj);
        ReturnStateInfo runInfo = new ReturnStateInfo();
        runInfo.setReturnCode(code);
        runInfo.setReturnMessage(msg);
        InterfaceCode interfaceCode = new InterfaceCode();
        interfaceCode.setData(Base64Encoding.encode(StringUtils.isBlank(data)?"":data));
        interfaceCode.setGlobalInfo(globalInfo);
        interfaceCode.setReturnStateInfo(runInfo);
        return JSON.toJSONString(interfaceCode);
    }

    /**
     * 封装参数校验结果
     * @param gre
     */
    public static Map<String,String> packageValidResult(GlobalReturnEnums gre) {
        Map<String, String> map = new HashMap<>();
        map.put("returnCode", gre.getCode());
        map.put("returnMsg", gre.getMsg());
        return map;
    }

    /**
     * 封装参数校验结果
     * @param gre
     */
    public static Map<String,String> packageValidResult(GlobalReturnEnums gre,int index) {
        Map<String, String> map = new HashMap<>();
        map.put("returnCode", gre.getCode());
        map.put("returnMsg", "明细第"+index+"条"+gre.getMsg());
        return map;
    }

    /**
     *  属期正则校验
     * @param month
     * @return
     * @throws Exception
     */
    public static boolean validMonth(String month){
        boolean matches =  Pattern.matches(MONTH_REGEX, month);
        return matches;
    }

    /**
     *  入账属期对比当前自然月
     * @param month
     * @return
     * @throws Exception
     */
    public static boolean validSqForDqy(String month){
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_YYYYMM);
        String dqy = format.format(new Date());
        return month.compareTo(dqy) > 0 ? true : false;
    }


    /**
     *  开票日期不能晚于入账属期校验
     * @param invoiceDate
     * @return
     */
    public static boolean validKprqAndRzsq(Date invoiceDate,String rzsq) {
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_YYYYMM);
        String kprq = format.format(invoiceDate);
        return rzsq.compareTo(kprq) < 0 ? true : false;
    }

    public static void main(String[] args) {
        Date date = DateUtils.strToDate("2023-12-31 21:41:45", "yyyy-MM-dd HH:mm:ss");
        boolean b = validKprqAndRzsq(date, "202401");
        System.out.println(b);
    }

    /**
     * 开票日期格式化为YYYYMMDD
     * @param invoiceDate
     * @return
     */
    public static String kprqFormat(Date invoiceDate) {
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_YYYYMMDD);
        String kprq = format.format(invoiceDate);
        return kprq;
    }

    /**
     * 开票日期格式化为YYYY_MM_DD
     * @param invoiceDate
     * @return
     */
    public static Date kprqFormat(String invoiceDate) {
        if (StringUtils.isBlank(invoiceDate)) {
            return null;
        }
        if (Pattern.matches(MONTH_REGEX_YYYY_MM_DD,invoiceDate)) {
            SimpleDateFormat format = new SimpleDateFormat(FORMAT_YYYY_MM_DD);
            Date kprq = null;
            try {
                kprq = format.parse(invoiceDate);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            return kprq;
        }

        if (Pattern.matches(MONTH_REGEX_YYYY_MM_DD_HHMMSS,invoiceDate)) {
            SimpleDateFormat format = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HHMMSS);
            Date kprq = null;
            try {
                kprq = format.parse(invoiceDate);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            return kprq;
        }

        if (Pattern.matches(MONTH_REGEX_YYYYMMDDHHMMSS,invoiceDate)) {
            SimpleDateFormat format = new SimpleDateFormat(FORMAT_YYYYMMDDHHMMSS);
            Date kprq = null;
            try {
                kprq = format.parse(invoiceDate);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            return kprq;
        }

        return null;
    }


    /**
     * 转换入账申请结果
     * @param fprzAuthStatus
     * @return
     */
    public static String parseRzjg(String fprzAuthStatus) {
        switch (fprzAuthStatus) {
            case "1":
                return "0";
            case "2":
                return "0";
            case "3":
                return "2";
            case "4":
                return "1";
            case "5":
                return "3";
            case "6":
                return "3";
            case "7":
                return "5";
            case "8":
                return "4";
            default:
                return null;
        }
    }

    /**
     * 转换下载结果
     * @param downloadStatus
     * @return
     */
    public static String parseXzjg(String downloadStatus) {
        switch (downloadStatus) {
            case "0":
                return "0";
            case "1":
                return "0";
            case "2":
                return "1";
            case "3":
                return "2";
            default:
                return null;
        }
    }

    /**
     * 发票类型转换  乐企转进项
     */
    public static String conversionInvoiceType(String invoiceType) {
        String conversionInvoiceTyp = "";
        switch (invoiceType) {
            case "81":
                conversionInvoiceTyp = "31";
                break;
            case "82":
                conversionInvoiceTyp = "32";
                break;
            case "83":
                conversionInvoiceTyp = "183";
                break;
            case "84":
                conversionInvoiceTyp = "184";
                break;
            case "85":
                conversionInvoiceTyp = "185";
                break;
            case "86":
                conversionInvoiceTyp = "186";
                break;
            case "87":
                conversionInvoiceTyp = "187";
                break;
            case "88":
                conversionInvoiceTyp = "188";
                break;
            case "51":
                conversionInvoiceTyp = "283";
                break;
            case "61":
                conversionInvoiceTyp = "161";
                break;
            default:
                conversionInvoiceTyp = invoiceType;
                break;
        }
        return conversionInvoiceTyp;
    }

    /**
     * 发票类型转换  进项转乐企
     */
    public static String conversionInvoiceTypeToLQ(String invoiceType) {
        String conversionInvoiceTyp = "";
        switch (invoiceType) {
            case "31":
                conversionInvoiceTyp = "81";
                break;
            case "32":
                conversionInvoiceTyp = "82";
                break;
            case "183":
                conversionInvoiceTyp = "83";
                break;
            case "184":
                conversionInvoiceTyp = "84";
                break;
            case "185":
                conversionInvoiceTyp = "85";
                break;
            case "186":
                conversionInvoiceTyp = "86";
                break;
            case "187":
                conversionInvoiceTyp = "87";
                break;
            case "188":
                conversionInvoiceTyp = "88";
                break;
            case "283":
                conversionInvoiceTyp = "51";
                break;
            case "161":
                conversionInvoiceTyp = "61";
                break;
            case "383":
                conversionInvoiceTyp = "83";
                break;
            default:
                conversionInvoiceTyp = invoiceType;
                break;
        }
        return conversionInvoiceTyp;
    }

    public static String parseRzzt(String lqRzzt) {

        switch (lqRzzt) {
            case "01":
                return "1";
            case "02":
                return "2";
            case "03":
                return "1";
            case "04":
                return "1";
            case "05":
                return "2";
            default:
                return null;
        }

    }

    public static Map<String, String> checkBatchMsg(String batchMsg, int pageNum) {
        if (StringUtils.isBlank(batchMsg)) {
            if (pageNum > 1) {
                return packageValidResult(GlobalReturnEnums.PARAM_PAGEN_NUM_ERROR_1);
            }
        } else {
            RedisBatchInfo ri = JsonUtils.getInstance().fromJson(batchMsg, RedisBatchInfo.class);
            if (pageNum > ri.getTotlePage()) {
                return packageValidResult(GlobalReturnEnums.PARAM_PAGEN_NUM_ERROR_2);
            }
            if (pageNum == 1) {
                return packageValidResult(GlobalReturnEnums.PARAM_PAGEN_NUM_ERROR_3);
            }
        }
        return null;
    }
}
