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.CodeProjectDTO;
import cn.ey.biz.module.system.controller.admin.code.vo.CodeProjectVO;
import cn.ey.biz.module.system.controller.admin.code.vo.HistoryVO;
import cn.ey.biz.module.system.service.code.CodeProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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/project")
@Validated
@Slf4j
public class CodeProjectController {

    @Resource
    private CodeProjectService codeProjectService;

    @PostMapping("/submit")
    @ApiOperation("提交")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<Long> submitProject(@Valid @RequestBody CodeProjectVO codeProjectVO) {
        Long codeProjectId = codeProjectService.submitProject(codeProjectVO);
        return R.success(codeProjectId);
    }

    @GetMapping("/get")
    @ApiOperation("获取生效的项目名称配置")
    @PreAuthorize("@ss.hasPermission('system:engage-code:edit')")
    public R<CodeProjectDTO> getCodeProject(@RequestParam(required = false) Long ver) {
        CodeProjectDTO codeProjectDTO=codeProjectService.getCodeProject(ver);
        return R.success(codeProjectDTO);
    }

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

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