package com.yunzhupaas.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.Page;
import com.yunzhupaas.base.model.module.ModuleModel;
import com.yunzhupaas.base.vo.ListVO;
import com.yunzhupaas.model.AppMenuListVO;
import com.yunzhupaas.model.UserMenuModel;
import com.yunzhupaas.permission.model.authorize.AuthorizeVO;
import com.yunzhupaas.permission.service.AuthorizeService;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.StringUtil;
import com.yunzhupaas.util.UserProvider;
import com.yunzhupaas.util.treeutil.ListToTreeUtil;
import com.yunzhupaas.util.treeutil.SumTree;
import com.yunzhupaas.util.treeutil.newtreeutil.TreeDotUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * app应用
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司（http://www.szlecheng.cn）
 * @date 2024-07-08
 */
@Tag(name = "app应用", description = "Menu")
@RestController
@RequestMapping("/api/app/Menu")
public class AppMenuController {
    @Autowired
    private AuthorizeService authorizeService;

    /**
     * 获取菜单列表
     *
     * @param page 分页模型
     * @return
     */
    @Operation(summary = "获取菜单列表")
    @GetMapping
    public ActionResult<ListVO<AppMenuListVO>> list(Page page) {
        AuthorizeVO authorizeModel = authorizeService.getAuthorize(false, false);
        List<ModuleModel> buttonListAll = authorizeModel.getModuleList().stream().filter(t -> "App".equals(t.getCategory())).collect(Collectors.toList());
        // 通过系统id捞取相应的菜单
        buttonListAll = buttonListAll.stream().filter(t -> UserProvider.getUser().getAppSystemId() != null && UserProvider.getUser().getAppSystemId().equals(t.getSystemId())).collect(Collectors.toList());
        List<ModuleModel> buttonList = buttonListAll;
        if (StringUtil.isNotEmpty(page.getKeyword())) {
            buttonList = buttonListAll.stream().filter(t -> t.getFullName().contains(page.getKeyword())).collect(Collectors.toList());
        }
        List<UserMenuModel> list = JsonUtil.getJsonToList(ListToTreeUtil.treeWhere(buttonList, buttonListAll), UserMenuModel.class);
        List<SumTree<UserMenuModel>> menuAll = TreeDotUtils.convertListToTreeDot(list, "-1");
        List<AppMenuListVO> data = JsonUtil.getJsonToList(menuAll, AppMenuListVO.class);
        ListVO listVO = new ListVO();
        listVO.setList(data);
        return ActionResult.success(listVO);
    }

    /**
     * 获取子集菜单
     *
     * @return
     */
    @Operation(summary = "获取子集菜单")
    @GetMapping("/getChildList/{id}")
    public ActionResult<List<AppMenuListVO>> getChildList(@PathVariable("id") String id) {
        AuthorizeVO authorizeModel = authorizeService.getAuthorize(false, false);
        List<ModuleModel> buttonListAll = authorizeModel.getModuleList().stream().filter(t -> "App".equals(t.getCategory())).collect(Collectors.toList());
        // 通过系统id捞取相应的菜单
        buttonListAll = buttonListAll.stream().filter(t -> UserProvider.getUser().getAppSystemId() != null && UserProvider.getUser().getAppSystemId().equals(t.getSystemId())).collect(Collectors.toList());
        Set<ModuleModel> models = new HashSet<>();
        next(buttonListAll,id,models);
        List<UserMenuModel> list = JsonUtil.getJsonToList(models, UserMenuModel.class);
        List<SumTree<UserMenuModel>> menuAll = TreeDotUtils.convertListToTreeDot(list);
        List<AppMenuListVO> data = JsonUtil.getJsonToList(menuAll, AppMenuListVO.class);
        return ActionResult.success(data);
    }

    private void next(List<ModuleModel> buttonListAll,String parentId,Set<ModuleModel> list){
        List<ModuleModel> menuList = buttonListAll.stream().filter(t -> t.getId().equals(parentId) || t.getParentId().equals(parentId)).collect(Collectors.toList());
        for (ModuleModel model : menuList) {
            if(!list.contains(model)){
                list.add(model);
                next(buttonListAll,model.getId(),list);
            }
        }
    }

}

