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.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractManagement;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractSupplementApproval;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractSupplementInfo;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractSupplementSignApproval;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.entity.tradingPlatform.Platform;
import com.sunxung.factoring.service.purchcontractmanagement.*;
import com.sunxung.factoring.service.sys.ISysAttachmentRefService;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 采购合同-补充信息表 变更 前端控制器
 * </p>
 *
 * @author daxian
 * @since 2022-03-14
 */
@RestController
@RequestMapping("/purchContract/change")
public class PurchContractSupplementInfoController {

    @Autowired
    IPurchContractSupplementInfoService contractSupplementInfoService;

    @Autowired
    IPurchContractSupplementApprovalService contractSupplementApprovalService;

    @Autowired
    IPurchContractSupplementElectronicInfoService supplementElectronicInfoService;

    @Autowired
    PurchContractService purchContractService;

    @Autowired
    ISysAttachmentRefService attachmentRefService;

    @Autowired
    IPurchContractSupplementApprovalService supplementApprovalService;

    @Autowired
    IPurchContractSupplementSignApprovalService supplementSignApprovalService;

    @Autowired
    FlowableService flowableService;

    @Autowired
    private IPurchSupplementContractSignConclusionService purchSupplementContractSignConclusionService;


    /**
     * 跳转采购合同变更审核页面
     *
     * @param businessKey 采购合同Id
     * @return
     */
    @GetMapping("/approval")
    public ResponseJson approvalAdd(Long businessKey) {
        return new ResponseJson(contractSupplementApprovalService.approval(businessKey));
    }

    /**
     * 制作合同
     *
     * @param purchContractSupplementApproval
     * @return
     */
    @RequestMapping("/makeContractPdf")
    @ResponseBody
    public ResponseJson makeContractPdf(@RequestBody @Validated PurchContractSupplementApproval purchContractSupplementApproval) {
        return new ResponseJson(contractSupplementInfoService.makeContract(purchContractSupplementApproval));
    }

    /**
     * 采购合同变更审核提交
     *
     * @return
     */
    @PostMapping("/approval/doAudit")
    @OperationLog(operationModule = "采购合同变更", operationType = OperationTypeEnum.INSERT, description = "采购合同变更审核提交")
    public Object approvalAudit(@RequestBody @Validated PurchContractSupplementApproval approval) {
        flowableService.claimTask(approval.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        contractSupplementApprovalService.doAudit(approval, approval.getBusinessKey(), approval.getTaskId());
        return new ResponseJson("销售合同变更审核完成");
    }


    /**
     * 采购合同变更签署审核页面
     *
     * @param businessKey 采购合同Id
     * @return
     */
    @RequestMapping("/final/approval")
    public ResponseJson approvalFinalAdd(Long businessKey) {

        contractSupplementInfoService.checkCallBack(businessKey);

        PurchContractSupplementSignApproval signApproval = new PurchContractSupplementSignApproval();

        PurchContractManagement purchContractManagement = purchContractService.getById(businessKey);
        PurchContractSupplementInfo supplementInfo = contractSupplementInfoService.getLastPruchContractSupplementInfoByContractManagementId(purchContractManagement.getId());
        if(supplementInfo != null && supplementInfo.getId() != null){
            List<FileStorage> fileStorageList = new ArrayList<>();
            AttachmentDto attachmentDto = AttachmentDto.builder().setEntityId(supplementInfo.getId()).setChildEnum(FileModuleDict.ChildEnum.PURCHASE_CONTRACT_SUPPLEMENT_SIGN_FILE).build();
            fileStorageList.addAll(attachmentRefService.getFiles(attachmentDto));
           signApproval.setFileStorageList(fileStorageList);
        }

        return new ResponseJson(signApproval);
    }

    /**
     * 采购合同变更签署审核
     *
     * @param signApproval 采购合同Id
     * @return
     */
    @RequestMapping("/final/sign/audit")
    @ResponseBody
    @OperationLog(operationModule = "采购合同变更", operationType = OperationTypeEnum.INSERT, description = "采购合同变更签署审核")
    public Object signAudit(@RequestBody PurchContractSupplementSignApproval signApproval) {
        flowableService.claimTask(signApproval.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        supplementSignApprovalService.audit(signApproval);
        return new ResponseJson("审核完成");
    }


}
