package com.pqs9300.serviceimpl.report;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.njcn.mapper.configuration.OverLimitMapper;
import com.njcn.pojo.configuration.OverLimit;
import com.pqs9300.mapper.report.GetVirtualDataMapper;
import com.pqs9300.pojo.report.Condition;
import com.pqs9300.pojo.report.OverLimitInfo;
import com.pqs9300.service.report.GetOverLimitDataService;

import tk.mybatis.mapper.entity.Example;

@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进一位计算
        //plt单独查询
        String tableName = condition.getB() ? "data_v" : "day_v";
        double count = Math.ceil(getVirtualDataMapper.getTotalCP95(condition, tableName).intValue() * 0.05);
        overLimitInfo.setCount(count);

        String tablename = condition.getB() ? "data_plt" : "day_plt";
        double pltCount = Math.ceil(getVirtualDataMapper.getTotalPltCP95(condition, tablename).intValue() * 0.05);
        overLimitInfo.setPltCount(pltCount);

        String pstName = condition.getB() ? "data_flicker" : "day_flicker";
        double pstCount = Math.ceil(getVirtualDataMapper.getTotalPstCP95(condition, pstName).intValue() * 0.05);
        overLimitInfo.setPstCount(pstCount);

        return overLimitInfo;
    }
}
