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.FoodCompanyMealApplicationService;
import com.jeesharp.business.modules.food.dto.FoodCompanyMealOrderPaidDTO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 企业团餐微信支付成功回调 → {@link FoodCompanyMealApplicationService#handleCompanyMealOrderPaid}。
 *
 * @author jeesharp
 * @since 2026-06-23
 */
@Service
@AllArgsConstructor
public class FoodCompanyMealWechatPayCallbackAdapter implements WechatPayCallbackAdapter {

    private final FoodCompanyMealApplicationService foodCompanyMealApplicationService;
    private final PayOrderApi payOrderApi;

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

    /** 企业团餐支付成功确认：查统一单实付金额后委托 handleCompanyMealOrderPaid。 */
    @Override
    public void confirm(String orderId, String payOrderId, String thirdPartyTradeNo) {
        FoodCompanyMealOrderPaidDTO dto = new FoodCompanyMealOrderPaidDTO();
        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());
        foodCompanyMealApplicationService.handleCompanyMealOrderPaid(dto);
    }
}
