package com.inkasso.factoring.settlement.web;

import com.alibaba.fastjson.JSONObject;
import com.inkasso.adapter.response.SuccessResponseData;
import com.inkasso.adapter.util.CollectionUtil;
import com.inkasso.auth.exception.ValidatorException;
import com.inkasso.factoring.common.entity.BusinessHistoricDetailVo;
import com.inkasso.factoring.common.entity.GridPage;
import com.inkasso.factoring.common.entity.ResponseJson;
import com.inkasso.factoring.creditmanagement.entity.Enterprise;
import com.inkasso.factoring.creditmanagement.entity.SupplierInfo;
import com.inkasso.factoring.creditmanagement.service.IEnterpriseService;
import com.inkasso.factoring.creditmanagement.service.ISupplierInfoService;
import com.inkasso.factoring.purchManagement.entity.CPurchContractSupplementInfo;
import com.inkasso.factoring.purchManagement.entity.PurchSupplementContractSignConclusion;
import com.inkasso.factoring.purchManagement.entity.vo.PurchContractChangeVo;
import com.inkasso.factoring.purchManagement.entity.vo.PurchContractVo;
import com.inkasso.factoring.settlement.entity.*;
import com.inkasso.factoring.settlement.model.request.BillSignRequest;
import com.inkasso.factoring.settlement.service.SettlementBasicInfoService;
import com.inkasso.factoring.settlement.service.SettlementManagementService;
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.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.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 结算单管理
 *
 * @author zhao.ms
 * @date 2023.11.27
 */
@Controller
@RequestMapping("bill")
public class SettlementController {
    @Autowired
    private SettlementManagementService settlementManagementService;

    @Autowired
    private SettlementBasicInfoService settlementBasicInfoService;

    @Autowired
    private ISupplierInfoService iSupplierInfoService;
    @Autowired
    private IEnterpriseService iEnterpriseService;

    /**
     * 结算单管理列表查询
     */
    @RequestMapping(value = "/findAllList")
    @ResponseBody
    public Object findAllList(SearchBusiness search) {
        Object allList = settlementManagementService.findAllList(search);
        return new SuccessResponseData(allList);
    }

    /**
     * 新建结算单
     * 2.0
     *
     * @param settlementBasicInfo
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("addBillBasicInformation")
//    @OperationLog(operationModule = "结算单",operationType = OperationTypeEnum.INSERT,description = "新建结算单")
    public Object add(SettlementBasicInfo settlementBasicInfo, HttpServletRequest request) throws Exception {
        settlementManagementService.add(settlementBasicInfo, request);
        return new SuccessResponseData("添加成功");
    }
    /**
     * 法务退回业务修改
     * 2.0
     *
     * @param taskId
     * @param businessKey 结算单ID
     * @return
     */
    @ResponseBody
    @RequestMapping("getDetail")
    public Object add(String taskId, Long businessKey) {
        SettlementBasicInfo settlementBasicInfo = settlementManagementService.getDetail(businessKey);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("billBasicInformation", settlementBasicInfo);
        jsonObject.put("taskId", taskId);
        jsonObject.put("businessKey", businessKey);
        return new SuccessResponseData(jsonObject);
    }

    /**
     * 结算单 根据供应商、核心企业，查询基础信息
     *
     * @param
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("getBillSettleBasicInfo")
    public Object getBillSettleBasicInfo(){

        return null;
    }

    /**
     * 获取业务相关验收单未结算的货物
     * 2.0
     *
     * @param conditionVo
     * @return
     */
    @ResponseBody
    @RequestMapping("getAcceptanceGoods")
    public Object getAcceptanceGoods(@RequestBody ConditionVo conditionVo) {
        return new SuccessResponseData(settlementManagementService.getCanBeBillGoodsInformation(conditionVo));
    }

    /**
     * 根据业务编号 获取结算货物集合
     *
     * @param businessNumber
     * @return
     */
    @GetMapping("getGoodsInfoListByBusinessNumber")
    @ResponseBody
    public Object findGoodsInfoList(@RequestParam String businessNumber) throws ParseException {
        List<SettlementGoodsInfo> goodsInfoList = settlementManagementService.findGoodsInfoList(businessNumber);
        return new SuccessResponseData(goodsInfoList);
    }

