package com.community.huiju.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.RoleMapper;
import com.community.huiju.dao.SysRoleMenuMapper;
import com.community.huiju.dao.SysUserRoleMapper;
import com.community.huiju.model.SysRole;
import com.community.huiju.model.SysRoleMenu;
import com.community.huiju.model.SysUserRole;
import com.community.huiju.model.TpTransaction;
import com.community.huiju.service.RoleServiceI;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author FXF
 * @date 2019-01-16
 */
@Service("roleService")
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, SysRole> implements RoleServiceI {
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;

	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;

	/**
	 * 获取角色列表
	 *
	 * @param roleName
	 * @param userElement
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public ResponseBean getRoleList(String roleName, UserElement userElement, Integer pageNum, Integer pageSize) {
		ResponseBean responseBean = new ResponseBean();
		Page page = new Page(pageNum,pageSize);
		// 分页查询
		SysRole role = new SysRole();
		role.setRoleName(roleName);
		role.setCommunityId(userElement.getCommunityId());
		List<SysRole> list = roleMapper.getRoleListByParams(page,role);
		Map<String, Object> map = Maps.newHashMap();
		map.put("list",list);
		map.put("total",page.getTotal());
		responseBean.addSuccess(map);
		return responseBean;
	}
	
	/**
	 * 添加角色
	 *
	 * @param sysRole
	 * @param userElement
	 * @return
	 */
	@Override
	public ResponseBean addRole(SysRole sysRole, UserElement userElement) {
		ResponseBean responseBean = new ResponseBean();
		//接收数据
		sysRole.setCommunityId(userElement.getCommunityId());
		Integer userId = userElement.getId();
		LocalDateTime dateTime = LocalDateTime.now();
		sysRole.setCreateUser(userId);
		sysRole.setCreateDate(dateTime);
		//校验角色名不能相同
		SysRole checkSysRole = roleMapper.selectByRoleName(sysRole.getRoleName(),userElement.getCommunityId());
		if (null != checkSysRole){
			responseBean.addError("存在重复的角色名称");
			return responseBean;
		}
		//插入角色表
		int size = roleMapper.insert(sysRole);
		if (size < 1){
			responseBean.addError("插入失败");
			return responseBean;
		}
		//维护角色与menu的关系表
		int count = addSysRoleMenu(sysRole);
		if (count < 1){
			responseBean.addError("插入失败");
			return responseBean;
		}
		responseBean.addSuccess(size);
		return responseBean;
	}
	
	private int addSysRoleMenu(SysRole sysRole) {
		List<SysRoleMenu> sysRoleMenuList = new ArrayList<SysRoleMenu>();
		List<Integer> menuIdList = sysRole.getMenuArray();

		menuIdList.stream().forEach(menuId -> {
			SysRoleMenu sysRoleMenu = new SysRoleMenu();
			sysRoleMenu.setCommunityId(sysRole.getCommunityId());
			sysRoleMenu.setRoleId(sysRole.getId());
			sysRoleMenu.setMenuId(menuId);
			sysRoleMenuList.add(sysRoleMenu);
		});
		return sysRoleMenuMapper.insertList(sysRoleMenuList);
	}
	
	/**
	 * 获取角色详情
	 *
	 * @param id
	 * @param userElement
	 * @return
	 */
	@Override
	public ResponseBean getRoleInfo(Integer id, UserElement userElement) {
		ResponseBean responseBean = new ResponseBean();
		//查询出这条角色信息
		SysRole sysRole = roleMapper.selectById(id);
		QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("role_id", id);
		queryWrapper.eq("community_id", sysRole.getCommunityId());
		List<Integer> menuArray = new ArrayList<Integer>();
		//查询这条角色对应的权限
		List<SysRoleMenu> sysRoleMenuList = sysRoleMenuMapper.selectList(queryWrapper);
		sysRoleMenuList.stream().forEach(SysRoleMenu -> {
			menuArray.add(SysRoleMenu.getMenuId());
		});
		sysRole.setMenuArray(menuArray);
		responseBean.addSuccess(sysRole);
		return responseBean;
	}
	
	/**
	 * 修改角色
	 *
	 * @param sysRole
	 * @param userElement
	 * @return
	 */
	@Override
	public ResponseBean updateRole(SysRole sysRole, UserElement userElement) {
		ResponseBean responseBean = new ResponseBean();
		//接收数据
		sysRole.setCommunityId(userElement.getCommunityId());
		sysRole.setUpdateUser(userElement.getId());
		sysRole.setUpdateDate(LocalDateTime.now());
		//校验角色名不能相同
		SysRole checkSysRole = roleMapper.selectByRoleName(sysRole.getRoleName(),userElement.getCommunityId());
		if (null != checkSysRole && !checkSysRole.getId().equals(sysRole.getId())){
			responseBean.addError("存在重复的角色名称");
			return responseBean;
		}
		//更新角色信息
		int size = roleMapper.updateById(sysRole);
		if (size < 1){
			responseBean.addError("更新失败");
			return responseBean;
		}
		//维护角色与menu的关系表,先全部删除,在重新添加
		QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("role_id", sysRole.getId());
		queryWrapper.eq("community_id", sysRole.getCommunityId());
		sysRoleMenuMapper.delete(queryWrapper);

		//重新添加
		int count = addSysRoleMenu(sysRole);
		if (count < 1){
			responseBean.addError("更新失败");
			return responseBean;
		}
		responseBean.addSuccess(size);
		return responseBean;
	}
	
	/**
	 * 批量删除角色
	 *
	 * @param sysRole
	 * @param userElement
	 * @return
	 */
	@Override
	public ResponseBean deleteRole(SysRole sysRole, UserElement userElement) {
		ResponseBean responseBean = new ResponseBean();
		List<Integer> sysRoleIdList = sysRole.getIdArray();
		//删除角色
		int size = roleMapper.deleteBatchIds(sysRoleIdList);
		if (size < 1){
			responseBean.addError("删除失败");
			return responseBean;
		}
		//维护角色与menu的关系表,全部删除
		sysRoleIdList.stream().forEach(roleId -> {
			QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("role_id", roleId);
			queryWrapper.eq("community_id", userElement.getCommunityId());
			sysRoleMenuMapper.delete(queryWrapper);

			// 删除角色和用户关系
			QueryWrapper<SysUserRole> deleteQueryWrapper = new QueryWrapper<>();
			deleteQueryWrapper.eq("role_id", roleId);
			deleteQueryWrapper.eq("community_id", userElement.getCommunityId());
			sysUserRoleMapper.delete(deleteQueryWrapper);
		});

		responseBean.addSuccess(size);
		return responseBean;
	}
}
