package com.zhiqiyun.open.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhiqiyun.open.core.mapper.RoleInfoMapper;
import com.zhiqiyun.open.core.models.user.RoleInfo;
import com.zhiqiyun.open.core.service.RoleInfoService;
import com.zhiqiyun.open.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @author jtoms
 */
@Service
public class OauthRoleInfoServiceImpl extends ServiceImpl<RoleInfoMapper, RoleInfo> implements RoleInfoService {

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void updateById(RoleInfo roleInfo, List<Long> listAuthorities) throws ServiceException {
        try {
            this.updateById(roleInfo);
            this.baseMapper.deleteAuthorities(Collections.singletonList(roleInfo.getId()));
            if (listAuthorities != null && listAuthorities.size() > 0) {
                this.baseMapper.insertAuthority(roleInfo.getId(), listAuthorities);
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void insert(RoleInfo roleInfo, List<Long> listAuthorities) throws ServiceException {
        try {
            this.save(roleInfo);
            this.baseMapper.deleteAuthorities(Collections.singletonList(roleInfo.getId()));
            if (listAuthorities != null && listAuthorities.size() > 0) {
                this.baseMapper.insertAuthority(roleInfo.getId(), listAuthorities);
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void deleteByIds(List<Long> roleIds) throws ServiceException {
        try {
            this.baseMapper.deleteAuthorities(roleIds);
            this.baseMapper.deleteBatchIds(roleIds);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public List<Long> findAuthorities(Long roleId) {
        return this.baseMapper.findAuthorities(roleId);
    }
//
//    @Override
//    public PageResult<RoleInfo> findPage(PageBounds bounds, Map<String, Object> queryWrapper) {
//        List<RoleInfo> listData = this.baseMapper.findPage(bounds, queryWrapper);
//
//        return bounds.newPageResult(listData);
//    }
//
//    @Override
//    public List<RoleInfo> findAll() {
//        return this.baseMapper.findAll();
//    }
}
