package com.cool.bang.modules.system.service.impl;

import com.cool.bang.common.base.BaseServiceImpl;
import com.cool.bang.modules.system.domain.po.Role;
import com.cool.bang.modules.system.mapper.RoleMapper;
import com.cool.bang.modules.system.service.IRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * system role service implementation class
 *
 * @author GuoPeiZhi
 * 2021-2-27 16:06
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    public Integer getCountByName(String name) {
        return baseMapper.selectCountByName(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRoleMenu(Role sysRole) {
        if (!CollectionUtils.isEmpty(sysRole.getMenuList())) {
            baseMapper.deleteRoleMenu(sysRole.getId());
            baseMapper.saveRoleMenu(sysRole.getId(), sysRole.getMenuList());
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRole(Integer id) {
        int count = baseMapper.delete(id);
        baseMapper.deleteRoleMenu(id);
        baseMapper.deleteUserRole(id);
        return count > 0;
    }
}
