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

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

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

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatek.frame.base.dto.PermissionDTO;
import com.huatek.frame.base.entity.Permission;
import com.huatek.frame.base.mapper.PermissionDao;
import com.huatek.frame.base.mapper.PermissionMapper;
import com.huatek.frame.base.service.PermissionService;
import com.huatek.frame.base.util.Constants;
import com.huatek.frame.base.vo.MenuVO;
import com.huatek.frame.base.vo.PermissionVO;
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 = PermissionService.class)
@Transactional(rollbackFor = Exception.class)
public class PermissionServiceImpl implements PermissionService {

	private static final String MENU = "menu";

	@Autowired
	private PermissionMapper permissionMapper;

	@Autowired
	private PermissionDao permissionDao;

	@Override
	public Map<String, Object> findPermissionPage(PermissionDTO dto) {
		PageHelper.startPage(dto.getPage(), dto.getLimit());
		dto.setDeleted(Constants.DEFAULT_NO);
		Page<PermissionVO> permissions = permissionMapper.selectPermissionPage(dto);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, permissions.getTotal(), "", permissions.getResult());
	}

	@Override
	public Map<String, Object> addPermission(Permission entity) {
		String permissionCode = entity.getPermission();
		WherePrams where = new WherePrams();
		where.where("permission", permissionCode);
		List<Permission> datas = permissionDao.list(where);
		if (!ObjectUtils.isEmpty(datas)) {
			return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "PROMISSION.PROMISSION.REPETITION", "");
		}
		String pid = entity.getParentId();
		Permission permission = permissionDao.get(pid);
		entity.setId(Uuid.getUUID());
		if (ObjectUtil.isEmpty(permission)) {
			entity.setLevel(1);		
		}else {
			if (permission.getResourceType().equals(MENU)){
				entity.setLevel(permission.getLevel() + 1);			
			}
		}
		entity.setDeleted(Constants.DEFAULT_NO);
		permissionDao.add(entity);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

	@Override
	public Map<String, Object> findPermission(String id) {
		PermissionVO vo = new PermissionVO();
		if (!HuatekTools.isEmpty(id)) {
			Permission entity = permissionDao.get(id);
			BeanUtils.copyProperties(entity, vo);
		}
		WherePrams where = new WherePrams();
		where.where("deleted", Constants.DEFAULT_NO);
		where.and("resource_type", Constants.MENU);
		// 查询所有的菜单 下拉框
		List<Permission> datas = permissionDao.list(where);
		vo.setDatas(datas);
		if (StringUtils.isEmpty(vo.getResourceType())) {
			vo.setResourceType(Constants.MENU);
		}
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
	}

	@Override
	public Map<String, Object> updatePermission(Permission entity) {
		String permissionCode = entity.getPermission();
		WherePrams where = new WherePrams();
		where.where("permission", permissionCode);
		where.and("id", W.NE, entity.getId());
		List<Permission> datas = permissionDao.list(where);
		if (!ObjectUtils.isEmpty(datas)) {
			return ResultUtil.put(ConstantUtil.JWT_CODE_FAIL, "PROMISSION.PROMISSION.REPETITION", "");
		}
		entity.setDeleted(Constants.DEFAULT_NO);
		permissionDao.updateLocal(entity);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", "");
	}

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

	@Override
	public Map<String, Object> findPermissions(String id) {
		Map<String, Object> map = new HashMap<>(1);
		map.put("deleted", Constants.DEFAULT_NO);
		map.put("resourceType", Constants.MENU);
		map.put("roleId", id);
		map.put("level", Constants.LEVEL_ONE);
		// 查询所有的菜单 下拉框
		List<MenuVO> datas = permissionMapper.findMenuTree(map);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", datas);
	}

}