package org.nl.system.controller.menu;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.ApiOperation;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.logging.annotation.Log;
import org.nl.system.service.menu.ISysMenuService;
import org.nl.system.service.menu.dao.SysMenu;
import org.nl.system.service.menu.dto.MenuDto;
import org.nl.system.service.menu.dto.MenuQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author generator
 * @since 2022-12-15
 */
@RestController
@RequestMapping("api/sysMenu")
public class SysMenuController {
    @Autowired
    private ISysMenuService iSysMenuService;

    @GetMapping(value = "/build")
    @ApiOperation("根据用户获取菜单")
    public ResponseEntity<Object> buildMenus(@Validated String systemType) {
        //校验系统表是否存在该系统类型
        return new ResponseEntity(iSysMenuService.buildMenus(systemType),HttpStatus.OK);
    }

    @ApiOperation("返回全部的菜单")
    @GetMapping(value = "/lazy")
    @SaCheckPermission(value = {"menu:list", "roles:list"}, mode = SaMode.AND)
    public ResponseEntity<Object> query(@RequestParam String pid) {
        return new ResponseEntity<>(iSysMenuService.getMenus(pid), HttpStatus.OK);
    }
    @ApiOperation("获取菜单列表")
    @PostMapping(value = "/getMenusByRole")
    @SaCheckPermission(value = {"menu:list", "roles:list"}, mode = SaMode.AND)
    public ResponseEntity<Object> getMenusByRole(@RequestBody JSONObject json) {
        String role_id = json.getString("role_id");
        String system_type = json.getString("system_type");
        String category = json.getString("category");
        return new ResponseEntity<>(iSysMenuService.getMenusByRole(role_id, system_type, category), HttpStatus.OK);
    }

    @ApiOperation("根据菜单ID返回所有子节点ID，包含自身ID")
    @GetMapping(value = "/child")
    @SaCheckPermission(value = {"menu:list", "roles:list"}, mode = SaMode.AND)
    public ResponseEntity<Object> child(@RequestParam String id) {
        Set<SysMenu> menuSet = new HashSet<>();
        List<SysMenu> menuList = iSysMenuService.getMenus(id);
        menuSet.add(iSysMenuService.findById(id));
        menuSet = iSysMenuService.getChildMenus(menuList, menuSet);
        Set<String> ids = menuSet.stream().map(SysMenu::getMenu_id).collect(Collectors.toSet());
        return new ResponseEntity<>(ids, HttpStatus.OK);
    }

    @GetMapping
    @ApiOperation("查询菜单")
    @SaCheckPermission("menu:list")
    public ResponseEntity<Object> pageQuery(MenuQuery query, PageQuery page) throws Exception {
        if (ObjectUtil.isEmpty(page.getSort())) {
            page.setSort("menu_sort,asc");
        }
        List list = iSysMenuService.query(query, page);
        return new ResponseEntity<>(TableDataInfo.build(list), HttpStatus.OK);
    }

    @ApiOperation("查询菜单:根据ID获取同级与上级数据")
    @PostMapping("/superior")
    @SaCheckPermission("menu:list")
    public ResponseEntity<Object> getSuperior(@RequestBody JSONObject param) {
        Set<MenuDto> menuDtos = new LinkedHashSet<>();
        String id=param.getString("ids");
        if (StrUtil.isNotEmpty(id)) {
            menuDtos.addAll(iSysMenuService.getSuperior(iSysMenuService.doToDto(iSysMenuService.findById(id)), new ArrayList<>()));
            return new ResponseEntity<>(iSysMenuService.buildTree(new ArrayList<>(menuDtos)), HttpStatus.OK);
        }
        return new ResponseEntity<>(iSysMenuService.getMenus(null), HttpStatus.OK);
    }

    @Log("新增菜单")
    @ApiOperation("新增菜单")
    @PostMapping
    @SaCheckPermission("menu:add")
    public ResponseEntity<Object> create(@RequestBody SysMenu form) {
        iSysMenuService.create(form);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改菜单")
    @ApiOperation("修改菜单")
    @PutMapping
    @SaCheckPermission("menu:edit")
    public ResponseEntity<Object> update( @RequestBody SysMenu form) {
        iSysMenuService.update(form);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除菜单")
    @ApiOperation("删除菜单")
    @DeleteMapping
    @SaCheckPermission("menu:del")
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
        Set<SysMenu> menuSet = new HashSet<>();
        for (String id : ids) {
            List<SysMenu> menuList = iSysMenuService.getMenus(id);
            menuSet.add(iSysMenuService.findById(id));
            menuSet = iSysMenuService.getChildMenus(menuList, menuSet);
        }
        iSysMenuService.delete(menuSet);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

