package com.gyee.frame.service;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.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.GyeepermissionMapper;
import com.gyee.frame.model.auto.Gyeepermission;
import com.gyee.frame.model.auto.GyeepermissionExample;
import com.gyee.frame.model.auto.GyeepermissionView;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.SnowflakeIdWorker;

/**
 *  GyeepermissionService
 * @Title: GyeepermissionService.java 
 * @Package com.gyee.frame.service 
 * @author gyee_自动生成
 * @email 1@qq.com
 * @date 2020-04-16 14:55:15  
 **/
@Service
public class GyeepermissionService implements BaseService<Gyeepermission, GyeepermissionExample>{
	@Autowired
	private GyeepermissionMapper gyeepermissionMapper;
	
      	   	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Gyeepermission> list(Tablepar tablepar,String name){
	        GyeepermissionExample testExample=new GyeepermissionExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andPermisssionnameLike("%"+name+"%");
	        }

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

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

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

	
	@Override
	public int updateByExample(Gyeepermission record, GyeepermissionExample example) {
		
		return gyeepermissionMapper.updateByExample(record, example);
	}

	@Override
	public List<Gyeepermission> selectByExample(GyeepermissionExample example) {
		
		return gyeepermissionMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(GyeepermissionExample example) {
		
		return gyeepermissionMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(GyeepermissionExample example) {
		
		return gyeepermissionMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param gyeepermission
	 * @return
	 */
	public int checkNameUnique(Gyeepermission gyeepermission){
		GyeepermissionExample example=new GyeepermissionExample();
		example.createCriteria().andPermisssionnameEqualTo(gyeepermission.getPermisssionname());
		List<Gyeepermission> list=gyeepermissionMapper.selectByExample(example);
		return list.size();
	}

	/**
	 * 
		 *@author:Li Miao
		 *
		 *@description: 查询模块权限
		 *
		 *@param:roleId
		 *
		 *@return:List<GyeepermissionView>
		 *
		 *@createTime:2020年4月20日
		 *
	 */
	public List<GyeepermissionView> queryPermissionByRoleId(String userid){
		List<Gyeepermission> allList=gyeepermissionMapper.queryPermissionByRoleId(userid);
		List<GyeepermissionView> list=new ArrayList();		
		for(int i=0;i<allList.size();i++){
			if(allList.get(i).getParent().equals("-1")){
				GyeepermissionView gyeePermissionView=new GyeepermissionView();
				gyeePermissionView.setId(allList.get(i).getId());
				gyeePermissionView.setPermisssionlevel(allList.get(i).getPermisssionlevel());
				gyeePermissionView.setPermisssionname(allList.get(i).getPermisssionname());
				gyeePermissionView.setDescribes(allList.get(i).getDescribes());
				gyeePermissionView.setRemark1(allList.get(i).getRemark1());
				gyeePermissionView.setRemark2(allList.get(i).getRemark2());
				gyeePermissionView.setParent(allList.get(i).getParent());
				gyeePermissionView.setIsable(allList.get(i).getIsable());
				List<GyeepermissionView> layerList=new ArrayList();
				layerList=this.getPermission(allList, allList.get(i).getId());
				gyeePermissionView.setGyeePermissionList(layerList);
				list.add(gyeePermissionView);
			}
			
		}
		return list;
	}
	
	/**
	 * 
		 *@author:Li Miao
		 *
		 *@description: 得到下一级的模块权限,递归算法
		 *
		 *@param:allList,parent
		 *
		 *@return:List<GyeepermissionView>
		 *
		 *@createTime:2020年4月20日
		 *
	 */
	public List<GyeepermissionView> getPermission(List<Gyeepermission> allList,String parent){
		List<GyeepermissionView> layerList=new ArrayList();
		for(int i=0;i<allList.size();i++){
			if(allList.get(i).getParent().equals(parent)){
				GyeepermissionView gyeePermissionView=new GyeepermissionView();
				gyeePermissionView.setId(allList.get(i).getId());
				gyeePermissionView.setPermisssionlevel(allList.get(i).getPermisssionlevel());
				gyeePermissionView.setPermisssionname(allList.get(i).getPermisssionname());
				gyeePermissionView.setDescribes(allList.get(i).getDescribes());
				gyeePermissionView.setRemark1(allList.get(i).getRemark1());
				gyeePermissionView.setRemark2(allList.get(i).getRemark2());
				gyeePermissionView.setParent(allList.get(i).getParent());
				gyeePermissionView.setIsable(allList.get(i).getIsable());
				List<GyeepermissionView> nextLayerList=new ArrayList();
				nextLayerList=this.getPermission(allList, allList.get(i).getId());
				gyeePermissionView.setGyeePermissionList(nextLayerList);
				layerList.add(gyeePermissionView);
			}
		}
		return layerList;
	}


}
