package com.njcn.system.service.impl;

import com.njcn.device.pq.pojo.param.LargeScreenParam;
import com.njcn.device.pq.pojo.vo.DataScaleVO;
import com.njcn.system.mapper.LargeScreenMapper;
import com.njcn.system.service.LargeScreenService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @version 1.0.0
 * @author: zbj
 * @date: 2023/04/10
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LargeScreenServiceImpl implements LargeScreenService {

    private final LargeScreenMapper largeScreenMapper;

    /**
     * 数据规模
     */
    @Override
    public List<DataScaleVO> getDataScale(LargeScreenParam largeScreenParam) {
        //创建返回VO
        List<DataScaleVO> result = new ArrayList<>();
        //创建模板
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //获取当前日期
        LocalDate today = LocalDate.now();
        String endTime = today.format(formatter);
        String startTime = "";
        //获取30天前的日期
        try {
            startTime = getDate(endTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        largeScreenParam.setSearchBeginTime(startTime);
        largeScreenParam.setSearchEndTime(endTime);
        List<DataScaleVO> list = largeScreenMapper.getDataScale(largeScreenParam);
        //获取区间中所有日期
        List<String> days = null;
        try {
            days = selectDate(startTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //加上今天
        days.add(endTime);
        for (String s : days) {
            DataScaleVO vo = new DataScaleVO();
            vo.setTimeId(s);
            result.add(vo);
        }
        //集合不为空
        if (list.size() > 0) {
            for (DataScaleVO dataScaleVO : result) {
                for (DataScaleVO scaleVO : list) {
                    if (Objects.equals(scaleVO.getTimeId(), dataScaleVO.getTimeId())) {
                        dataScaleVO.setDataStatis(scaleVO.getDataStatis());
                    }
                }
            }
            for (DataScaleVO vo : result) {
                if (vo.getDataStatis() == null) {
                    vo.setDataStatis(0.0f);
                }
            }
            return result;
        } else {
            return result;
        }
    }

    /**
     * 传入字符串日期获取当前日期30天日期
     *
     * @param stringDate
     * @throws Exception
     */
    public static String getDate(String stringDate) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(stringDate); // 将字符串日期转换为Date对象
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -30); // 计算30天前的日期
        Date newDate = calendar.getTime(); // 获取新日期
        String newDateStr = sdf.format(newDate); // 将新日期转换为字符串日期
        return newDateStr;
    }

    /**
     * 获取传入起始月结束月中所有天
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<String> selectDate(String startTime, String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date startDate = sdf.parse(startTime);
        Date endDate = sdf.parse(endTime);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);

        List<String> dates = new ArrayList<>();
        while (calendar.getTime().before(endDate)) {
            Date date = calendar.getTime();
            String dateStr = sdf.format(date);
            dates.add(dateStr);
            calendar.add(Calendar.DATE, 1);
        }
        return dates;
    }
}
