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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.alarm.model.auto.ProBasicEquipment;
import com.gyee.alarm.mapper.auto.ProBasicEquipmentMapper;
import com.gyee.alarm.model.auto.ProBasicPowerstation;
import com.gyee.alarm.model.auto.ProBasicProject;
import com.gyee.alarm.service.auto.IProBasicEquipmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.model.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 风机&光伏 服务实现类
 * </p>
 *
 * @author shilin
 * @since 2022-10-31
 */
@Service
public class ProBasicEquipmentServiceImpl extends ServiceImpl<ProBasicEquipmentMapper, ProBasicEquipment> implements IProBasicEquipmentService {

    @Override
    public List<ProBasicEquipment> windturbineList() {
        List<ProBasicEquipment> list = baseMapper.selectList(null);
        return list;
    }

    @Override
    public List<ProBasicEquipment> selectWindturbineByStationid(String stationId) {
        List<ProBasicEquipment> lst = selectWindturbineByCondition2(stationId);

        Map<Integer, ProBasicEquipment> map = new HashMap<>();
        if (lst.size()<=0) {
            return lst;
        }
        for(int i = 0;i<lst.size();i++) {
            String s = lst.get(i).getName();
            String regEx = "[^0-9]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(s);
            String trim = m.replaceAll("").trim();
            int i1 = Integer.parseInt(trim);
            map.put(i1,lst.get(i));
        }
        List<Map.Entry<Integer, ProBasicEquipment>> entryList1 = new ArrayList<Map.Entry<Integer, ProBasicEquipment>>(map.entrySet());
        Collections.sort(entryList1, new Comparator<Map.Entry<Integer, ProBasicEquipment>>() {
            @Override
            public int compare(Map.Entry<Integer, ProBasicEquipment> o1, Map.Entry<Integer, ProBasicEquipment> o2) {
                return o1.getKey().compareTo(o2.getKey()); // 升序排序
            }
        });
        List<ProBasicEquipment> list = new ArrayList<>();
        for (int i = 0;i<entryList1.size();i++){
            Map.Entry<Integer, ProBasicEquipment> maps = entryList1.get(i);
            list.add(maps.getValue());
        }
        return list;
    }


    public List<ProBasicEquipment> selectWindturbineByCondition2(String stationId) {
        QueryWrapper<ProBasicEquipment> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(stationId)) {
            qw.lambda().eq(ProBasicEquipment::getWindpowerstationId, stationId);
        }
        List<ProBasicEquipment> list = baseMapper.selectList(qw);
        return list;
    }
}
