package com.sunxung.factoring.web.salescontractmanagement;

import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
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.salescontractmanagement.*;
import com.sunxung.factoring.service.salescontractmanagement.*;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description : 销售合同管理控制层
 * @Author : daxian
 * @Date: 2022/3/4
 */
@RestController
@RequestMapping("/salesContract")
public class SalesContractController {

    private final String prefix = "salesContractManagement";
    @Autowired
    private SalesContractService salesContractService;
    @Autowired
    private SalesContractApprovalService salesContractApprovalService;
    @Autowired
    private FlowableService flowableService;
    @Autowired
    private ISalesContractReturnBondService salesContractReturnBondService;
    @Autowired
    private ISalesContractSupplementTemplateApprovalService salesContractSupplementTemplateApprovalService;
    @Autowired
    private ISalesContractTemplateFirstApprovalInfoService salesContractTemplateFirstApprovalInfoService;
    @Autowired
    private ISalesContractTemplateAgainApprovalInfoService salesContractTemplateAgainApprovalInfoService;
    @Autowired
    private ISalesSignConfirmationApprovalService salesSignConfirmationApprovalService;

    /**
     * 模板初审数据初始化
     *
     * @return
     */
    @GetMapping("/firstTemplate/approval")
    public ResponseJson approval() {
        return new ResponseJson(salesContractTemplateFirstApprovalInfoService.approval());
    }

    /**
     * 模板初审提交接口
     *
     * @param templateFirstApprovalInfo
     * @return
     */
    @PostMapping("/firstTemplate/doApproval")
    public ResponseJson doApproval(@RequestBody @Validated SalesContractTemplateFirstApprovalInfo templateFirstApprovalInfo) {
        flowableService.claimTask(templateFirstApprovalInfo.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        salesContractTemplateFirstApprovalInfoService.doApproval(templateFirstApprovalInfo);
        return new ResponseJson("操作成功！");
    }

    /**
     * 模板复审提交
     *
     * @param againApprovalInfo
     * @return
     */
    @PostMapping("/againTemplate/doApproval")
    public ResponseJson againApproval(@RequestBody @Validated SalesContractTemplateAgainApprovalInfo againApprovalInfo) {
        flowableService.claimTask(againApprovalInfo.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        salesContractTemplateAgainApprovalInfoService.audit(againApprovalInfo);
        return new ResponseJson("操作成功！");
    }

    /**
     * 销售合同审核信息
     * @param id
     * @return
     */
    @GetMapping("/approvalDetail")
    public ResponseJson approvalDetail(Long id){
        return new ResponseJson(salesContractApprovalService.getDetail(id));
    }

    /**
     * 法务审核
     *
     * @param salesContractApproval 销售合同审核
     * @return
     */
    @PostMapping("/approval/doAudit")
    @OperationLog(operationModule = "销售合同", operationType = OperationTypeEnum.INSERT, description = "法务审核")
    public Object approvalDoAudit(@RequestBody @Validated SalesContractApproval salesContractApproval) {
        flowableService.claimTask(salesContractApproval.getTaskId(),ThreadLocalUtil.getUserId(),ConstantUtil.LEGAL_SPECIALIST);
        salesContractApprovalService.audit(salesContractApproval);
        return new ResponseJson("审核完成");
    }

    /**
     * 解析货物信息Excel接口
     *
     * @param file
     * @return
     */
    @PostMapping("/doImportGoodsInfo")
    public Object doImportGoodsInfo(@RequestParam("file") MultipartFile file) {
        return salesContractService.importGoodsInfo(file);
    }


    /**
     * 下载所有销售合同文件
     *
     * @param id
     * @param response
     */
    @GetMapping("/downloadAllSalesFile")
    @OperationLog(operationModule = "销售合同", operationType = OperationTypeEnum.OTHER, description = "下载所有销售合同文件")
    public void downloadAllSalesContractFile(Long id, HttpServletResponse response) {
        salesContractService.downloadAllSalesContractFile(id, response);
    }

    /**
     * 下载销售合同出证文件
     *
     * @param id
     * @param response
     */
    @GetMapping("/downloadSalesReport")
    @OperationLog(operationModule = "销售合同", operationType = OperationTypeEnum.OTHER, description = "下载所有销售合同出证文件")
    public void downloadSalesReport(Long id, HttpServletResponse response) throws IOException {
        salesContractService.downloadReportUrl(id, response);
    }

    /**
     * 销售合同变更签署审核-详情
     * @param id
     * @return
     */
    @GetMapping("/sign/approval")
    public ResponseJson signApproval(Long id){
        return new ResponseJson(salesSignConfirmationApprovalService.approval(id));
    }

    /**
     *
     * @param salesSignConfirmationApproval
     * @return
     */
    @PostMapping("/doApproval")
    @OperationLog(operationModule = "销售合同", operationType = OperationTypeEnum.OTHER, description = "签署确认审核")
    public ResponseJson doApproval(@RequestBody SalesSignConfirmationApproval salesSignConfirmationApproval){
        flowableService.claimTask(salesSignConfirmationApproval.getTaskId(),ThreadLocalUtil.getUserId(),ConstantUtil.LEGAL_SPECIALIST);
        salesSignConfirmationApprovalService.doApproval(salesSignConfirmationApproval);
        return new ResponseJson("操作成功！");
    }

}
