package com.zt.plat.module.contractorder.controller.admin.contract;

import cn.hutool.json.JSONObject;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.contractorder.api.ContractApi;
import com.zt.plat.module.contractorder.api.dto.order.PurchaseOrderWithDetailsDTO;
import com.zt.plat.module.contractorder.api.vo.contract.*;
import com.zt.plat.module.contractorder.api.vo.contract.international.IntContract;
import com.zt.plat.module.contractorder.api.vo.contract.international.IntContractPageReq;
import com.zt.plat.module.contractorder.api.vo.contract.international.IntPushContractReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.contract.ContractMainDO;
import com.zt.plat.module.contractorder.service.contract.ContractService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zt.plat.framework.common.pojo.CommonResult.error;
import static com.zt.plat.framework.common.pojo.CommonResult.success;
import static com.zt.plat.module.contractorder.enums.ErrorCodeConstants.CONTRACT_SUBMIT_ERP_FAIL;

@Slf4j
@Tag(name = "管理后台 - 合同管理")
@RestController
@RequestMapping("/base/contract-order/contract")
@Validated
public class ContractController implements BusinessControllerMarker {

    @Resource
    private ContractService contractService;
    @Resource
    private ContractApi contractApi;

    @GetMapping("/page")
    @Operation(summary = "获得合同分页列表")
    @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<PageResult<ContractRespVO>> getPage(@Valid ContractPageReqVO pageReqVO) {
        PageResult<ContractMainDO> pageResult = contractService.getContractPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractRespVO.class));
    }

    @GetMapping("/nots")
    @Operation(summary = "查询不计价规则列表")
    //@PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<PageResult<NotRespVO>> getNots(NotsQueryReqVO queryReqVO) {
        return success(contractService.getNots(queryReqVO));
    }

    @GetMapping("/demotes")
    @Operation(summary = "查询参数降级规则列表")
    //@PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<PageResult<DemoteRespVO>> getDemotes(DemotesQueryReqVO queryReqVO) {
        return success(contractService.getDemotes(queryReqVO));
    }

    @GetMapping("/formulas")
    @Operation(summary = "查询结算公式列表")
    //@PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<PageResult<FormulaRespVO>> getFormulas(FormulasQueryReqVO queryReqVO) {
        return success(contractService.getFormulas(queryReqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "新增合同")
    //@PreAuthorize("@ss.hasAnyPermissions({'base:contract:create','purchaseContract:add'})")
    public CommonResult<JSONObject> create(@Valid @RequestBody ContractSaveReqVO reqVO) {
        Long id = contractService.createContract(reqVO);
        return success(new JSONObject().putOnce("id", id));
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同详情")
    @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<ContractRespVO> get(@RequestParam("id") Long id) {
        ContractRespVO contractRespVO = contractService.get(id);
        return success(contractRespVO);
    }

    @GetMapping("/get/by-system-contract-number")
    @Operation(summary = "根据系统合同编号获得合同详情")
   // @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<ContractRespVO> getBySystemContractNumber(@RequestParam("systemContractNumber") String systemContractNumber) {
        ContractRespVO contractRespVO = contractService.getBySystemContractNumber(systemContractNumber);
        return success(contractRespVO);
    }

    @PutMapping("update")
    @Operation(summary = "修改合同")
    //@PreAuthorize("@ss.hasAnyPermissions({'base:contract:update','purchaseContract:edit'})")
    public CommonResult<Boolean> update(@Valid @RequestBody ContractSaveReqVO reqVO) {
        return success(contractService.update(reqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同")
    //@PreAuthorize("@ss.hasAnyPermissions('base:contract:delete')")
    public CommonResult<List<JSONObject>> delete(@RequestBody List<Long> ids) {
        return success(contractService.delete(ids));
    }

    @PostMapping("/download")
    @Operation(summary = "下载文件")
    //@PreAuthorize("@ss.hasAnyPermissions('base:contract:download')")
    public ResponseEntity<ByteArrayResource> download(@RequestBody List<Long> ids) {
        return contractService.download(ids);
    }

    @PostMapping("/cancel")
    @Operation(summary = "作废")
    //@PreAuthorize("@ss.hasAnyPermissions('base:contract:cancel')")
    public CommonResult<Boolean> cancel(@RequestBody List<Long> ids) {
        return success(contractService.cancel(ids));
    }

    @PostMapping("/complete")
    @Operation(summary = "完结")
    //@PreAuthorize("@ss.hasAnyPermissions('base:contract:complete')")
    public CommonResult<Boolean> complete(@RequestBody List<Long> ids) {
        return success(contractService.complete(ids));
    }

    @PostMapping("/archive")
    @Operation(summary = "归档")
    //@PreAuthorize("@ss.hasAnyPermissions('base:contract:archive')")
    public CommonResult<Boolean> archive(@RequestBody List<Long> ids) {
        return success(contractService.archive(ids));
    }

    @GetMapping("/submit/approval")
    @Operation(summary = "合同提交审批")
   // @PreAuthorize("@ss.hasAnyPermissions('base:contract:approval')")
    public CommonResult<Boolean> submitApproval(@RequestParam("id") Long id) {
        return success(contractService.submitApproval(id));
    }

    @PostMapping("/approval")
    @Operation(summary = "合同审批")
   // @PreAuthorize("@ss.hasAnyPermissions('base:contract:approval')")
    public CommonResult<Boolean> approval(@Valid @RequestBody ApprovalReqVO reqVO) {
        return success(contractService.approval(reqVO));
    }

    @PostMapping("/submit/erp")
    @Operation(summary = "提交ERP")
   // @PreAuthorize("@ss.hasAnyPermissions('base:contract:erp')")
    public CommonResult<JSONObject> submitErp(@RequestParam("id") Long id) {
        JSONObject res = contractService.submitErp(id);
        return res.getBool("success") ? success(res) : error(CONTRACT_SUBMIT_ERP_FAIL.getCode(), res.getStr("data"));
    }

    @GetMapping("/list/up-not-relation")
    @Operation(summary = "获得上游未关联合同列表")
    @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<List<ContractRespVO>> getListUpNotRelation(@RequestParam("id") Long id) {
        return success(contractService.getListUpNotRelation(id));
    }

    @GetMapping("/list/down-not-relation")
    @Operation(summary = "获得下游未关联合同列表")
   // @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<List<ContractRespVO>> getListDownNotRelation(@RequestParam("id") Long id) {
        return success(contractService.getListDownNotRelation(id));
    }

    @GetMapping("/get/up-relation")
    @Operation(summary = "获得上游关联的合同数据")
    //@PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<ContractRespVO> getUpRelation(@RequestParam("id") Long id) {
        return success(contractService.getUpRelation(id));
    }

    @GetMapping("/get/down-relation")
    @Operation(summary = "获得下游关联的合同数据")
    //@PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<ContractRespVO> getDownRelation(@RequestParam("id") Long id) {
        return success(contractService.getDownRelation(id));
    }

    @PostMapping("/relation")
    @Operation(summary = "关联合同")
   // @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<Boolean> relation(@RequestBody RelationReqVo reqVo) {
        return success(contractService.relation(reqVo));
    }

    @PostMapping("/push")
    @Operation(summary = "国贸2.0系统推送合同")
    @PreAuthorize("@ss.hasAnyPermissions('base:contract:create')")
    public void push(@Valid @RequestBody IntPushContractReqVO pushReqVO) {
        contractApi.push(pushReqVO);
    }

    @PostMapping("/logistics/list/page")
    @Operation(summary = "国贸2.0系统合同分页查询")
    @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<PageResult<IntContract>> logisticsListPage(IntContractPageReq pageReq) {
        return contractApi.logisticsListPage(pageReq);
    }

    @PostMapping("/order-by-order-no")
    @Operation(summary = "通过订单编号获取订单信息", description = "通过订单编号获取订单信息")
    @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<List<PurchaseOrderWithDetailsDTO>> getOrderByOrderNo(@RequestBody List<String> orderNoS){
        return contractApi.getOrderByOrderNo(orderNoS);
    }

    @GetMapping("/transaction-info/by-paper-number")
    @Operation(summary = "根据合同编号获得交易信息")
    @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<TransactionInfoRespVO> transactionInfoByPaperNumber(
            @RequestParam("contractPaperNumber") String contractPaperNumber
    ) {
        return success(contractService.transactionInfoByPaperNumber(contractPaperNumber));
    }

    @PostMapping("/relationContract")
    @Operation(summary = "关联合同-新")
    // @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<Boolean> relationContract(@RequestBody RelationContractVO reqVo) {
        contractService.relationContract(reqVo);
        return success(true);
    }


    @GetMapping("/page-no-permission")
    @Operation(summary = "获得合同分页列表-没有权限")
    @PreAuthorize("@ss.hasAnyPermissions({'base:contract:get','purchaseContract:detail'})")
    public CommonResult<PageResult<ContractRespVO>> getPageNoPermission(@Valid ContractPageReqVO pageReqVO) {
        PageResult<ContractMainDO> pageResult = contractService.getPageNoPermission(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContractRespVO.class));
    }

}
