package com.securitycontrol.screen.service.impl;

import com.securitycontrol.entity.screen.dto.ConstructionQualityDto;
import com.securitycontrol.entity.screen.vo.ConstructionQualityVo;
import com.securitycontrol.screen.mapper.ConstructionQualityMapper;
import com.securitycontrol.screen.service.ConstructionQualityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service(value = "ConstructionQualityService")
public class ConstructionQualityImpl implements ConstructionQualityService {

    @Resource(name = "ConstructionQualityMapper")
    private ConstructionQualityMapper mapper;


    @Override
    public List<ConstructionQualityVo> getConstructionAlarm(ConstructionQualityDto dto) {
        List<ConstructionQualityVo> list = new ArrayList<>();
        try {
            list = mapper.getConstructionAlarm(dto);
        } catch (Exception e) {
            log.error("获取告警管理列表",e);
        }
        return list;
    }

    @Override
    public List<ConstructionQualityVo> getQualityInspection(ConstructionQualityDto dto) {
        return mapper.getQualityInspection(dto);
    }

    @Override
    public List<ConstructionQualityVo> getConstructionQualityList(ConstructionQualityDto dto) {
        List<ConstructionQualityVo> list = new ArrayList<>();
        try {
            list = mapper.getConstructionQualityList(dto);
        } catch (Exception e) {
            log.error("获取告警管理列表",e);
        }
        return list;
    }

    @Override
    public List<ConstructionQualityVo> getDetectionRecord(ConstructionQualityDto dto) {
        YearMonth currentYearMonth = YearMonth.now();
        YearMonth[] lastTwelveMonths = new YearMonth[12];
        for (int i = 0; i < 12; i++) {
            lastTwelveMonths[i] = currentYearMonth.minusMonths(11 - i);
        }
        List<ConstructionQualityVo> list = new ArrayList<>();
        for (YearMonth lastTwelveMonth : lastTwelveMonths) {
            dto.setMonth(String.valueOf(lastTwelveMonth));
            List<ConstructionQualityVo> byMonth = mapper.getByMonth(dto);
            if (byMonth.isEmpty()) {
                ConstructionQualityVo emptyData = new ConstructionQualityVo();
                emptyData.setDetectionNumber(String.valueOf(0));
                emptyData.setQualificationNumber(String.valueOf(0));
                emptyData.setMonth(String.valueOf(lastTwelveMonth));
                list.add(emptyData);
            } else {
                list.addAll(byMonth);
            }
        }
        return list;
    }



}
