package io.hmit.modules.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.modules.user.dao.UserRoleDao;
import io.hmit.modules.user.dto.RoleDTO;
import io.hmit.modules.user.dto.UserRoleDTO;
import io.hmit.modules.user.entity.RoleEntity;
import io.hmit.modules.user.entity.UserRoleEntity;
import io.hmit.modules.user.service.UserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 用户权限对应表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-06-01
 */
@Service
public class UserRoleServiceImpl extends CrudServiceImpl<UserRoleDao, UserRoleEntity, UserRoleDTO> implements UserRoleService {

    @Override
    public QueryWrapper<UserRoleEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<UserRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public UserRoleDTO getRoleByUser(Long userID) {
        UserRoleEntity entity = baseDao.selectOne(new QueryWrapper<UserRoleEntity>().eq("user_id",userID));
        return ConvertUtils.sourceToTarget(entity,UserRoleDTO.class);
    }

    @Override
    public List<UserRoleDTO> getRoleByUsers(Long userID) {
        List<UserRoleEntity> entity = baseDao.selectList(new QueryWrapper<UserRoleEntity>().eq("user_id",userID));
        return ConvertUtils.sourceToTarget(entity,UserRoleDTO.class);
    }

    @Override
    public UserRoleDTO getByRoleAUser(Long roleId, Long userId) {
        UserRoleEntity entity = baseDao.selectOne(new QueryWrapper<UserRoleEntity>()
                .eq("role_id",roleId).eq("user_id",userId));
        return ConvertUtils.sourceToTarget(entity,UserRoleDTO.class);
    }

}
