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.PurchContractApproval;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractBasicInfo;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractManagement;
import com.sunxung.factoring.entity.purchcontractmanagement.PurchContractSignApproval;
import com.sunxung.factoring.entity.sys.FileStorage;
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description : 采购合同签署审核controller层
 * @Author : L.Jia
 * @Date: 2021-09-23 14:56
 */

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

    private final String prefix = "purchaseContract";

    @Autowired
    private IPurchContractSignApprovalService purchContractSignApprovalService;
    @Autowired
    private PurchContractService purchContractService;
    @Autowired
    private PurchContractBasicInfoService purchContractBasicInfoService;
    @Autowired
    private ISysAttachmentRefService attachmentRefService;
    @Autowired
    FlowableService flowableService;
    @Autowired
    private PurchContractApprovalService purchContractApprovalService;
    @Autowired
    private PurchContractElectronicInfoService purchContractElectronicInfoService;
    /**
     * 跳转签署审核页面
     *
     * @param id 采购合同Id
     * @return
     */
    @RequestMapping("/add")
    public ResponseJson add( Long id) {
        purchContractElectronicInfoService.checkCallBack(id);
        PurchContractManagement purchContractManagement = purchContractService.getById(id);
        PurchContractBasicInfo purch = purchContractBasicInfoService.lambdaQuery().eq(PurchContractBasicInfo::getPurchContractManagementId, id).one();
        PurchContractApproval latestPurchContractApproval = purchContractApprovalService.getLatestPurchContractApproval(purch.getId(), 1);
        if(purch != null && purch.getId() != null){
            List<FileStorage> fileStorageList = new ArrayList<>();
            fileStorageList.addAll(attachmentRefService.getFiles(AttachmentDto.builder().setEntityId(purch.getId())
                    .setChildEnum(FileModuleDict.ChildEnum.PURCHASE_CONTRACT_SIGN_FILE).build()));
            latestPurchContractApproval.setFileStorages(fileStorageList);
        }
        return new ResponseJson(latestPurchContractApproval);
    }

    /**
     * 签署审核接口
     *
     * @param purchContractSignApproval 采购合同签署审核信息
     * @return
     */
    @PostMapping("/doAudit")
    @ResponseBody
    @OperationLog(operationModule = "采购合同",operationType = OperationTypeEnum.OTHER,description = "签署审核")
    public Object doAudit(@RequestBody PurchContractSignApproval purchContractSignApproval) {
        flowableService.claimTask(purchContractSignApproval.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        purchContractSignApprovalService.audit(purchContractSignApproval, purchContractSignApproval.getBusinessKey(), purchContractSignApproval.getTaskId());
        return new ResponseJson("审核完成");
    }

    /**
     * 法务签署  采购合同预览
     *
     * @param businessKey
     * @param taskId
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/doPriviewPurchContract")
    @ResponseBody
    public Object doPriviewPurchContract(String businessKey, String taskId, HttpServletRequest request, HttpServletResponse response) {
        return purchContractSignApprovalService.priviewPurchContract(businessKey, taskId, request, response);
    }

    /**
     * 法务签署  采购合同下载
     */
    @GetMapping(value = "/exportPurchContract")
    @OperationLog(operationModule = "采购合同",operationType = OperationTypeEnum.OTHER,description = "法务签署 采购合同下载")
    public void exportPurchContract(String businessKey, String taskId, HttpServletRequest request, HttpServletResponse response) {
        purchContractSignApprovalService.exportPurchContract(businessKey, taskId, request, response);
    }

}
