package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppBaseMenuPermissionMapper;
import org.wcs.mapper.intf.AppBaseMenuPermissionService;
import org.wcs.model.bo.db.DataBaseActionResult;
import org.wcs.model.po.app.AppBaseMenuPermission;

import java.util.List;

/**
 * 菜单权限数据访问层
 */
@Service
public class AppBaseMenuPermissionDao extends ServiceImpl<AppBaseMenuPermissionMapper, AppBaseMenuPermission> implements AppBaseMenuPermissionService {

    /**
     * 查询所有菜单权限
     * @return 菜单权限列表
     */
    @Override
    public List<AppBaseMenuPermission> queryAll() {
        try{
            return super.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据菜单ID查询菜单权限
     * @param menuId 菜单ID
     * @return 菜单权限列表
     */
    @Override
    public List<AppBaseMenuPermission> queryByMenuId(String menuId) {
        LambdaQueryWrapper<AppBaseMenuPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(menuId != null, AppBaseMenuPermission::getMenuId, menuId);
        queryWrapper.orderByAsc(AppBaseMenuPermission::getPermissionId);
        try{
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据权限ID查询菜单权限
     * @param permissionId 权限ID
     * @return 菜单权限列表
     */
    @Override
    public List<AppBaseMenuPermission> queryByPermissionId(String permissionId) {
        if(permissionId == null) {
            return null;
        }
        LambdaQueryWrapper<AppBaseMenuPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppBaseMenuPermission::getPermissionId, permissionId);
        queryWrapper.orderByAsc(AppBaseMenuPermission::getPermissionId);
        try{
            return super.list(queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 添加菜单权限
     * @param appBaseMenuPermission 菜单权限
     * @return 添加结果
     */
    @Override
    public DataBaseActionResult<Integer> insert(AppBaseMenuPermission appBaseMenuPermission) {
        if(appBaseMenuPermission == null) {
            return new DataBaseActionResult<>(0, new Exception("菜单权限不能为空"));
        }
        try {
            int insert = super.baseMapper.insert(appBaseMenuPermission);
            return new DataBaseActionResult<>(insert);
        } catch (Exception e) {
            return new DataBaseActionResult<>(0, e);
        }
    }

    /**
     * 根据权限ID删除菜单权限
     * @param permissionId 权限ID
     * @return 删除结果
     */
    @Override
    public DataBaseActionResult<Integer> deleteByPermissionId(String permissionId) {
        if(permissionId == null) {
            return new DataBaseActionResult<>(0, new Exception("菜单ID不能为空"));
        }
        LambdaQueryWrapper<AppBaseMenuPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppBaseMenuPermission::getPermissionId, permissionId);
        try {
            int delete = super.baseMapper.delete(queryWrapper);
            return new DataBaseActionResult<>(delete);
        } catch (Exception e) {
            return new DataBaseActionResult<>(0, e);
        }
    }
}
