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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.modules.fhjw.dao.UserRoleDao;
import io.hmit.modules.fhjw.dto.FhjwTempDTO;
import io.hmit.modules.fhjw.dto.UserRoleDTO;
import io.hmit.modules.fhjw.entity.UserRoleEntity;
import io.hmit.modules.fhjw.service.FhjwTempService;
import io.hmit.modules.fhjw.service.UserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 用户权限对应表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-06-03
 */
@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");
        String userId = (String)params.get("userId");

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

        return wrapper;
    }

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

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

    @Override
    public void deleteByUserId(Long userId) {
        baseDao.deleteByUserId(userId);
    }
}
