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.ElectronicSignatureUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.purchcontractmanagement.*;
import com.sunxung.factoring.entity.purchcontractmanagement.vo.PurchContractVo;
import com.sunxung.factoring.entity.purchcontractmanagement.vo.SearchGoods;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.purchcontractmanagement.*;
import com.sunxung.factoring.service.supplier.ISupplierInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author Yafan Wang
 * @Date 2023/12/18 15:44
 */
@RestController
@RequestMapping("/api/purchManagement")
public class PurchContractApiController {

    @Autowired
    private PurchContractService purchContractService;
    @Autowired
    private IPurchContractSignConclusionService purchContractSignConclusionService;
    @Autowired
    private PurchContractElectronicInfoService purchContractElectronicInfoService;
    @Autowired
    private ISupplierInfoService supplierInfoService;
    @Autowired
    private IEnterpriseService enterpriseService;
    @Autowired
    private IPurchContractSupplementInfoService supplementInfoService;
    @Autowired
    private IPurchContractSupplementElectronicInfoService supplementElectronicInfoService;

    /**
     * 采购合同录入提交接口
     * @param purchContractVo 采购合同信息
     * @return
     */
    @PostMapping("/doEnter")
    @OperationLog(operationModule = "采购合同",operationType = OperationTypeEnum.INSERT,description = "采购合同录入提交接口")
    public Object doAdd(@RequestBody PurchContractVo purchContractVo) {
        purchContractService.add(purchContractVo);
        return new ResponseJson("采购合同录入完成");
    }


    /**
     * 获取签署确认信息
     * @param businessKey
     * @return
     */
    @GetMapping("/signPage")
    public ResponseJson signPage(@RequestParam Long businessKey){
        return new ResponseJson(purchContractSignConclusionService.signPage(businessKey));
    }

    /**
     * 签署确认-提交
     * @return
     */
    @PostMapping("/doSign")
    public ResponseJson doSign(@RequestBody PurchContractSignConclusion purchContractSignConclusion){
        purchContractSignConclusionService.sign(purchContractSignConclusion);
        return new ResponseJson("提交成功！");
    }

    /**
     * 获取文件签署状态
     *
     * @param electronicInfoId
     * @return
     */
    @GetMapping("/signType")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseJson getSignType(@RequestParam("electronicInfoId") String electronicInfoId,  @RequestParam("contractType") Integer contractType) throws ValidatorException {
        if (StringUtil.isEmpty(electronicInfoId) || StringUtil.isEmpty(contractType + "") ) {
            throw new ValidatorException(CodeUtil.FAIL, "参数不能为空!");
        }

        boolean result = false;
        if (1 == contractType) {
            PurchContractElectronicInfo electronicInfo = purchContractElectronicInfoService.getById(electronicInfoId);
            if (electronicInfo != null && !PurchContractElectronicInfo.SIGN_STATUS_FINISH.equals(electronicInfo.getSignStatus())) {
                String customerId = electronicInfo.getFddCustomerId();
                String contractId = electronicInfo.getContractId();
                String transactionId = electronicInfo.getTransactionId();
                String signResult = ElectronicSignatureUtil.getSignInfo(customerId, contractId, transactionId);
                if (signResult.equals("3000")) {
                    result = true;
                    purchContractElectronicInfoService.doSignCallBackByQuery(contractId, true);
                }
            } else {
                result = true;
            }
        } else if (2 == contractType) {
            PurchContractSupplementElectronicInfo supplementElectronicInfo = supplementElectronicInfoService.getById(electronicInfoId);
            if (supplementElectronicInfo != null && !PurchContractSupplementElectronicInfo.SIGN_STATUS_FINISH.equals(supplementElectronicInfo.getSignStatus())) {
                Long purchContractSupplementInfoId = supplementElectronicInfo.getcPurchContractSupplementInfoId();
                PurchContractSupplementInfo purchContractSupplementInfo = supplementInfoService.getById(purchContractSupplementInfoId);
                String contractId = supplementElectronicInfo.getContractId();
                Long purchContractManagementId = purchContractSupplementInfo.getPurchContractManagementId();
                PurchContractManagement purchContractManagement = purchContractService.getById(purchContractManagementId);
                Long supplierId = purchContractManagement.getSupplierId();
                SupplierInfo supplierInfo = supplierInfoService.get(supplierId);
                Enterprise supplier = enterpriseService.getById(supplierInfo.getcEnterpriseId());
                String signResult = ElectronicSignatureUtil.getSignInfo(supplier.getFddCustomerId(), contractId, supplementElectronicInfo.getTransactionId());
                if (signResult.equals("3000")) {
                    result = true;
                    supplementInfoService.doSignChangeCallBackByQuery(contractId, true);
                }
            } else {
                result = true;
            }
        } else {
            throw new ValidatorException(CodeUtil.PARAMETER_ERROR, "参数错误");
        }
        return new ResponseJson(result);
    }

    /**
     * 签署确认-查看历史
     * @return
     */
    @GetMapping("/view")
    public Object getView(@RequestParam Long id){
        return purchContractSignConclusionService.getView(id);
    }

    /**
     * 获取所有已签署文件
     * @return
     */
    @GetMapping("/getAllPurchSignFile")
    public ResponseJson getAllSignFile(@RequestParam Long id, HttpServletResponse response){
        return new ResponseJson(purchContractSignConclusionService.getAllSignFile(id,response));
    }



}
