package com.gyee.frame.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.WindpowerstationMapper;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.auto.WindpowerstationExample;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风场 WindpowerstationService
 * @Title: WindpowerstationService.java 
 * @Package com.gyee.frame.service 
 * @author gyee_自动生成
 * @email 1@qq.com
 * @date 2019-12-31 14:58:09  
 **/
@Slf4j
@CacheConfig(cacheNames = "stations")
@Service
public class WindpowerstationService implements BaseService<Windpowerstation, WindpowerstationExample> {
	@Resource
	private WindpowerstationMapper windpowerstationMapper;
	
      	   	      	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param tablepar
	 * @param name
	 * @return
	 */
	 public PageInfo<Windpowerstation> list(Tablepar tablepar, String name){
	        WindpowerstationExample testExample=new WindpowerstationExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andCodeLike("%"+name+"%");
	        }

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<Windpowerstation> list= windpowerstationMapper.selectByExample(testExample);
	        PageInfo<Windpowerstation> pageInfo = new PageInfo<Windpowerstation>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			WindpowerstationExample example=new WindpowerstationExample();
			example.createCriteria().andIdIn(lista);
			return windpowerstationMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public Windpowerstation selectByPrimaryKey(String id) {
				
			return windpowerstationMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(Windpowerstation record) {
		return windpowerstationMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(Windpowerstation record) {
				
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
			
				
		return windpowerstationMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(Windpowerstation record, WindpowerstationExample example) {
		
		return windpowerstationMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(Windpowerstation record, WindpowerstationExample example) {
		
		return windpowerstationMapper.updateByExample(record, example);
	}

	@Override
	public List<Windpowerstation> selectByExample(WindpowerstationExample example) {
		
		return windpowerstationMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(WindpowerstationExample example) {
		
		return windpowerstationMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(WindpowerstationExample example) {
		
		return windpowerstationMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param windpowerstation
	 * @return
	 */
	public int checkNameUnique(Windpowerstation windpowerstation){
		WindpowerstationExample example=new WindpowerstationExample();
		example.createCriteria().andCodeEqualTo(windpowerstation.getCode());
		List<Windpowerstation> list=windpowerstationMapper.selectByExample(example);
		return list.size();
	}

	/**
	 * 查询所有的场站
	 *
	 * @return key:SBQ_FDC  value:石板泉风电场
	 */
	@Cacheable(cacheNames = "findStationAll")
	public List<Map<String, String>> findStationAll() {
		List<Map<String, String>> list = new ArrayList<>();

		WindpowerstationExample example = new WindpowerstationExample();
		example.setOrderByClause("ordernum ASC");

		List<Windpowerstation> stations = windpowerstationMapper.selectByExample(example);
		for (Windpowerstation station : stations){
			// 只显示风场
			if (station.getId().contains("_FDC")) {
				Map<String, String> map = new HashMap<>();
				map.put(station.getId(), station.getName());
				list.add(map);
			}
		}

		return list;
	}
}
