package com.njcn.serviceimpl.excelreport;

import com.njcn.mapper.configuration.OverLimitMapper;
import com.njcn.mapper.excelreport.GetVirtualMapper;
import com.njcn.pojo.configuration.OverLimit;
import com.njcn.pojo.excelreport.ConditionData;
import com.njcn.pojo.excelreport.OverLimitInfoData;
import com.njcn.service.excelreport.GetOverLimitService;
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 GetOverLimitServiceImpl implements GetOverLimitService {
    @Autowired
    private OverLimitMapper overLimitMapper;

    @Autowired
    private GetVirtualMapper getVirtualMapper;

    @Override
    public OverLimitInfoData getOverLimitData(ConditionData conditionData) {
        OverLimitInfoData overLimitInfoData = new OverLimitInfoData();
        //查询监测点限值
        Example example = new Example(OverLimit.class);
        example.createCriteria().andEqualTo("myIndex", conditionData.getPointIndex());
        List<OverLimit> overLimit = overLimitMapper.selectByExample(example);
        overLimitInfoData.setOverLimitRate(overLimit);
        //查询时间段内共有多少条记录，并*0.95取整处理，用来计算CP95值 降序*0.05进一位计算
        //plt单独查询
        String tableName = conditionData.getB() ? "data_v" : "day_v";
        double count = Math.ceil(getVirtualMapper.getTotalCP95(conditionData, tableName).intValue() * 0.05);
        overLimitInfoData.setCount(count);

        String tablename = conditionData.getB() ? "data_plt" : "day_plt";
        double pltCount = Math.ceil(getVirtualMapper.getTotalPltCP95(conditionData, tablename).intValue() * 0.05);
        overLimitInfoData.setPltCount(pltCount);

        String pstName = conditionData.getB() ? "data_flicker" : "day_flicker";
        double pstCount = Math.ceil(getVirtualMapper.getTotalPstCP95(conditionData, pstName).intValue() * 0.05);
        overLimitInfoData.setPstCount(pstCount);

        return overLimitInfoData;
    }
}
