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.CodeEpDTO;
import cn.ey.biz.module.system.controller.admin.code.vo.CodeEpVO;
import cn.ey.biz.module.system.controller.admin.code.vo.HistoryVO;
import cn.ey.biz.module.system.service.code.CodeEpService;
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/ep")
@Validated
@Slf4j
public class CodeEpController {

    @Resource
    private CodeEpService codeEpService;

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

    @GetMapping("/get")
    @ApiOperation("获取生效的项目角色配置")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<CodeEpDTO> getCodeEp(@RequestParam(required = false) Long ver) {
        return R.success(codeEpService.getCodeEpLevel(ver));
    }

    @GetMapping("/front/get")
    @ApiOperation("前台获取生效的项目角色配置")
    @PreAuthorize("@ss.hasPermission('system:engage-code:query')")
    public R<CodeEpVO> getCodeEpForFront(@RequestParam(required = false) Long ver) {
        CodeEpVO codeEpVO=codeEpService.getCodeEpLevelForFront(ver);
        return R.success(codeEpVO);

    }

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

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