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.spring.InitialRunner;
import com.gyee.frame.common.support.Convert;
import com.gyee.frame.mapper.auto.WindTurbineTestingPointAiMapper;
import com.gyee.frame.model.auto.WindTurbineTestingPointAi;
import com.gyee.frame.model.auto.WindTurbineTestingPointAiExample;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

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

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

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

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

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

	
	@Override
	public int updateByExample(WindTurbineTestingPointAi record, WindTurbineTestingPointAiExample example) {
		
		return windTurbineTestingPointAi2Mapper.updateByExample(record, example);
	}

	@Override
	public List<WindTurbineTestingPointAi> selectByExample(WindTurbineTestingPointAiExample example) {
		
		return windTurbineTestingPointAi2Mapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(WindTurbineTestingPointAiExample example) {
		
		return windTurbineTestingPointAi2Mapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(WindTurbineTestingPointAiExample example) {
		
		return windTurbineTestingPointAi2Mapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param windTurbineTestingPointAi
	 * @return
	 */
	public int checkNameUnique(WindTurbineTestingPointAi windTurbineTestingPointAi){
		WindTurbineTestingPointAiExample example=new WindTurbineTestingPointAiExample();
		example.createCriteria().andCodeEqualTo(windTurbineTestingPointAi.getCode());
		List<WindTurbineTestingPointAi> list=windTurbineTestingPointAi2Mapper.selectByExample(example);
		return list.size();
	}

	public WindTurbineTestingPointAi getWindTurbineTestingPointAi2(String wtId, String unicode)  {

		WindTurbineTestingPointAi point=new WindTurbineTestingPointAi();
		if(StringUtils.notEmp(wtId) && StringUtils.notEmp(unicode) )
		{
			 if(InitialRunner.wtmap.containsKey(wtId))
			{
				if(InitialRunner.wtPointmap.containsKey(wtId))
				{
					Map<String, WindTurbineTestingPointAi> map=InitialRunner.wtPointmap.get(wtId);
					if(map.containsKey(unicode))
					{
						point=map.get(unicode);
					}
				}
			}
		}

		return point;
	}


	/**
	 * 通过统一编码查询指定场站的点
	 *
	 * @param station     场站
	 * @param wtId        风机ID
	 * @param uniformCode 统一编码
	 * @return
	 */
	public List<WindTurbineTestingPointAi> findPointsByUniformCodeAndStation(String station, String wtId, String uniformCode) {
		WindTurbineTestingPointAiExample example = new WindTurbineTestingPointAiExample();
		example.createCriteria().andUniformcodeEqualTo(uniformCode)
			                    .andWindturbineidEqualTo(wtId)
								.andWindpowerstationidEqualTo(station);

		List<WindTurbineTestingPointAi> list = windTurbineTestingPointAi2Mapper.selectByExample(example);

		return list;
	}

	public List<String> findPointByUniformcode(List<String> list,List<String> models){
		List<String> lists = new ArrayList<>();

		for (String model : models){
			for (String ls : list){
				WindTurbineTestingPointAiExample example = new WindTurbineTestingPointAiExample();
				example.createCriteria().andUniformcodeEqualTo(ls).andModelidLike("%"+model+"%");

				List<WindTurbineTestingPointAi> winds = windTurbineTestingPointAi2Mapper.selectByExample(example);

				if (winds == null || winds.size() == 0)
					continue;

				for (WindTurbineTestingPointAi wind : winds) {
					if (wind != null && wind.getId().contains("FJ")) {
						lists.add(wind.getId() + "$" + wind.getName() + "@" + wind.getModelid());
//						System.out.println(wind.getId() + "," + wind.getModelid());
						break;
					}
				}
			}
		}


		return lists;
	}
}
