package cn.pluss.platform.menuInfo.impl;

import cn.pluss.platform.entity.MenuInfo;
import cn.pluss.platform.entity.UserInfo;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.MenuInfoMapper;
import cn.pluss.platform.menuInfo.MenuInfoService;
import cn.pluss.platform.userInfo.UserInfoService;
import cn.pluss.platform.util.tree.TreeUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Transactional
@Service("menuInfoService")
public class MenuInfoServiceImpl extends ServiceImpl<MenuInfoMapper, MenuInfo> implements MenuInfoService {

    @Autowired
    private MenuInfoMapper menuInfoMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserInfoService uiService;

    @Override
    public List<MenuInfo> getMenuTree(MenuInfo menuInfo) {
        String userId = request.getHeader("userId");
        UserInfo userInfo = uiService.getById(userId);
        if (!"admin".equals(userInfo.getLoginName())) {
            throw new MsgException("拒绝访问");
        }

        List<MenuInfo> menuInfos = queryMenuInfoList(menuInfo);
        for (MenuInfo info : menuInfos) {
            info.setSort(info.getMenuOrder());
            info.setTitle(info.getName());
            if (info.getParentId() != null) {
                info.setPId(info.getParentId() + "");
            }
        }
        return TreeUtils.getSortTree(menuInfos);
    }

    @Override
    public List<MenuInfo> getRoleConfigMenuTree(MenuInfo menuInfo, String roleId) {
        String userId = request.getHeader("userId");
        UserInfo userInfo = uiService.getById(userId);
        if (!"admin".equals(userInfo.getLoginName())) {
            throw new MsgException("拒绝访问");
        }

        List<MenuInfo> menuInfos = queryRoleMenuInfoList(menuInfo, roleId);
        for (MenuInfo info : menuInfos) {
            info.setSort(info.getMenuOrder());
            info.setTitle(info.getName());
            if (info.getParentId() != null) {
                info.setPId(info.getParentId() + "");
            }
        }

        List<MenuInfo> result = TreeUtils.getSortTree(menuInfos);
        clear(result);
        return result;
    }


    @Override
    public MenuInfo queryMenuInfo(MenuInfo menuInfo) {
        return menuInfoMapper.queryMenuInfo(menuInfo);
    }

    @Override
    public Integer saveMenuInfo(MenuInfo menuInfo) {
        return menuInfoMapper.saveMenuInfo(menuInfo);
    }

    @Override
    public void updateMenuInfo(MenuInfo menuInfo) {
        menuInfoMapper.updateMenuInfo(menuInfo);
    }

    @Override
    public void deleteMenuInfo(MenuInfo menuInfo) {
        menuInfoMapper.deleteMenuInfo(menuInfo);
    }

    @Override
    public List<MenuInfo> queryMenuInfoPage(Map map) {
        return menuInfoMapper.queryMenuInfoPage(map);
    }

    @Override
    public Integer queryMenuInfoPageCount(Map map) {
        return menuInfoMapper.queryMenuInfoPageCount(map);
    }

    @Override
    public List<MenuInfo> getPermissionNames(String loginName) {
        return menuInfoMapper.getPermissionNames(loginName);
    }

    @Override
    public List<MenuInfo> queryPermitMenuInfoById(Map map) {
        return menuInfoMapper.queryPermitMenuInfoById(map);
    }

    @Override
    public List<MenuInfo> queryMenuInfoListOrderById(MenuInfo menuInfo) {
        return menuInfoMapper.queryMenuInfoListOrderById(menuInfo);
    }

    @Override
    public List<MenuInfo> queryMenuInfoByRoleId(Map map) {
        return menuInfoMapper.queryMenuInfoByRoleId(map);
    }
}

