package com.purui.prims.auth.authentication.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.request.RoleReq;
import com.purui.prims.auth.api.authentication.response.RoleResp;
import com.purui.prims.auth.api.authentication.service.RoleService;
import com.purui.prims.auth.authentication.persistence.dataobject.RoleDO;
import com.purui.prims.auth.authentication.persistence.mapper.RoleDOMapper;
import com.purui.prims.common.util.BeanMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleDOMapper, RoleDO>
        implements RoleService {
    @Override
    public void saveOrUpdate(RoleReq roleReq, User user) {
        QueryWrapper<RoleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RoleDO::getId, roleReq.getId());
        RoleDO roleDO = getOne(queryWrapper);
        if (roleDO == null) {
            roleDO = new RoleDO();
        }
        BeanMapper.mapper(roleReq, roleDO);
        super.saveOrUpdate(roleDO);
    }

    @Override
    public void delete(Long id) {
        removeById(id);
    }

    @Override
    public List<RoleResp> getRoles() {
        return list().stream().map(organizationDO -> {
            RoleResp roleResp = new RoleResp();
            BeanMapper.mapper(organizationDO, roleResp);
            return roleResp;
        }).collect(Collectors.toList());
    }
}
