package com.inkasso.factoring.acceptanceManagement.web;

import com.inkasso.factoring.acceptanceManagement.entity.AcceptanceSearch;
import com.inkasso.factoring.acceptanceManagement.entity.CAcceptanceBasicInfo;
import com.inkasso.factoring.acceptanceManagement.entity.CAcceptancePersonInfo;
import com.inkasso.factoring.acceptanceManagement.entity.CAcceptanceSignConfirmation;
import com.inkasso.factoring.acceptanceManagement.service.ICAcceptanceBasicInfoService;
import com.inkasso.factoring.acceptanceManagement.service.ICAcceptanceSignConfirmationService;
import com.inkasso.factoring.common.entity.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping("/acceptance")
public class CAcceptanceController {

    @Autowired
    private ICAcceptanceBasicInfoService acceptanceBasicInfoService;
    @Autowired
    private ICAcceptanceSignConfirmationService acceptanceSignConfirmationService;

    /**
     * 初始化参数数组长度最大值
     *
     * @param binder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setAutoGrowNestedPaths(true);
        binder.setAutoGrowCollectionLimit(1024);
    }

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

    /**
     * 验收单录入
     */
    @GetMapping("/findDetail")
    @ResponseBody
    public ResponseJson findDetail(Long id) {
        return new ResponseJson(acceptanceBasicInfoService.findDetail(id));
    }


    /**
     * 验收单录入
     */
    @GetMapping("/timeLine")
    @ResponseBody
    public ResponseJson timeLine(Long id) {
        return acceptanceBasicInfoService.timeLine(id);
    }


    /**
     * 验收单修改    (重新提交)
     */
    @PostMapping("/updateDetail")
    @ResponseBody
    public ResponseJson updateDetail(@RequestBody  CAcceptanceBasicInfo acceptanceBasicInfo, HttpServletRequest request) throws IOException {
        acceptanceBasicInfoService.updateDetail(acceptanceBasicInfo, request);

        return new ResponseJson("操作成功");
    }


    /**
     * 上传货物信息Excel
     * 2.0
     *
     * @param file
     * @return
     */
    @PostMapping("/doImportGoodsInfo")
    @ResponseBody
    public ResponseJson doImportGoodsInfo(@RequestParam("file") MultipartFile file) {
        return new ResponseJson(acceptanceBasicInfoService.importGoodsInfo(file));
    }


    /**
     * 下载验收单货物信息模板
     *
     * @return
     */
    @RequestMapping("/downloadFile")
    public void downloadFile(HttpServletResponse response) {
        acceptanceBasicInfoService.downloadFile(response);
    }


    /**
     * 预览-已签署的上游验收单文件
     *
     * @return
     */
    @RequestMapping("/viewSignFile/{id}")
    public ResponseJson viewSignFile(@PathVariable("id") Long acceptanceId) {
        return new ResponseJson(acceptanceBasicInfoService.viewSignFile(acceptanceId));
    }


    /**
     * 下载-已签署的上游验收单文件
     * @return
     */
    @RequestMapping("/downloadSignFile/{id}")
    public void downloadSignFile(@PathVariable("id") Long acceptanceId,HttpServletResponse response) {
        acceptanceBasicInfoService.downloadSignFile(acceptanceId,response);
    }


    /**
     * 新建验收单 /修改验收单
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseJson add(@RequestBody  CAcceptanceBasicInfo acceptanceBasicInfo, HttpServletRequest request) throws Exception {
        return new ResponseJson(acceptanceBasicInfoService.add(acceptanceBasicInfo, request));
    }

    /**
     * 申请审核
     */
    @GetMapping("/applyApproval")
    @ResponseBody
    public ResponseJson applyApproval(@RequestParam("id")Long id,@RequestParam("taskId")Long taskId) {

        return acceptanceBasicInfoService.applyApprval(id,taskId);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @ResponseBody
    public ResponseJson list(AcceptanceSearch search) {
        return new ResponseJson(acceptanceBasicInfoService.getBySearch(search));
    }



    /**
     * 删除验收单
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteAcceptance")
    @ResponseBody
    public ResponseJson delete(Long id) {
        acceptanceBasicInfoService.deleteAcceptance(id);
        return new ResponseJson("删除成功");
    }


    /**
     * 签署确认页面
     *
     * @return
     */
    @RequestMapping("sign/confirm")
    public ResponseJson signConfirmPage(Long businessKey) {
        return new ResponseJson(acceptanceSignConfirmationService.getDetail(businessKey));
    }


    /**
     *  预览未签署的 pdf
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("showPdf")
    @ResponseBody
    public ResponseJson showPdf(@RequestBody  CAcceptanceBasicInfo acceptanceBasicInfo) throws IOException {

        return new ResponseJson(acceptanceBasicInfoService.downloadPdf(acceptanceBasicInfo));
    }

    /**
     * 签署确认
     */

    @RequestMapping("add/sign/confirm")
    @ResponseBody
    public ResponseJson signConfirm(@RequestBody  CAcceptanceSignConfirmation acceptanceSignConfirmation) throws IOException {
        acceptanceSignConfirmationService.signConfirm(acceptanceSignConfirmation);
        return new ResponseJson("签署确认完成");
    }



}
