package com.gyee.frame.service;

import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyee.frame.common.base.BaseService;
import com.gyee.frame.common.support.ConvertUtil;
import com.gyee.frame.mapper.auto.TvuePermissionRoleMapper;
import com.gyee.frame.mapper.auto.TvueRoleMapper;
import com.gyee.frame.mapper.custom.RoleVueDao;
import com.gyee.frame.model.auto.TvuePermissionRole;
import com.gyee.frame.model.auto.TvuePermissionRoleExample;
import com.gyee.frame.model.auto.TvueRole;
import com.gyee.frame.model.auto.TvueRoleExample;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class VueRoleService implements BaseService<TvueRole, TvueRoleExample> {
	//角色mapper
	@Resource
	private TvueRoleMapper tvueRoleMapper;
	//自定义角色dao
	@Resource
	private RoleVueDao roleVueDao;
	//自动生成的权限角色映射mapper
	@Resource
	private TvuePermissionRoleMapper tvuePermissionRoleMapper;
	
	
	/**
	 * 分页查询

	 * @return
	 */
	 public PageInfo<TvueRole> list(Tablepar tablepar,String searchText){
	        TvueRoleExample testExample=new TvueRoleExample();
	        testExample.setOrderByClause("id+0 DESC");
	        if(searchText!=null&&!"".equals(searchText)){
	        	testExample.createCriteria().andNameLike("%"+searchText+"%");
	        }

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<TvueRole> list= tvueRoleMapper.selectByExample(testExample);
	        PageInfo<TvueRole> pageInfo = new PageInfo<TvueRole>(list);
	        return  pageInfo;
	 }
	 
	 /**
	  * 查询全部角色集合
	  * @return
	  */
	 public List<TvueRole> queryList(){
		 TvueRoleExample tsysRoleExample=new TvueRoleExample();
		 return tvueRoleMapper.selectByExample(tsysRoleExample);
	 }

	
	 /**
	  * 
	  */	
	@Override
	@Transactional
	public int deleteByPrimaryKey(String ids) {
		List<String> lista=ConvertUtil.toListStrArray(ids);
		//先删除角色下面的所有权限
		TvuePermissionRoleExample permissionRoleExample=new TvuePermissionRoleExample();
		permissionRoleExample.createCriteria().andRoleIdIn(lista);
		tvuePermissionRoleMapper.deleteByExample(permissionRoleExample);
		//在删除角色
		TvueRoleExample example=new TvueRoleExample();
		example.createCriteria().andIdIn(lista);
		return tvueRoleMapper.deleteByExample(example);
	}


	
	@Override
	public int insertSelective(TvueRole record) {
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		return tvueRoleMapper.insertSelective(record);
	}
	
	/**
	 * 添加角色绑定权限
	 * @param record 角色信息
	 * @param prem 权限id集合
	 * @return
	 */
	@Transactional
	public int insertRoleAndPrem(TvueRole record,String prem) {
		//添加雪花主键id
		String roleid=SnowflakeIdWorker.getUUID();
		record.setId(roleid);
		//添加权限
		List<String> prems=ConvertUtil.toListStrArray(prem);
		for (String premid : prems) {
			TvuePermissionRole tsysPermissionRole=new TvuePermissionRole(RandomUtil.randomUUID() , roleid, premid);
			tvuePermissionRoleMapper.insertSelective(tsysPermissionRole);
		}
		return tvueRoleMapper.insertSelective(record);
	}

	@Override
	public TvueRole selectByPrimaryKey(String id) {
		
		return tvueRoleMapper.selectByPrimaryKey(id);
	}

	
	@Override
	public int updateByPrimaryKeySelective(TvueRole record) {
		return tvueRoleMapper.updateByPrimaryKeySelective(record);
	}
	
	/**
	 * 修改用户角色 以及下面绑定的权限
	 * @param record
	 * @return
	 */
	@Transactional
	public int updateRoleAndPrem(TvueRole record,String prem) {
		//先删除角色下面的所有权限
		TvuePermissionRoleExample permissionRoleExample=new TvuePermissionRoleExample();
		permissionRoleExample.createCriteria().andRoleIdEqualTo(record.getId());
		tvuePermissionRoleMapper.deleteByExample(permissionRoleExample);
		//添加权限关联
		List<String> prems=ConvertUtil.toListStrArray(prem);
		for (String pre : prems) {
			TvuePermissionRole permissionRole=new TvuePermissionRole(RandomUtil.randomUUID(), record.getId(), pre);
			tvuePermissionRoleMapper.insertSelective(permissionRole);
		}
		
		return tvueRoleMapper.updateByPrimaryKeySelective(record);
	}
	

	
	@Override
	public int updateByExampleSelective(TvueRole record, TvueRoleExample example) {
		
		return tvueRoleMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(TvueRole record, TvueRoleExample example) {
		
		return tvueRoleMapper.updateByExample(record, example);
	}

	@Override
	public List<TvueRole> selectByExample(TvueRoleExample example) {
		
		return tvueRoleMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(TvueRoleExample example) {
		
		return tvueRoleMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(TvueRoleExample example) {
		
		return tvueRoleMapper.deleteByExample(example);
	}
	
	/**
	 * 检查角色name
	 * @param tsysUser
	 * @return
	 */
	public int checkNameUnique(TvueRole tsysRole){
		TvueRoleExample example=new TvueRoleExample();
		example.createCriteria().andNameEqualTo(tsysRole.getName());
		List<TvueRole> list=tvueRoleMapper.selectByExample(example);
		return list.size();
	}
	
	
	/**
	 * 根据用户id查询角色
	 * @param userid
	 * @return
	 */
	public List<TvueRole> queryUserRole(String userid){
		return roleVueDao.queryUserRole(userid);
	}
	
}
