package com.gyee.wisdom.alarm.sharding.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.wisdom.alarm.sharding.entity.AlarmCount;
import com.gyee.wisdom.alarm.sharding.entity.Windturbine;
import com.gyee.wisdom.alarm.sharding.mapper.AlarmCountMapper;
import com.gyee.wisdom.alarm.sharding.mapper.WindturbineMapper;
import com.gyee.wisdom.alarm.sharding.model.WindturbineExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author Wang Jiawen
 * @purpose
 * @date 2019-08-30 16:45
 */
@Service
@Slf4j
public class WindturbineService extends ServiceImpl<WindturbineMapper, Windturbine> {
      @Autowired
      private WindturbineMapper windturbineMapper;
      public List<Windturbine> windturbineList(){
         return windturbineMapper.selectWindturbines();
      }
      public List<Windturbine> selectByExample(WindturbineExample example) {
        return windturbineMapper.selectByExample(example);
    }

    public List< Windturbine> selectWindturbineByStationid(String stationId) {
        List<Windturbine> lst = windturbineMapper.selectWindturbineByCondition2(stationId);
        Map<Integer, Windturbine> 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, Windturbine>> entryList1 = new ArrayList<Map.Entry<Integer, Windturbine>>(map.entrySet());
        Collections.sort(entryList1, new Comparator<Map.Entry<Integer, Windturbine>>() {
            @Override
            public int compare(Map.Entry<Integer, Windturbine> o1, Map.Entry<Integer, Windturbine> o2) {
                return o1.getKey().compareTo(o2.getKey()); // 升序排序
            }
        });
        List<Windturbine> list = new ArrayList<>();
        for (int i = 0;i<entryList1.size();i++){
            Map.Entry<Integer, Windturbine> maps = entryList1.get(i);
            list.add(maps.getValue());
        }
        return list;
    }

    public Windturbine selectWindturbineByWindturbineid(String windturbineid) {
        return windturbineMapper.selectById(windturbineid);
    }
}
