package cn.pluss.platform.roleMenuInfo.impl;

import cn.pluss.platform.entity.RoleMenuInfo;
import cn.pluss.platform.mapper.RoleMenuInfoMapper;
import cn.pluss.platform.roleMenuInfo.RoleMenuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author nb2020
 */
@Service("roleMenuInfoService")
public class RoleMenuInfoServiceImpl extends ServiceImpl<RoleMenuInfoMapper, RoleMenuInfo> implements RoleMenuInfoService {

    @Override
    public void saveRoleMenuInfo(RoleMenuInfo roleMenuInfo) {
        baseMapper.saveRoleMenuInfo(roleMenuInfo);
    }

    @Override
    public void deleteRoleMenuInfo(RoleMenuInfo roleMenuInfo) {
        baseMapper.deleteRoleMenuInfo(roleMenuInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRoleMenuList(String roleId, String[] menuIdArr) {
        // 删除已存在的关联信息
        RoleMenuInfo entity = new RoleMenuInfo();
        entity.setRoleId(Long.parseLong(roleId));
        baseMapper.delete(new QueryWrapper<>(entity));

        if (menuIdArr == null || menuIdArr.length == 0) {
            return;
        }

        List<RoleMenuInfo> roleMenuInfoList = new ArrayList<>();
        for (String menuId : menuIdArr) {
            RoleMenuInfo menuInfo = new RoleMenuInfo();
            menuInfo.setRoleId(Long.parseLong(roleId));
            menuInfo.setMenuId(Long.parseLong(menuId));
            roleMenuInfoList.add(menuInfo);
        }

        saveBatch(roleMenuInfoList);
    }

}

