package com.czg.order.entity;

import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 台桌订单统计表 实体类。
 *
 * @author zs
 * @since 2025-03-07
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table("tb_shop_table_order_statistic")
public class ShopTableOrderStatistic implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Id(keyType = KeyType.Auto)
    private Long id;

    /**
     * 台桌id
     */
    private Long tableId;
    /**
     * 台桌id
     */
    private String tableCode;
    /**
     * 台桌名称
     */
    private String tableName;
    /**
     * 区域名称
     */
    private String areaName;
    /**
     * 订单数量
     */
    private Long orderCount;

    /**
     * 订单金额
     */
    private BigDecimal orderAmount;

    /**
     * 店铺id
     */
    private Long shopId;

    /**
     * 创建日期 年月日
     */
    private Date createDay;
    /**
     * 退款数量
     */
    private Long refundCount;
    /**
     * 退款金额
     */
    private BigDecimal refundAmount;


    /**
     * 判断是否有有效数据
     */
    public boolean hasValidData() {
        return (orderCount != null && orderCount > 0) ||
                (orderAmount != null && orderAmount.compareTo(BigDecimal.ZERO) > 0) ||
                (refundCount != null && refundCount > 0) ||
                (refundAmount != null && refundAmount.compareTo(BigDecimal.ZERO) > 0);
    }


    public static List<ShopTableOrderStatistic> mergeWithStream(
            List<ShopTableOrderStatistic> realTimeData,
            List<ShopTableOrderStatistic> historyData) {
        // 最简单的合并方式
        return Stream.concat(realTimeData.stream(), historyData.stream())
                .filter(item -> item != null && item.getTableId() != null)
                .collect(Collectors.toMap(
                        ShopTableOrderStatistic::getTableId,
                        item -> item,
                        (item1, item2) -> {
                            ShopTableOrderStatistic merged = new ShopTableOrderStatistic();
                            merged.setTableId(item1.getTableId());
                            merged.setTableCode(item1.getTableCode());
                            merged.setTableName(item1.getTableName());
                            merged.setAreaName(item1.getAreaName());

                            // 数值字段求和
                            merged.setOrderCount(addLong(item1.getOrderCount(), item2.getOrderCount()));
                            merged.setOrderAmount(addBigDecimal(item1.getOrderAmount(), item2.getOrderAmount()));
                            merged.setRefundCount(addLong(item1.getRefundCount(), item2.getRefundCount()));
                            merged.setRefundAmount(addBigDecimal(item1.getRefundAmount(), item2.getRefundAmount()));
                            return merged;
                        }
                ))
                .values()
                .stream()
                .sorted(Comparator.comparing(
                        (ShopTableOrderStatistic item) -> item.getOrderCount() != null ? item.getOrderCount() : 0L,
                        // 降序排列
                        Comparator.reverseOrder()
                ))
                .toList();
    }

    // 辅助方法
    private static Long addLong(Long a, Long b) {
        return (a != null ? a : 0L) + (b != null ? b : 0L);
    }

    private static BigDecimal addBigDecimal(BigDecimal a, BigDecimal b) {
        return (a != null ? a : BigDecimal.ZERO).add(b != null ? b : BigDecimal.ZERO);
    }
}

