package com.yc.ship.module.trade.service.order.handler;

import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.dataobject.refund.RefundDO;
import com.yc.ship.module.trade.service.order.TradeOrderRepositoryService;
import com.yc.ship.module.trade.service.order.bo.TradeOrderBO;
import com.yc.ship.module.trade.service.refund.TradeRefundRepositoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * &#064;Description ：优惠券处理
 * &#064;Author ：qsl
 * &#064;Date ：2025/9/4 15:23
 */
@Component
@Order(TradeOrderHandler.COUPON)
@Slf4j
public class CouponHandler implements TradeOrderHandler{

    @Resource
    private TradeOrderRepositoryService tradeOrderRepositoryService;
    @Resource
    private TradeRefundRepositoryService tradeRefundRepositoryService;

    @Override
    public void beforeOrderCreate(TradeOrderBO order) {
//        if(order.getExtraInfo().getCouponCodeId() == null){
//            return;
//        }
//        //只处理一个
//        TradeOrderDetailBO tradeOrderDetailBO = order.getTradeOrderDetailBOList().get(0);
//        List<String> specTypeList = tradeOrderDetailBO.getTradeDetailDOList().stream().map(item -> item.getSpecType()).distinct().collect(Collectors.toList());
//        CouponCheckReqDto reqDto = new CouponCheckReqDto();
//        reqDto.setOrderId(order.getTradeOrderDO().getId());
//        reqDto.setAmount(order.getTradeOrderDO().getAmount());
//        reqDto.setProductId(tradeOrderDetailBO.getTradeDetailDOList().get(0).getPackProductId());
//        reqDto.setCouponCodeId(order.getExtraInfo().getCouponCodeId());
//        reqDto.setMemberId(Long.parseLong(order.getTradeOrderDO().getMemberId()));
//        reqDto.setPayAmount(order.getTradeOrderDO().getPayAmount());
//        reqDto.setSkuIds(CollUtil.join(specTypeList,","));
//        reqDto.setTravelDate(DateUtil.toLocalDateTime(order.getTradeOrderDO().getTravelDate()));
//        reqDto.setCategoryId(tradeOrderDetailBO.getProductSpuRespDTO().getCategoryId());
//        //验证优惠券
//        couponApi.checkCoupon(reqDto).getCheckedData();
    }

    @Override
    public void afterOrderCreate(TradeOrderBO order) {
        try {
            if(order.getExtraInfo().getCouponCodeId() == null){
                return;
            }
            //优惠券使用
//            TradeOrderDetailBO tradeOrderDetailBO = order.getTradeOrderDetailBOList().get(0);
//            CouponUseReqDto reqDto = new CouponUseReqDto();
//            reqDto.setOrderId(order.getTradeOrderDO().getId());
//            reqDto.setAmount(order.getTradeOrderDO().getAmount());
//            reqDto.setProductId(tradeOrderDetailBO.getTradeDetailDOList().get(0).getPackProductId());
//            reqDto.setCouponCodeId(order.getExtraInfo().getCouponCodeId());
//            reqDto.setMemberId(Long.parseLong(order.getTradeOrderDO().getMemberId()));
//            reqDto.setPayAmount(order.getTradeOrderDO().getPayAmount());
//            couponApi.useCoupon(reqDto).getCheckedData();
//            //绑定关系
//            TradeOrderBindDO tradeOrderBindDO = new TradeOrderBindDO();
//            tradeOrderBindDO.setId(IdWorker.getId());
//            tradeOrderBindDO.setOrderId(order.getTradeOrderDO().getId());
//            tradeOrderBindDO.setBindId(order.getExtraInfo().getCouponCodeId());
//            tradeOrderBindDO.setType(TradeOrderBindEnum.COUPON.getType());
//            BigDecimal discountAmount = order.getTradeOrderDO().getAmount().subtract(order.getTradeOrderDO().getPayAmount());
//            tradeOrderBindDO.setCustom(discountAmount.toPlainString());
//            tradeOrderRepositoryService.saveBindOrder(tradeOrderBindDO);
        }catch (Exception e){
            log.error("优惠券处理异常",e);
        }
    }


    @Override
    public void afterCancelOrder(TradeOrderDO tradeOrderDO) {
        try {
            //退优惠券
//            couponApi.refundCoupon(tradeOrderDO.getId());
        }catch (Exception e){
            log.error("优惠券处理异常",e);
        }
    }

    @Override
    public void afterRefundOrder(Long refundId) {
        try {
            //所有都退了才退,退全款
            RefundDO refundDO = tradeRefundRepositoryService.getById(refundId);
            TradeOrderDO tradeOrderDO = tradeOrderRepositoryService.getById(refundDO.getOrderId());
            if(tradeOrderDO.getPayAmount().compareTo(refundDO.getRefundAmount())==0){
//                couponApi.refundCoupon(tradeOrderDO.getId());
            }
        }catch (Exception e){
            log.error("优惠券处理异常",e);
        }

    }
}
