package com.huatek.frame.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.frame.base.dto.RoleDTO;
import com.huatek.frame.base.entity.Group;
import com.huatek.frame.base.entity.Permission;
import com.huatek.frame.base.entity.Role;
import com.huatek.frame.base.entity.RolePermission;
import com.huatek.frame.base.mapper.GroupDao;
import com.huatek.frame.base.mapper.PermissionDao;
import com.huatek.frame.base.mapper.RoleDao;
import com.huatek.frame.base.mapper.RoleMapper;
import com.huatek.frame.base.mapper.RolePermissionDao;
import com.huatek.frame.base.mapper.UserDao;
import com.huatek.frame.base.service.RoleService;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.frame.base.util.Constants;
import com.huatek.frame.base.vo.RoleUserVO;
import com.huatek.frame.base.vo.RoleVO;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.sql.beans.WherePrams;
import com.huatek.torch.sql.where.W;
import com.huatek.torch.util.tools.HuatekTools;
import com.huatek.torch.util.uuid.Uuid;

import cn.hutool.core.util.ObjectUtil;

/**
 * 系统_角色 ServiceImpl
 *
 * @author key_dong
 * @date 2018-7-11 14:03:46
 */
@Component
@Service(interfaceClass = RoleService.class)
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private GroupDao groupDao;

	@Autowired
	private RolePermissionDao rolePermissionDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private PermissionDao permissionDao;

	@Override
	public Map<String, Object> findRolePage(RoleDTO dto) {
		PageHelper.startPage(dto.getPage(), dto.getLimit());
		dto.setDeleted(Constants.DEFAULT_NO);
		Page<RoleVO> roles = roleMapper.selectRolePage(dto);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, roles.getTotal(), "", roles.getResult());
	}

	@Override
	public Map<String, Object> addRole(Role entity) {
		WherePrams prams = new WherePrams();
		prams.where("role", entity.getRole());
		List<Role> lists = roleDao.list(prams);
		if (!ObjectUtils.isEmpty(lists)) {
			return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "ROLE.ROLE.REPETITION", "");
		}
		entity.setId(Uuid.getUUID());
		entity.setDeleted(Constants.DEFAULT_NO);
		roleDao.add(entity);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

	@Override
	public Map<String, Object> findRole(String id) {
		RoleVO vo = new RoleVO();
		if (!HuatekTools.isEmpty(id)) {
			Role entity = roleDao.get(id);
			if (!ObjectUtils.isEmpty(entity)) {
				BeanUtils.copyPropertiesWithIgnoreNull(entity, vo);
			}
		}
		WherePrams where = new WherePrams();
		where.where("deleted", Constants.DEFAULT_NO);
		List<Group> datas = groupDao.list(where);
		vo.setDatas(datas);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
	}

	@Override
	public Map<String, Object> updateRole(Role entity) {
		WherePrams prams = new WherePrams();
		prams.where("role", entity.getRole());
		prams.and("id", W.NE, entity.getId());
		List<Role> lists = roleDao.list(prams);
		if (!ObjectUtils.isEmpty(lists)) {
			return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "ROLE.ROLE.REPETITION", "");
		}
		roleDao.updateLocal(entity);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

	@Override
	public Map<String, Object> deltetRole(String[] ids) {
		for (String id : ids) {
			Role entity = new Role();
			entity.setDeleted(Constants.DEFAULT_YES);
			entity.setId(id);
			roleDao.updateLocal(entity);
		}
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

	@Override
	public Map<String, Object> updateRolePermission(String[] pIds, String rId) {
		List<String> permissionIds = new ArrayList<>(10);
		WherePrams where = new WherePrams();
		where.where("rid", rId);
		rolePermissionDao.del(where);
		List<RolePermission> collections = new ArrayList<>(5);
		for (String pid : pIds) {
			Permission permission = permissionDao.get(pid);
			Permission permissionParent = permissionDao.get(permission.getParentId());
			permissionIds.add(permission.getId());
			if (ObjectUtil.isNotEmpty(permissionParent) && null != permissionParent.getLevel() &&  1 != permissionParent.getLevel()) {
				permissionParent = permissionDao.get(permissionParent.getParentId());
				permissionIds.add(permissionParent.getId());
				if (ObjectUtil.isNotEmpty(permissionParent) && null != permissionParent.getLevel() && 1 != permissionParent.getLevel()) {
					permissionParent = permissionDao.get(permissionParent.getParentId());
					if (ObjectUtil.isNotEmpty(permissionParent) && null != permissionParent.getLevel() && 1 != permissionParent.getLevel()) {
						permissionIds.add(permissionParent.getId());
					}
				}
			}
			collections.add(new RolePermission(pid, rId));
		} 
		HashSet<String> set = new HashSet<>();
		set.addAll(permissionIds);
		permissionIds = new ArrayList<>(set);
		for (String id : permissionIds) {
			collections.add(new RolePermission(id, rId));
		}
		rolePermissionDao.add(collections);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}
//	@Override
//	public Map<String, Object> updateRolePermission(String[] pIds, String rId) {
//		WherePrams where = new WherePrams();
//		where.where("rid", rId);
//		rolePermissionDao.del(where);
//		List<RolePermission> collections = new ArrayList<>(5);
//		for (String pid : pIds) {
//			collections.add(new RolePermission(pid, rId));
//		}
//		rolePermissionDao.add(collections);
//		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
//	}

	@Override
	public Map<String, Object> findRoles(String uid, String token) {
		Map<String, Object> where = new HashMap<>(1);
		where.put("deleted", Constants.DEFAULT_NO);
		where.put("uid", uid);
		String groupId = userDao.get(uid).getGroupId();
		where.put("groupId", groupId);
		List<RoleUserVO> datas = roleMapper.findRoles(where);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", datas);
	}

	@Override
	public List<Role> findRoles() {
		List<Role> lists = roleDao.list(new WherePrams().where("deleted",Constants.DEFAULT_NO));
		return lists;
	}
	
	@Override
	public List<RoleVO> findRoles(String uid) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id", uid);
		map.put("deleted",Constants.DEFAULT_NO);
		List<RoleVO> lists = roleMapper.findUserRoles(map);
		return lists;
	}
}