package com.hai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.dao.SecRoleMapper;
import com.hai.dao.SecRolePermissionRelMapper;
import com.hai.dao.SecUserMapper;
import com.hai.dao.SecUserRoleRelMapper;
import com.hai.entity.*;
import com.hai.service.SecRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("secRoleService")
public class SecRoleServiceImpl implements SecRoleService {

	@Resource
	private SecRoleMapper secRoleMapper;

	@Resource
	private SecUserMapper secUserMapper;


	@Resource
	private SecUserRoleRelMapper secUserRoleRelMapper;

	@Resource
	private SecRolePermissionRelMapper secRolePermissionRelMapper;

	@Override
	public SecRole getRoleByUserId(Long userId) throws Exception {
		if(userId != null){
			SecUserRoleRelExample exp = new SecUserRoleRelExample();
			exp.createCriteria().andUserIdEqualTo(userId);
			List<SecUserRoleRel> rels = secUserRoleRelMapper.selectByExample(exp);
			if(rels != null && rels.size() != 0){
				return secRoleMapper.selectByPrimaryKey(rels.get(0).getRoleId());
			}
		}
		return null;
	}
	@Override
	public SecRole findById(Long sId) {
		return secRoleMapper.selectByPrimaryKey(sId);
	}

	@Override
	public PageInfo<SecRole> findPage(Long companyId, Integer templetType, Integer roleType, String roleName, Integer pageNum, Integer pageSize) {
		SecRoleExample example = new SecRoleExample();
		SecRoleExample.Criteria criteria = example.createCriteria();
		if (roleType != null) {
			criteria.andRoleTypeEqualTo(roleType);
		}
		if (companyId != null) {
			criteria.andCompanyIdEqualTo(companyId);
		}else {
			criteria.andCompanyIdIsNull();
		}
		if (templetType != null) {
			criteria.andTempletTypeEqualTo(templetType);
		}
		criteria.andEnableStatusNotEqualTo(2);
		if (StringUtils.isNotBlank(roleName)) {
			criteria.andRoleNameLike("%" + roleName + "%");
		}
		example.setOrderByClause("templet_type desc,primary_role desc,role_type, create_time desc");
		PageHelper.startPage(pageNum,pageSize);
		return  new PageInfo<SecRole>(secRoleMapper.selectByExample(example));
	}

	@Override
	public SecRole findByName(Long companyId, String roleName) {
		SecRoleExample example = new SecRoleExample();
		SecRoleExample.Criteria criteria = example.createCriteria();
		criteria.andEnableStatusNotEqualTo(2).andRoleNameEqualTo(roleName);
		if (companyId != null) {
			criteria.andCompanyIdEqualTo(companyId);
		}
		List<SecRole> list = secRoleMapper.selectByExample(example);
		if (list == null || list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}

	@Override
	public void addRole(SecRole role) {
		secRoleMapper.insert(role);
	}

	@Override
	public void modifyRole(SecRole role) {
		secRoleMapper.updateByPrimaryKey(role);
	}

	@Override
	public List<SecUser> getUsersByRoleId(Long roleId) {
		SecUserRoleRelExample relExample = new SecUserRoleRelExample();
		relExample.createCriteria().andRoleIdEqualTo(roleId);
		List<SecUserRoleRel> rprs = secUserRoleRelMapper.selectByExample(relExample);
		if(rprs != null && rprs.size() != 0){
			List<Long> userIds = new ArrayList<Long>();
			for(SecUserRoleRel rel:rprs){
				userIds.add(rel.getUserId());
			}
			SecUserExample userExample = new SecUserExample();
			userExample.createCriteria().andIdIn(userIds);
			return secUserMapper.selectByExample(userExample);
		}else {
			return null;
		}
	}

	@Override
	public List<SecRole> getAllRole(Long companyId, List<Integer> roleTypes, Integer primaryRole, Integer templetType) {
		SecRoleExample example = new SecRoleExample();
		SecRoleExample.Criteria criteria = example.createCriteria();
		if (companyId != null) {
			criteria.andCompanyIdEqualTo(companyId);
		}else {
			criteria.andCompanyIdIsNull();
		}
		if (roleTypes != null && roleTypes.size() != 0) {
			criteria.andRoleTypeIn(roleTypes);
		}
		if (primaryRole != null) {
			criteria.andPrimaryRoleEqualTo(primaryRole);
		}
		if (templetType != null) {
			criteria.andTempletTypeEqualTo(templetType);
		}
		criteria.andEnableStatusEqualTo(1);
		example.setOrderByClause("primary_role desc,role_type, create_time desc");
		return secRoleMapper.selectByExample(example);
	}

	@Override
	public SecRole findMainRole(Long companyId) {
		SecRoleExample example = new SecRoleExample();
		example.createCriteria().andCompanyIdEqualTo(companyId).andEnableStatusEqualTo(1).andPrimaryRoleEqualTo(1);
		List<SecRole> roleList = secRoleMapper.selectByExample(example);
		if (roleList != null && roleList.size() == 1) {
			return roleList.get(0);
		}else {
			return null;
		}
	}
}
