package com.njcn.user.service.impl;

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

import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @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<UserScaleVO> getUserScale(LargeScreenParam largeScreenParam) {
        //创建返回VO
        List<UserScaleVO> result = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //获取当前年第一天日期
        Year year = Year.now();
        String firstDay = year.atDay(1).format(formatter);
        //获取当前天字符串日期
        LocalDate today = LocalDate.now();
        String endDay = today.format(formatter);
        //替换属性
        largeScreenParam.setSearchBeginTime(firstDay);
        largeScreenParam.setSearchEndTime(endDay);
        List<UserScaleVO> list = largeScreenMapper.getUserScale(largeScreenParam);
        //获取传入起始月结束月中所有月份
        List<String> monthList = selectDate(largeScreenParam.getSearchBeginTime(),
                largeScreenParam.getSearchEndTime());

        for (String s : monthList) {
            UserScaleVO vo = new UserScaleVO();
            vo.setTimeId(s);
            result.add(vo);
        }
        //集合不为空
        if (list.size()>0){
            for (UserScaleVO userScaleVO : result) {
                for (UserScaleVO scaleVO : list) {
                    if (Objects.equals(scaleVO.getTimeId(),userScaleVO.getTimeId())){
                        userScaleVO.setMonthIncrementNum(scaleVO.getMonthIncrementNum());
                    }
                }
            }
            for (UserScaleVO vo : result) {
                if (vo.getMonthIncrementNum()==null){
                    vo.setMonthIncrementNum(0);
                }
            }
            int count = 0;
            for (int i = 0; i < result.size(); i++) {
                count = count + result.get(i).getMonthIncrementNum();
                result.get(i).setIncrementNum(count);
            }
            return result;
        }else{
            return result;
        }
    }


    /**
     * 获取传入起始月结束月中所有月份
     * @param startDate
     * @param endDate
     * @return
     */
    public List<String> selectDate(String startDate, String endDate) {
        LocalDate start = LocalDate.parse(startDate, DateTimeFormatter.ISO_LOCAL_DATE);
        LocalDate end = LocalDate.parse(endDate, DateTimeFormatter.ISO_LOCAL_DATE);

        List<String> allMonths = new ArrayList<>();
        YearMonth current = YearMonth.from(start);

        while (!current.isAfter(YearMonth.from(end))) {
            allMonths.add(current.format(DateTimeFormatter.ofPattern("yyyy-MM")));
            current = current.plusMonths(1);
        }
        return allMonths;
    }
}
