package org.springblade.flow.permission.service;

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

import org.springblade.flow.permission.entity.UserEntity;
import org.springblade.flow.permission.entity.UserRelationEntity;
import org.springblade.flow.permission.enums.UserRelationEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import cn.hutool.core.collection.CollUtil;

/**
 * 
 * @author william
 * @date 2022年8月1日 上午11:17:08
 */
@Service
public class UserRelationService {

	@Autowired
	private User4FlowService userService;

	/**
	 * 根据对象主键获取列表
	 *
	 * @param objectId 对象主键
	 * @return
	 */
	public List<UserRelationEntity> getListByObjectId(String objectId) {
		List<UserRelationEntity> list = new ArrayList<>();
		QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(UserEntity::getPositionId, objectId).eq(UserEntity::getEnabledMark, 1)
				.eq(UserEntity::getIsDeleted, 0);
		List<UserEntity> userList = userService.list(queryWrapper);
		if (CollUtil.isNotEmpty(userList)) {
			for (UserEntity userEntity : userList) {
				UserRelationEntity ur = new UserRelationEntity();
				ur.setUserId(userEntity.getId());
				ur.setObjectId(objectId);
				ur.setObjectType(UserRelationEnum.Position.getName());
				list.add(ur);
			}
		} else {
			queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(UserEntity::getRoleId, objectId).eq(UserEntity::getEnabledMark, 1)
					.eq(UserEntity::getIsDeleted, 0);
			userList = userService.list(queryWrapper);
			if (CollUtil.isNotEmpty(userList)) {
				for (UserEntity userEntity : userList) {
					UserRelationEntity ur = new UserRelationEntity();
					ur.setUserId(userEntity.getId());
					ur.setObjectId(objectId);
					ur.setObjectType(UserRelationEnum.Role.getName());
					list.add(ur);
				}
			}
		}

		return list;
	}

	/**
	 * 根据对象主键获取列表
	 *
	 * @param objectIdList 对象主键
	 * @return
	 */
	public List<UserRelationEntity> getListByObjectIdAll(List<String> objectIdList) {
		List<UserRelationEntity> list = new ArrayList<>();
		if (CollUtil.isNotEmpty(objectIdList)) {
			for (String objectId : objectIdList) {
				List<UserRelationEntity> childList = getListByObjectId(objectId);
				if (CollUtil.isNotEmpty(childList)) {
					list.addAll(childList);
				}
			}
		}
		return list;
	}

	public List<UserRelationEntity> getByUserId(String userId) {
		List<UserRelationEntity> list = new ArrayList<>();
		UserEntity user = userService.getById(userId);
		UserRelationEntity relationRole = new UserRelationEntity();
		relationRole.setObjectId(user.getRoleId());
		relationRole.setObjectType(UserRelationEnum.Role.name());

		UserRelationEntity relationPosition = new UserRelationEntity();
		relationPosition.setObjectId(user.getPositionId());
		relationPosition.setObjectType(UserRelationEnum.Position.name());

		list.add(relationPosition);
		list.add(relationRole);
		return list;
	}

}
