package com.pqs9000.serviceimpl.report;

import com.njcn.mapper.configuration.OverLimitMapper;
import com.njcn.pojo.configuration.OverLimit;
import com.pqs9000.mapper.report.GetVirtualDataMapper;
import com.pqs9000.pojo.report.Condition;
import com.pqs9000.pojo.report.OverLimitInfo;
import com.pqs9000.service.report.GetOverLimitDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class GetOverLimitDataServiceImpl implements GetOverLimitDataService {
    @Autowired
    private OverLimitMapper overLimitMapper;

    @Autowired
    private GetVirtualDataMapper getVirtualDataMapper;

    @Override
    public OverLimitInfo getOverLimitData(Condition condition) {
        OverLimitInfo overLimitInfo = new OverLimitInfo();
        //查询监测点限值
        Example example = new Example(OverLimit.class);
        example.createCriteria().andEqualTo("myIndex", condition.getPointIndex());
        List<OverLimit> overLimit = overLimitMapper.selectByExample(example);
        overLimitInfo.setOverLimitRate(overLimit);
        //查询时间段内共有多少条记录，并*0.95取整处理，用来计算CP95值 降序*0.05进一位计算
        double count;
        double pltCount;
        double pstCount;
        if(condition.getB()){
             count = Math.ceil(getVirtualDataMapper.getTotalCP95Data(condition).intValue() * 0.05);
             pltCount = Math.ceil(getVirtualDataMapper.getTotalPltCP95Data(condition).intValue() * 0.05);
             pstCount = Math.ceil(getVirtualDataMapper.getTotalPstCP95Data(condition).intValue() * 0.05);
        }else{
             count = Math.ceil(getVirtualDataMapper.getTotalCP95Day(condition).intValue() * 0.05);
             pltCount = Math.ceil(getVirtualDataMapper.getTotalPltCP95Day(condition).intValue() * 0.05);
             pstCount = Math.ceil(getVirtualDataMapper.getTotalPstCP95Day(condition).intValue() * 0.05);
        }
        overLimitInfo.setCount(count);
        overLimitInfo.setPltCount(pltCount);
        overLimitInfo.setPstCount(pstCount);
        return overLimitInfo;
    }
}
