package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.domain.PcFactory;
import cn.cecep.talroad.domain.analysis.SRaAbNalysisCar;
import cn.cecep.talroad.domain.analysis.SRaAbNalysisLowCar;
import cn.cecep.talroad.domain.analysis.SRaControlScheme;
import cn.cecep.talroad.service.IAmDoorMonitorAccountFiService;
import cn.cecep.talroad.service.IPcFactoryService;
import cn.cecep.talroad.service.analysis.IMonitoringAnalyseService;
import cn.cecep.talroad.service.analysis.ISRaAbNalysisCarService;
import cn.cecep.talroad.service.analysis.ISRaAbNalysisLowCarService;
import cn.cecep.talroad.service.analysis.ISRaControlSchemeService;
import cn.cecep.talroad.vo.AmDoorMonitorAccountFiVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: cjh
 * @Date: 2023-04-01 17:52
 * @Class: MonitoringAnalyseServiceImpl
 * @Deseription:
 * @Version V1.0
 */
@Slf4j
@Service
public class MonitoringAnalyseServiceImpl implements IMonitoringAnalyseService {

    @Autowired
    private IAmDoorMonitorAccountFiService amDoorMonitorAccountFiService;
    @Autowired
    private IPcFactoryService pcFactoryService;
    @Autowired
    private ISRaAbNalysisLowCarService raAbNalysisLowCarService;
    @Autowired
    private ISRaAbNalysisCarService raAbNalysisCarService;
    @Autowired
    private ISRaControlSchemeService raControlSchemeService;

    @Override
    public void doAnalyseHourCarEnterAndOut() {
        List<SRaAbNalysisCar> carList = new ArrayList<>();
        Date start = DateUtils.addDays(new Date(),-1);
        String lastHourBegin = DateUtils.parseDateToStr("yyyy-MM-dd HH:00:00",start);
        String lastHourEnd = DateUtils.parseDateToStr("yyyy-MM-dd HH:59:59",start);
        QueryWrapper<SRaControlScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stop_car","1");
        queryWrapper.lt("control_start_time",start);
        queryWrapper.gt("control_end_time",start);
        List<SRaControlScheme> list = raControlSchemeService.list(queryWrapper);
        if(list!=null&&list.size()>0){
            list.forEach(c ->{
                // 查询每个企业所有管控时间段内国四及以下车辆进出记录
                List<AmDoorMonitorAccountFiVo> resultList = amDoorMonitorAccountFiService.selectCarEntryCount(c.getControlStartTime(),c.getControlEndTime(),lastHourBegin,lastHourEnd);
                // 车辆进出次数不为空
                if(resultList!=null&&resultList.size()>0) {
                    resultList.forEach(r->{
                        PcFactory pc = pcFactoryService.selectPcFactoryById(r.getFactoryId());
                        SRaAbNalysisCar low = new SRaAbNalysisCar();
                        low.setFactoryId(r.getFactoryId());
                        low.setFactoryName(pc.getFactoryName());
                        low.setDistrictCode(pc.getCityCode());
                        low.setDistrictName(pc.getCityName());
                        low.setIndustryCategoryCode(pc.getIndustryCategory());
                        low.setIndustryCategoryName(pc.getIndusName());
                        low.setPerformanceLevel(pc.getPreConLevel());
                        low.setInOutNumber(Long.valueOf(r.getQuantity()));
                        low.setAnalyseTimeStart(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,c.getControlStartTime()));
                        low.setAnalyseTimeEnd(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,c.getControlEndTime()));
                        carList.add(low);
                    });
                }
            });
            if(carList!=null&&carList.size()>0) {
                raAbNalysisCarService.saveBatch(carList);
            }
        }

    }

    @Override
    public void doAnalyseDayCarEnterAndOut() {
        List<SRaAbNalysisLowCar> carList = new ArrayList<>();
        Map<String,SRaAbNalysisLowCar> lowCarMap = new HashMap<>();
        Date current = new Date();
        // 获取前一天时间
        Date start = DateUtils.addHours(current,-1);
        String lastDayBegin = DateUtils.parseDateToStr("yyyy-MM-dd HH:00:00",start);
        String lastDayEnd = DateUtils.parseDateToStr("yyyy-MM-dd HH:59:59",start);
        List<AmDoorMonitorAccountFiVo> list = amDoorMonitorAccountFiService.selectLabelAccount(lastDayBegin,lastDayEnd);

        if(list!=null&&list.size()>0){
            Map<String, List<AmDoorMonitorAccountFiVo>> map = list.stream().collect(Collectors.groupingBy(AmDoorMonitorAccountFiVo::getFactoryId));
            map.forEach((key, voList) -> {
                SRaAbNalysisLowCar low = lowCarMap.get(key);
                if(low==null){
                    low = new SRaAbNalysisLowCar();
                }
                SRaAbNalysisLowCar finalLow = low;
                voList.forEach(vo->{
                    switch (vo.getDischargeStage()){
                        case "V":
                            finalLow.setFiveNumber(Long.valueOf(vo.getQuantity()));
                        case "VI":
                            finalLow.setSixNumber(Long.valueOf(vo.getQuantity()));
                        case "NE":
                            finalLow.setNeNumber(Long.valueOf(vo.getQuantity()));
                            break;
                        default:
                            if (finalLow.getInOutNumber() != null) {
                                finalLow.setInOutNumber(finalLow.getInOutNumber() + Long.valueOf(vo.getQuantity()));
                            } else {
                                finalLow.setInOutNumber(Long.valueOf(vo.getQuantity()));
                            }
                            break;
                    }
                    
                });
                finalLow.setFactoryId(key);
                PcFactory pc = pcFactoryService.selectPcFactoryById(key);
                finalLow.setFactoryName(pc.getFactoryName());
                finalLow.setDistrictCode(pc.getCountryCode());
                finalLow.setDistrictName(pc.getCountryName());
                finalLow.setIndustryCategoryCode(pc.getIndustryCategory());
                finalLow.setIndustryCategoryName(pc.getIndusName());
                finalLow.setPerformanceLevel(pc.getPreConLevel());
                finalLow.setAnalyseTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,lastDayBegin));

                carList.add(finalLow);
            });
            if(carList!=null&&carList.size()>0) {
                raAbNalysisLowCarService.saveBatch(carList);
            }
        }
    }
}
