package com.kym.service.impl;

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

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

    private final WashDayService washDayService;

    private final OrderWashDayService orderWashDayService;

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

    @Override
    public void computeDiscount(WashOrder order, Account account) {
        // 获取订单关联的洗车日活动信息
        var orderWashDay = orderWashDayService.lambdaQuery().eq(OrderWashDay::getOrderId,  order.getOrderId()).oneOpt();
        orderWashDay.ifPresent(owd -> {
            var washDay = washDayService.lambdaQuery().eq(WashDay::getId, owd.getWashDayId()).oneOpt();
            washDay.ifPresent(wd -> {
                // 折扣
                var discount = wd.getDiscount();
                // 计算折扣金额
                var discountAmount = order.getAmount() * discount / 100;
                order.setDiscountAmount(discountAmount);
                // 订单实付金额
                order.setAmountReceivable(order.getAmount() - discountAmount);
            });
        });

    }
}
