package com.trade.client.trade.exportrebate.dto;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.trade.service.filestorage.dto.FileAbstractDto;
import com.trade.service.trade.exportrebate.domain.TaxRebateExportDetails;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Getter
@Setter
public class ExportTaxRebateDetailsDto {

    /**
     * 客户id
     */
    private Long customerUserId;
    /**
     * 申报年月（所属期）（yyyyMM）
     */
    private String declarationMonthYear;
    /**
     * 申报批次
     */
    private String declarationBatch;
    /**
     * 申报序号
     */
    private String declarationSerialNo;
    /**
     * 海关报关单号（18位）
     */
    private String customsDeclarationNo;
    /**
     * 海关报关单号（18位）
     */
    private FileAbstractDto customsDeclarationFile;
    /**
     * 代理公司统一社会信用代码/注册号
     */
    private String agencyCertNo;
    /**
     * 代理公司名称
     */
    private String agencyName;
    /**
     * 进口商统一社会信用代码/注册号
     */
    private String importerCertNo;
    /**
     * 进口商名称
     */
    private String importerName;
    /**
     * 销售订单号
     */
    private String salesOrderNo;
    /**
     * 销售合同id
     */
    private Long salesContractId;
    /**
     * 销售合同号
     */
    private String salesContractNo;
    /**
     * 出口发票号（形式发票号）
     */
    private String proformaInvoiceNo;
    /**
     * 退税额（生产企业：免抵退税额）
     */
    private BigDecimal taxRefundAmount;
    /**
     * 出口日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date exportDate;
    /**
     * 出口数量
     */
    private BigDecimal exportQuantity;
    /**
     * 美元离岸价(出口金额)
     */
    private BigDecimal fobPriceUsd;
    /**
     * 退税状态，枚举rebate_process_status
     */
    private String status;
    /**
     * 采购发票数量
     */
    private Long purchaseInvoiceCount;
    /**
     * 出口明细
     */
    List<ExportDetailDto> exportDetailList;
    /**
     * 进货明细
     */
    List<PurchaseDetailDto> purchaseDetailList;

    public ExportTaxRebateDetailsDto(List<TaxRebateExportDetails> taxRebateExportDetailsList){
        if (CollUtil.isEmpty(taxRebateExportDetailsList)) {
            return;
        }
        this.customerUserId = taxRebateExportDetailsList.get(0).getCustomerUserId();
        this.declarationMonthYear = taxRebateExportDetailsList.get(0).getDeclarationMonthYear();
        this.declarationBatch = taxRebateExportDetailsList.get(0).getDeclarationBatch();
        this.declarationSerialNo = taxRebateExportDetailsList.get(0).getDeclarationSerialNo();
        this.customsDeclarationNo = taxRebateExportDetailsList.get(0).getCustomsDeclarationNo();
        this.agencyCertNo = taxRebateExportDetailsList.get(0).getAgencyCertNo();
        this.importerCertNo = taxRebateExportDetailsList.get(0).getImporterCertNo();
        this.salesOrderNo = taxRebateExportDetailsList.get(0).getSalesOrderNo();
        this.salesContractId = taxRebateExportDetailsList.get(0).getSalesContractId();
        this.salesContractNo = taxRebateExportDetailsList.get(0).getSalesContractNo();
        this.proformaInvoiceNo = taxRebateExportDetailsList.get(0).getProformaInvoiceNo();
        this.exportDate = taxRebateExportDetailsList.get(0).getExportDate();
        this.status = taxRebateExportDetailsList.get(0).getStatus();
        this.exportQuantity = taxRebateExportDetailsList.stream().map(TaxRebateExportDetails::getExportQuantity)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        this.fobPriceUsd = taxRebateExportDetailsList.stream().map(TaxRebateExportDetails::getFobPriceUsd)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        this.taxRefundAmount = taxRebateExportDetailsList.stream().map(TaxRebateExportDetails::getTaxRefundAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }
}
