package com.trade.client.trade.purchasecontract.controller;

import com.trade.client.trade.purchasecontract.dto.PurchaseContractSearchDto;
import com.trade.client.trade.purchasecontract.dto.PurchaseContractUploadDto;
import com.trade.common.core.domain.ResponseResult;
import com.trade.common.core.page.GridPage;
import com.trade.service.filestorage.dto.FileAbstractDto;
import com.trade.service.trade.purchasecontract.dto.PurchaseContractDetailDto;
import com.trade.service.trade.purchasecontract.dto.PurchaseContractDto;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.trade.client.trade.purchasecontract.service.PurchaseContractService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 采购合同接口
 *
 * @author hlb
 * @since 2024-09-07 16:30:10
 */
@RestController
@RequestMapping("/purchaseContract")
public class PurchaseContractController {

    /**
     * 服务对象
     */
    @Resource
    private PurchaseContractService purchaseContractService;

    /**
     * 分页查询采购合同基础信息
     *
     * @param search 查询实体
     * @return 所有数据
     */
    @GetMapping("/pages")
    public ResponseResult<GridPage<PurchaseContractDto>> getPages(PurchaseContractSearchDto search) {
        return ResponseResult.success(purchaseContractService.getPurchaseContractBySearch(search));
    }

    /**
     * 通过主键查询采购合同详细信息
     *
     * @param id 主键
     * @return 采购合同详细信息
     */
    @GetMapping("/{id}")
    public ResponseResult<PurchaseContractDetailDto> queryById(@PathVariable Long id) {
        return ResponseResult.success(purchaseContractService.findPurchaseContractDetailById(id));
    }

    /**
     * 编辑采购合同
     * 
     * @param operate 操作：保存（save）；或提交（submit）
     * @param purchaseContractDto 实体对象
     * @return 新增结果
     */
    @PostMapping("/upsert")
    public ResponseResult<List<FileAbstractDto>> upsertPurchaseContract(@RequestParam String operate,
            @RequestBody PurchaseContractDetailDto purchaseContractDto) {
        return ResponseResult.success(purchaseContractService.upsertPurchaseContract(operate, purchaseContractDto));
    }

    /**
     * 上传采购合同
     *
     * @param uploadDto
     * @return 上传结果
     */
    @PostMapping("/upload")
    public ResponseResult<Boolean> uploadPurchaseContract(@RequestBody @Validated PurchaseContractUploadDto uploadDto) {
        return ResponseResult.success(purchaseContractService.uploadPurchaseContract(uploadDto));
    }

    /**
     * 采购合同（含承诺函）电子签章
     *
     * @param purchaseContractId 采购合同ID
     * @return 电子签章结果
     */
    @PostMapping("/esign")
    public ResponseResult<Boolean> esignPurchaseContract(@RequestParam Long purchaseContractId) {
        return ResponseResult.success(purchaseContractService.esignPurchaseContract(purchaseContractId));
    }
}
