package com.zt.plat.module.base.service.doctemplate.render.provider;

import com.zt.plat.module.base.service.doctemplate.render.IBusinessDataProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * 订单数据提供者
 * 提供订单相关的业务数据
 *
 * @author hwc
 */
@Component("orderDataProvider")
@Slf4j
public class OrderDataProvider implements IBusinessDataProvider {

    @Override
    public Map<String, Object> provideData(Object context, List<String> fieldNames) {
        Long orderId = null;
        if (context instanceof Long) {
            orderId = (Long) context;
        } else if (context instanceof String) {
            try {
                orderId = Long.valueOf((String) context);
            } catch (NumberFormatException e) {
                log.warn("无效的订单ID格式: {}", context);
                return Map.of();
            }
        }

        if (orderId == null) {
            log.warn("订单ID为空");
            return Map.of();
        }

        log.debug("获取订单数据，订单ID: {}, 请求字段: {}", orderId, fieldNames);

        // TODO: 实际实现应该：
        // 1. 根据订单ID从数据库或服务中查询订单信息
        // 2. 根据请求的字段名返回对应的数据
        // 3. 处理数据类型转换

        // 临时实现：返回一些示例数据
        Map<String, Object> data = new HashMap<>();

        // 模拟订单数据
        for (String fieldName : fieldNames) {
            switch (fieldName) {
                case "orderNumber":
                case "order_number":
                    data.put(fieldName, "ORD-2025-" + String.format("%06d", orderId % 100000));
                    break;
                case "customer":
                case "customer_name":
                    data.put(fieldName, "中铜制造有限公司");
                    break;
                case "totalAmount":
                case "total_amount":
                    data.put(fieldName, 2500000.00);
                    break;
                case "orderDate":
                case "order_date":
                    data.put(fieldName, "2025-01-20");
                    break;
                case "deliveryAddress":
                case "delivery_address":
                    data.put(fieldName, "上海市浦东新区金桥路123号");
                    break;
                case "contactPerson":
                case "contact_person":
                    data.put(fieldName, "张经理");
                    break;
                case "contactPhone":
                case "contact_phone":
                    data.put(fieldName, "13800138000");
                    break;
                default:
                    log.debug("未处理的订单字段: {}", fieldName);
                    break;
            }
        }

        log.debug("返回订单数据: {}", data.keySet());
        return data;
    }
}