package com.jeesharp.business.pay.adapter;

import com.jeesharp.business.modules.finance.api.PayOrderApi;
import com.jeesharp.business.modules.finance.payorder.vo.PayOrderInfoVO;
import com.jeesharp.business.modules.food.application.FoodPersonalMealApplicationService;
import com.jeesharp.business.modules.food.dto.FoodPersonalMealOrderPaidDTO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 个人订餐微信支付成功回调 → {@link FoodPersonalMealApplicationService#handlePaid}。
 * <p>与美食团购回调 {@link FoodWechatPayCallbackAdapter}（bizType=FOOD）区分。</p>
 *
 * @author jeesharp
 * @since 2026-06-09
 */
@Service
@AllArgsConstructor
public class FoodMealWechatPayCallbackAdapter implements WechatPayCallbackAdapter {

    private final FoodPersonalMealApplicationService foodPersonalMealApplicationService;
    private final PayOrderApi payOrderApi;

    @Override
    public MobilePayBizType bizType() {
        return MobilePayBizType.FOOD_MEAL;
    }

    /** 个人订餐支付成功确认：查统一单实付金额后委托 handlePaid。 */
    @Override
    public void confirm(String orderId, String payOrderId, String thirdPartyTradeNo) {
        FoodPersonalMealOrderPaidDTO dto = new FoodPersonalMealOrderPaidDTO();
        dto.setOrderId(orderId);
        dto.setPayOrderId(payOrderId);
        // 实付金额、支付时间从财务统一单读取（回调报文不含业务侧明细）
        PayOrderInfoVO payOrder = payOrderApi.get(payOrderId);
        BigDecimal paidAmount = payOrder == null ? null : payOrder.getPayAmount();
        dto.setPaidAmount(paidAmount);
        dto.setPaidTime(payOrder == null ? null : payOrder.getPayTime());
        foodPersonalMealApplicationService.handlePaid(dto);
    }
}
