package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysMenuClick;
import com.ruoyi.system.mapper.SysMenuClickMapper;
import com.ruoyi.system.service.ISysMenuClickService;
import com.ruoyi.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 菜单点击计数Service业务层处理
 * 
 * @author gyt
 * @date 2025-11-12
 */
@Service
public class SysMenuClickServiceImpl implements ISysMenuClickService 
{
    @Autowired
    private SysMenuClickMapper sysMenuClickMapper;
    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 查询菜单点击计数
     * 
     * @param id 菜单点击计数主键
     * @return 菜单点击计数
     */
    @Override
    public SysMenuClick selectSysMenuClickById(Long id)
    {
        return sysMenuClickMapper.selectSysMenuClickById(id);
    }

    /**
     * 查询菜单点击计数列表
     * 
     * @param sysMenuClick 菜单点击计数
     * @return 菜单点击计数
     */
    @Override
    public List<SysMenuClick> selectSysMenuClickList()
    {
        List<SysMenuClick> menuClickList = sysMenuClickMapper.selectSysMenuClickList(new SysMenuClick());
        // 获取当前用户有权限的菜单列表
        List<SysMenu> permittedMenus = getPermittedMenusForCurrentUser();
        Set<String> permittedMenuIds = permittedMenus.stream()
                .map(SysMenu::getMenuName)
                .collect(Collectors.toSet());

        // 过滤有权限的菜单
        List<SysMenuClick> permittedPopularMenus = menuClickList.stream()
                .filter(menu -> permittedMenuIds.contains(menu.getPageName()))
                .limit(8) // 取前8个
                .collect(Collectors.toList());
        return permittedPopularMenus;
    }
    /**
     * 获取当前用户有权限的菜单
     */
    private List<SysMenu> getPermittedMenusForCurrentUser() {
        // 使用Ruoyi的权限服务（推荐）
        if (SecurityUtils.getLoginUser() != null) {
            Long userId = SecurityUtils.getUserId();
            return sysMenuService.getMenuListByWelcome(userId);
        }

        // 如果是匿名访问，返回空列表
        return new ArrayList<>();
    }

    /**
     * 新增菜单点击计数
     * 
     * @param sysMenuClick 菜单点击计数
     * @return 结果
     */
    @Override
    public int insertSysMenuClick(SysMenuClick sysMenuClick)
    {
        // 查询该路由是否存在
        SysMenuClick query = new SysMenuClick();
        query.setPageRoute(sysMenuClick.getPageRoute());
        List<SysMenuClick> menuClickList = sysMenuClickMapper.selectSysMenuClickList(query);
        if(menuClickList != null && !menuClickList.isEmpty()){
            SysMenuClick menuClick = menuClickList.get(0);
            // 如果存在 计数
            Long count = menuClick.getClickCount() + 1;
            SysMenuClick updateData = new SysMenuClick();
            updateData.setId(menuClick.getId());
            updateData.setClickCount(count);
            updateData.setLastClickTime(DateUtils.getNowDate());
            sysMenuClickMapper.updateSysMenuClick(updateData);
        }else{
            sysMenuClick.setCreateTime(DateUtils.getNowDate());
            sysMenuClick.setLastClickTime(DateUtils.getNowDate());
            sysMenuClickMapper.insertSysMenuClick(sysMenuClick);
        }
        return 1;
    }

    /**
     * 修改菜单点击计数
     * 
     * @param sysMenuClick 菜单点击计数
     * @return 结果
     */
    @Override
    public int updateSysMenuClick(SysMenuClick sysMenuClick)
    {
        sysMenuClick.setUpdateTime(DateUtils.getNowDate());
        return sysMenuClickMapper.updateSysMenuClick(sysMenuClick);
    }

    /**
     * 批量删除菜单点击计数
     * 
     * @param ids 需要删除的菜单点击计数主键
     * @return 结果
     */
    @Override
    public int deleteSysMenuClickByIds(Long[] ids)
    {
        return sysMenuClickMapper.deleteSysMenuClickByIds(ids);
    }

    /**
     * 删除菜单点击计数信息
     * 
     * @param id 菜单点击计数主键
     * @return 结果
     */
    @Override
    public int deleteSysMenuClickById(Long id)
    {
        return sysMenuClickMapper.deleteSysMenuClickById(id);
    }
}
