package com.mvc.service.extend;

import com.mvc.entity.TAiread;
import com.mvc.model.StationAccModel;
import com.mvc.model.TAffiliatedunitModel;
import com.mvc.service.TAffiliatedunitService;
import com.mvc.service.copy.TAireadService;
import com.mvc.service.external.ZbdxDataService;
import com.mvc.service.taizhang.StationAccService;
import com.mvc.utils.DateUtil;
import com.mvc.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Component
public class CompleteData {
    private static Logger log = LoggerFactory.getLogger(ZbdxDataService.class);

    @Resource
    private TAireadService aireadService;

    @Resource
    private StationAccService stationAccService;

    @Resource
    private TAffiliatedunitService affiliatedunitService;

    private static final List<String> UNIT_CODE_LIST = new ArrayList<>(List.of("ce0d78d2e18f4016ac4153f0cc9be356", "547bb32cf52441809c93e8ce161a876a"));

    /**
     * 补数据(前值法)
     */
    public void completeDataIfIncomplete() {
        Date today = DateUtil.getZeroTimeOfDay(new Date());//今日0点
        Date yesterday = DateUtil.getOtherDateByCurrentDate(today, -1);//昨日0点
        Date dayBeforeYesterday = DateUtil.getOtherDateByCurrentDate(today, -2);//前日0点

        for (String unitCode : UNIT_CODE_LIST) {
            TAffiliatedunitModel unitModel = affiliatedunitService.findTAffiliatedunit(unitCode);
            if(unitModel.getUnitType().equals("4")){//4级单位,无子级
                List<StationAccModel> accModels = stationAccService.findStationAccByUnitCode(unitCode);
                processDataByStationAcc(yesterday, dayBeforeYesterday, accModels);
            }else {
                List<TAffiliatedunitModel> unitModels = affiliatedunitService.findAffiliatedunitByCreateUnitCode2(unitCode);
                for (TAffiliatedunitModel model : unitModels) {
                    List<StationAccModel> accModels = stationAccService.findStationAccByUnitCode(model.getUnitCode());
                    processDataByStationAcc(yesterday, dayBeforeYesterday, accModels);
                }
            }
        }
    }

    private void processDataByStationAcc(Date yesterday, Date dayBeforeYesterday, List<StationAccModel> accModels) {
        for (StationAccModel accModel : accModels) {
            List<TAiread> aireads = aireadService.findAllTaireadByStationCodeAndTime(accModel.getStationCode(), dayBeforeYesterday, yesterday);
            Optional<TAiread> optional = aireads.stream().filter(s -> s.getSampleTime().equals(yesterday)).findFirst();
            if (optional.isPresent()) {//有昨日数据不处理
                continue;
            }

            optional = aireads.stream().filter(s -> s.getSampleTime().equals(dayBeforeYesterday)).findFirst();
            if (!optional.isPresent()) {//无前日数据不处理(从有前日的数据开始补,以前的先不管) 2024-08-20 guozhao
                continue;
            }

            TAiread airead = new TAiread();
            BeanUtils.copyProperties(optional.get(), airead);
            airead.setId(null);
            airead.setAireadId(StringUtil.getUuid());
            airead.setSampleTime(yesterday);//采样时间设置为昨日
            airead.setRptTime(DateUtil.getOtherDateByCurrentDate(airead.getRptTime(), 1));//前天发报时间+1天
            airead.setRevTime(DateUtil.getOtherDateByCurrentDate(airead.getRevTime(), 1));
            airead.setSoftVer(null);//去掉版本来来表示补的数据
            airead.setAiVersion(null);
            airead.setRawValue(null);
            airead.setIdentValue(null);

            aireadService.addTAiread(airead);
            log.info("前值补数据成功,站点:" + accModel.getStationCode() + ",时间:" + DateUtil.formateDate(yesterday, "yyyy-MM-dd"));
        }
    }
}
