package com.mvc.service.external;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TAireadMapper;
import com.mvc.entity.TAiread;
import com.mvc.model.*;
import com.mvc.model.external.UNCRequestModel;
import com.mvc.model.external.UNCResponseModel;
import com.mvc.service.TAffiliatedunitService;
import com.mvc.service.copy.TAireadService;
import com.mvc.service.taizhang.StationAccService;
import com.mvc.utils.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 中北大学对外接口处理
 */
@Service
public class UNCRequestService {
    @Resource
    private StationAccService stationAccService;

    @Resource
    private TAireadMapper aireadMapper;

    @Resource
    private TAireadService aireadService;

    @Resource
    private TAffiliatedunitService affiliatedunitService;

    /**
     * -----一户多表,合并计算的方式  开始-----
     */
    public UNCResponseModel queryDataByAffiliatedUnit(TAffiliatedunitModel affiliatedunitModel) {
        UNCResponseModel resultModel = null;

        List<StationAccModel> accModels = stationAccService.findStationAccByUnitCode(affiliatedunitModel.getUnitCode());

        BigDecimal sum = BigDecimal.ZERO;
        Date queryTime = DateUtil.getZeroTimeOfDay(new Date());//当日0点
        queryTime = DateUtil.getOtherDateByMinute(queryTime, -1);//昨日23:59(为了让用户取得昨日的数据)
        //Date yesterday = DateUtil.getZeroTimeOfDay(queryTime);//昨日0点(判断三条数是否相同)

        TAiread airead = null;
        for (StationAccModel accModel : accModels) {
            airead = aireadMapper.findlastTAireadBydatatime(accModel.getStationCode(), queryTime);
            if (airead == null || airead.getResultValue() == null) {//数据不完整
                break;
            }

            sum = sum.add(airead.getResultValue());

            /*if(airead.getSampleTime().compareTo(yesterday)!=0){
                //采样时间不同,是否可用--待定?
            }*/
        }
        if (airead != null) {
            airead.setResultValue(sum);
        }

        resultModel = new UNCResponseModel(airead, affiliatedunitModel.getUnitArea());
        return resultModel;
    }

    public ResPageModel queryDataByPage(UNCRequestModel requestModel, CustomUserModel userModel) {
        ResPageModel resultModels = null;
        List<UNCResponseModel> responseModelList = new ArrayList<>();
        Date queryTime = DateUtil.getZeroTimeOfDay(new Date());//当日0点
        queryTime = DateUtil.getOtherDateByMinute(queryTime, -1);//昨日23:59(为了让用户取得昨日的数据)

        Page<Object> pageObject = PageHelper.startPage(requestModel.getPageNum(), 1000);
        List<TAffiliatedunitModel> unitModels = affiliatedunitService.findAffiliatedunitByCreateUnitCode2(userModel.getUnitCode());
        for (TAffiliatedunitModel unitModel : unitModels) {
            List<StationAccModel> accModels = stationAccService.findStationAccByUnitCode(unitModel.getUnitCode());

            BigDecimal sum = BigDecimal.ZERO;
            TAiread airead = null;
            int num = 0;
            for (StationAccModel accModel : accModels) {
                airead = aireadMapper.findlastTAireadBydatatime(accModel.getStationCode(), queryTime);
                if (airead == null || airead.getResultValue() == null) {
                    break;
                }
                sum = sum.add(airead.getResultValue());
                num++;
            }

            if (num != accModels.size()) {//部分设备无数据
                sum = new BigDecimal(-1);//用-1表示本次数据无效
            }

            if (airead != null) {
                airead.setResultValue(sum);
            }

            UNCResponseModel model = new UNCResponseModel(airead, unitModel.getUnitArea());
            responseModelList.add(model);
        }

        resultModels = new ResPageModel(responseModelList, pageObject);
        return resultModels;
    }
    /** -----一户多表,合并计算的方式  结束----- */


    /** -----一表一号,单独上报的方式  开始----- */
    /**
     * @param requestModel
     * @return
     */
    /*public ResPageModel queryDataByPage(UNCRequestModel requestModel, CustomUserModel userModel) {
        ResPageModel resultModels = null;

        List<StationAccModel> accModels = stationAccService.findAllStationAccListByUnitCode(userModel.getUnitCode());

        if (accModels.isEmpty()) {
            return resultModels;
        }
        List<String> list = new ArrayList<>();
        for (StationAccModel accModel : accModels) {
            list.add(accModel.getStationCode());
        }

        Page<Object> pageObject = PageHelper.startPage(requestModel.getPageNum(), 1000);
        List<TAiread> aireads = aireadMapper.findLatestDataByStationCodes(list.toArray(new String[list.size()]));

        List<UNCResponseModel> responseModelList = new ArrayList<>();
        for (TAiread airead : aireads) {
            Optional<StationAccModel> optModel = accModels.stream().filter(s -> s.getStationCode().equals(airead.getStationCode())).findFirst();
            if (!optModel.isPresent()) {
                continue;
            }
            UNCResponseModel model = new UNCResponseModel(airead, optModel.get().getUserDefinedCode());
            responseModelList.add(model);
        }
        resultModels = new ResPageModel(responseModelList, pageObject);
        return resultModels;
    }*/

    /**
     * 根据站点台账获取数据
     *
     * @param accModel
     * @return
     */
    /*public UNCResponseModel queryDataByDeviceId(StationAccModel accModel) {
        UNCResponseModel resultModel = null;

        TAiread airead = aireadMapper.findlastTAireadBydatatime(accModel.getStationCode(), DateUtil.getZeroTimeOfDay(new Date()));
        if (airead != null && airead.getId() != null) {
            resultModel = new UNCResponseModel(airead, accModel.getUserDefinedCode());
        }

        return resultModel;
    }*/
    /** -----一表一号,单独上报的方式  结束----- */
}
