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.CodeOtherDTO;
import cn.ey.biz.module.system.controller.admin.code.vo.CodeOtherVO;
import cn.ey.biz.module.system.controller.admin.code.vo.HistoryVO;
import cn.ey.biz.module.system.service.code.CodeOtherService;
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 = "管理后台 - 其他规则")
@RestController
@RequestMapping("/system/code/other")
@Validated
public class CodeOtherController {

    @Resource
    private CodeOtherService codeOtherService;

    @PostMapping("/submit")
    @ApiOperation("提交")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<Long> submitOther(@Valid @RequestBody CodeOtherVO codeOtherVO) {
        Long codeCustomerId = codeOtherService.submitOther(codeOtherVO);
        return R.success(codeCustomerId);
    }

    @GetMapping("/get")
    @ApiOperation("获取生效的其他配置")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<CodeOtherDTO> getCodeOther(@RequestParam(required = false) Long ver) {
        return R.success(codeOtherService.getCodeOther(ver));
    }

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

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