package com.gyee.frame.service;

import com.gyee.frame.common.base.BaseService;
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.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;

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

/**
 *  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>{
	@Resource
	private GyeePermissionMapper gyeepermissionMapper;
	
      	   	      	      	      	      	      	      	      	      	


	@Override
	public int deleteByPrimaryKey(String ids) {


			return -1;


	}

	
	@Override
	public GyeePermission selectByPrimaryKey(String id) {
				
			return null;
				
	}


	@Override
	public int updateByPrimaryKeySelective(GyeePermission record) {
		return -1;
	}


	/**
	 * 添加
	 */
	@Override
	public int insertSelective(GyeePermission record) {

		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());


		return -1;
	}


	@Override
	public int updateByExampleSelective(GyeePermission record, GyeePermissionExample example) {

		return -1;
	}


	@Override
	public int updateByExample(GyeePermission record, GyeePermissionExample example) {

		return -1;
	}

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


	@Override
	public long countByExample(GyeePermissionExample example) {

		return -1;
	}


	@Override
	public int deleteByExample(GyeePermissionExample example) {

		return -1;
	}

	/**
	 * 检查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){
		GyeePermissionExample example=new GyeePermissionExample();
		List<GyeePermission> allList=gyeepermissionMapper.selectByExample(example);

		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;
	}


}
