package com.securitycontrol.screen.service.impl;

import com.securitycontrol.common.core.utils.aes.StringHelper;
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;

/**
 * @author Lenovo
 */
@Slf4j
@Service(value = "ConstructionQualityService")
public class ConstructionQualityImpl implements ConstructionQualityService {

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

    /**
     * 施工质量-告警内容
     * @param dto
     * @return List<OperatingQualityVo>
     * &#064;description
     * @author jjLv
     * @date 2024/3/27 19:54
     */
    @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;
    }

    /**
     * 施工质量-质量检测设备
     * @param dto
     * @return List<OperatingQualityVo>
     * &#064;description
     * @author jjLv
     * @date 2024/3/27 19:54
     */
    @Override
    public List<ConstructionQualityVo> getQualityInspection(ConstructionQualityDto dto) {
        return mapper.getQualityInspection(dto);
    }

    /**
     * 施工质量-施工质量列表
     * @param dto
     * @return List<OperatingQualityVo>
     * &#064;description
     * @author jjLv
     * @date 2024/3/27 19:54
     */
    @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;
    }

    /**
     * 查询施工质量历史数据信息
     * @param dto
     * @return
     */
    @Override
    public List<ConstructionQualityVo> getHistoryList(ConstructionQualityDto dto) {
        List<ConstructionQualityVo> list = new ArrayList<>();
        try{
            if(StringHelper.isNotEmpty(dto.getTimes())){
                dto.setStartTime(dto.getTimes().split(" - ")[0]);
                dto.setEndTime(dto.getTimes().split(" - ")[1]);
            }

            list = mapper.getHistoryList(dto);

        }catch (Exception e){
            log.error(e.toString(),e);
        }


        return list;
    }

    /**
     * 施工质量-检测记录对比
     * @param dto
     * @return List<OperatingQualityVo>
     * &#064;description
     * @author jjLv
     * @date 2024/3/27 19:54
     */
    @Override
    public List<ConstructionQualityVo> getDetectionRecord(ConstructionQualityDto dto) {
        final int NUM_MONTHS_IN_YEAR = 12;
        YearMonth currentYearMonth = YearMonth.now();
        YearMonth[] lastTwelveMonths = new YearMonth[12];
        for (int i = 0; i < NUM_MONTHS_IN_YEAR; 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;
    }



}
