package com.codvision.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.auth.entity.SysMenu;
import com.codvision.auth.entity.dto.SysMenuQueryDTO;
import com.codvision.auth.entity.vo.SysMenuVO;
import com.codvision.auth.enums.MenuCategory;
import com.codvision.auth.mapper.SysMenuMapper;
import com.codvision.auth.service.ISysMenuService;
import com.codvision.base.enums.DelFlag;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import com.codvision.webcore.util.PageUtil;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 系统菜单 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2023-11-09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuMapper, SysMenu, SysMenuVO> implements ISysMenuService {

    @Override
    public SysMenuVO getDetail(Long id) {
        SysMenuVO vo = new SysMenuVO();
        SysMenu po = this.getById(id);
        if (null != po) {
            BeanUtil.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public List<SysMenuVO> listAll(SysMenu queryBean) {
        QueryWrapper<SysMenu> wrapper = PageUtil.initWrapper(queryBean);
        List<SysMenu> list = this.list(wrapper);
        return BeanUtil.copyToList(list, SysMenuVO.class);
    }

    @Override
    public IPage<SysMenuVO> findAll(QueryPage<SysMenu> queryPage, SysMenuQueryDTO queryDTO) {
        LambdaQueryWrapper<SysMenu> queryWrapper = Wrappers.lambdaQuery();
        if (ObjectUtil.isNotEmpty(queryDTO)) {
            queryWrapper.like(StrUtil.isNotBlank(queryDTO.getName()), SysMenu::getName, queryDTO.getName())
                    .eq(ObjectUtil.isNotEmpty(queryDTO.getType()), SysMenu::getType, queryDTO.getType())
                    .like(StrUtil.isNotBlank(queryDTO.getPathName()), SysMenu::getPathName, queryDTO.getPathName());
            if (ObjectUtil.isNotEmpty(queryDTO.getIgnoreApi())) {
                queryWrapper.ne(queryDTO.getIgnoreApi(), SysMenu::getCategory, MenuCategory.API.getCategory());
            }
            if (StrUtil.isNotBlank(queryDTO.getCategory())) {
                Object[] categories = queryDTO.getCategory().split(",");
                queryWrapper.in(SysMenu::getCategory, categories);
            }
        }
        queryWrapper.eq(SysMenu::getDelFlag, DelFlag.NO_DELETED.getFlag()).orderByAsc(SysMenu::getSortOrder);
        IPage<SysMenuVO> page = this.getBasePage(queryPage.getPage(), queryWrapper);
        return page;
    }

    @Override
    public void addSysMenu(SysMenu sysMenu) {
        isExistMenuPathName(sysMenu.getPathName());
        if (ObjectUtil.isEmpty(sysMenu.getPid())) {
            sysMenu.setPid(0);
        }
        this.save(sysMenu);
    }

    @Override
    public void updateSysMenu(SysMenu sysMenu) {
        SysMenu checkMenu = this.getById(sysMenu.getId());
        Preconditions.checkArgument(ObjectUtil.isNotEmpty(checkMenu), 30001);

        if (!sysMenu.getPathName().equals(checkMenu.getPathName())) {
            isExistMenuPathName(sysMenu.getPathName());
        }

        this.updateById(sysMenu);
    }

    @Override
    public List<SysMenuVO> getUserMenuList(SysMenuQueryDTO queryDTO) {
        return this.getBaseMapper().getUserMenuList(queryDTO);
    }

    /**
     * 菜单路径是否存在
     *
     * @param pathName 菜单路径
     */
    private void isExistMenuPathName(String pathName) {
        SysMenu menu = this.getOne(Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getPathName, pathName));
        Preconditions.checkArgument(ObjectUtil.isEmpty(menu), "权限路径名称已存在！");
    }
}