    /**
     * 跳转签署页面
     *
     * @param taskId
     * @param businessKey
     * @return
     */
    @RequestMapping("sign")
    @ResponseBody
    public Object signPage(@RequestParam String taskId,@RequestParam Long businessKey) {
        SettlementElectronicInfo settlementElectronicInfo = settlementManagementService.autoSign(businessKey, taskId);
        SettlementBasicInfo settlementBasicInfo = settlementManagementService.getDetail(businessKey);
        Long supplierId = settlementBasicInfo.getSupplierId();
        SupplierInfo supplierInfo = iSupplierInfoService.getById(settlementBasicInfo.getSupplierId());
        Enterprise enterprise = iEnterpriseService.getById(supplierInfo.getcEnterpriseId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("settlementElectronicInfo", settlementElectronicInfo);
        jsonObject.put("taskId", taskId);
        jsonObject.put("supplierName", enterprise.getCorporateName());
        jsonObject.put("businessKey", businessKey);
        return new SuccessResponseData(jsonObject);
    }

    /**
     * 获取文件签署状态
     *
     * @param electronicInfoId
     * @return
     */
    @RequestMapping("/signType")
    @ResponseBody
    public Object getSignType(@RequestParam String electronicInfoId, @RequestParam String taskId, @RequestParam String basicId) throws ValidatorException {
        return new SuccessResponseData(settlementManagementService.getSignTppe(electronicInfoId, taskId, basicId));
    }


    /**
     * 一键计算质保金
     * 2.0
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("calculation")
    public Object calculation(@RequestBody  SettlementBasicInfo settlementBasicInfo) {
        Object calculation = settlementManagementService.calculation(settlementBasicInfo);
        return new SuccessResponseData(calculation);
    }

    /**
     * 生成PDF保存并预览(未签署)
     * 2.0
     *
     * @param settlementBasicInfo
     * @param response
     * @return
     */
    @RequestMapping("downloadPdf")
    @ResponseBody
//    @OperationLog(operationModule = "结算单",operationType = OperationTypeEnum.OTHER,description = "生成PDF保存并预览(未签署)")
    public Object downloadPdf(SettlementBasicInfo settlementBasicInfo, HttpServletResponse response) {
        return new SuccessResponseData(settlementManagementService.downloadPdf(settlementBasicInfo, response));
    }

    /**
     * 下载（已签署）
     * 2.0
     *
     * @return
     */
    @RequestMapping("downloadPdfSign")
//    @OperationLog(operationModule = "结算单",operationType = OperationTypeEnum.OTHER,description = "下载（已签署）")
    public void downloadPdfSign(Long settlementBasicInfoId, HttpServletResponse res) {
        settlementManagementService.downloadPdfSign(settlementBasicInfoId, res);
    }

    /**
     * 查看已签署的 结算单
     * @param settlementBasicInfoId
     * @param res
     * @return
     */
    @RequestMapping("showPdfSign")
    @ResponseBody
    public Object  showPdfSign(Long settlementBasicInfoId, HttpServletResponse res) {
        return   new SuccessResponseData(settlementManagementService.showPdfSign(settlementBasicInfoId));
    }

    /**
     * 下载结算单出证
     * 2.0
     *
     * @return
     */
    @RequestMapping("download/report")
    @ResponseBody
//    @OperationLog(operationModule = "结算单",operationType = OperationTypeEnum.OTHER,description = "下载结算单出证")
    public ResponseEntity<Object> downloadReport(Long id, HttpServletResponse res) {
        return settlementManagementService.downloadReport(id, res);
    }

    /**
     * 计算对账日期
     * 2.0
     *
     * @param settlementBasicInfo
     * @return
     */
    @ResponseBody
    @RequestMapping("getExpectedSettlementDate")
    public Object getExpectedSettlementDate(@RequestBody SettlementBasicInfo settlementBasicInfo) {
        return new SuccessResponseData(settlementManagementService.getExpectedSettlementDate(settlementBasicInfo));
    }

    /**
     * 上传货物信息(下游结算）)Excel
     * 2.0
     *
     * @param file
     * @return
     */
    @PostMapping("/doImportGoodsInfo")
    @ResponseBody
    public Object doImportGoodsInfo(@RequestParam("file") MultipartFile file) {
        return new SuccessResponseData(settlementManagementService.importGoodsInfo(file));
    }

    /**
     * 下载货物信息(下游结算)模板
     *
     * @return
     */
    @RequestMapping("/downloadFile")
    public void downloadFile(HttpServletResponse response) {
        settlementManagementService.downloadFile(response);
    }

    /**
     * 结算单签署提交
     */
    @RequestMapping("/finalStatementsign")
    @ResponseBody
    public Object billSign(@RequestBody BillSignRequest billSignRequest) {
        return new SuccessResponseData(settlementManagementService.billSign(billSignRequest));
    }

    /**
     * 结算单单价调整提交
     *
     */
    @RequestMapping("/billUnitPriceAdjust")
    @ResponseBody
    public Object billUnitPriceAdjust(@RequestBody SettlementBasicInfo settlementBasicInfo){
        settlementManagementService.billUnitPriceAdjust(settlementBasicInfo);
        return new SuccessResponseData("修改成功");
    }

    /**
     * 修改结算
     *
     * @param settlementBasicInfo
     * @return
     */
    @ResponseBody
    @RequestMapping("updateBill")
//    @OperationLog(operationModule = "结算单",operationType = OperationTypeEnum.INSERT,description = "修改结算")
    public Object updateBill(SettlementBasicInfo settlementBasicInfo){
        settlementManagementService.updateBill(settlementBasicInfo);
        return new SuccessResponseData("修改成功");
    }

    /**
     * 一键下载
     *
     * @param id
     * @param response
     */
    @GetMapping("downloadAllSettlementFile")
    @ResponseBody
//    @OperationLog(operationModule = "结算单",operationType = OperationTypeEnum.OTHER,description = "一键下载")
    public void downloadAllSettlementFile(Long id, HttpServletResponse response) {
        settlementBasicInfoService.downloadAllSettlementFile(id, response);
    }
    /**
     * 经办人（核心企业）
     *
     * @param businessNumber
     */
    @RequestMapping("findOperatorByBusinessNumber")
    @ResponseBody
    public Object findOperatorByBusinessNumber(String businessNumber) {
        Object operatorByBusinessNumber = settlementManagementService.findOperatorByBusinessNumber(businessNumber);
        return new SuccessResponseData(operatorByBusinessNumber);
    }

    /**
     * 查看
     *
     * @return
     */
    @GetMapping("/doBusinessHistoryView")
    @ResponseBody
    public Object doBusinessHistoryView(@RequestParam Long businessKey, @RequestParam String businessType) {
        Object o = settlementManagementService.doBusinessHistoryView(businessKey,businessType);
        return new SuccessResponseData(o);
    }

}
