package org.jeecg.modules.system.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysPermissionDataRule;
import org.jeecg.modules.system.entity.SysRolePermission;
import org.jeecg.modules.system.mapper.SysPermissionDataRuleMapper;
import org.jeecg.modules.system.service.ISysPermissionDataRuleService;
import org.jeecg.modules.system.service.ISysRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * <p>
 * 菜单权限规则  服务实现类
 * </p>
 *
 * @author huangzhilin
 * @since 2019-04-01
 */
@Service
public class SysPermissionDataRuleImpl extends ServiceImpl<SysPermissionDataRuleMapper, SysPermissionDataRule>
		implements ISysPermissionDataRuleService {

	@Autowired
	private ISysPermissionDataRuleService permRuleService;

	@Resource
	private SysPermissionDataRuleMapper sysPermissionDataRuleMapper;

	@Resource
	private ISysRolePermissionService sysRolePermissionService;

	/**
	 * 根据菜单id查询其对应的权限数据
	 */
	@Override
	public List<SysPermissionDataRule> getPermRuleListByPermId(String permissionId) {
		LambdaQueryWrapper<SysPermissionDataRule> query = new LambdaQueryWrapper<SysPermissionDataRule>();
		query.eq(SysPermissionDataRule::getPermissionId, permissionId);
		query.orderByDesc(SysPermissionDataRule::getCreateTime);
		List<SysPermissionDataRule> permRuleList = permRuleService.list(query);
		return permRuleList;
	}

	/**
	 * 根据前端传递的权限名称和权限值参数来查询权限数据
	 */
	@Override
	public List<SysPermissionDataRule> queryPermissionRule(SysPermissionDataRule permRule) {
		QueryWrapper<SysPermissionDataRule> queryWrapper = QueryGenerator.initQueryWrapper(permRule, null);
		return permRuleService.list(queryWrapper);
	}

	@Override
	public List<SysPermissionDataRule> queryPermissionDataRules(String username,String permissionId) {
		List<String> idsList = this.baseMapper.queryDataRuleIds(username, permissionId);
		if(idsList==null || idsList.size()==0) {
			return null;
		}
		Set<String> set = new HashSet<String>();
		for (String ids : idsList) {
			if(ids==null) {
				continue;
			}
			String arr[] = ids.split(",");
			for (String id : arr) {
				if(oConvertUtils.isNotEmpty(id)) {
					set.add(id);
				}
			}
		}
		return this.baseMapper.selectList(new QueryWrapper<SysPermissionDataRule>().in("id", set));
	}

	/***
	 * @Title:数据规则授权
	        * @Description:TODO
	        * @param sysRolePermission
	        * @return void
	        * @exception
	        * @author 陈创
	        * @date 2019/7/1 12:47
	        * @updatehistory:
	 *   2019/7/1 12:47 修改
	        */
	@Override
	public void updateDataRule(SysRolePermission sysRolePermission) {
		sysPermissionDataRuleMapper.updateDataRule(sysRolePermission);
	}

	/***
	 * @Title:查询已经授权规则
	        * @Description:TODO
	        * @param [sysRolePermission]
	        * @return java.util.List<java.lang.String>
	        * @exception 
	        * @author 陈创
	        * @date 2019/7/24 17:15
	        * @updatehistory:
	 *   2019/7/24 17:15 新增
	        */
	@Override
	public List<String> getDataRule(SysRolePermission sysRolePermission) {
		//查询角色菜单权限数据
		SysRolePermission sysRolePermission1= null;
		try {
			sysRolePermission1 = sysPermissionDataRuleMapper.getDataRule(sysRolePermission);
			List<String> dataRuleList = new ArrayList<>();
			if (sysRolePermission1 != null && !sysRolePermission1.equals("")) {
				//截取成数组
				String[] dataRules = sysRolePermission1.getDataRuleIds().split(",");
				for (String s : dataRules) {
					dataRuleList.add(s);
				}
			}
			return dataRuleList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
