package com.gyee.runeconomy.service.auto.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.model.StringUtils;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.mapper.auto.ProEconWtPowerCurveFittingMapper;
import com.gyee.runeconomy.model.auto.ProEconWtPowerCurveFitting;
import com.gyee.runeconomy.service.auto.IProEconWtPowerCurveFittingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 风电机功率拟合曲线 服务实现类
 * </p>
 *
 * @author shilin
 * @since 2022-10-21
 */
@Service
public class ProEconWtPowerCurveFittingServiceImpl extends ServiceImpl<ProEconWtPowerCurveFittingMapper, ProEconWtPowerCurveFitting> implements IProEconWtPowerCurveFittingService {

    @Resource
    private ProEconWtPowerCurveFittingMapper proEconWtPowerCurveFittingMapper;

    @Override
    public int deleteProEconWtPowerCurveFitting() {
        return proEconWtPowerCurveFittingMapper.deleteProEconWtPowerCurveFitting();
    }

    @Override
    public Map<String, List<ProEconWtPowerCurveFitting>> getgroupProEconWtPowerCurveFittingList(String windturbineIds) {

        QueryWrapper<ProEconWtPowerCurveFitting> qw = new QueryWrapper<>();


        if (StringUtils.isNotEmpty(windturbineIds)) {
            String[] wtidArray = windturbineIds.split(",");
            List<String> wtidList = Arrays.asList(wtidArray);
            qw.lambda().in(ProEconWtPowerCurveFitting::getWindturbineId, wtidList);
        } else {
            return null;
        }

        qw.lambda().orderByAsc(ProEconWtPowerCurveFitting::getSpeed);
        System.out.println("开始查询" + System.currentTimeMillis());
        List<ProEconWtPowerCurveFitting> list = baseMapper.selectList(qw);
        System.out.println("查询结束" + System.currentTimeMillis());

        List<ProEconWtPowerCurveFitting> fittingList = list.stream().filter(i -> (i.getSpeed() % Double.valueOf(0.5)) == 0).collect(Collectors.toList());
        ;

        fittingList.stream().forEach(i -> {
            i.setNemCode(CacheContext.wtmap.get(i.getWindturbineId()).getAname());
        });
        Map<String, List<ProEconWtPowerCurveFitting>> map = fittingList.stream().collect(Collectors.groupingBy(ProEconWtPowerCurveFitting::getNemCode));
        return map;
    }
}
