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

import java.util.ArrayList;
import java.util.List;

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.SysRolePermissionMapper;
import org.jeecg.modules.system.service.ISysPermissionDataRuleService;
import org.jeecg.modules.system.service.ISysRolePermissionService;

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 org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author scott
 * @since 2018-12-21
 */
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {

	@Resource
	private SysRolePermissionMapper sysRolePermissionMapper;

	@Resource
	private ISysPermissionDataRuleService iSysPermissionDataRuleService;

	@Override
	public void saveRolePermission(String roleId, String permissionIds) {
//		LambdaQueryWrapper<SysRolePermission> query = new QueryWrapper<SysRolePermission>().lambda().eq(SysRolePermission::getRoleId, roleId);
//		this.remove(query);
//		List<SysRolePermission> list = new ArrayList<SysRolePermission>();
//		String arr[] = permissionIds.split(",");
//		for (String p : arr) {
//			if(oConvertUtils.isNotEmpty(p)) {
//				SysRolePermission rolepms = new SysRolePermission(roleId, p);
//				list.add(rolepms);
//			}
//		}
//		this.saveBatch(list);
		LambdaQueryWrapper<SysRolePermission> query = new QueryWrapper<SysRolePermission>().lambda().eq(SysRolePermission::getRoleId, roleId);
		List<SysRolePermission> list1 = this.list(query);

//		this.remove(query);
//		List<SysRolePermission> list = new ArrayList<SysRolePermission>();
		String arr[] = permissionIds.split(",");
//		for (String p : arr) {
//			if(oConvertUtils.isNotEmpty(p)) {
//				SysRolePermission rolepms = new SysRolePermission(roleId, p);
//				list.add(rolepms);
//			}
//		}
//		this.saveBatch(list);
		//如果有相同的permissionId
		List<String> delList = new ArrayList<String>();
		for (SysRolePermission sysRolePermission : list1) {
			int i = 0;
			for (String p : arr) {
				if(sysRolePermission.getPermissionId().equals(p)){
					i++;
				}
			}
			if(i==0){
				delList.add(sysRolePermission.getId());
			}
		}
		if(!delList.isEmpty()){
			removeByIds(delList);
		}
		List<SysRolePermission> addList = new ArrayList<SysRolePermission>();
		for (String p : arr) {
			int i = 0;
			for (SysRolePermission sysRolePermission : list1) {
				if (sysRolePermission.getPermissionId().equals(p)) {
					i++;
				}
			}
			if(i==0){
				SysRolePermission rolepms = new SysRolePermission(roleId, p);
				addList.add(rolepms);
			}
		}
		if(!addList.isEmpty()) {
			//获取菜单对应的数据权限
			getPermissionDataRule(addList);
			saveBatch(addList);
		}
	}

	//获取菜单对应的数据权限
	public void getPermissionDataRule(List<SysRolePermission> sysRolePermissionList){
		List<String> permissId=new ArrayList<>();//菜单id
		if(sysRolePermissionList!=null&&sysRolePermissionList.size()>0){
			//循环要保存的角色菜单对象
			for (SysRolePermission sysRolePermission:
					sysRolePermissionList) {
				//获取菜单id
				permissId.add(sysRolePermission.getPermissionId());
			}
			//获取对应菜单存在的角色id
			LambdaQueryWrapper<SysPermissionDataRule> lambdaQueryWrapper=new LambdaQueryWrapper<>();
			lambdaQueryWrapper.in(SysPermissionDataRule::getPermissionId,permissId);
			List<SysPermissionDataRule> sysPermissionDataRuleList=iSysPermissionDataRuleService.list(lambdaQueryWrapper);
			if(sysPermissionDataRuleList!=null&&sysPermissionDataRuleList.size()>0){
				//循环角色菜单
				for (SysRolePermission rolePermission:sysRolePermissionList) {
					//循环菜单对应的数据权限
					for(SysPermissionDataRule permissionDataRule:sysPermissionDataRuleList){
						//找到菜单对应的数据权限
						if(rolePermission.getPermissionId().equals(permissionDataRule.getPermissionId())){
							rolePermission.setDataRuleIds(permissionDataRule.getId());
						}
					}
				}
			}

		}
	}

	@Override
	public void updateDataRuleIdsAsNull(SysRolePermission sysRolePermission) {
		sysRolePermissionMapper.updateDataRuleIdsAsNull(sysRolePermission);
	}
}
