package cn.ey.biz.module.system.controller.admin.code;

import cn.ey.biz.common.pojo.PageParam;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.api.code.dto.CodeBlockingLevelDTO;
import cn.ey.biz.module.system.controller.admin.code.vo.CodeBlockingLevelVO;
import cn.ey.biz.module.system.controller.admin.code.vo.HistoryVO;
import cn.ey.biz.module.system.service.code.CodeBlockingLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "管理后台 - BlockingLevel")
@RestController
@RequestMapping("/system/code/blocking")
@Validated
public class CodeBlockingLevelController {

    @Resource
    private CodeBlockingLevelService codeBlockingLevelService;

    @PostMapping("/submit")
    @ApiOperation("提交")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<Long> submitBlockingLevel(@Valid @RequestBody CodeBlockingLevelVO codeBlockingLevelVO) {
        Long blockingLevel = codeBlockingLevelService.submitBlockingLevel(codeBlockingLevelVO);
        return R.success(blockingLevel);
    }

    @GetMapping("/get")
    @ApiOperation("获取生效的BlockingLevel配置")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<CodeBlockingLevelDTO> getCodeBlockingLevel(@RequestParam(required = false) Long ver) {
        return R.success(codeBlockingLevelService.getCodeBlockingLevel(ver));
    }

    @PostMapping("/history")
    @ApiOperation("获取BlockingLevel历史记录")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<PageResult<HistoryVO>> getHistory(@RequestBody PageParam pageParam) {
        PageResult<HistoryVO> histories = codeBlockingLevelService.getHistory(pageParam);
        return R.success(histories);
    }

    @PostMapping("/revert")
    @ApiOperation("生效指定版本")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<?> revertHistory(@RequestParam("ver") Long ver) {
        codeBlockingLevelService.revertHistory(ver);
        return R.success(null);
    }
}
