/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.auth.controller.manager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.auth.entity.SysMenu;
import com.codvision.auth.entity.dto.SysMenuQueryDTO;
import com.codvision.auth.entity.vo.SysMenuVO;
import com.codvision.auth.service.ISysMenuService;
import com.codvision.auth.support.UserContext;
import com.codvision.authcore.annotation.RequiresPermissions;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.annotation.ApiSecret;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统菜单 管理模块
 *
 * @author lingee
 * @date 2023-11-09
 */
@RestController
@RequestMapping("/v1/manager/sysMenu")
@Api(tags = "平台管理端-系统菜单管理模块")
public class ManagerSysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;

    @ApiOperation(value = "获取系统菜单详情", notes = "获取系统菜单详情接口")
    @PostMapping("/getDetail")
    public ResponseEntity<SysMenuVO> getById(@ApiParam(value = "系统菜单ID", required = true) @RequestParam Long id) {
        SysMenuVO vo = sysMenuService.getDetail(id);
        return new ResponseEntity<SysMenuVO>().ok().data(vo);
    }

    @ApiOperation(value = "获取系统菜单分页列表", notes = "获取系统菜单分页列表接口")
    @PostMapping("/findAll")
    @RequiresPermissions("sys_menu_list")
    public ResponseEntity<IPage<SysMenuVO>> findAll(@RequestParam(required = false) String name,
                                                    @RequestParam(required = false) Integer type,
                                                    @RequestParam(required = false) Boolean ignoreApi,
                                                    @ApiParam(value = "菜单路径") @RequestParam(value = "pathName", required = false) String pathName,
                                                    @ApiParam(value = "菜单类型(多个逗号隔开)") @RequestParam(value = "category", required = false) String category,
                                                    @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                    @RequestParam(value = "size", required = false, defaultValue = "1000") int size) {
        QueryPage<SysMenu> queryPage = new QueryPage<>(page, size);

        SysMenuQueryDTO queryDTO = new SysMenuQueryDTO(name, type, ignoreApi, category, category);

        IPage<SysMenuVO> pageResult = sysMenuService.findAll(queryPage, queryDTO);
        return new ResponseEntity<IPage<SysMenuVO>>().ok().data(pageResult);
    }

    @ApiOperation(value = "新增系统菜单", notes = "新增系统菜单接口")
    @PostMapping("/add")
    @RequiresPermissions("sys_menu_add")
    public ResponseEntity<String> addSysMenu(@Valid @RequestBody SysMenu sysMenu) {
        sysMenuService.addSysMenu(sysMenu);
        return new ResponseEntity<String>().ok().message("添加成功");
    }

    @ApiOperation(value = "批量删除系统菜单", notes = "批量删除系统菜单接口")
    @PostMapping("/batchDel")
    @RequiresPermissions("sys_menu_del")
    public ResponseEntity<String> batchDel(@ApiParam(value = "系统菜单ID列表", required = true) @RequestBody List<Integer> menuIdList) {
        if (CollectionUtils.isEmpty(menuIdList)) {
            throw new BusinessException(30002);
        }

        sysMenuService.update(Wrappers.<SysMenu>lambdaUpdate().setSql("del_flag = 1, del_key = id").in(SysMenu::getId, menuIdList));
        return new ResponseEntity<String>().ok().message("删除成功");
    }

    @ApiOperation(value = "编辑系统菜单", notes = "编辑接系统菜单接口")
    @PostMapping("/update")
    @RequiresPermissions("sys_menu_edit")
    public ResponseEntity<String> updateSysMenu(@RequestBody SysMenu sysMenu) {
        if (null == sysMenu.getId() || StringUtils.isBlank(sysMenu.getPathName())) {
            throw new BusinessException(30002);
        }

        sysMenuService.updateSysMenu(sysMenu);
        return new ResponseEntity<String>().ok().message("编辑成功");
    }

    @PostMapping("/getUserMenuList")
    @ApiOperation(value = "权限菜单列表", notes = "获取权限菜单列表接口")
    @RequiresPermissions("sys_menu_list")
    public ResponseEntity<List<SysMenuVO>> getUserMenuList(@RequestParam(required = false) String name,
                                                         @RequestParam(required = false) Integer type,
                                                         @ApiParam(value = "pathName") @RequestParam(value = "pathName", required = false) String pathName,
                                                         @ApiParam(value = "菜单类型(多个逗号隔开)") @RequestParam(value = "category", required = false) String category) {
        ResponseEntity<List<SysMenuVO>> responseEntity = new ResponseEntity<>();
        SysMenuQueryDTO queryDTO = new SysMenuQueryDTO(name, type, null, pathName, category);

        queryDTO.setUid(UserContext.get().getId());

        List<SysMenuVO> list = sysMenuService.getUserMenuList(queryDTO);

        return responseEntity.data(list);
    }
}
