package com.sckw.order.serivce;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.sckw.core.common.enums.enums.DictTypeEnum;
import com.sckw.core.exception.BusinessException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.constant.NumberConstant;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.DateUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.order.api.model.*;
import com.sckw.order.dao.KwoTradeOrderMapper;
import com.sckw.order.model.SalesReportRawData;
import com.sckw.order.model.dto.*;
import com.sckw.order.model.vo.req.WbTOrderDataStsParam;
import com.sckw.order.model.vo.req.WbTOrderExcelStsParam;
import com.sckw.order.model.vo.res.TOrderDataStsResVO;
import com.sckw.order.model.vo.res.WbTOrderDataStsResVO;
import com.sckw.order.model.vo.res.WbTOrderExcelStsResVO;
import com.sckw.order.model.vo.res.WorkbenchPurchaseVO;
import com.sckw.payment.api.dubbo.PaymentDubboService;
import com.sckw.payment.api.model.dto.SettlementMoney;
import com.sckw.product.api.dubbo.GoodsInfoService;
import com.sckw.product.api.dubbo.GoodsStatisticsService;
import com.sckw.product.api.model.GoodsNumStsParam;
import com.sckw.product.api.model.KwpGoods;
import com.sckw.system.api.RemoteSystemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @desc: 贸易订单统计service
 * @author: yzc
 * @date: 2023-09-13 8:36
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwoTradeOrderStatisticsService {

    private final KwoTradeOrderService kwoTradeOrderService;
    private final KwoTradeOrderMapper tradeOrderMapper;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private GoodsInfoService goodsInfoService;
    @DubboReference(version = "1.0.0", group = "design", check = false)
    private GoodsStatisticsService goodsStatisticsService;
    @DubboReference(version = "1.0.0", group = "design", check = false)
    private PaymentDubboService paymentDubboService;
    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    /**
     * @desc: 工作台贸易订单表格统计
     * @author: yzc
     * @date: 2023-09-13 15:46
     * @Param params:
     * @return: java.util.List<com.sckw.order.model.vo.res.WbTOrderExcelStsResVO>
     */
    public List<WbTOrderExcelStsResVO> workbenchExcel(WbTOrderExcelStsParam params) {
        Integer dateType = params.getDateType();
        Date startTime;
        if (Objects.equals(1, dateType)) {
            startTime = DateUtils.getStartTime(3, LocalDateTime.now());
        } else if (Objects.equals(3, dateType)) {
            startTime = DateUtils.getYearStartTime();
        } else {
            throw new BusinessException("Invalid date type");
        }
        List<WbTOrderExcelStsResVO> res = buildFormatDate(dateType, startTime);
        WbTOrderExcelStsReqDTO reqDTO = new WbTOrderExcelStsReqDTO();
        reqDTO.setUnitType(String.valueOf(params.getOrderType()))
                .setDateFormat(DateUtils.getDateFormat(dateType))
                .setStartTime(startTime)
                .setTopEntId(LoginUserHolder.getEntId())
                .setStsFactorsOne(params.getStsFactorsOne())
                .setStsFactorsTwo(params.getStsFactorsTwo());
        Map<String, WbTOrderExcelStsResDTO> map = kwoTradeOrderService.workbenchExcel(reqDTO).stream()
                .collect(Collectors.toMap(WbTOrderExcelStsResDTO::getFormatDate, e -> e, (k1, k2) -> k1));
        res.forEach(e -> {
            WbTOrderExcelStsResDTO dto = map.get(e.getFormatDate());
            if (Objects.nonNull(dto)) {
                BigDecimal one = dto.getStsFactorsOneValue();
                if (Objects.equals(params.getStsFactorsOne(), 1)) {
                    one = one.divide(NumberConstant.TEN_THOUSAND, 2, RoundingMode.HALF_UP);
                }
                BigDecimal two = dto.getStsFactorsTwoValue();
                if (Objects.equals(params.getStsFactorsTwo(), 1)) {
                    two = two.divide(NumberConstant.TEN_THOUSAND, 2, RoundingMode.HALF_UP);
                }
                e.setStsFactorsOneValue(one);
                e.setStsFactorsTwoValue(two);
            }
        });
        return res;
    }

    /**
     * @desc: 构建时间转换格式
     * @author: yzc
     * @date: 2023-09-13 15:45
     * @Param dateType:
     * @Param startTime:
     * @return: java.util.List<com.sckw.order.model.vo.res.WbTOrderExcelStsResVO>
     */
    private List<WbTOrderExcelStsResVO> buildFormatDate(Integer dateType, Date startTime) {
        LocalDate start = DateUtils.dateToLocalDate(startTime);
        LocalDate end = LocalDate.now();
        List<WbTOrderExcelStsResVO> res = new ArrayList<>();
        if (dateType == 1) {
            while (start.isBefore(end)) {
                WbTOrderExcelStsResVO vo = new WbTOrderExcelStsResVO();
                vo.setFormatDate(start.toString());
                start = start.plusDays(1);
                res.add(vo);
            }
            WbTOrderExcelStsResVO vo = new WbTOrderExcelStsResVO();
            vo.setFormatDate(end.toString());
            res.add(vo);
        } else {
            //当月第一天
            end = end.with(TemporalAdjusters.firstDayOfMonth());
            while (start.isBefore(end)) {
                WbTOrderExcelStsResVO vo = new WbTOrderExcelStsResVO();
                vo.setFormatDate(DateUtils.localDateToString(start, "yyyy-MM"));
                start = start.plusMonths(1);
                res.add(vo);
            }
            WbTOrderExcelStsResVO vo = new WbTOrderExcelStsResVO();
            vo.setFormatDate(DateUtils.localDateToString(end, "yyyy-MM"));
            res.add(vo);
        }
        return res;
    }

    /**
     * @desc: 工作台贸易订单数据统计
     * @author: yzc
     * @date: 2023-09-11 15:32
     * @Param params:
     * @return: com.sckw.report.model.vo.WbTOrderDataStsResVO
     */
    public WbTOrderDataStsResVO workbenchData(WbTOrderDataStsParam params) {
        WbTOrderDataStsResVO res = new WbTOrderDataStsResVO();
        List<TOrderDataStsResVO> list = new ArrayList<>();
        Long entId = LoginUserHolder.getEntId();
        String unit = params.getUnit();
        Integer orderType = params.getOrderType();
        Integer dateType = params.getDateType();
        Date currentStart = DateUtils.getStartTime(dateType, LocalDateTime.now());
        Date lastEnd = DateUtils.getLastEndTime(dateType);
        Date lastStart = DateUtils.getStartTime(dateType, DateUtil.toLocalDateTime(lastEnd));
        //当前数据
        TOrderDataStsResVO currentDataSts = new TOrderDataStsResVO();
        //商品数量
        GoodsNumStsParam goodsNumStsParam = new GoodsNumStsParam();
        goodsNumStsParam.setTopEntId(entId).setUnit(unit);
        Long currentGoodsNum = goodsStatisticsService.getOnShelfGoodsNum(goodsNumStsParam);
        //待付款金额
        SettlementMoney settlement = Objects.equals(orderType, 1) ?
                paymentDubboService.countPurchase(entId, dateType) : paymentDubboService.countSell(entId, dateType);
        //订单统计
        WorkbenchOrderStsDTO currentOrderSts = getOrderStsInfo(orderType, unit, currentStart, null);
        BigDecimal currentAmount = BigDecimal.valueOf(currentOrderSts.getOrderAmount());
        BigDecimal currentPrice = BigDecimal.valueOf(currentOrderSts.getOrderPrice());
        currentDataSts.setType(NumberConstant.ONE)
                .setGoodsNum(currentGoodsNum)
                .setOrderAmount(currentAmount)
                .setOrderPrice(currentPrice)
                .setWaitCollectedPrice(new BigDecimal(settlement.getCurrent()))
                .setNotArrivedAmount(currentAmount.subtract(BigDecimal.valueOf(currentOrderSts.getOrderActualAmount())));
        list.add(currentDataSts);

        //上周/上月数据
        TOrderDataStsResVO lastDataSts = new TOrderDataStsResVO();
        //商品数量
        goodsNumStsParam.setEndDate(lastEnd);
        Long lastGoodsNum = goodsStatisticsService.getOnShelfGoodsNum(goodsNumStsParam);
        //订单统计
        WorkbenchOrderStsDTO lastOrderSts = getOrderStsInfo(orderType, unit, lastStart, lastEnd);
        BigDecimal lastAmount = BigDecimal.valueOf(lastOrderSts.getOrderAmount());
        BigDecimal lastPrice = BigDecimal.valueOf(lastOrderSts.getOrderPrice());
        lastDataSts.setType(NumberConstant.TWO)
                .setGoodsNum(lastGoodsNum)
                .setOrderAmount(lastAmount)
                .setOrderPrice(lastPrice)
                .setWaitCollectedPrice(new BigDecimal(settlement.getBefore()))
                .setNotArrivedAmount(lastAmount.subtract(BigDecimal.valueOf(lastOrderSts.getOrderActualAmount())));
        list.add(lastDataSts);
        res.setTOrderDataStsList(list);
        //商品数量环比
        BigDecimal goodsNumRingRate = getRingRate(new BigDecimal(currentGoodsNum), new BigDecimal(lastGoodsNum));
        boolean goodsNumFlag = goodsNumRingRate.compareTo(BigDecimal.ZERO) >= 0;
        goodsNumRingRate = goodsNumRingRate.abs();
        //订单总量环比
        BigDecimal orderAmountRingRate = getRingRate(currentAmount, lastAmount);
        boolean orderAmountFlag = orderAmountRingRate.compareTo(BigDecimal.ZERO) >= 0;
        orderAmountRingRate = orderAmountRingRate.abs();
        //订单总金额环比
        BigDecimal orderPriceRingRate = getRingRate(currentPrice, lastPrice);
        boolean orderPriceFlag = orderPriceRingRate.compareTo(BigDecimal.ZERO) >= 0;
        orderPriceRingRate = orderPriceRingRate.abs();
        //未到货量环比
        BigDecimal notArrivedAmountRingRate = getRingRate(currentDataSts.getNotArrivedAmount(), lastDataSts.getNotArrivedAmount());
        boolean notArrivedAmountFlag = notArrivedAmountRingRate.compareTo(BigDecimal.ZERO) >= 0;
        notArrivedAmountRingRate = notArrivedAmountRingRate.abs();

        res.setGoodsNumRingRate(goodsNumRingRate)
                .setGoodsNumFlag(goodsNumFlag)
                .setOrderAmountRingRate(orderAmountRingRate)
                .setOrderAmountFlag(orderAmountFlag)
                .setOrderPriceRingRate(orderPriceRingRate)
                .setOrderPriceFlag(orderPriceFlag)
                .setNotArrivedAmountRingRate(notArrivedAmountRingRate)
                .setNotArrivedAmountFlag(notArrivedAmountFlag)
                .setWaitCollectedPriceRingRate(new BigDecimal(settlement.getPercent().replace("%", "")))
                .setWaitCollectedPriceFlag(Objects.equals(settlement.getFlag(), 1));
        return res;
    }

    private BigDecimal getRingRate(BigDecimal current, BigDecimal last) {
        BigDecimal res;
        if (last.compareTo(BigDecimal.ZERO) == 0) {
            res = (current.subtract(last)).compareTo(BigDecimal.ZERO) != 0 ? NumberConstant.ONE_HUNDRED : BigDecimal.ZERO;
        } else {
            res = (current.subtract(last)).multiply(NumberConstant.ONE_HUNDRED).divide(last, 2, RoundingMode.HALF_UP);
        }
        return res;
    }

    /**
     * @desc: 获取订单统计信息
     * @author: yzc
     * @date: 2023-09-11 17:55
     * @Param orderType:
     * @Param unit:
     * @Param start:
     * @Param end:
     * @return: com.sckw.report.model.dto.WorkbenchOrderStsDTO
     */
    private WorkbenchOrderStsDTO getOrderStsInfo(Integer orderType, String unit, Date start, Date end) {
        OrderStsInfoDTO dto = new OrderStsInfoDTO();
        dto.setOrderType(orderType).setUnit(unit)
                .setEntId(LoginUserHolder.getEntId())
                .setStartCreateTime(start)
                .setEndCreateTime(end);
        List<Integer> excludeStatuses = Arrays.asList(0, 2);
        WorkbenchOrderStsDTO result = tradeOrderMapper.getOrderStsInfo(dto, excludeStatuses);
        return Objects.isNull(result) ? new WorkbenchOrderStsDTO() : result;
    }

    /**
     * @desc: 工作台采购订单列表
     * @author: yzc
     * @date: 2023-09-13 19:37
     * @return: java.util.List<com.sckw.report.model.vo.WorkbenchPurchaseVO>
     */
    public List<WorkbenchPurchaseVO> workbenchPurchaseList() {
        Long procureTopEntId = LoginUserHolder.getEntId();
        List<Integer> excludeStatuses = Arrays.asList(0, 2, 7);
        List<WorkbenchPurchaseDTO> orders = tradeOrderMapper.workbenchPurchaseList(procureTopEntId, excludeStatuses);
        if (CollUtil.isEmpty(orders)) {
            return Collections.emptyList();
        }
        Map<String, Map<String, String>> dict = remoteSystemService.queryDictByType(List.of(
                DictTypeEnum.UNIT_TYPE.getType(), DictTypeEnum.PRODUCT_NAME_TYPE.getType()));
        Map<String, String> unitMap, goodsTypeMap;
        if (CollUtil.isNotEmpty(dict)) {
            unitMap = CollUtil.isNotEmpty(dict.get(DictTypeEnum.UNIT_TYPE.getType())) ?
                    dict.get(DictTypeEnum.UNIT_TYPE.getType()) : new HashMap<>(Global.NUMERICAL_SIXTEEN);
            goodsTypeMap = CollUtil.isNotEmpty(dict.get(DictTypeEnum.PRODUCT_NAME_TYPE.getType())) ?
                    dict.get(DictTypeEnum.PRODUCT_NAME_TYPE.getType()) : new HashMap<>(Global.NUMERICAL_SIXTEEN);
        } else {
            unitMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
            goodsTypeMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
        }
        List<Long> goodsIds = orders.stream().map(WorkbenchPurchaseDTO::getGoodsId).toList();
        Map<Long, KwpGoods> goodsMap = goodsInfoService.getGoodsByIds(goodsIds);
        List<WorkbenchPurchaseVO> result = new ArrayList<>();
        orders.forEach(e -> {
            WorkbenchPurchaseVO vo = BeanUtils.copyProperties(e, WorkbenchPurchaseVO.class);
            vo.setUnitLabel(unitMap.get(e.getUnit())).setAmount(setScale(e.getAmount()));
            KwpGoods goods = goodsMap.get(e.getGoodsId());
            if (Objects.nonNull(goods)) {
                vo.setGoodsName(goods.getName()).setGoodsType(goods.getGoodsType())
                        .setGoodsTypeLabel(goodsTypeMap.get(goods.getGoodsType()));
            }
            result.add(vo);
        });
        return result;
    }


    /**
     * @desc: 设置两位小数
     * @author: yzc
     * @date: 2023-09-26 10:11
     * @Param d:
     * @return: java.lang.BigDecimal
     */
    private BigDecimal setScale(BigDecimal d) {
        if (Objects.isNull(d) || BigDecimal.ZERO.compareTo(d) == 0) {
            return NumberConstant.ZERO_TWO;
        }
        return d.setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 查询销售报表数据
     *
     * @param queryDto 查询参数
     * @return 销售报表数据列表
     */
    public List<SalesReportDataVo> querySalesReportData(SalesReportQueryDto queryDto) {
        if (StringUtils.isBlank(queryDto.getEndTime())) {
            queryDto.setEndTime(DateUtils.getCurrentTime());
        }
        // 将字符串时间转换为 LocalDateTime 用于比较
        LocalDateTime endDateTime = LocalDateTime.parse(queryDto.getEndTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime startDateTime = null;
        if (StringUtils.isNotBlank(queryDto.getStartTime())) {
            startDateTime = LocalDateTime.parse(queryDto.getStartTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }

        // 1. 查询基础销售订单数据
        List<SalesReportRawData> rawDataList = tradeOrderMapper.querySalesReportRawData(
                queryDto.getCurEntId(),
                queryDto.getTradeEntId(),
                queryDto.getGoodsId()
        );

        // 2. 收集商品ID（过滤 null）
        List<Long> goodsIds = rawDataList.stream()
                .map(SalesReportRawData::getGoodsId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());

        // 3. 调用商品服务获取商品名称
        Map<Long, KwpGoods> goodsMap = new HashMap<>();
        if (!goodsIds.isEmpty()) {
            goodsMap = goodsInfoService.getGoodsByIds(goodsIds);
        }

        // 4. 按客户和商品分组汇总数据
        Map<Long, Map<Long, SalesReportDataVo>> customerGoodsMap = new HashMap<>();

        for (SalesReportRawData rawData : rawDataList) {
            Long goodsId = rawData.getGoodsId();
            // 过滤商品ID为 null 的数据
            if (goodsId == null) {
                continue;
            }
            Long customerId = rawData.getCustomerId();
            // 金额/数量判空处理
            BigDecimal amount = Objects.equals(rawData.getChargeType(), 1) ? rawData.getLoadAmount() : rawData.getUnloadAmount();
            amount = Objects.isNull(amount) ? BigDecimal.ZERO : amount;
            BigDecimal price = rawData.getPrice() != null ? rawData.getPrice() : BigDecimal.ZERO;
            BigDecimal unitPrice = rawData.getUnitPrice() != null ? rawData.getUnitPrice() : BigDecimal.ZERO;

            String createTimeStr = rawData.getCreateTime();
            if (createTimeStr == null) {
                continue;
            }
            LocalDateTime createTime = LocalDateTime.parse(createTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

            // 初始化客户和商品的映射
            customerGoodsMap.putIfAbsent(customerId, new HashMap<>());
            Map<Long, SalesReportDataVo> goodsMapForCustomer = customerGoodsMap.get(customerId);
            goodsMapForCustomer.putIfAbsent(goodsId, new SalesReportDataVo());

            SalesReportDataVo vo = goodsMapForCustomer.get(goodsId);
            // 初始化 BigDecimal 字段（防止 null）
            if (vo.getCumulativeQuantity() == null) {
                vo.setCumulativeQuantity(BigDecimal.ZERO);
            }
            if (vo.getCumulativeAmount() == null) {
                vo.setCumulativeAmount(BigDecimal.ZERO);
            }
            if (vo.getSalesQuantity() == null) {
                vo.setSalesQuantity(BigDecimal.ZERO);
            }
            if (vo.getSalesAmount() == null) {
                vo.setSalesAmount(BigDecimal.ZERO);
            }

            // 设置基本信息
            vo.setCustomerId(customerId);
            vo.setCustomerName(rawData.getCustomerName());
            vo.setGoodsId(goodsId);
            KwpGoods goods = goodsMap.get(goodsId);
            if (goods != null) {
                vo.setGoodsName(goods.getName());
            }

            // 累计数据（createTime <= endTime）
            if (createTime.isBefore(endDateTime)) {
                vo.setCumulativeQuantity(vo.getCumulativeQuantity().add(amount));
                vo.setCumulativeAmount(vo.getCumulativeAmount().add(price));
                if (vo.getCumulativeQuantity().compareTo(BigDecimal.ZERO) > 0) {
                    vo.setCumulativeAveragePrice(vo.getCumulativeAmount()
                            .divide(vo.getCumulativeQuantity(), 2, RoundingMode.HALF_UP));
                }
                // 当前时间段数据
                if (startDateTime == null) {
                    // 无开始时间：所有累计数据也计入销售
                    vo.setSalesQuantity(vo.getSalesQuantity().add(amount));
                    vo.setSalesAmount(vo.getSalesAmount().add(NumberUtil.mul(unitPrice, amount)));
                    if (vo.getSalesQuantity().compareTo(BigDecimal.ZERO) > 0) {
                        vo.setAveragePrice(vo.getSalesAmount()
                                .divide(vo.getSalesQuantity(), 2, RoundingMode.HALF_UP));
                    }
                } else if (createTime.isAfter(startDateTime)) {
                    vo.setSalesQuantity(vo.getSalesQuantity().add(amount));
                    vo.setSalesAmount(vo.getSalesAmount().add(NumberUtil.mul(unitPrice, amount)));
                    if (vo.getSalesQuantity().compareTo(BigDecimal.ZERO) > 0) {
                        vo.setAveragePrice(vo.getSalesAmount()
                                .divide(vo.getSalesQuantity(), 2, RoundingMode.HALF_UP));
                    }
                }
            }
        }

        // 5. 转换为列表并返回
        List<SalesReportDataVo> result = new ArrayList<>();
        for (Map<Long, SalesReportDataVo> goodsMapForCustomer : customerGoodsMap.values()) {
            result.addAll(goodsMapForCustomer.values());
        }

        // 6. 排序（处理 null 名称）
        result.sort(Comparator.comparing(SalesReportDataVo::getCustomerName, Comparator.nullsLast(String::compareTo))
                .thenComparing(SalesReportDataVo::getGoodsName, Comparator.nullsLast(String::compareTo)));

        return result;
    }

    public List<DailyShipmentStatisticsDto> getDailyShipmentByCategory(Long entId, Integer days) {
        Date endTime = new Date();
        Date startTime = DateUtils.getStartTime(days, LocalDateTime.now());
        List<DailyShipmentStatisticsDTO> list = tradeOrderMapper.selectDailyShipmentByCategory(entId, startTime, endTime);
        return list.stream().map(dto -> {
            DailyShipmentStatisticsDto result = new DailyShipmentStatisticsDto();
            result.setOrderDate(dto.getOrderDate());
            result.setGoodsType(dto.getGoodsType());
            result.setActualAmount(dto.getActualAmount());
            return result;
        }).collect(Collectors.toList());
    }

    public List<DailySalesStatisticsDto> getDailySalesByCategory(Long entId, Integer days) {
        Date endTime = new Date();
        Date startTime = DateUtils.getStartTime(days, LocalDateTime.now());
        List<DailySalesStatisticsDTO> list = tradeOrderMapper.selectDailySalesByCategory(entId, startTime, endTime);
        return list.stream().map(dto -> {
            DailySalesStatisticsDto result = new DailySalesStatisticsDto();
            result.setOrderDate(dto.getOrderDate());
            result.setGoodsType(dto.getGoodsType());
            result.setSalesAmount(dto.getSalesAmount());
            return result;
        }).collect(Collectors.toList());
    }

    public List<GoodsVolumeStatisticsDto> getGoodsVolumeTop5(Long entId) {
        List<GoodsVolumeStatisticsDTO> list = tradeOrderMapper.selectGoodsVolumeTop5(entId);
        return list.stream().map(dto -> {
            GoodsVolumeStatisticsDto result = new GoodsVolumeStatisticsDto();
            result.setGoodsName(dto.getGoodsName());
            result.setTotalVolume(dto.getTotalVolume());
            return result;
        }).collect(Collectors.toList());
    }

    public List<GoodsAmountStatisticsDto> getGoodsAmountTop5(Long entId) {
        List<GoodsAmountStatisticsDTO> list = tradeOrderMapper.selectGoodsAmountTop5(entId);
        return list.stream().map(dto -> {
            GoodsAmountStatisticsDto result = new GoodsAmountStatisticsDto();
            result.setGoodsName(dto.getGoodsName());
            result.setTotalAmount(dto.getTotalAmount());
            return result;
        }).collect(Collectors.toList());
    }
}

