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.GyeeorganMapper;
import com.gyee.frame.model.auto.Gyeeorgan;
import com.gyee.frame.model.auto.GyeeorganExample;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.model.ticket.Labor;
import com.gyee.frame.service.ticket.LaborService;
import com.gyee.frame.util.SnowflakeIdWorker;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 *  GyeeorganService
 * @Title: GyeeorganService.java 
 * @Package com.gyee.frame.service 
 * @author gyee_自动生成
 * @email 1@qq.com
 * @date 2020-04-17 14:18:03  
 **/
@Service
public class GyeeorganService implements BaseService<Gyeeorgan, GyeeorganExample>{
	@Resource
	private GyeeorganMapper gyeeorganMapper;
	@Resource
	private LaborService laborService;;
      	   	      	      	      	      	
	/**
	 * 分页查询
	 * @return
	 */
	 public PageInfo<Gyeeorgan> list(Tablepar tablepar,String name){
	        GyeeorganExample testExample=new GyeeorganExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andDescribesLike("%"+name+"%");
	        }

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

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

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

	
	@Override
	public int updateByExample(Gyeeorgan record, GyeeorganExample example) {
		
		return gyeeorganMapper.updateByExample(record, example);
	}

	@Override
	public List<Gyeeorgan> selectByExample(GyeeorganExample example) {
		
		return gyeeorganMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(GyeeorganExample example) {
		
		return gyeeorganMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(GyeeorganExample example) {
		
		return gyeeorganMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param gyeeorgan
	 * @return
	 */
	public int checkNameUnique(Gyeeorgan gyeeorgan){
		GyeeorganExample example=new GyeeorganExample();
		example.createCriteria().andDescribesEqualTo(gyeeorgan.getDescribes());
		List<Gyeeorgan> list=gyeeorganMapper.selectByExample(example);
		return list.size();
	}
	
	/**
	 * 
		 *@author:Li Miao
		 *
		 *@description: 通过userid查询风场权限
		 *
		 *@param:userid
		 *
		 *@return:List<Gyeeorgan>
		 *
		 *@createTime:2020年4月17日
		 *
	 */
	public List<Gyeeorgan> queryOrganByUserId(String userid){

		Labor labor=laborService.getLaborItem(userid);

		if(null!=labor && StringUtils.isNotEmpty(labor.getDeptnum()))
		{
			String dept=labor.getDeptnum();

			List<Gyeeorgan> ls=new ArrayList<>();
			if(dept.indexOf("电站")>0)
			{
				Gyeeorgan vo=new Gyeeorgan();
				vo.setName("MHS_FDC");
				vo.setDescribes("麻黄山风电场");
				ls.add(vo);
			}else if(dept.indexOf("电站")>0){
				Gyeeorgan vo=new Gyeeorgan();
				switch(dept) {
					case "麻黄山风电场" :
						vo.setName("MHS_FDC");
						vo.setDescribes("麻黄山风电场");
						break;
					case "牛首山风电场" :
						vo.setName("NSS_FDC");
						vo.setDescribes("牛首山风电场");
						break;
					case "青山风电场" :
						vo.setName("QS_FDC");
						vo.setDescribes("青山风电场");
						break;
					case "石板泉风电场" :
						vo.setName("SBQ_FDC");
						vo.setDescribes("石板泉风电场");
						break;
					case "香山风电场" :
						vo.setName("XS_FDC");
						vo.setDescribes("香山风电场");
						break;

					default:
						break;
				}
				ls.add(vo);
			}else {
				Gyeeorgan vo=new Gyeeorgan();
				vo.setName("0");
				vo.setDescribes("国电电力宁夏新能源");
				ls.add(vo);
				for(Windpowerstation wp:InitialRunner.wpls)
				{
						if(wp.getId().endsWith("FDC"))
						{
							Gyeeorgan vo1=new Gyeeorgan();
							vo1.setName(wp.getId());
							vo1.setDescribes(wp.getName());
							ls.add(vo1);
						}
				}
				return ls;
			}
		}
		return gyeeorganMapper.queryOrganByUserId(userid);
	}


	public List<Gyeeorgan> gyeeOrganAll(){

		List<Gyeeorgan> ls=new ArrayList<>();
		Gyeeorgan vo=new Gyeeorgan();
		vo.setName("0");
		vo.setDescribes("国电电力宁夏新能源");
		ls.add(vo);
		for(Windpowerstation wp:InitialRunner.wpls)
		{
			if(wp.getId().endsWith("FDC"))
			{
				Gyeeorgan vo1=new Gyeeorgan();
				vo1.setName(wp.getId());
				vo1.setDescribes(wp.getName());
				ls.add(vo1);
			}
		}
		return ls;
	}

}
