package com.kym.service.miniapp.impl;

import com.kym.entity.admin.Coupon;
import com.kym.entity.miniapp.Account;
import com.kym.entity.miniapp.ChargeOrder;
import com.kym.entity.miniapp.OrderCoupon;
import com.kym.entity.miniapp.UserCoupon;
import com.kym.service.admin.CouponService;
import com.kym.service.miniapp.DiscountService;
import com.kym.service.miniapp.OrderCouponService;
import com.kym.service.miniapp.UserCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券计算
 *
 * @author skyline
 */
@Service
@Slf4j
public class CouponDiscountHandle implements DiscountService {
    private final OrderCouponService orderCouponService;
    private final UserCouponService userCouponService;

    private final CouponService couponService;

    public CouponDiscountHandle(OrderCouponService orderCouponService, UserCouponService userCouponService, CouponService couponService) {
        this.orderCouponService = orderCouponService;
        this.userCouponService = userCouponService;
        this.couponService = couponService;
    }


    @Override
    @Transactional
    public void computeDiscount(ChargeOrder chargeOrder, Account account) {
        int discountAmount = 0;

        try {
            // 获取订单优惠券
            var orderCoupon = orderCouponService.lambdaQuery()
                    .eq(OrderCoupon::getStartChargeSeq, chargeOrder.getStartChargeSeq())
                    .eq(OrderCoupon::getUserId, chargeOrder.getUserId())
                    .one();

            // 查询用户优惠券信息
            var userCoupon = userCouponService.lambdaQuery()
                    .eq(UserCoupon::getId, orderCoupon.getUserCouponId())
                    .eq(UserCoupon::getUserId, chargeOrder.getUserId())
                    .one();

            // 检查是否达到使用门槛
            if (chargeOrder.getServiceMoney() >= userCoupon.getMinServiceMoney()) {
                // 根据优惠券类型处理
                switch (userCoupon.getCouponType()) {
                    case Coupon.COUPON_TYPE_满减券:
                        discountAmount = userCoupon.getDiscount();
                        break;
                    case Coupon.COUPON_TYPE_折扣券:
                        discountAmount = chargeOrder.getServiceMoney() * (100 - userCoupon.getDiscount()) / 100;
                        break;
                    default:
                        break;
                }

                // 订单金额、优惠金额更新
                chargeOrder.setDiscountAmount(discountAmount);
                chargeOrder.setServiceMoneyDiscount(discountAmount);
                chargeOrder.setPayAmount(chargeOrder.getTotalMoney() - discountAmount);

                // 更新用户优惠券信息
                userCouponService.lambdaUpdate()
                        .set(UserCoupon::getStatus, UserCoupon.STATUS_无效)
                        .set(UserCoupon::getUsageStatus, UserCoupon.USAGE_STATUS_已使用)
                        .eq(UserCoupon::getId, userCoupon.getId())
                        .update();

                // 更新订单优惠券关联信息
                orderCouponService.lambdaUpdate()
                        .set(OrderCoupon::getDiscountAmount, discountAmount)
                        .set(OrderCoupon::getStatus, OrderCoupon.STATUS_使用成功)
                        .eq(OrderCoupon::getStartChargeSeq, chargeOrder.getStartChargeSeq())
                        .eq(OrderCoupon::getUserCouponId, userCoupon.getId())
                        .update();

                // 优惠券使用情况数据
                couponService.usedQuantifyCount(userCoupon.getCouponId());
            } else {
                orderCouponService.lambdaUpdate()
                        .set(OrderCoupon::getStatus, OrderCoupon.STATUS_使用失败)
                        .eq(OrderCoupon::getStartChargeSeq, chargeOrder.getStartChargeSeq())
                        .eq(OrderCoupon::getUserCouponId, userCoupon.getId())
                        .update();
                log.error("订单使用优惠券失败：订单号：{}，优惠券id{}：服务费未达到优惠券使用门槛", chargeOrder.getStartChargeSeq(), userCoupon.getCouponId());
            }
        } catch (Exception e) {
            log.error("处理优惠券失败", e);
            // 可以添加一些错误处理逻辑，比如重试机制、发送告警等
        }
    }


}
