package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.SysEmployeePositionDao;
import com.ym.mec.biz.dal.dao.SysPositionDao;
import com.ym.mec.biz.dal.dao.TenantInfoDao;
import com.ym.mec.biz.dal.dto.SimpleUserDto;
import com.ym.mec.biz.dal.dto.SysEmployeePositionDto;
import com.ym.mec.biz.dal.entity.SysEmployeePosition;
import com.ym.mec.biz.dal.entity.TenantInfo;
import com.ym.mec.biz.dal.enums.SysUserRoleEnum;
import com.ym.mec.biz.service.SysEmployeePositionService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.common.tenant.TenantContextHolder;
import com.ym.mec.util.collection.MapUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SysEmployeePositionServiceImpl extends BaseServiceImpl<Integer, SysEmployeePosition>  implements SysEmployeePositionService {
	
	@Autowired
	private SysEmployeePositionDao sysEmployeePositionDao;
	@Autowired
	private SysPositionDao positionDao;
	@Autowired
	private TenantInfoDao tenantInfoDao;

	@Override
	public BaseDAO<Integer, SysEmployeePosition> getDAO() {
		return sysEmployeePositionDao;
	}

    @Override
	@Transactional(rollbackFor = Exception.class)
    public void batchAddPosition(Integer userId, List<Integer> positionIds, Integer tenantId) {
		if(positionIds != null && positionIds.size() > 0){
			sysEmployeePositionDao.batchAddPosition(userId,positionIds,tenantId);
		}
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delPosition(Integer userId, Integer tenantId) {
		sysEmployeePositionDao.delPosition(userId,tenantId);
	}

    @Override
    public Map<String, List<SimpleUserDto>> findOrganPosition(String organIds, Integer tenantId) {
		List<SimpleUserDto> organPosition = sysEmployeePositionDao.findOrganPosition(organIds, tenantId);
		if(organPosition != null && organPosition.size() > 0){
			Map<Integer, List<SimpleUserDto>> collect = organPosition.stream().collect(Collectors.groupingBy(e -> e.getPositionId()));
			Map<String,List<SimpleUserDto>> resultMap = new HashMap<>(collect.size());
			Map<Integer,String> positionNameMap = MapUtil.convertMybatisMap(positionDao.findMapByIds(collect.keySet()));
			collect.keySet().forEach(e->resultMap.put(positionNameMap.get(e),collect.get(e)));
			return resultMap;
		}
		return new HashMap<>();
    }

    @Override
    public Boolean onlyForRole(SysUserRoleEnum role, Integer userId,Boolean isSuper) {
		//如果是超管
		if(isSuper){
			return false;
		}
		//如果是机构管理员
		Integer tenantId = TenantContextHolder.getTenantId();
		TenantInfo tenantInfo = tenantInfoDao.selectById(tenantId);
		if(tenantInfo.getUserId() != null && tenantInfo.getUserId().equals(userId)){
			return false;
		}
		//判断角色
		List<SysUserRoleEnum> roleEnums = sysEmployeePositionDao.queryPositionList(userId,tenantId);
		if(roleEnums != null && roleEnums.size() == 1 && roleEnums.contains(role)){
			return true;
		}
		return false;
    }

	@Override
	public List<SysUserRoleEnum> queryAllRole(Integer userId,Boolean isSuper) {
		//如果是超管
		if(isSuper){
			return new ArrayList<>(Arrays.asList(SysUserRoleEnum.values()));
		}
		//如果是机构管理员
		Integer tenantId = TenantContextHolder.getTenantId();
		TenantInfo tenantInfo = tenantInfoDao.selectById(tenantId);
		if(tenantInfo.getUserId() != null && tenantInfo.getUserId().equals(userId)){
			return new ArrayList<>(Arrays.asList(SysUserRoleEnum.values()));
		}
		return sysEmployeePositionDao.queryPositionList(userId,tenantId);
	}

    @Override
    public Set<Integer> queryUserIdByRoleId(SysUserRoleEnum[] roleArr, Integer organId) {
		if(roleArr == null || roleArr.length == 0 || organId == null){
			return new HashSet<>();
		}
		Integer tenantId = TenantContextHolder.getTenantId();
		return sysEmployeePositionDao.queryUserIdByRoleId(roleArr,organId,tenantId);
    }

	@Override
	public List<SysEmployeePositionDto> queryByUserId(Integer userId) {
		return sysEmployeePositionDao.queryByUserId(userId);
	}
}