package com.yhy.module.core.controller.admin.workbench;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.workbench.vo.audit.AuditApproveDivisionUpdateReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.audit.AuditDivisionTreeRespVO;
import com.yhy.module.core.controller.admin.workbench.vo.audit.AuditModeCreateReqVO;
import com.yhy.module.core.controller.admin.workbench.vo.audit.AuditModeRespVO;
import com.yhy.module.core.service.workbench.AuditModeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 审核模式 Controller
 *
 * @author yhy
 */
@Tag(name = "工作台 - 审核模式")
@RestController
@RequestMapping("/core/wb/audit-mode")
@Validated
public class AuditModeController {

    @Resource
    private AuditModeService auditModeService;

    @PostMapping("/create")
    @Operation(summary = "创建审核模式（复制审定快照）")
    @PreAuthorize("@ss.hasPermission('core:wb-audit:create')")
    public CommonResult<Long> createAuditMode(@Valid @RequestBody AuditModeCreateReqVO createReqVO) {
        return success(auditModeService.createAuditMode(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除审核模式")
    @Parameter(name = "id", description = "审核模式ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-audit:delete')")
    public CommonResult<Boolean> deleteAuditMode(@RequestParam("id") Long id) {
        auditModeService.deleteAuditMode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取审核模式详情")
    @Parameter(name = "id", description = "审核模式ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-audit:query')")
    public CommonResult<AuditModeRespVO> getAuditMode(@RequestParam("id") Long id) {
        return success(auditModeService.getAuditMode(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获取项目下的审核模式列表")
    @Parameter(name = "projectId", description = "项目ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb-audit:query')")
    public CommonResult<List<AuditModeRespVO>> getAuditModeList(@RequestParam("projectId") Long projectId) {
        return success(auditModeService.getAuditModeList(projectId));
    }

    @PutMapping("/update-approve")
    @Operation(summary = "更新审定数据")
    @PreAuthorize("@ss.hasPermission('core:wb-audit:update')")
    public CommonResult<Boolean> updateApproveData(@Valid @RequestBody AuditApproveDivisionUpdateReqVO updateReqVO) {
        auditModeService.updateApproveData(updateReqVO);
        return success(true);
    }

    @GetMapping("/division-tree")
    @Operation(summary = "获取分部分项树（包含送审、审定、差异）")
    @Parameters({
            @Parameter(name = "auditModeId", description = "审核模式ID", required = true, example = "1"),
            @Parameter(name = "compileTreeId", description = "编制模式树的单位工程节点ID", required = true, example = "1")
    })
    @PreAuthorize("@ss.hasPermission('core:wb-audit:query')")
    public CommonResult<List<AuditDivisionTreeRespVO>> getDivisionTree(
            @RequestParam("auditModeId") Long auditModeId,
            @RequestParam("compileTreeId") Long compileTreeId) {
        return success(auditModeService.getDivisionTree(auditModeId, compileTreeId));
    }
}
