package com.njcn.csdevice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.csdevice.mapper.CsDataSetMapper;
import com.njcn.csdevice.pojo.po.CsDataSet;
import com.njcn.csdevice.pojo.vo.LineTargetVO;
import com.njcn.csdevice.service.ICsDataSetService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 数据集表 服务实现类
 * </p>
 *
 * @author xuyang
 * @since 2023-05-31
 */
@Service
public class CsDataSetServiceImpl extends ServiceImpl<CsDataSetMapper, CsDataSet> implements ICsDataSetService {

    @Override
    public List<CsDataSet> findDataSetByModelId(String modelId, Integer clDev) {
        return this.lambdaQuery()
                .eq(CsDataSet::getPid,modelId)
                .eq(CsDataSet::getClDev,clDev)
                .and(i->i.eq(CsDataSet::getDataType,"Stat").or().isNull(CsDataSet::getDataType))
                .list();
    }

    @Override
    public List<CsDataSet> findDataSetByModelId(String modelId) {
        return this.lambdaQuery()
                .eq(CsDataSet::getPid,modelId)
                .in(CsDataSet::getType, Arrays.asList(0,2))
                .eq(CsDataSet::getStoreFlag,1)
//                .and(i->i.eq(CsDataSet::getDataType,"Stat").or().isNull(CsDataSet::getDataType))
                .orderByAsc(CsDataSet::getIdx)
                .list();
    }

    @Override
    public List<LineTargetVO> getDataSet(String modelId,Integer clDev) {
        return this.baseMapper.getDataSet(modelId,clDev);
    }

    @Override
    public List<CsDataSet> getModuleDataSet(String modelId) {
        return this.lambdaQuery().eq(CsDataSet::getPid,modelId).ne(CsDataSet::getClDev,0).list();
    }

    @Override
    public List<CsDataSet> getDataSetBySetIds(List<String> setIds) {
        return this.lambdaQuery().in(CsDataSet::getId,setIds).list();
    }

    @Override
    public CsDataSet getBaseDataSet(String modelId, Integer clDev) {
        List<CsDataSet> list = this.lambdaQuery()
                .eq(CsDataSet::getPid,modelId)
                .eq(CsDataSet::getClDev,clDev)
                .and(i->i.eq(CsDataSet::getDataType,"Rt").or().isNull(CsDataSet::getDataType))
                .eq(CsDataSet::getStoreFlag,0)
                .list();
        return list.stream().min(Comparator.comparingInt(CsDataSet::getIdx)).get();
    }

    @Override
    public CsDataSet getHarmonicDataSet(String modelId, Integer clDev,Integer target) {
        LambdaQueryWrapper<CsDataSet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CsDataSet::getPid,modelId)
                .eq(CsDataSet::getClDev,clDev)
                .and(i->i.eq(CsDataSet::getDataType,"Rt").or().isNull(CsDataSet::getDataType))
                .eq(CsDataSet::getStoreFlag,0);
        //谐波电压含有率
        if (target == 0) {
            wrapper.eq(CsDataSet::getName,"Ds$Pqd$Rt$HarmV$0".concat(clDev.toString()));
        } else if (target == 1) {
            wrapper.eq(CsDataSet::getName,"Ds$Pqd$Rt$HarmI$0".concat(clDev.toString()));
        } else if (target == 2) {
            wrapper.eq(CsDataSet::getName,"Ds$Pqd$Rt$InHarmV$0".concat(clDev.toString()));
        }
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public CsDataSet getDataSetByIdx(String modelId, Integer idx) {
        return this.lambdaQuery()
                .eq(CsDataSet::getPid,modelId)
                .in(CsDataSet::getIdx, idx)
                .one();
    }

    @Override
    public List<CsDataSet> getDataSetByModelId(List<String> modelList) {
        return this.lambdaQuery()
                .in(CsDataSet::getPid,modelList)
                .list();
    }

}
