package net.wicp.tams.app.duckula.controller.service.cas.impl;

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

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import net.wicp.tams.app.duckula.controller.bean.constant.UnitType;
import net.wicp.tams.app.duckula.controller.bean.models.SysOrg;
import net.wicp.tams.app.duckula.controller.bean.models.SysRole;
import net.wicp.tams.app.duckula.controller.bean.models.SysRoleAssign;
import net.wicp.tams.app.duckula.controller.bean.models.SysUser;
import net.wicp.tams.app.duckula.controller.dao.SqlMapper;
import net.wicp.tams.app.duckula.controller.dao.SysRoleAssignMapper;
import net.wicp.tams.app.duckula.controller.dao.SysRoleMapper;
import net.wicp.tams.app.duckula.controller.dao.SysUserMapper;
import net.wicp.tams.app.duckula.controller.service.cas.IOrgService;
import net.wicp.tams.app.duckula.controller.service.cas.IStaffService;
import net.wicp.tams.common.constant.dic.YesOrNo;

@Service
public class StaffService implements IStaffService {
	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private SysRoleMapper sysRoleMapper;

	@Autowired
	private IOrgService orgService;
	@Autowired
	private SysRoleAssignMapper sysRoleAssignMapper;

	@Override
	public List<SysUser> findValidStaff(SysUser sysUser) {
		QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
		queryWrapper.eq("status", YesOrNo.yes.name());
		List<SysUser> selectList = sysUserMapper.selectList(queryWrapper);
		return selectList;
	}

	@Override
	public SysUser findValidStaff(long staffId) {
		SysUser sysUser = sysUserMapper.selectById(staffId);
		if (YesOrNo.yes.name().equals(sysUser.getStatus())) {
			return sysUser;
		}
		return null;
	}

	@Override
	public List<SysRole> findRolesByStaff(long staffId) {
		List<SysRole> findRolesByStaff = sysRoleMapper.findRolesByStaff(staffId);
		return findRolesByStaff;
	}

	@Override
	public List<SysRole> findRolesByUnit(UnitType unitType, long objId, Boolean includeAncest) {
		Long[] all = new Long[0];
		if (includeAncest) {
			List<SysOrg> allorg = orgService.findAncestOrg(objId);
			if (CollectionUtils.isNotEmpty(allorg)) {
				for (SysOrg org : allorg) {
					long[] temp = findRoleIdsByUnit(unitType.findByName(org.getUnitType()), org.getId());
					for (int i = 0; i < temp.length; i++) {
						if (!ArrayUtils.contains(all, temp[i])) {
							all = ArrayUtils.add(all, temp[i]);
						}
					}
				}
			}
		} else {
			long[] temp = findRoleIdsByUnit(unitType, objId);
			for (int i = 0; i < temp.length; i++) {
				if (!ArrayUtils.contains(all, temp[i])) {
					all = ArrayUtils.add(all, temp[i]);
				}
			}
		}
		List<SysRole> retList = findRoles(Arrays.asList(all));
		return retList;
	}

	@Override
	public List<SysRole> findValidRole(boolean isSuper) {
		QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
		queryWrapper.eq("status", YesOrNo.yes.name());
		if (!isSuper) {
			queryWrapper.eq("is_super", YesOrNo.no.name());
		}
		List<SysRole> selectList = sysRoleMapper.selectList(queryWrapper);
		return selectList;
	}

	private long[] findRoleIdsByUnit(UnitType unitType, long objId) {
		QueryWrapper<SysRoleAssign> queryWrapper = new QueryWrapper<SysRoleAssign>();
		queryWrapper.eq("assign_type", unitType.name());
		queryWrapper.eq("assign_value", objId);
		List<SysRoleAssign> selectList = sysRoleAssignMapper.selectList(queryWrapper);
		if (CollectionUtils.isNotEmpty(selectList)) {
			long[] ret = new long[selectList.size()];
			for (int i = 0; i < ret.length; i++) {
				ret[i] = selectList.get(i).getId().longValue();
			}
			return ret;
		}
		return new long[0];
	}

	private List<SysRole> findRoles(List<Long> roleIds) {
		List<SysRole> retlist = new ArrayList<SysRole>();
		if (CollectionUtils.isNotEmpty(roleIds)) {
			QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
			queryWrapper.eq("status", YesOrNo.yes.name());
			queryWrapper.in("id", roleIds);
			retlist = sysRoleMapper.selectList(queryWrapper);
		}
		return retlist;
	}

}
