package com.njcn.harmonic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.njcn.device.pq.pojo.param.PulicTimeStatisParam;
import com.njcn.harmonic.mapper.RStatComassesDMapper;
import com.njcn.harmonic.pojo.po.PQSComAssesPO;
import com.njcn.harmonic.pojo.po.PqsComasses;
import com.njcn.harmonic.pojo.vo.AssesVO;
import com.njcn.harmonic.service.AssesService;
import com.njcn.harmonic.util.ComAssesUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author denghuajun
 * @date 2022/3/2
 *
 */
@Service
@AllArgsConstructor
public class AssesServiceImpl implements AssesService {


    private final ComAssesUtil comAssesUtil;

    private final RStatComassesDMapper rStatComassesDMapper;
    @Override
    public AssesVO getQualityAssessData(PulicTimeStatisParam pulicTimeStatisParam) {
        AssesVO assesVO = new AssesVO();
        List<Float> floatList = new ArrayList<>();
        Float syn = getCondition(pulicTimeStatisParam.getId(), pulicTimeStatisParam.getSearchBeginTime(), pulicTimeStatisParam.getSearchEndTime());
        floatList.add(syn);
        if (StringUtils.isNotBlank(pulicTimeStatisParam.getPeriodBeginTime()) && StringUtils.isNotBlank(pulicTimeStatisParam.getPeriodEndTime())) {
            floatList.add(getCondition(pulicTimeStatisParam.getId(), pulicTimeStatisParam.getPeriodBeginTime(), pulicTimeStatisParam.getPeriodEndTime()));
        }
        assesVO.setSyn(floatList);
        return assesVO;
    }

    /**
     * 查询监测点的数据完整性
     */
    private Float getCondition(String lineList, String startTime, String endTime) {
        float synData;
        if (!lineList.isEmpty()) {
            List<PQSComAssesPO> avgCount = rStatComassesDMapper.getAvgCount(Arrays.asList(lineList),
                    DateUtil.beginOfDay(DateUtil.parse(startTime)).toString(),
                    DateUtil.beginOfDay(DateUtil.parse(endTime)).toString());
            List<PqsComasses> communicateList = BeanUtil.copyToList(avgCount,PqsComasses.class);
            if (!CollectionUtils.isEmpty(communicateList)) {
                synData = comAssesUtil.getAllComAss(communicateList);
            } else {
                //有监测点，但是无数据的处理
                return 3.14159f;
            }
        } else {
            //无监测点前台统一替换为“/”
            return 3.1415f;
        }
        return synData;
    }
}
