package com.sunxung.factoring.web.acceptancesheet;

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.acceptance.AcceptanceBasicInfo;
import com.sunxung.factoring.entity.acceptance.AcceptancePersonInfo;
import com.sunxung.factoring.entity.acceptance.AcceptanceSignConfirmation;
import com.sunxung.factoring.entity.acceptance.AcceptanceSignConfirmationElectronicInfo;
import com.sunxung.factoring.entity.salescontractmanagement.CSalesSignConfirmationElectronicInfo;
import com.sunxung.factoring.entity.steelwaiter.SearchVo;
import com.sunxung.factoring.service.acceptancesheet.AcceptanceBasicInfoService;
import com.sunxung.factoring.service.acceptancesheet.IAcceptancePersonInfoService;
import com.sunxung.factoring.service.acceptancesheet.IAcceptanceSignConfirmationElectronicInfoService;
import com.sunxung.factoring.service.acceptancesheet.IAcceptanceSignConfirmationService;
import com.sunxung.factoring.service.customerinformation.CustomerService;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * 验收单管理模块-审核
 *
 * @author liutao
 * @date 2021.9.10
 */
@Controller
@RequestMapping("api/acceptance")
public class ApiAcceptanceController {

    @Autowired
    private AcceptanceBasicInfoService acceptanceBasicInfoService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private IAcceptancePersonInfoService acceptancePersonInfoService;


    @Autowired
    private IAcceptanceSignConfirmationService acceptanceSignConfirmationService;

    @Autowired
    private IAcceptanceSignConfirmationElectronicInfoService acceptanceSignConfirmationElectronicInfoService;

    /**
     * 添加验收单
     * 2.0
     *
     * @param acceptanceBasicInfo
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("addAcceptance")
    @OperationLog(operationModule = "验收单",operationType = OperationTypeEnum.INSERT,description = "验收单开启流程")
    public ResponseJson add(@RequestBody  AcceptanceBasicInfo acceptanceBasicInfo) throws Exception {
        acceptanceBasicInfoService.add(acceptanceBasicInfo);
        return new ResponseJson("操作成功！");
    }

    /**
     * 修改验收单
     * 2.0
     *
     * @param acceptanceBasicInfo
     * @return
     * @throws IOException
     */
    @RequestMapping("updateDetailHandleProcess")
    @ResponseBody
    @OperationLog(operationModule = "验收单",operationType = OperationTypeEnum.UPDATE,description = "修改验收单流程处理")
    public ResponseJson updateDetailHandleProcess(@RequestBody AcceptanceBasicInfo acceptanceBasicInfo){
        acceptanceBasicInfoService.updateDetailHandleProcess(acceptanceBasicInfo);
        return new ResponseJson("更新完成");
    }


    /**
     * 签署确认
     * @param acceptanceSignConfirmation
     * @return
     * @throws IOException
     */
    @RequestMapping("sign/handleProcess")
    @ResponseBody
    @OperationLog(operationModule = "验收单",operationType = OperationTypeEnum.OTHER,description = "签署确认处理流程")
    public ResponseJson signConfirm(@RequestBody  AcceptanceSignConfirmation acceptanceSignConfirmation) {
        acceptanceSignConfirmationService.handleProcess(acceptanceSignConfirmation);
        return new ResponseJson("签署确认完成");
    }


    /**
     * 跳转法务审核页面
     * 2.0
     *
     * @return
     */
    @RequestMapping("approval")
    public ResponseJson approval(Long id) {
        return new ResponseJson(acceptanceBasicInfoService.findFileList(id));
    }


    /**
     * 删除验收单
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteAcceptance")
    @ResponseBody
    @OperationLog(operationModule = "验收单",operationType = OperationTypeEnum.OTHER,description = "删除验收单")
    public ResponseJson delete(Long id) {
        acceptanceBasicInfoService.deleteAcceptanceBasicInfo(id);
        return new ResponseJson("删除成功");
    }

    /**
     * 生成上游验收单并下载（已签署）
     * 2.0
     *
     * @return
     */
    @RequestMapping("downloadPdfSign")
    @ResponseBody
    @OperationLog(operationModule = "验收单",operationType = OperationTypeEnum.OTHER,description = "生成上游验收单并下载（已签署）")
    public ResponseEntity<Object> downloadPdfSign(Long acceptanceBasicInfoId, HttpServletResponse res) throws Exception {
        return acceptanceBasicInfoService.downloadPdfSign(acceptanceBasicInfoId, res);
    }

    /**
     * 一键下载
     *
     * @param id
     * @param response
     */
    @GetMapping("/downloadAllfile")
    @OperationLog(operationModule = "验收单",operationType = OperationTypeEnum.OTHER,description = "一键下载")
    public void downloadAllfile(Long id, HttpServletResponse response) {
//        acceptanceBasicInfoService.downloadAllfile(id, response);
    }

    /**
     * 获取采购合同人员信息 销售合同人员信息
     */
    @GetMapping("/personInfoList")
    @ResponseBody
    public ResponseJson personInfoList( Long supplierId, Long customerId, String businessNumber) {
        List<AcceptancePersonInfo> acceptancePersonInfos = acceptanceBasicInfoService.personInfoList(supplierId, customerId, businessNumber);
        return new ResponseJson(acceptancePersonInfos);
    }

    /**
     * 获取采购合同人员信息 销售合同人员信息
     */
    @RequestMapping("/downloadGoodsInfos")
    @ResponseBody
    public void downloadGoodsInfos(SearchVo searchVo, HttpServletResponse response){
        acceptanceBasicInfoService.downloadGoodsInfos(searchVo,response);
    }


    /**
     *  验收合同签署回调
     */
    @PostMapping("/signBack")
    @ResponseBody
    public void signBack(@RequestBody AcceptanceSignConfirmationElectronicInfo acceptanceSignConfirmationElectronicInfo){
        acceptanceSignConfirmationElectronicInfoService.doSignCallBack(acceptanceSignConfirmationElectronicInfo);
    }

}