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.RoleDao;
import io.hmit.modules.user.dto.RoleDTO;
import io.hmit.modules.user.entity.RoleEntity;
import io.hmit.modules.user.service.RoleService;
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;

/**
 * 用户权限
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-06-01
 */
@Service
public class RoleServiceImpl extends CrudServiceImpl<RoleDao, RoleEntity, RoleDTO> implements RoleService {

    @Autowired
    private RoleDao roleDao;

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

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

        return wrapper;
    }


    @Override
    public List<RoleDTO> getAllRoleList() {
        List<RoleEntity> roleEntities = roleDao.getAllList();
        return ConvertUtils.sourceToTarget(roleEntities,RoleDTO.class);
    }

    @Override
    public RoleDTO getRoleByName(String name) {
        RoleEntity entity = baseDao.selectOne(new QueryWrapper<RoleEntity>().eq("name",name));
        return ConvertUtils.sourceToTarget(entity,RoleDTO.class);
    }
}
