package com.sunxung.factoring.web.acceptancesheet;

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.component.util.ValidatorUtil;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.ValidatorEntity;
import com.sunxung.factoring.entity.acceptance.*;
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.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.ui.Model;
import org.springframework.validation.annotation.Validated;
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;

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

    @Autowired
    private AcceptanceBasicInfoService acceptanceBasicInfoService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private IAcceptancePersonInfoService acceptancePersonInfoService;

    @Autowired
    private IAcceptanceSignConfirmationService acceptanceSignConfirmationService;

    /**
     * 添加验收单
     * 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("操作成功！");
    }


    @ResponseBody
    @RequestMapping("acceptanceDetail/{id}")
    public ResponseJson acceptanceDetail(@PathVariable("id") Long id) throws Exception {
        AcceptanceBasicInfo detail = acceptanceBasicInfoService.getDetail(id);
        return new ResponseJson(detail);
    }


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


    /**
     * 修改验收单
     * 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(AcceptanceSignConfirmation acceptanceSignConfirmation) {
        acceptanceSignConfirmationService.handleProcess(acceptanceSignConfirmation);
        return new ResponseJson("签署确认完成");
    }

    /**
     * 法务审核
     * 2.0
     *
     * @param acceptanceApproval
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("legalReview")
    @OperationLog(operationModule = "验收单",operationType = OperationTypeEnum.OTHER,description = "法务审核")
    public ResponseJson legalReview(@Validated AcceptanceApproval acceptanceApproval) throws Exception {
        //拾取task
        flowableService.claimTask(acceptanceApproval.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        acceptanceBasicInfoService.legalReview(acceptanceApproval);
        return new ResponseJson("审核完成");
    }

    /**
     * 删除验收单
     *
     * @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);
    }



    /**
     * 下载签署时候的存证文件
     * 2.0
     *
     * @return
     */
    @RequestMapping("downloadCertificate")
    @ResponseBody
    @OperationLog(operationModule = "下载证书文件",operationType = OperationTypeEnum.OTHER,description = "下载证书文件")
    public ResponseEntity<Object> downloadCertificate(Long acceptanceBasicInfoId, HttpServletResponse res) throws Exception {
       return  acceptanceBasicInfoService.downloadCertificate(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);
    }





    /*************************************************************************************************************/

    /**
     * 法务审核页面-内容
     * @return
     */
    @RequestMapping("businessReview")
    @ResponseBody
    public ResponseJson businessApproval(@RequestBody  AcceptanceBusinessApproval businessApproval) {
        acceptanceBasicInfoService.businessReview(businessApproval);
        return new ResponseJson();
    }
}