package com.xjrsoft.module.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.module.system.dto.SaveXjrMenuQuickDto;
import com.xjrsoft.module.system.entity.XjrMenuQuick;
import com.xjrsoft.module.system.mapper.XjrMenuQuickMapper;
import com.xjrsoft.module.system.service.IXjrMenuQuickService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @title: 快捷菜单
 * @Author fanxp
 * @Date: 2023-11-11
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class XjrMenuQuickServiceImpl extends ServiceImpl<XjrMenuQuickMapper, XjrMenuQuick> implements IXjrMenuQuickService {

    private final XjrMenuQuickMapper menuQuickMapper;

    /**
     * 保存快捷菜单配置
     *
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean save(SaveXjrMenuQuickDto dto) {
        switch (dto.getDataType()) {
            case 1:
                menuQuickMapper.delete(Wrappers.lambdaQuery(XjrMenuQuick.class).eq(XjrMenuQuick::getUserId, dto.getUserId()));
            case 2:
                menuQuickMapper.delete(Wrappers.lambdaQuery(XjrMenuQuick.class).eq(XjrMenuQuick::getRoleId, dto.getRoleId()));
        }
        dto.getMenus().forEach((node) -> {
            XjrMenuQuick item = new XjrMenuQuick();
            item.setMenuId(node.getMenuId());
            item.setSystemId(node.getSystemId());
            item.setUserId(dto.getUserId());
            item.setRoleId(dto.getRoleId());
            menuQuickMapper.insert(item);
        });
        return true;
    }
}
