package cn.superdesk.uniorder.service.accounting.unified.supplier.Impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.enums.common.CommonDirectionEnum;
import cn.superdesk.uniorder.common.enums.common.CommonNeedReviseMarkEnum;
import cn.superdesk.uniorder.common.enums.common.CommonOrderGoodsIdEnum;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderDimCreateByEnum;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplier;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.*;
import cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.*;
import cn.superdesk.uniorder.service.accounting.unified.supplier.AccountingUnifiedSupplierServiceOpStatistics;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderGoodsService;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2022/1/11 上午10:41
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/1/11 上午10:41
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccountingUnifiedSupplierServiceOpStatisticsImpl implements AccountingUnifiedSupplierServiceOpStatistics {


    @Autowired
    private UnifiedOrderService      unifiedOrderService;
    @Autowired
    private UnifiedOrderGoodsService unifiedOrderGoodsService;


    @Autowired
    private AccountingUnifiedSupplierMapper accountingUnifiedSupplierMapper;

    @Autowired
    private AccountingUnifiedSupplierOrderMapper    accountingUnifiedSupplierOrderMapper;
    @Autowired
    private AccountingUnifiedSupplierXOrderMapperEx accountingUnifiedSupplierXOrderMapperEx;
    @Autowired
    private AccountingUnifiedSupplierXGoodsMapper   accountingUnifiedSupplierXGoodsMapper;
    @Autowired
    private AccountingUnifiedSupplierXGoodsMapperEx accountingUnifiedSupplierXGoodsMapperEx;

    public int statistics(Long accountingOrderId) {
        return 0;
    }

    public int statistics(String accountingOrderSn) {
        return 0;
    }

    /**
     * 统计
     *
     * @return
     */
    public int statistics(AccountingUnifiedSupplier statementOfAccount) {

        int ret = 0;

        if (ObjectUtil.isNull(statementOfAccount.getId())) {
            return 0;
        }

        if (StrUtil.isBlank(statementOfAccount.getAccountingOrderSn())) {
            return 0;
        }

        Map<String, Object> params = new HashMap<>();
        params.put("accountingOrderSn", statementOfAccount.getAccountingOrderSn());

        if (AccountingOrderDimCreateByEnum.订单.getKey().compareTo(statementOfAccount.getDimCreateBy()) == 0) {

            AccountingOrderUnifiedSupplierStatisticsFixVoDimOrder fixVoDimOrder = checkAmountErrorForFixDimOrder(params);
            ret = statisticsRevise(statementOfAccount.getId(), fixVoDimOrder);

        } else if (AccountingOrderDimCreateByEnum.商品.getKey().compareTo(statementOfAccount.getDimCreateBy()) == 0) {

            AccountingOrderUnifiedSupplierStatisticsFixVoDimGoods fixVoDimGoods = checkAmountErrorForFixDimGoods(params);
            ret = statisticsRevise(statementOfAccount.getId(), fixVoDimGoods);

        }

        return ret;
    }

    public int statisticsRevise(Long accountingOrderId, AccountingOrderUnifiedSupplierStatisticsVo statisticsVo) {


        int ret = 0;

        /**************************************** 修订 start ****************************************/

        AccountingUnifiedSupplier fixEntity = new AccountingUnifiedSupplier();

        fixEntity.setId(accountingOrderId);

        fixEntity.setOrderQuantity(statisticsVo.getOrderQuantity());
        fixEntity.setGoodsQuantity(statisticsVo.getGoodsQuantity());
        fixEntity.setGoodsItemQuantity(statisticsVo.getGoodsItemQuantity());

        fixEntity.setOrderAmount(statisticsVo.getOrderAmount());
        fixEntity.setRefundOrderAmount(statisticsVo.getRefundOrderAmount());

        fixEntity.setGoodsAmount(statisticsVo.getGoodsAmount());
        fixEntity.setRefundGoodsAmount(statisticsVo.getRefundGoodsAmount());

        fixEntity.setOrderCostAmount(statisticsVo.getOrderCostAmount());
        fixEntity.setRefundOrderCostAmount(statisticsVo.getRefundOrderCostAmount());

        fixEntity.setAccountingOrderAmount(statisticsVo.getAccountingOrderAmount());
        fixEntity.setAccountingOrderAmountWithFreight(statisticsVo.getAccountingOrderAmountWithFreight());

        fixEntity.setAccountingOrderCostAmount(statisticsVo.getAccountingOrderCostAmount());
        fixEntity.setAccountingOrderCostAmountWithFreight(statisticsVo.getAccountingOrderCostAmountWithFreight());

        fixEntity.setGoodsCostAmount(statisticsVo.getGoodsCostAmount());
        fixEntity.setRefundGoodsCostAmount(statisticsVo.getRefundGoodsCostAmount());

        fixEntity.setGoodsTransportAmount(statisticsVo.getGoodsTransportAmount());
        fixEntity.setRefundGoodsTransportAmount(statisticsVo.getRefundGoodsTransportAmount());

        fixEntity.setNeedReviseMark(CommonNeedReviseMarkEnum.正常.getKey());

        ret = accountingUnifiedSupplierMapper.updateByPrimaryKeySelective(fixEntity);

        /**************************************** 修订   end ****************************************/

        return ret;
    }


    // statisticsVo
    public void sugarUpRetarded(AccountingOrderUnifiedSupplierListItemVo unifiedSupplierStatementOfAccountVo, AccountingOrderUnifiedSupplierStatisticsVo statisticsVo) {

        unifiedSupplierStatementOfAccountVo.setAccountingOrderAmount(statisticsVo.getAccountingOrderAmount());
        unifiedSupplierStatementOfAccountVo.setAccountingOrderAmountWithFreight(statisticsVo.getAccountingOrderAmountWithFreight());

        // 商品成本 - 商品退成本
        unifiedSupplierStatementOfAccountVo.setAccountingOrderCostAmount(statisticsVo.getAccountingOrderCostAmount());
        // 商品成本 - 商品退成本 + 运费 + 退单退运费
        unifiedSupplierStatementOfAccountVo.setAccountingOrderCostAmountWithFreight(statisticsVo.getAccountingOrderCostAmountWithFreight());

        /****************************************** 商品角度统计 start ******************************************/

        unifiedSupplierStatementOfAccountVo.setGoodsAmount(statisticsVo.getGoodsAmount());
        unifiedSupplierStatementOfAccountVo.setGoodsCostAmount(statisticsVo.getGoodsCostAmount());

        unifiedSupplierStatementOfAccountVo.setRefundGoodsAmount(statisticsVo.getRefundGoodsAmount());
        unifiedSupplierStatementOfAccountVo.setRefundGoodsCostAmount(statisticsVo.getRefundGoodsCostAmount());

        unifiedSupplierStatementOfAccountVo.setGoodsTransportAmount(statisticsVo.getGoodsTransportAmount());
        unifiedSupplierStatementOfAccountVo.setRefundGoodsTransportAmount(statisticsVo.getRefundGoodsTransportAmount());

        /****************************************** 数量 start ******************************************/

        unifiedSupplierStatementOfAccountVo.setOrderQuantity(statisticsVo.getOrderQuantity());
        unifiedSupplierStatementOfAccountVo.setGoodsQuantity(statisticsVo.getGoodsQuantity());
        unifiedSupplierStatementOfAccountVo.setGoodsItemQuantity(statisticsVo.getGoodsItemQuantity());

        /****************************************** 订单角度 没作用 start ******************************************/

        unifiedSupplierStatementOfAccountVo.setOrderCostAmount(statisticsVo.getOrderCostAmount());
        unifiedSupplierStatementOfAccountVo.setOrderTransportAmount(statisticsVo.getOrderTransportAmount());
    }

    /**
     * TODO 待合并 修正列表数据显示
     *
     * @param unifiedSupplierStatementOfAccountVo
     */
    public void fixDimOrder(AccountingOrderUnifiedSupplierListItemVo unifiedSupplierStatementOfAccountVo) {

        // 得到 accountingOrderSn
        Map<String, Object> params = new HashMap<>();
        params.put("accountingOrderSn", unifiedSupplierStatementOfAccountVo.getAccountingOrderSn());

        AccountingOrderUnifiedSupplierStatisticsFixVoDimOrder fixVoDimOrder = checkAmountErrorForFixDimOrder(params);


        /**************************************** 修显 start ****************************************/

        sugarUpRetarded(unifiedSupplierStatementOfAccountVo, fixVoDimOrder);

        /**************************************** 修显   end ****************************************/


        /**************************************** 修订 start ****************************************/

        statisticsRevise(unifiedSupplierStatementOfAccountVo.getId(), fixVoDimOrder);

        /**************************************** 修订   end ****************************************/


    }

    /**
     * TODO 待合并 修正列表数据显示
     *
     * @param unifiedSupplierStatementOfAccountVo
     */
    public void fixDimGoods(AccountingOrderUnifiedSupplierListItemVo unifiedSupplierStatementOfAccountVo) {

        // 得到 accountingOrderSn
        Map<String, Object> params = new HashMap<>();
        params.put("accountingOrderSn", unifiedSupplierStatementOfAccountVo.getAccountingOrderSn());
        AccountingOrderUnifiedSupplierStatisticsFixVoDimGoods fixVoDimGoods = checkAmountErrorForFixDimGoods(params);


        /**************************************** 修显 start ****************************************/

        sugarUpRetarded(unifiedSupplierStatementOfAccountVo, fixVoDimGoods);

        /**************************************** 修显   end ****************************************/


        /**************************************** 修订 start ****************************************/

        statisticsRevise(unifiedSupplierStatementOfAccountVo.getId(), fixVoDimGoods);

        /**************************************** 修订   end ****************************************/


    }

    /**
     * 对账单 详情 订单维度
     *
     * @param params
     * @return
     */
    public AccountingOrderUnifiedSupplierStatisticsFixVoDimOrder checkAmountErrorForFixDimOrder(Map<String, Object> params) {

        log.error("checkAmountErrorForFixDimOrder\n\n\n{}\n\n\n", AnJsonUtils.toJson(params));


        // 先得出一份 对账单 X 商品 的数据
        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder> result = accountingUnifiedSupplierXOrderMapperEx.checkAmountErrorForFixDimOrder(params);

        /****************************************** 正单-商品 start ******************************************/

        List<Predicate<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder>> predicatesByPlusGoods = new ArrayList<>();
        predicatesByPlusGoods.add(
                r -> r.getGoodsName() != null
                        && (!r.getGoodsName().equals(CommonOrderGoodsIdEnum.运费.getDesc()) || !r.getGoodsName().equals(CommonOrderGoodsIdEnum.商户承担运费.getDesc()))
                        && null != r.getDirection()
                        && r.getDirection().equals(CommonDirectionEnum.正向.getKey())
        );

        /****************************************** 正单-商品   end ******************************************/


        /****************************************** 正单-运费 start ******************************************/

        List<Predicate<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder>> predicatesByTransportPlusGoods = new ArrayList<>();
        predicatesByTransportPlusGoods.add(
                r -> r.getGoodsName() != null
                        && (r.getGoodsName().equals(CommonOrderGoodsIdEnum.运费.getDesc()) || r.getGoodsName().equals(CommonOrderGoodsIdEnum.商户承担运费.getDesc()))
                        && null != r.getDirection()
                        && r.getDirection().equals(CommonDirectionEnum.正向.getKey()));

        /****************************************** 正单-运费   end ******************************************/


        /****************************************** 退单-商品 start ******************************************/

        List<Predicate<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder>> predicatesByMinusGoods = new ArrayList<>();
        predicatesByMinusGoods.add(
                r -> r.getGoodsName() != null
                        && (!r.getGoodsName().equals(CommonOrderGoodsIdEnum.退单退运费))
                        && null != r.getDirection()
                        && r.getDirection().equals(CommonDirectionEnum.负向.getKey())
        );

        /****************************************** 退单-商品   end ******************************************/

        /****************************************** 退单-退运费 start ******************************************/

        List<Predicate<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder>> predicatesByTransportMinusGoods = new ArrayList<>();
        predicatesByTransportMinusGoods.add(
                r -> r.getGoodsName() != null
                        && (r.getGoodsName().equals(CommonOrderGoodsIdEnum.退单退运费))
                        && null != r.getDirection()
                        && r.getDirection().equals(CommonDirectionEnum.负向.getKey())
        );

        /****************************************** 退单-退运费   end ******************************************/


        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder> predicatesByPlusGoodsFilter           = AnLamdaUtils.filters(result, predicatesByPlusGoods);
        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder> predicatesByMinusGoodsFilter          = AnLamdaUtils.filters(result, predicatesByMinusGoods);
        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder> predicatesByTransportPlusGoodsFilter  = AnLamdaUtils.filters(result, predicatesByTransportPlusGoods);
        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimOrder> predicatesByTransportMinusGoodsFilter = AnLamdaUtils.filters(result, predicatesByTransportMinusGoods);

        BigDecimal orderAmount       = BigDecimal.ZERO;
        BigDecimal refundOrderAmount = BigDecimal.ZERO;

        BigDecimal goodsAmount       = BigDecimal.ZERO;
        BigDecimal refundGoodsAmount = BigDecimal.ZERO;

        BigDecimal orderTransportAmount = BigDecimal.ZERO;

        BigDecimal orderCostAmount       = BigDecimal.ZERO;
        BigDecimal refundOrderCostAmount = BigDecimal.ZERO;

        /**************************************** 主要是这些 start ****************************************/

        BigDecimal goodsCostAmount       = BigDecimal.ZERO;
        BigDecimal refundGoodsCostAmount = BigDecimal.ZERO;

        BigDecimal goodsTransportAmount       = BigDecimal.ZERO;
        BigDecimal refundGoodsTransportAmount = BigDecimal.ZERO;

        /**************************************** 主要是这些 start ****************************************/


        if (CollUtil.isNotEmpty(predicatesByPlusGoodsFilter)) {
            goodsAmount = predicatesByPlusGoodsFilter
                    .stream()
                    .map(item -> item.getPrice() == null ? BigDecimal.ZERO : item.getPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        if (CollUtil.isNotEmpty(predicatesByMinusGoodsFilter)) {
            refundGoodsAmount = predicatesByMinusGoodsFilter
                    .stream()
                    .map(item -> item.getPrice() == null ? BigDecimal.ZERO : item.getPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        if (CollUtil.isNotEmpty(predicatesByPlusGoodsFilter)) {
            goodsCostAmount = predicatesByPlusGoodsFilter
                    .stream()
                    .map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        if (CollUtil.isNotEmpty(predicatesByMinusGoodsFilter)) {
            refundGoodsCostAmount = predicatesByMinusGoodsFilter
                    .stream()
                    .map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        if (CollUtil.isNotEmpty(predicatesByTransportPlusGoodsFilter)) {
            goodsTransportAmount = predicatesByTransportPlusGoodsFilter
                    .stream()
                    .map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        if (CollUtil.isNotEmpty(predicatesByTransportMinusGoodsFilter)) {
            refundGoodsTransportAmount = predicatesByTransportPlusGoodsFilter
                    .stream()
                    .map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        BigDecimal accountingOrderAmount = goodsAmount.add(refundGoodsAmount);
        BigDecimal accountingOrderAmountWithFreight = accountingOrderAmount
                .add(goodsTransportAmount)
                .add(refundGoodsTransportAmount);


        BigDecimal accountingOrderCostAmount = goodsCostAmount.add(refundGoodsCostAmount);
        BigDecimal accountingOrderCostAmountWithFreight = accountingOrderCostAmount
                .add(goodsTransportAmount)
                .add(refundGoodsTransportAmount);

        log.info("核算 : (订单维度)" +
                        "\ngoodsCostAmount: {}" +
                        "\nrefundGoodsCostAmount: {}" +
                        "\ngoodsTransportAmount: {}" +
                        "\nrefundGoodsTransportAmount: {}" +
                        "\naccountingOrderCostAmount: {}" +
                        "\naccountingOrderCostAmountWithFreight: {}",

                goodsCostAmount,
                refundGoodsCostAmount,
                goodsTransportAmount,
                refundGoodsTransportAmount,
                accountingOrderCostAmount,
                accountingOrderCostAmountWithFreight
        );

        Integer orderQuantity     = 0;
        Integer goodsQuantity     = 0;
        Integer goodsItemQuantity = 0;

        goodsQuantity = result.size();


        AccountingOrderUnifiedSupplierStatisticsFixVoDimOrder unifiedSupplierStatementOfAccountVo = AccountingOrderUnifiedSupplierStatisticsFixVoDimOrder.builder()

                .accountingOrderAmount(accountingOrderAmount)
                .accountingOrderAmountWithFreight(accountingOrderAmountWithFreight)

                .accountingOrderCostAmount(accountingOrderCostAmount)
                .accountingOrderCostAmountWithFreight(accountingOrderCostAmountWithFreight)

                .orderAmount(orderAmount)
                .refundOrderAmount(refundOrderAmount)

                .orderTransportAmount(orderTransportAmount)

                .orderCostAmount(orderCostAmount)
                .refundOrderCostAmount(refundOrderCostAmount)

                .goodsAmount(goodsAmount)
                .refundGoodsAmount(refundGoodsAmount)

                .goodsCostAmount(goodsCostAmount)
                .refundGoodsCostAmount(refundGoodsCostAmount)

                .goodsTransportAmount(goodsTransportAmount)
                .refundGoodsTransportAmount(refundGoodsTransportAmount)

                .orderQuantity(orderQuantity)
                .goodsQuantity(goodsQuantity)
                .goodsItemQuantity(goodsItemQuantity)

                .build();

        return unifiedSupplierStatementOfAccountVo;
    }

    /**
     * 对账单 详情 商品维度
     *
     * @param params
     * @return
     */
    public AccountingOrderUnifiedSupplierStatisticsFixVoDimGoods checkAmountErrorForFixDimGoods(Map<String, Object> params) {

        log.info("对账单 详情 商品维度 {}", AnJsonUtils.toJson(params));

        // 先得出一份 对账单 X 商品 的数据
        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimGoods> result = accountingUnifiedSupplierXGoodsMapperEx.checkAmountErrorForFixDimGoods(params);


        /****************************************** 正单-商品 start ******************************************/

        List<Predicate<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimGoods>> predicatesByPlusGoods = new ArrayList<>();
        predicatesByPlusGoods.add(
                r -> r.getGoodsName() != null
                        && !r.getGoodsName().equals(CommonOrderGoodsIdEnum.运费.getDesc())
                        && !r.getGoodsName().equals(CommonOrderGoodsIdEnum.商户承担运费.getDesc())
                        && null != r.getDirection()
                        && r.getDirection().equals(CommonDirectionEnum.正向.getKey())
        );

        /****************************************** 正单-商品   end ******************************************/


        /****************************************** 正单-运费 start ******************************************/

        List<Predicate<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimGoods>> predicatesByTransportPlusGoods = new ArrayList<>();
        predicatesByTransportPlusGoods.add(
                r -> r.getGoodsName() != null
                        && (r.getGoodsName().equals(CommonOrderGoodsIdEnum.运费.getDesc()) || r.getGoodsName().equals(CommonOrderGoodsIdEnum.商户承担运费.getDesc()))
                        && null != r.getDirection()
                        && r.getDirection().equals(CommonDirectionEnum.正向.getKey()));

        /****************************************** 正单-运费   end ******************************************/


        /****************************************** 退单-商品 start ******************************************/

        List<Predicate<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimGoods>> predicatesByMinusGoods = new ArrayList<>();
        predicatesByMinusGoods.add(
                r -> r.getGoodsName() != null
                        && (!r.getGoodsName().equals(CommonOrderGoodsIdEnum.退单退运费.getDesc()))
                        && null != r.getDirection()
                        && r.getDirection().equals(CommonDirectionEnum.负向.getKey())
        );

        /****************************************** 退单-商品   end ******************************************/

        /****************************************** 退单-退运费 start ******************************************/

        List<Predicate<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimGoods>> predicatesByTransportMinusGoods = new ArrayList<>();
        predicatesByTransportMinusGoods.add(
                r -> r.getGoodsName() != null
                        && r.getGoodsName().equals(CommonOrderGoodsIdEnum.退单退运费.getDesc())
                        && null != r.getDirection()
                        && r.getDirection().equals(CommonDirectionEnum.负向.getKey())
        );

        /****************************************** 退单-退运费   end ******************************************/


        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimGoods> predicatesByPlusGoodsFilter           = AnLamdaUtils.filters(result, predicatesByPlusGoods);
        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimGoods> predicatesByMinusGoodsFilter          = AnLamdaUtils.filters(result, predicatesByMinusGoods);
        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimGoods> predicatesByTransportPlusGoodsFilter  = AnLamdaUtils.filters(result, predicatesByTransportPlusGoods);
        List<AccountingOrderUnifiedSuplierCheckAmountErrorForFixDimGoods> predicatesByTransportMinusGoodsFilter = AnLamdaUtils.filters(result, predicatesByTransportMinusGoods);

        BigDecimal orderAmount       = BigDecimal.ZERO;
        BigDecimal refundOrderAmount = BigDecimal.ZERO;

        BigDecimal goodsAmount       = BigDecimal.ZERO;
        BigDecimal refundGoodsAmount = BigDecimal.ZERO;

        BigDecimal orderTransportAmount = BigDecimal.ZERO;

        BigDecimal orderCostAmount       = BigDecimal.ZERO;
        BigDecimal refundOrderCostAmount = BigDecimal.ZERO;

        /**************************************** 主要是这些 start ****************************************/

        BigDecimal goodsCostAmount       = BigDecimal.ZERO;
        BigDecimal refundGoodsCostAmount = BigDecimal.ZERO;

        BigDecimal goodsTransportAmount       = BigDecimal.ZERO;
        BigDecimal refundGoodsTransportAmount = BigDecimal.ZERO;

        /**************************************** 主要是这些 start ****************************************/


        /**************************************** 销售价 start ****************************************/

        if (CollUtil.isNotEmpty(predicatesByPlusGoodsFilter)) {
            goodsAmount = predicatesByPlusGoodsFilter
                    .stream()
                    .map(item -> item.getPrice() == null ? BigDecimal.ZERO : item.getPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        if (CollUtil.isNotEmpty(predicatesByMinusGoodsFilter)) {
            refundGoodsAmount = predicatesByMinusGoodsFilter
                    .stream()
                    .map(item -> item.getPrice() == null ? BigDecimal.ZERO : item.getPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        /**************************************** 销售价   end ****************************************/

        if (CollUtil.isNotEmpty(predicatesByPlusGoodsFilter)) {
            goodsCostAmount = predicatesByPlusGoodsFilter
                    .stream()
                    .map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        if (CollUtil.isNotEmpty(predicatesByMinusGoodsFilter)) {
            refundGoodsCostAmount = predicatesByMinusGoodsFilter
                    .stream()
                    .map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        if (CollUtil.isNotEmpty(predicatesByTransportPlusGoodsFilter)) {
            goodsTransportAmount = predicatesByTransportPlusGoodsFilter
                    .stream()
                    .map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        if (CollUtil.isNotEmpty(predicatesByTransportMinusGoodsFilter)) {
            refundGoodsTransportAmount = predicatesByTransportMinusGoodsFilter
                    .stream()
                    .map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }

        BigDecimal accountingOrderAmount = goodsAmount.add(refundGoodsAmount);
        BigDecimal accountingOrderAmountWithFreight = accountingOrderAmount
                .add(goodsTransportAmount)
                .add(refundGoodsTransportAmount);


        BigDecimal accountingOrderCostAmount = goodsCostAmount.add(refundGoodsCostAmount);
        BigDecimal accountingOrderCostAmountWithFreight = accountingOrderCostAmount
                .add(goodsTransportAmount)
                .add(refundGoodsTransportAmount);

        log.info("核算 : (商品维度)" +
                        "\ngoodsCostAmount: {}" +
                        "\nrefundGoodsCostAmount: {}" +
                        "\ngoodsTransportAmount: {}" +
                        "\nrefundGoodsTransportAmount: {}" +
                        "\naccountingOrderCostAmount: {}" +
                        "\naccountingOrderCostAmountWithFreight: {}",

                goodsCostAmount,
                refundGoodsCostAmount,
                goodsTransportAmount,
                refundGoodsTransportAmount,
                accountingOrderCostAmount,
                accountingOrderCostAmountWithFreight
        );

        Integer orderQuantity     = 0;
        Integer goodsQuantity     = 0;
        Integer goodsItemQuantity = 0;

        goodsQuantity = result.size();

        AccountingOrderUnifiedSupplierStatisticsFixVoDimGoods accountingOrderUnifiedSupplierStatisticsFixVoDimGoods = AccountingOrderUnifiedSupplierStatisticsFixVoDimGoods.builder()

                .accountingOrderAmount(accountingOrderAmount)
                .accountingOrderAmountWithFreight(accountingOrderAmountWithFreight)

                .accountingOrderCostAmount(accountingOrderCostAmount)
                .accountingOrderCostAmountWithFreight(accountingOrderCostAmountWithFreight)

                .orderAmount(orderAmount)
                .refundOrderAmount(refundOrderAmount)

                .orderTransportAmount(orderTransportAmount)

                .orderCostAmount(orderCostAmount)
                .refundOrderCostAmount(refundOrderCostAmount)

                .goodsAmount(goodsAmount)
                .refundGoodsAmount(refundGoodsAmount)

                .goodsCostAmount(goodsCostAmount)
                .refundGoodsCostAmount(refundGoodsCostAmount)

                .goodsTransportAmount(goodsTransportAmount)
                .refundGoodsTransportAmount(refundGoodsTransportAmount)

                .orderQuantity(orderQuantity)
                .goodsQuantity(goodsQuantity)
                .goodsItemQuantity(goodsItemQuantity)

                .build();

        return accountingOrderUnifiedSupplierStatisticsFixVoDimGoods;


    }
}
