package com.rongwei.bscommon.sys.utils;

import com.rongwei.commonservice.serial.service.SysSerialNumberService;
import com.rongwei.commonservice.service.RedisService;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utils class
 * 工具类
 *
 * @author XH
 * @date 2021/05/27
 */
@Component
public class DateUtils {

    @Autowired
    private RedisService autoRedisService;
    @Autowired
    private SysSerialNumberService autoSysSerialNumberService;

    private static RedisService redisService;
    private static SysSerialNumberService sysSerialNumberService;


    /**
     * 时间格式
     */
    public static class DatePattern {
        public static final String DATE_PATTERN_YMD_HOUR = "yyyy/MM/dd HH";
    }

    private static final Logger log = LoggerFactory.getLogger(DateUtils.class.getName());
    private static final Pattern STR_IS_DATE_LAYOUT_PATTERN = Pattern.compile("^(?:(?!0000)[0-9]{4}([-/.年]?)(?:(?:0?[1-9]|1[0-2])([-/.月]?)(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])([-/.月]?)(?:29|30)([日]?)|(?:0?[13578]|1[02])([-/.月]?)31)([日]?)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.月]?)0?2([-/.月]?)29([日]?))$");
    /**
     * excel 导入时间格式类型
     */
    private static final List<String> IMPORT_EXCEL_DATE_FORMAT_TYPE = new ArrayList<String>() {{
        add(DatePattern.DATE_PATTERN_YMD_HOUR);
    }};

    /**
     * 获取当前的登陆人信息
     *
     * @return
     */
    public static SysUserVo getCurrentUser() {
        // 获取当前登录人信息
        SysUserVo currUser = null;
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            if (request != null) {
                String token = request.getHeader("token");
                if (com.rongwei.rwcommon.utils.StringUtils.isNotEmpty(token)) {
                    currUser = redisService.getLoginUser(token);
                }
            }
        }
        return currUser;
    }


    /**
     * 校验字符串是否为 日期格式
     *
     * @param str
     * @return
     */
    public static boolean strIsDateLayout(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        Matcher matcher = STR_IS_DATE_LAYOUT_PATTERN.matcher(str);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

    public static Date importExcelFormatExcel(XSSFCell objValue) {
        if (objValue == null) {
            return null;
        }
        if (StringUtils.isBlank(objValue.toString())) {
            return null;
        }
        try {
            return objValue.getDateCellValue();
        } catch (Exception e) {
            log.debug("当前单元格不是时间格式");
        }
        AtomicReference<Date> date = new AtomicReference<>();
        IMPORT_EXCEL_DATE_FORMAT_TYPE.forEach(format -> {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.ENGLISH);
            try {
                date.set(simpleDateFormat.parse(objValue.toString()));
            } catch (ParseException e) {
                log.error("excel解析时 时间格式化出现异常!时间格式：{},日期：{}", format, objValue);
            }
        });
        return date.get();
    }
}
