package com.lantone.security.web;

import com.lantone.common.api.CommonResult;
import com.lantone.security.dto.GetMenuByIdDTO;
import com.lantone.security.dto.GetMenusTreeDTO;
import com.lantone.security.vo.AddMenuVO;
import com.lantone.security.vo.UpdateMenuVO;
import com.lantone.security.facade.FuncManagementFacade;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Description: 功能管理API
 * @author: songxl
 * @time: 2021/7/20 12:39
 */
@RestController
@Api(value = "功能管理API", tags = { "功能管理API" })
@RequestMapping("/funcManage")
@EnableAsync
public class FuncManagementController {
    @Autowired
    private FuncManagementFacade funcManagementFacade;

    @ApiOperation(value = "获取功能菜单列表 [by:songxl]")
    @PostMapping("/getMenusTree")
    public CommonResult<List<GetMenusTreeDTO>> getMenusTree() {
        return CommonResult.success(funcManagementFacade.getMenusTree());
    }

    @ApiOperation(value = "添加功能菜单 [by:songxl]")
    @PostMapping("/addMenu")
    @Transactional
    public CommonResult<Boolean> addMenu(@RequestBody @Valid AddMenuVO addMenuVO) {
        return CommonResult.success(funcManagementFacade.addMenu(addMenuVO));
    }

    @ApiOperation(value = "修改功能菜单 [by:songxl]")
    @PostMapping("/updateMenu")
    @Transactional
    public CommonResult<Boolean> updateMenu(@RequestBody @Valid UpdateMenuVO updateMenuVO) {
        return CommonResult.success(funcManagementFacade.updateMenu(updateMenuVO));
    }

    @ApiOperation(value = "查看功能菜单 [by:songxl]")
    @PostMapping("/getMenuById")
    public CommonResult<GetMenuByIdDTO> getMenuById(@RequestParam @Valid @NotNull(message = "功能菜单编号为空") Long id, @RequestParam @Valid @NotNull(message = "系统编号为空") Long softwareId) {
        return CommonResult.success(funcManagementFacade.getMenuById(id, softwareId));
    }

    @ApiOperation(value = "删除功能菜单 [by:songxl]")
    @PostMapping("/deleteMenu")
    @Transactional
    public CommonResult<Boolean> deleteMenu(@RequestParam @Valid @NotNull(message = "功能菜单编号为空") Long id) {
        return CommonResult.success(funcManagementFacade.deleteMenu(id));
    }

    @ApiOperation(value = "启用禁用功能菜单 [by:songxl]")
    @PostMapping("/disableMenu")
    @Transactional
    public CommonResult<Boolean> disableMenu(@RequestParam @Valid @NotNull(message = "功能菜单id为空") Long id, @RequestParam @Valid @NotBlank(message = "启用禁用status为空") String status) {
        return CommonResult.success(funcManagementFacade.disableMenu(id, status));
    }

    @ApiOperation(value = "加载所有资源与角色访问对应关系[by:cy]")
    @PostMapping("/initResourceRolesMap")
    @ApiIgnore
    public CommonResult<Boolean> initResourceRolesMap() {
        return CommonResult.success(funcManagementFacade.initResourceRolesMap());
    }

}