package com.cool.store.service.impl;

import com.cool.store.constants.CommonConstants;
import com.cool.store.dao.SysMenuDAO;
import com.cool.store.dao.SysRoleMenuDAO;
import com.cool.store.dto.menu.AddMenuDTO;
import com.cool.store.entity.SysMenuDO;
import com.cool.store.entity.SysRoleMenuDO;
import com.cool.store.enums.MenuTypeEnum;
import com.cool.store.enums.PlatFormTypeEnum;
import com.cool.store.service.MenuService;
import com.cool.store.utils.CommonNodeUtils;
import com.cool.store.vo.menu.MenuAuthTreeVO;
import com.cool.store.vo.menu.MenuTreeVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangchenbiao
 * @FileName: MenuServiceImpl
 * @Description:
 * @date 2023-06-08 16:43
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private SysMenuDAO sysMenuDAO;
    @Resource
    private SysRoleMenuDAO sysRoleMenuDAO;

    @Override
    public List<MenuTreeVO> getUserMenus(String userId, String roleId) {
        List<SysMenuDO> menuList= sysMenuDAO.selectMenuAll(null);
        List<SysRoleMenuDO> roleMenuList = sysRoleMenuDAO.getRoleMenuByRoleId(roleId);
        List<Long> anthMenuIdList = menuList.stream().filter(o -> MenuTypeEnum.AUTH.getCode().equals(o.getMenuType())).map(SysMenuDO::getId).collect(Collectors.toList());
        List<Long> roleMenuIdList = roleMenuList.stream().map(SysRoleMenuDO::getMenuId).filter(anthMenuIdList::contains).collect(Collectors.toList());
        Set<Long> authMenuSet = new HashSet<>();
        //1倒推菜单列表 2.转换成树`
        if (CollectionUtils.isEmpty(roleMenuIdList)) {
            return new ArrayList<>();
        }
        authMenuSet.addAll(roleMenuIdList);
        Map<Long, Long> idMap = menuList.stream().filter(a -> a.getId() != null && a.getParentId() != null).collect(Collectors.toMap(SysMenuDO::getId, SysMenuDO::getParentId));
        for (Long menuId : roleMenuIdList) {
            getParentNode(menuId, idMap, authMenuSet);
        }
        menuList = menuList.stream().filter(data -> authMenuSet.contains(data.getId())).collect(Collectors.toList());
        //根据角色获取菜单
        return MenuTreeVO.dealMenuTree(CommonConstants.ZERO_LONG, menuList);
    }

    @Override
    public List<MenuAuthTreeVO> getAllMenus() {
        List<SysMenuDO> menuList= sysMenuDAO.selectMenuAll(null);
        return MenuAuthTreeVO.dealMenuTree(CommonConstants.ZERO_LONG, menuList);
    }

    @Override
    public Long addMenu(AddMenuDTO param, MenuTypeEnum menuType) {
        return sysMenuDAO.addMenu(AddMenuDTO.convertDO(param, menuType));
    }

    @Override
    public Long deleteMenuAuth(Long id) {
        List<SysMenuDO> menuList = sysMenuDAO.selectMenuAll(null);
        List<Long> idList = menuList.stream().map(SysMenuDO::getId).collect(Collectors.toList());
        Map<Long, List<Long>> parentGroupMap = menuList.stream().collect(Collectors.groupingBy(SysMenuDO::getParentId, Collectors.mapping(SysMenuDO::getId, Collectors.toList())));
        List<Long> allChildList = CommonNodeUtils.getAllChildListContainSelf(0L,id, idList, parentGroupMap);
        sysMenuDAO.batchDeleteMenu(allChildList);
        return null;
    }

    @Override
    public Integer sortMenu(List<Long> ids) {
        List<SysMenuDO> list = new LinkedList<>();
        for (int i = 1; i <= ids.size(); i++) {
            SysMenuDO sysMenuDO = new SysMenuDO();
            sysMenuDO.setSort(i);
            sysMenuDO.setId(ids.get(i - 1));
            list.add(sysMenuDO);
        }
        return sysMenuDAO.batchUpdateMenu(list);
    }

    private void getParentNode(Long menuId, Map<Long, Long> idMap, Set<Long> authMenuList) {
        Long parentId = idMap.get(menuId);
        authMenuList.add(parentId);
        if (parentId!=null&&parentId != 0) {
            getParentNode(parentId, idMap, authMenuList);
        }
    }
}
