package com.sunxung.factoring.web.salescontractmanagement;

import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.salescontractmanagement.CSalesContract;
import com.sunxung.factoring.entity.salescontractmanagement.CSalesContractBasicInfo;
import com.sunxung.factoring.entity.salescontractmanagement.CSalesSignConfirmation;
import com.sunxung.factoring.entity.salescontractmanagement.CSalesSignConfirmationElectronicInfo;
import com.sunxung.factoring.service.customerinformation.IElectronicSignaturePersonalService;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.salescontractmanagement.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description 销售合同管理对外接口
 * @Author Yafan Wang
 * @Date 2023/12/27 15:08
 */
@RestController
@RequestMapping("api/salesManagement")
public class SalesContractApiController {

    @Autowired
    private SalesContractService salesContractService;
    @Autowired
    private ICSalesContractBasicInfoService salesContractBasicInfoService;
    @Autowired
    private ICSalesSignConfirmationService salesSignConfirmationService;
    @Autowired
    private IEnterpriseService enterpriseService;
    @Autowired
    private IElectronicSignaturePersonalService electronicSignaturePersonalService;
    @Autowired
    private ICSalesSignConfirmationElectronicInfoService signConfirmationElectronicInfoService;
    @Autowired
    private SalesContractApprovalService salesContractApprovalService;

    @RequestMapping("/doCreate")
    @OperationLog(operationModule = "销售合同", operationType = OperationTypeEnum.INSERT, description = "销售合同录入保存")
    public ResponseJson doCreate(@RequestBody CSalesContract salesContractManagement) {
        salesContractService.doCreate(salesContractManagement);
        return new ResponseJson("操作成功！");
    }

    /**
     * 销售合同录入保存
     *
     * @param salesContractBasicInfo 销售合同信息
     * @return
     */
    @PostMapping("/doEnter")
    @OperationLog(operationModule = "销售合同", operationType = OperationTypeEnum.INSERT, description = "销售合同录入保存")
    public ResponseJson doEnter(@RequestBody CSalesContractBasicInfo salesContractBasicInfo) {
        salesContractBasicInfoService.add(salesContractBasicInfo);
        return new ResponseJson("销售合同录入完成");
    }

    /**
     * 上传签署文件
     */
    @PostMapping("/uploadContract")
    public ResponseJson uploadContract(@RequestBody CSalesSignConfirmation salesSignConfirmation) {
        return new ResponseJson(salesContractApprovalService.uploadContract(salesSignConfirmation.getElectronicSignaturePersonal(),
                salesSignConfirmation, salesSignConfirmation.getSignConfirmationElectronicInfos()));
    }

    /**
     * 回调
     * @param signConfirmationElectronicInfo
     */
    @PostMapping("/signBack")
    public void signBack(@RequestBody CSalesSignConfirmationElectronicInfo signConfirmationElectronicInfo){
        signConfirmationElectronicInfoService.doSignCallBack(signConfirmationElectronicInfo);
    }

    /**
     * 签署确认-提交
     * @param salesSignConfirmation
     * @return
     */
    @PostMapping("/doSign")
    public ResponseJson doSign(@RequestBody CSalesSignConfirmation salesSignConfirmation){
        salesSignConfirmationService.doAdd(salesSignConfirmation);
        return new ResponseJson("操作成功！");
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @GetMapping("/view")
    public ResponseJson getView(@RequestParam Long id){
        return new ResponseJson(salesSignConfirmationService.getView(id));
    }

    /**
     * 下载销售合同货物信息模板
     *
     * @return
     */
    @GetMapping("/downloadFile")
    public void downloadFile(HttpServletResponse response) {
        salesContractService.downloadTemplateFile(response);
    }


}
