package com.jeesharp.business.modules.mall.service.settle;

import com.jeesharp.business.modules.finance.api.PaySettleBizOrderProvider;
import com.jeesharp.business.modules.finance.api.dto.PaySettleBizOrderSnapshot;
import com.jeesharp.business.modules.finance.paymentrecord.enums.PayRecordBizSceneEnum;
import com.jeesharp.business.modules.mall.api.MallFinanceReadApi;
import com.jeesharp.business.modules.mall.api.dto.MallFinanceOrderDTO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商城业态 {@link PaySettleBizOrderProvider} 实现，委托 {@link MallFinanceReadApi}。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@Service
@AllArgsConstructor
public class MallPaySettleBizOrderProvider implements PaySettleBizOrderProvider {

    private final MallFinanceReadApi mallFinanceReadApi;

    @Override
    public String bizScene() {
        return PayRecordBizSceneEnum.MALL_ORDER.getCode();
    }

    @Override
    public PaySettleBizOrderSnapshot getOrder(String bizOrderId) {
        return toSnapshot(mallFinanceReadApi.getOrder(bizOrderId));
    }

    @Override
    public List<PaySettleBizOrderSnapshot> listSubOrdersForPay(String payBatchId, String bizOrderId) {
        return mallFinanceReadApi.listSubOrdersForPay(payBatchId, bizOrderId).stream()
                .map(MallPaySettleBizOrderProvider::toSnapshot)
                .toList();
    }

    @Override
    public String findFirstOrderIdByOrderNo(String orderNo) {
        return mallFinanceReadApi.findFirstOrderIdByOrderNo(orderNo);
    }

    @Override
    public String findPayBatchIdByBatchNo(String batchNo) {
        return mallFinanceReadApi.findPayBatchIdByBatchNo(batchNo);
    }

    @Override
    public Map<String, String> mapOrderNoByIds(Collection<String> bizOrderIds) {
        return mallFinanceReadApi.mapOrderNoByIds(bizOrderIds);
    }

    @Override
    public Map<String, String> mapBatchNoByIds(Collection<String> payBatchIds) {
        return mallFinanceReadApi.mapBatchNoByIds(payBatchIds);
    }

    private static PaySettleBizOrderSnapshot toSnapshot(MallFinanceOrderDTO order) {
        if (order == null) {
            return null;
        }
        PaySettleBizOrderSnapshot snapshot = new PaySettleBizOrderSnapshot();
        snapshot.setBizOrderId(order.getId());
        snapshot.setOrderNo(order.getOrderNo());
        snapshot.setTenantId(order.getTenantId());
        snapshot.setPayAmount(order.getPayAmount());
        snapshot.setTotalAmount(order.getTotalAmount());
        snapshot.setPayBatchId(order.getPayBatchId());
        return snapshot;
    }

}
