package com.jeesharp.business.modules.mall.application.api;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.mall.api.MallFinanceReadApi;
import com.jeesharp.business.modules.mall.api.dto.MallFinanceOrderDTO;
import com.jeesharp.business.modules.mall.api.dto.MallPayBatchSummaryDTO;
import com.jeesharp.business.modules.mall.entity.MallOrder;
import com.jeesharp.business.modules.mall.entity.MallPayBatch;
import com.jeesharp.business.modules.mall.service.MallOrderService;
import com.jeesharp.business.modules.mall.service.MallPayBatchService;
import com.jeesharp.business.modules.mall.vo.MallOrderQueryVO;
import com.jeesharp.business.modules.mall.vo.MallPayBatchQueryVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * {@link MallFinanceReadApi} 实现，基于商城领域服务只读查询（Application 层 api 包）。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@Service
@AllArgsConstructor
public class MallFinanceReadApiImpl implements MallFinanceReadApi {

    private final MallOrderService mallOrderService;
    private final MallPayBatchService mallPayBatchService;

    @Override
    public MallFinanceOrderDTO getOrder(String orderId) {
        if (StrUtil.isBlank(orderId)) {
            return null;
        }
        return toFinanceOrder(mallOrderService.get(orderId.trim()));
    }

    @Override
    public List<MallFinanceOrderDTO> listSubOrdersForPay(String payBatchId, String bizOrderId) {
        if (StrUtil.isNotBlank(payBatchId)) {
            return mallOrderService.listByPayBatchId(payBatchId.trim()).stream()
                    .map(this::toFinanceOrder)
                    .toList();
        }
        if (StrUtil.isNotBlank(bizOrderId)) {
            MallFinanceOrderDTO single = getOrder(bizOrderId);
            return single != null ? List.of(single) : Collections.emptyList();
        }
        return Collections.emptyList();
    }

    @Override
    public String findFirstOrderIdByOrderNo(String orderNo) {
        if (StrUtil.isBlank(orderNo)) {
            return null;
        }
        MallOrderQueryVO query = new MallOrderQueryVO();
        query.setOrderNo(orderNo.trim());
        List<MallOrder> orders = mallOrderService.findList(query);
        return orders.isEmpty() ? null : orders.get(0).getId();
    }

    @Override
    public String findPayBatchIdByBatchNo(String batchNo) {
        if (StrUtil.isBlank(batchNo)) {
            return null;
        }
        MallPayBatchQueryVO query = new MallPayBatchQueryVO();
        query.setBatchNo(batchNo.trim());
        List<MallPayBatch> batches = mallPayBatchService.findList(query);
        return batches.isEmpty() ? null : batches.get(0).getId();
    }

    @Override
    public MallPayBatchSummaryDTO getPayBatch(String payBatchId) {
        if (StrUtil.isBlank(payBatchId)) {
            return null;
        }
        MallPayBatch batch = mallPayBatchService.get(payBatchId.trim());
        if (batch == null) {
            return null;
        }
        MallPayBatchSummaryDTO dto = new MallPayBatchSummaryDTO();
        dto.setId(batch.getId());
        dto.setBatchNo(batch.getBatchNo());
        return dto;
    }

    @Override
    public Map<String, String> mapOrderNoByIds(Collection<String> mallOrderIds) {
        Map<String, String> map = new HashMap<>();
        if (mallOrderIds == null) {
            return map;
        }
        for (String id : mallOrderIds) {
            if (StrUtil.isBlank(id) || map.containsKey(id)) {
                continue;
            }
            MallFinanceOrderDTO order = getOrder(id);
            if (order != null && StrUtil.isNotBlank(order.getOrderNo())) {
                map.put(id, order.getOrderNo());
            }
        }
        return map;
    }

    @Override
    public Map<String, String> mapBatchNoByIds(Collection<String> payBatchIds) {
        Map<String, String> map = new HashMap<>();
        if (payBatchIds == null) {
            return map;
        }
        for (String id : payBatchIds) {
            if (StrUtil.isBlank(id) || map.containsKey(id)) {
                continue;
            }
            MallPayBatchSummaryDTO batch = getPayBatch(id);
            if (batch != null && StrUtil.isNotBlank(batch.getBatchNo())) {
                map.put(id, batch.getBatchNo());
            }
        }
        return map;
    }

    private MallFinanceOrderDTO toFinanceOrder(MallOrder order) {
        if (order == null) {
            return null;
        }
        MallFinanceOrderDTO dto = new MallFinanceOrderDTO();
        dto.setId(order.getId());
        dto.setOrderNo(order.getOrderNo());
        dto.setTenantId(order.getTenantId());
        dto.setPayAmount(order.getPayAmount());
        dto.setTotalAmount(order.getTotalAmount());
        dto.setPayBatchId(order.getPayBatchId());
        return dto;
    }
}
