package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppBaseMenuMapper;
import org.wcs.mapper.intf.AppBaseMenuService;
import org.wcs.model.po.app.AppBaseGroupPermission;
import org.wcs.model.po.app.AppBaseMenu;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 菜单表数据层持久类
 */
@Service
@Slf4j
public class AppBaseMenuDao extends ServiceImpl<AppBaseMenuMapper, AppBaseMenu> implements AppBaseMenuService {

    /**
     * 查询一个用户组的所有菜单记录
     * @param userGroup 用户组
     * @return 结果
     */
    @Override
    public List<AppBaseMenu> queryPermission(String userGroup) {
        if(AppStringUtils.isEmpty(userGroup)) {
            return null;
        }
        try {
            return baseMapper.queryPermission(userGroup);
        } catch (Exception ex) {
            log.error("查询权限失败：{}，用户组：{}", ex, userGroup);
            return null;
        }
    }

    /**
     * 根据条件模糊查询
     * @param appBaseMenu 查询条件
     * @return 查询结果
     */
    @Override
    public List<AppBaseMenu> queryIndistinct(AppBaseMenu appBaseMenu) {
        try {
            if(AppBeanUtils.isAllPropertyNull(appBaseMenu)) {
                return super.list();
            }
            LambdaQueryWrapper<AppBaseMenu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(appBaseMenu.getMenuName() != null, AppBaseMenu::getMenuName, appBaseMenu.getMenuName());
            queryWrapper.like(appBaseMenu.getMenuIco() != null, AppBaseMenu::getMenuIco, appBaseMenu.getMenuIco());
            queryWrapper.like(appBaseMenu.getMenuId() != null, AppBaseMenu::getMenuId, appBaseMenu.getMenuId());
            queryWrapper.like(appBaseMenu.getRouterName() != null, AppBaseMenu::getRouterName, appBaseMenu.getRouterName());
            queryWrapper.like(appBaseMenu.getFatherMenuId() != null, AppBaseMenu::getFatherMenuId, appBaseMenu.getFatherMenuId());
            queryWrapper.like(appBaseMenu.getMenuLevel() != null, AppBaseMenu::getMenuLevel, appBaseMenu.getMenuLevel());
            queryWrapper.like(appBaseMenu.getMenuStatus() != null, AppBaseMenu::getMenuStatus, appBaseMenu.getMenuStatus());
            return super.list(queryWrapper);
        } catch (Exception ex) {
            log.error("模糊查询菜单信息失败", ex);
            return null;
        }
    }

    /**
     * 查询菜单信息
     * @param appBaseMenu 查询条件
     * @return 查询结果
     */
    @Override
    public List<AppBaseMenu> query(AppBaseMenu appBaseMenu) {
        if(AppBeanUtils.isAllPropertyNull(appBaseMenu)) {
            return null;
        }
        LambdaQueryWrapper<AppBaseMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(appBaseMenu.getMenuName() != null, AppBaseMenu::getMenuName, appBaseMenu.getMenuName())
                .eq(appBaseMenu.getMenuIco() != null, AppBaseMenu::getMenuIco, appBaseMenu.getMenuIco())
                .eq(appBaseMenu.getMenuId() != null, AppBaseMenu::getMenuId, appBaseMenu.getMenuId())
                .eq(appBaseMenu.getRouterName() != null, AppBaseMenu::getRouterName, appBaseMenu.getRouterName())
                .eq(appBaseMenu.getFatherMenuId() != null, AppBaseMenu::getFatherMenuId, appBaseMenu.getFatherMenuId())
                .eq(appBaseMenu.getMenuLevel() != null, AppBaseMenu::getMenuLevel, appBaseMenu.getMenuLevel())
                .eq(appBaseMenu.getMenuStatus() != null, AppBaseMenu::getMenuStatus, appBaseMenu.getMenuStatus());
        try {
            return super.list(queryWrapper);
        } catch (Exception ex) {
            log.error("查询菜单信息失败", ex);
            return null;
        }
    }

    /**
     * 批量插入数据
     * @param menus 菜单
     * @return 插入结果
     */
    @Override
    public int insert(AppBaseMenu... menus) {
        if(menus.length < 1) {
            return 0;
        }
        try {
            return super.saveBatch(Arrays.stream(menus).toList()) ? menus.length : 0;
        } catch (Exception ex) {
            log.error("插入菜单信息失败", ex);
            return 0;
        }
    }

    /**
     * 删除菜单信息
     * @param menuId 菜单ID
     * @return 结果
     */
    @Override
    public int delete(String menuId) {
        if(AppStringUtils.isEmpty(menuId)) {
            return 0;
        }
        try {
            return super.removeById(menuId) ? 1 : 0;
        } catch (Exception ex) {
            log.error("删除菜单信息失败", ex);
            return 0;
        }
    }

    /**
     * 更新菜单信息
     * @param appBaseMenu 菜单信息
     * @return 结果
     */
    @Override
    public int update(AppBaseMenu appBaseMenu) {
        if(appBaseMenu == null) {
            return 0;
        }
        try {
            return super.updateById(appBaseMenu) ? 1 : 0;
        } catch (Exception ex) {
            log.error("更新菜单信息失败", ex);
            return 0;
        }
    }
}
