package com.fs.common.utils;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

public class TimeUtils
{
    public static Integer formatTime(Integer beginTime) {
        if (beginTime < 1000000 && beginTime % 100 == 12) {
            beginTime = beginTime + 89;
        } else if (beginTime > 1000000) {
            int month = DateUtil.month(DateUtil.parse(beginTime.toString(), "yyyyMMdd")) + 1;
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) {
                if (beginTime % 100 == 31) {
                    beginTime = beginTime + 70;
                } else {
                    beginTime++;
                }
            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (beginTime % 100 == 30) {
                    beginTime = beginTime + 71;
                } else {
                    beginTime++;
                }
            } else if (month == 2) {
                int year = DateUtil.year(DateUtil.parse(beginTime.toString(), "yyyyMMdd"));
                if (DateUtil.isLeapYear(year) && beginTime % 100 == 29) {
                    beginTime = beginTime + 72;
                } else if (!DateUtil.isLeapYear(year) && beginTime % 100 == 28) {
                    beginTime = beginTime + 73;
                } else {
                    beginTime++;
                }
            } else if (month == 12) {
                if (beginTime % 100 == 31) {
                    beginTime = beginTime + 8870;
                } else {
                    beginTime++;
                }
            }
        } else {
            beginTime++;
        }
        return beginTime;
    }

    public static TimeEntity parseTime(String type,String startTime,String endTime) {
        Date beginDate = DateUtil.date();
        Date endDate = DateUtil.date();
        int cycleNum = 12;
        String sqlDateFormat = "%Y%m";
        String dateFormat = "yyyyMM";

        if (StrUtil.isNotEmpty(type)) {
            if (type.matches("^[\\d]{8}$")){
                sqlDateFormat = "%Y%m%d";
                dateFormat = "yyyyMMdd";
            }
            switch (type) {
                case "9":
                    beginDate = DateUtil.beginOfYear(DateUtil.date());
                    endDate = DateUtil.endOfYear(DateUtil.date());
                    break;
                case "10":
                    beginDate = DateUtil.beginOfYear(DateUtil.offsetMonth(DateUtil.date(), -12));
                    endDate = DateUtil.endOfYear(DateUtil.offsetMonth(DateUtil.date(), -12));
                    break;
                case "7":
                    beginDate = DateUtil.beginOfQuarter(DateUtil.date());
                    endDate = DateUtil.endOfQuarter(DateUtil.date());
                    cycleNum = 3;
                    break;
                case "8":
                    beginDate = DateUtil.beginOfQuarter(DateUtil.offsetMonth(DateUtil.date(), -3));
                    endDate = DateUtil.endOfQuarter(DateUtil.offsetMonth(DateUtil.date(), -3));
                    cycleNum = 3;
                    break;
                case "5":
                    beginDate = DateUtil.beginOfMonth(DateUtil.date());
                    endDate = DateUtil.endOfMonth(DateUtil.date());
                    sqlDateFormat = "%Y%m%d";
                    dateFormat = "yyyyMMdd";
                    cycleNum = (int) DateUtil.between(beginDate, endDate, DateUnit.DAY) + 1;
                    break;
                case "6":
                    beginDate = DateUtil.beginOfMonth(DateUtil.offsetMonth(DateUtil.date(), -1));
                    endDate = DateUtil.endOfMonth(DateUtil.offsetMonth(DateUtil.date(), -1));
                    sqlDateFormat = "%Y%m%d";
                    dateFormat = "yyyyMMdd";
                    cycleNum = (int) DateUtil.between(beginDate, endDate, DateUnit.DAY) + 1;
                    break;
                case "3":
                    beginDate = DateUtil.beginOfWeek(DateUtil.date());
                    endDate = DateUtil.endOfWeek(DateUtil.date());
                    sqlDateFormat = "%Y%m%d";
                    dateFormat = "yyyyMMdd";
                    cycleNum = 7;
                    break;
                case "4":
                    beginDate = DateUtil.beginOfWeek(DateUtil.offsetWeek(DateUtil.date(), -1));
                    endDate = DateUtil.endOfWeek(DateUtil.offsetWeek(DateUtil.date(), -1));
                    sqlDateFormat = "%Y%m%d";
                    dateFormat = "yyyyMMdd";
                    cycleNum = 7;
                    break;
                case "1":
                    beginDate = DateUtil.beginOfDay(DateUtil.date());
                    endDate = DateUtil.endOfDay(DateUtil.date());
                    sqlDateFormat = "%Y%m%d";
                    dateFormat = "yyyyMMdd";
                    cycleNum = 1;
                    break;
                case "2":
                    beginDate = DateUtil.offsetDay(DateUtil.date(), -1);
                    endDate = DateUtil.offsetDay(DateUtil.date(), -1);
                    sqlDateFormat = "%Y%m%d";
                    dateFormat = "yyyyMMdd";
                    cycleNum = 1;
                    break;
                default:
                    break;
            }
        } else if (StrUtil.isNotEmpty(startTime) && StrUtil.isNotEmpty(endTime)) {
            Date start;
            Date end;
            if (startTime.length() == 6) {
                start = DateUtil.parse(startTime, "yyyyMM");
                end = DateUtil.endOfMonth(DateUtil.parse(endTime, "yyyyMM"));
            } else {
                start = DateUtil.parse(startTime);
                end = DateUtil.parse(endTime);
            }
            Integer startMonth = Integer.valueOf(DateUtil.format(start, "yyyyMM"));
            Integer endMonth = Integer.valueOf(DateUtil.format(end, "yyyyMM"));
            if (startMonth.equals(endMonth)) {
                sqlDateFormat = "%Y%m%d";
                dateFormat = "yyyyMMdd";
                Long diffDay = DateUtil.between(start, end, DateUnit.DAY);
                cycleNum = diffDay.intValue() + 1;
            } else {
                sqlDateFormat = "%Y%m";
                dateFormat = "yyyyMM";
                int diffYear = Integer.valueOf(endMonth.toString().substring(0, 4)) - Integer.valueOf(startMonth.toString().substring(0, 4));
                int diffMonth = endMonth % 100 - startMonth % 100 + 1;
                cycleNum = diffYear * 12 + diffMonth;
            }
            beginDate = start;
            endDate = end;
        }
        Integer beginTime = Integer.valueOf(DateUtil.format(beginDate, dateFormat));
        Integer finalTime = Integer.valueOf(DateUtil.format(endDate, dateFormat));
        return new TimeEntity(sqlDateFormat, dateFormat, beginDate, endDate, cycleNum, beginTime, finalTime, null);
    }

    public static class TimeEntity implements Serializable {
        public TimeEntity(String sqlDateFormat, String dateFormat, Date beginDate, Date endDate, Integer cycleNum, Integer beginTime, Integer finalTime, Long[] userIds) {
            this.sqlDateFormat = sqlDateFormat;
            this.dateFormat = dateFormat;
            this.beginDate = beginDate;
            this.endDate = endDate;
            this.cycleNum = cycleNum;
            this.beginTime = beginTime;
            this.finalTime = finalTime;
            this.userIds = userIds;

        }

        /**
         * sql日期格式化
         */
        private String sqlDateFormat;

        private Long companyId;

        public Long getCompanyId() {
            return companyId;
        }

        public void setCompanyId(Long companyId) {
            this.companyId = companyId;
        }
        /**
         * 日期格式化
         */
        private String dateFormat;

        /**
         * 开始时间
         */
        private Date beginDate;

        /**
         * 结束时间
         */
        private Date endDate;

        /**
         * 周期
         */
        private Integer cycleNum;

        /**
         * 开始时间 字符串格式 如20200101
         */
        private Integer beginTime;

        /**
         * 结束时间 字符串格式 如20200101
         */
        private Integer finalTime;

        /**
         * user列表
         */
        private Long[] userIds;

        public Integer getBeginTime() {
            return beginTime;
        }

        public Map<String, Object> toMap() {
            return BeanUtil.beanToMap(this);
        }

        public String getSqlDateFormat() {
            return sqlDateFormat;
        }

        public void setSqlDateFormat(String sqlDateFormat) {
            this.sqlDateFormat = sqlDateFormat;
        }

        public String getDateFormat() {
            return dateFormat;
        }

        public void setDateFormat(String dateFormat) {
            this.dateFormat = dateFormat;
        }

        public Date getBeginDate() {
            return beginDate;
        }

        public void setBeginDate(Date beginDate) {
            this.beginDate = beginDate;
        }

        public Date getEndDate() {
            return endDate;
        }

        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }

        public Integer getCycleNum() {
            return cycleNum;
        }

        public void setCycleNum(Integer cycleNum) {
            this.cycleNum = cycleNum;
        }


        public void setBeginTime(Integer beginTime) {
            this.beginTime = beginTime;
        }

        public Integer getFinalTime() {
            return finalTime;
        }

        public void setFinalTime(Integer finalTime) {
            this.finalTime = finalTime;
        }

        public Long[] getUserIds() {
            return userIds;
        }

        public void setUserIds(Long[] userIds) {
            this.userIds = userIds;
        }
    }
}
