package com.njcn.prepare.harmonic.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.njcn.common.pojo.constant.BizParamConstant;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class PublicUtil {

    /**
     * 获取某年某月的第一天
     */
    public static String getFisrtDayOfMonth(int year,int month){
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    /**
     * 获取某月的最后一天
     */
    public static String getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    /**
     * 根据类型获取日期的开始和结束
     */
    public static Map<String,DateTime> getBeginAndEndByDate(Integer type, Date date)
    {
        String typeStr = String.valueOf(type);
        Map<String,DateTime> result = new HashMap<>();
        DateTime startTime = null;
        DateTime endTime = null;
        //年 季 月 周 日
        if (BizParamConstant.STAT_BIZ_YEAR.equals(typeStr)){
            startTime = DateUtil.beginOfYear(date);
            endTime = DateUtil.endOfYear(date);
        }else if(BizParamConstant.STAT_BIZ_QUARTER.equals(typeStr)){
            startTime = DateUtil.beginOfQuarter(date);
            endTime = DateUtil.endOfQuarter(date);
        }else if(BizParamConstant.STAT_BIZ_MONTH.equals(typeStr)){
            startTime = DateUtil.beginOfMonth(date);
            endTime = DateUtil.endOfMonth(date);
        }else if(BizParamConstant.STAT_BIZ_WEEK.equals(typeStr)){
            startTime = DateUtil.beginOfWeek(date);
            endTime = DateUtil.endOfWeek(date);
        }else if(BizParamConstant.STAT_BIZ_DAY.equals(typeStr)){
            startTime = DateUtil.beginOfDay(date);
            endTime = DateUtil.endOfDay(date);
        }
        result.put("startTime",startTime);
        result.put("endTime",endTime);
        return result;
    }

    /**
     * @Description:  返回当前季度
     * @Param: [datadate]时间格式yyyy-MM-dd
     * @return: java.lang.String
     * @Author: clam
     * @Date: 2022/11/30
     */
    public static Integer getNowQuarter(String datadate )
    {
        Integer temp =Integer.valueOf (datadate.substring (5, 7)) ;
        return temp%3==0?temp/3:temp/3+1;
    }

    public static void main(String[] args) {
//       Integer temp =Integer.valueOf ("2002-08-07".substring (5, 7)) ;
//        System.out.println (111 );
//        System.out.println ( PublicUtil.getNowQuarter ("2002-01-07"));

        System.out.println(getDayByMonth(2023,03));
        LocalDate currentDate = LocalDate.now();

        // 获取当前季度的第一个月份
        Month firstMonthOfQuarter = currentDate.getMonth().firstMonthOfQuarter();

        // 获取当前季度的最后一个月份
        Month lastMonthOfQuarter = firstMonthOfQuarter.plus(2);

        // 计算当前季度的总天数
        int totalDaysInQuarter = 0;
        for (Month month = firstMonthOfQuarter; !month.equals(lastMonthOfQuarter.plus(1)); month = month.plus(1)) {
            totalDaysInQuarter += month.length(currentDate.isLeapYear());
        }

        System.out.println("Total days in the current quarter: " + totalDaysInQuarter);


    }
    /**
    * @Description: 获取当前日期的当前季度总天数
    * @Param:
    * @return: java.lang.Integer
    * @Author: clam
    * @Date: 2023/6/6
    */

    public static Integer getQtrDays(LocalDate currentDate ){
        Month firstMonthOfQuarter = currentDate.getMonth().firstMonthOfQuarter();

        // 获取当前季度的最后一个月份
        Month lastMonthOfQuarter = firstMonthOfQuarter.plus(2);

        // 计算当前季度的总天数
        int totalDaysInQuarter = 0;
        for (Month month = firstMonthOfQuarter; !month.equals(lastMonthOfQuarter.plus(1)); month = month.plus(1)) {
            totalDaysInQuarter += month.length(currentDate.isLeapYear());
        }
        return totalDaysInQuarter;

    }


        /**
         * 根据当前年、月获取每一天日期
         */
    public static List<String> getDayByMonth(int yearParam, int monthParam){
        List<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(yearParam, monthParam-1, 1);
        //年份
        int year = calendar.get(Calendar.YEAR);
        //月份
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String date=null;
            if(month<10 && i<10){
                date = year +"-0"+month+"-0"+i;
            }
            if(month<10 && i>=10){
                date = year +"-0"+month+"-"+i;
            }
            if(month>=10 && i<10){
                date = year +"-"+month+"-0"+i;
            }
            if(month>=10 && i>=10){
                date = year +"-"+month+"-"+i;
            }
            list.add(date);
            if (Objects.equals(date,getYesterday())){
                break;
            }
        }
        return list;
    }

    /**
     * 获取昨天日期
     */
    public static String getYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
        return sp.format(d);
    }

    /**
     * 根据指定的时间获取昨日时间
     */
    public static String getYesterday(String time) {
        LocalDate localDate = LocalDate.parse(time).minusDays(1);
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    /**
     * 获取去年今日
     */
    public static String getLastYearDay(String time) {
        LocalDate localDate = LocalDate.parse(time).minusYears(1);
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }


//    /**
//     * @Description: 根据电压字典获取电压等级
//     * @Param: [id, dictDataList]
//     * @return: java.lang.Double
//     * @Author: clam
//     * @Date: 2023/2/23
//     */
//    public static Double getVoltage(String id, List<DictData> dictDataList) {
//        for (DictData dictData : dictDataList) {
//            if (dictData.getId ( ).equals (id)) {
//                Double result = Double.valueOf (dictData.getValue ( ));
//                return result;
//            }
//        }
//        return null;
//    }

    
}
