/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.listener;

import com.yami.shop.bean.event.ReceiptOrderEvent;
import com.yami.shop.bean.order.ConfirmOrderOrder;
import com.yami.shop.dao.ShopWalletMapper;
import com.yami.shop.service.OrderRefundService;
import com.yami.shop.service.OrderService;
import com.yami.shop.service.ShopWalletLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * 确认收货的事件
 */
@Slf4j
@Component("defaultReceiptOrderListener")
@AllArgsConstructor
public class ReceiptOrderListener {

    private final ShopWalletMapper shopWalletMapper;

    private final ShopWalletLogService shopWalletLogService;

    private final MapperFacade mapperFacade;

    private final OrderRefundService orderRefundService;

    private final OrderService orderService;

    @EventListener(ReceiptOrderEvent.class)
    @Order(ConfirmOrderOrder.DISTRIBUTION)
    public void defaultReceiptOrderEvent(ReceiptOrderEvent event) {
//
//        com.yami.shop.bean.model.Order order = event.getOrder();
//        //如果是积分订单无需操作
//        if(Objects.equals(order.getOrderType() , OrderType.SCORE.value())){
//            return;
//        }
//        Long shopId = order.getShopId();
//        System.out.println(shopId);
//        ShopWallet shopWallet = shopWalletMapper.selectOne(new LambdaQueryWrapper<ShopWallet>().eq(ShopWallet::getShopId, shopId));
//        System.out.println(shopWallet);
//        ShopWallet newShopWallet = mapperFacade.map(shopWallet, ShopWallet.class);
//
//        System.out.println(newShopWallet);
//        newShopWallet.setUpdateTime(new Date());
//
//        // 退款成功金额
//        double refundSuccessAmount = orderRefundService.sumRefundSuccessAmountByOrderId(order.getOrderId());
//
//        // 分销占用金额
//        double distributionAmount = orderService.sumTotalDistributionAmountByOrderItem(order.getOrderItems());
//        // 平台占用金额
//        double platformAmount = orderRefundService.sumRefundSuccessPlatformAmountByOrderId(order.getOrderId());
//
//
//        // 结算金额 = (用户支付金额 + 平台补贴金额) - 退款成功金额 - 分销占用金额
//        double shopAmount = Arith.add(order.getActualTotal(), order.getPlatformAmount());
//        double settledAmount = Arith.sub(Arith.sub(Arith.sub(shopAmount, refundSuccessAmount), distributionAmount),platformAmount);
//        // 减去未结算金额
//        newShopWallet.setUnsettledAmount(Arith.sub(shopWallet.getUnsettledAmount(),settledAmount));
//        // 增加结算金额
//        newShopWallet.setSettledAmount(Arith.add(shopWallet.getSettledAmount(),settledAmount));
//        // 增加总结算金额
//        newShopWallet.setTotalSettledAmount(Arith.add(shopWallet.getTotalSettledAmount(),settledAmount));
//        // 插入收入记录
//        //'海博订单状态：0-待支付,
//        // 20-订单已接单(待拣货),
//        // 30-订单待配送（拣货完成/自提类订单为待自提）,
//        // 40-订单配送中 ,
//        // 50-订单取消待审核,
//        // 60-订单已取消,
//        // 70-订单已送达,
//        // 80-订单已完成'
//        if (Objects.equals(order.getHbOrderStatus(), OrderStatus.CLOSE.value()) && Objects.equals(order.getCloseType(), OrderCloseType.REFUND.value())) {
//            shopWalletLogService.saveShopWalletLog(shopWallet,newShopWallet, ShopWalletChangeReason.ORDER_REFUND,order.getOrderNumber());
//        } else {
//            shopWalletLogService.saveShopWalletLog(shopWallet,newShopWallet, ShopWalletChangeReason.ORDER_SUCCESS,order.getOrderNumber());
//        }
//        // 使用乐观锁进行更新
//        if (shopWalletMapper.updateById(newShopWallet)!=1) {
//            log.error("更新店铺钱包失败，无法确认收货，稍后再试");
//            // 不要让用户看到更新店铺钱包失败的信息
//            throw new GlobalException("服务器繁忙，请稍后再试");
//        }
    }

}
