package io.wdd.server.coreService.impl;

import io.wdd.server.beans.mapper.ServerRoleDTOMapper;
import io.wdd.server.beans.po.ServerRolePO;
import io.wdd.server.beans.vo.ServerRoleVO;
import io.wdd.server.coreService.CoreRoleService;
import io.wdd.server.service.ServerRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CoreRoleServiceImpl implements CoreRoleService {

    @Resource
    ServerRoleService serverRoleService;

    @Override
    public List<ServerRolePO> roleGetAll() {
        return serverRoleService.list();
    }

    @Override
    public boolean roleCreate(ServerRoleVO serverRoleVO) {

        if (!checkDataValid(serverRoleVO)) {
            return false;
        }

        ServerRolePO serverRolePO = ServerRoleDTOMapper.INSTANCE.voToPo(serverRoleVO);

        return serverRoleService.save(serverRolePO);
    }

    private boolean checkDataValid(ServerRoleVO serverRoleVO) {

        if (StringUtils.isBlank(serverRoleVO.getRoleName())) {
            return false;
        }

        return serverRoleVO.getRoleNum() < 128 && serverRoleVO.getRoleNum() >= 0;
    }

    @Override
    public boolean roleUpdate(ServerRolePO serverRolePO) {

        if (!checkDataValid(ServerRoleDTOMapper.INSTANCE.poToVo(serverRolePO))) {
            return false;
        }

        return serverRoleService.saveOrUpdate(serverRolePO);
    }

    @Override
    public boolean roleDelete(Long roleId) {
        return serverRoleService.removeById(roleId);
    }
}
