package com.njcn.rdms.module.project.controller.admin.performance;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.constant.PerformanceConstants;
import com.njcn.rdms.module.project.controller.admin.performance.vo.PerformanceTemplatePageReqVO;
import com.njcn.rdms.module.project.controller.admin.performance.vo.PerformanceTemplateRespVO;
import com.njcn.rdms.module.project.controller.admin.performance.vo.PerformanceTemplateUploadReqVO;
import com.njcn.rdms.module.project.service.performance.PerformanceTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 绩效模板")
@RestController
@RequestMapping("/project/performance-templates")
@Validated
public class PerformanceTemplateController {

    @Resource
    private PerformanceTemplateService performanceTemplateService;

    @GetMapping("/current")
    @Operation(summary = "获取当前生效绩效模板")
    @PreAuthorize("@ss.hasPermission('" + PerformanceConstants.PERMISSION_TEMPLATE_QUERY + "')")
    public CommonResult<PerformanceTemplateRespVO> current() {
        return success(performanceTemplateService.getCurrentTemplate());
    }

    @GetMapping("/page")
    @Operation(summary = "获取绩效模板分页")
    @PreAuthorize("@ss.hasPermission('" + PerformanceConstants.PERMISSION_TEMPLATE_QUERY + "')")
    public CommonResult<PageResult<PerformanceTemplateRespVO>> page(@Valid PerformanceTemplatePageReqVO reqVO) {
        return success(performanceTemplateService.getTemplatePage(reqVO));
    }

    @PostMapping("/upload")
    @Operation(summary = "保存绩效模板上传记录")
    @PreAuthorize("@ss.hasPermission('" + PerformanceConstants.PERMISSION_TEMPLATE_UPDATE + "')")
    public CommonResult<Long> upload(@Valid @RequestBody PerformanceTemplateUploadReqVO reqVO) {
        return success(performanceTemplateService.uploadTemplate(reqVO));
    }

    @PostMapping("/{id}/activate")
    @Operation(summary = "启用绩效模板")
    @PreAuthorize("@ss.hasPermission('" + PerformanceConstants.PERMISSION_TEMPLATE_UPDATE + "')")
    public CommonResult<Boolean> activate(@PathVariable("id") Long id) {
        performanceTemplateService.activateTemplate(id);
        return success(true);
    }
}
