package com.kym.service.impl;

import com.kym.entity.Account;
import com.kym.entity.OrderWashDay;
import com.kym.entity.WashOrder;
import com.kym.service.DiscountService;
import com.kym.service.OrderWashDayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 洗车日优惠计算
 */
@Service
@Slf4j
public class WashDayDiscountHandle implements DiscountService {

    private final OrderWashDayService orderWashDayService;

    public WashDayDiscountHandle(OrderWashDayService orderWashDayService) {
        this.orderWashDayService = orderWashDayService;
    }

    @Override
    public void computeDiscount(WashOrder order, Account account) {
        // 设备已上报实际优惠金额，以设备值为准，记录到订单优惠日关联表
        var orderWashDay = orderWashDayService.lambdaQuery().eq(OrderWashDay::getOrderId, order.getOrderId()).oneOpt();
        orderWashDay.ifPresent(owd -> {
            owd.setDiscountAmount(order.getDiscountMoney());
            orderWashDayService.updateById(owd);
        });
    }
}
