package com.ruoyi.ccdi.project.controller;

import com.ruoyi.ccdi.project.domain.dto.CcdiEvidenceQueryDTO;
import com.ruoyi.ccdi.project.domain.dto.CcdiEvidenceSaveDTO;
import com.ruoyi.ccdi.project.domain.vo.CcdiEvidenceVO;
import com.ruoyi.ccdi.project.service.CcdiProjectAccessService;
import com.ruoyi.ccdi.project.service.ICcdiEvidenceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
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 java.util.List;

/**
 * 项目证据Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/ccdi/evidence")
@Tag(name = "项目证据")
public class CcdiEvidenceController extends BaseController {

    @Resource
    private ICcdiEvidenceService evidenceService;

    @Resource
    private CcdiProjectAccessService projectAccessService;

    /**
     * 保存证据
     */
    @PostMapping
    @Operation(summary = "保存证据")
    @PreAuthorize("@ss.hasPermi('ccdi:project:edit')")
    public AjaxResult saveEvidence(@Validated @RequestBody CcdiEvidenceSaveDTO dto) {
        projectAccessService.assertCanOperate(dto.getProjectId());
        CcdiEvidenceVO vo = evidenceService.saveEvidence(dto, SecurityUtils.getUsername());
        return AjaxResult.success("证据入库成功", vo);
    }

    /**
     * 查询项目证据列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询项目证据列表")
    public AjaxResult listEvidence(CcdiEvidenceQueryDTO queryDTO) {
        projectAccessService.assertCanRead(queryDTO.getProjectId());
        List<CcdiEvidenceVO> list = evidenceService.listEvidence(queryDTO);
        return AjaxResult.success(list);
    }

    /**
     * 查询证据详情
     */
    @GetMapping("/{evidenceId}")
    @Operation(summary = "查询证据详情")
    public AjaxResult getEvidence(@PathVariable Long evidenceId) {
        projectAccessService.assertCanReadByEvidenceId(evidenceId);
        CcdiEvidenceVO vo = evidenceService.getEvidence(evidenceId);
        return AjaxResult.success(vo);
    }
}
