package com.sunxung.factoring.web.purchcontractmanagement;

import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.businessmanagement.BusinessBasicInfo;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractManagement;
import com.sunxung.factoring.entity.purchcontractmanagement.vo.PurchContractApprovalVo;
import com.sunxung.factoring.entity.tradingPlatform.Platform;
import com.sunxung.factoring.service.businessmanagement.BusinessBasicInfoService;
import com.sunxung.factoring.service.project.IProjectInformationService;
import com.sunxung.factoring.service.purchcontractmanagement.PurchContractApprovalService;
import com.sunxung.factoring.service.purchcontractmanagement.PurchContractService;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import com.sunxung.factoring.service.tradingPlatform.PlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description : 采购合同审核controller层
 * @Author : JiaYf
 * @Date: 2021-09-18 15:30
 */

@RestController
@RequestMapping("/purchContract/approval")
public class PurchContractApprovalController {

    @Autowired
    private PurchContractApprovalService purchContractApprovalService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private PlatformService platformService;

    @Autowired
    private PurchContractService purchContractService;


    /**
     * 跳转采购合同审核页面
     *
     * @param businessKey 采购合同Id
     * @return
     */
    @GetMapping("/add")
    public ResponseJson add(Long businessKey) {
        //根据ID获取 采购合同基本信息
        PurchContractApprovalVo approvalVo = purchContractApprovalService.getDetail(businessKey);
        return new ResponseJson(approvalVo);
    }

    /**
     * 制作合同
     *
     * @param purchContractApprovalVo
     * @return
     */
    @PostMapping("makeContractPdf")
    public ResponseJson makeContractPdf(@RequestBody PurchContractApprovalVo purchContractApprovalVo) {
        PurchContractManagement purchContractManagement = purchContractService.getById(purchContractApprovalVo.getBusinessKey());
        //贸易平台的信息
        Platform platform = platformService.getById(purchContractManagement.getTradingPlatformId());
        if (!platform.getAuthStatus().equals(1) || !platform.isAutoSignStatus()) {
            throw new ValidatorException(CodeUtil.FAIL, "贸易平台未认证或者未开启自动签署，无法制作合同！");
        }
        return new ResponseJson(purchContractApprovalService.makeContractPdf(purchContractApprovalVo, true));
    }

    /**
     * 采购合同审核提交
     *
     * @param purchContractApprovalVo 采购合同审核信息
     * @return
     */
    @PostMapping("/doAudit")
    @OperationLog(operationModule = "采购合同", operationType = OperationTypeEnum.INSERT, description = "采购合同审核提交")
    public Object doAudit(@RequestBody PurchContractApprovalVo purchContractApprovalVo) {
        flowableService.claimTask(purchContractApprovalVo.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        Boolean makeContract = true;
        if (purchContractApprovalVo.getVisible().equals(0)) {
            makeContract = false;
        }
        purchContractApprovalService.makeContractPdf(purchContractApprovalVo,  makeContract);
        return new ResponseJson("审核完成");
    }
}
