package com.mortals.xhx.common.utils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.mortals.xhx.common.code.SystemConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Title: 申请日期转化
 * @Package: com.atm.affair.util
 * @Description: (用一句话描述该文件做什么)
 * @Author: tinker
 * @Date: 4/9/21
 * @Version: V1.0
 */
@Slf4j
public class ExcelConvertDateProperty implements Converter<Date> {
    private static final String DEFAULT_PATTERN = "yyyy-MM-dd";

    @Override
    public Class supportJavaTypeKey() {
        return null;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return null;
    }

    @Override
    public Date convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Date date = new Date();
        try {
            if (StringUtils.isEmpty(cellData.getStringValue())) {
                String dateTime = "" ;
                if(SystemConstant.BEOR){
                    dateTime = DateByAffairUtils.randomDate(SystemConstant.DAYS);
                }else {
                    dateTime = DateByAffairUtils.randomBetweenDate(SystemConstant.DAYS);
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_PATTERN);
                date = DateByAffairUtils.getDayRadomWorkingTime(simpleDateFormat.parse(dateTime),null, null);
            } else {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_PATTERN);
                Date dateNew = simpleDateFormat.parse(cellData.getStringValue());
                date = DateByAffairUtils.getDayRadomWorkingTime(dateNew, null, null);
            }

        } catch (Exception e) {
            log.error("转换日期失败："+e.getMessage());
            String dateTime = "" ;
            if(SystemConstant.BEOR){
                dateTime = DateByAffairUtils.randomDate(SystemConstant.DAYS);
            }else {
                dateTime = DateByAffairUtils.randomBetweenDate(SystemConstant.DAYS);
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_PATTERN);
            date = DateByAffairUtils.getDayRadomWorkingTime(simpleDateFormat.parse(dateTime),null, null);
            return date;
        }
        return date;


    }

    @Override
    public CellData convertToExcelData(Date date, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return null;
    }

    public static void main(String[] args) {
        Date date = new Date();

        try {
            if (StringUtils.isEmpty("2021-04-07") ){
                date = DateByAffairUtils.getTodayRadomWorkingTime(null, null);
            } else {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_PATTERN);
                Date dateNew = simpleDateFormat.parse("2021-04-07");
                date = DateByAffairUtils.getDayRadomWorkingTime(dateNew, null, null);

                String dateTime = DateByAffairUtils.randomBetweenDate(2);
                date = DateByAffairUtils.getDayRadomWorkingTime(simpleDateFormat.parse(dateTime),null, null);
                System.out.println("====="+date);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(date);

    }

}
