package com.fdkankan.platform.user.strategy.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fdkankan.platform.goods.service.ICameraDetailService;
import com.fdkankan.platform.order.entity.VirtualOrder;
import com.fdkankan.platform.order.service.IVirtualOrderService;
import com.fdkankan.platform.user.strategy.OrderStrategy;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Log4j2
@Component
public class RechargeOrderImpl implements OrderStrategy {
    @Autowired
    private IVirtualOrderService virtualOrderService;
    @Autowired
    private ICameraDetailService cameraDetailService;
    @Autowired
    private ObjectMapper mapper;

    @Override
    public void handleOrder(String orderSn, String tradeNo, String openId, int paymentTypeName, Long spaceId) throws Exception {
        VirtualOrder responseVirtualOrder = virtualOrderService.getByOrderSn(orderSn);
        if (responseVirtualOrder == null) {
            log.error("找不到订单：" + orderSn);
            throw new Exception("找不到订单，out_trade_no错误");
        }

        // 更新充值订单
        boolean result = cameraDetailService.updateCameraBalance(responseVirtualOrder.getCameraId(), 0, responseVirtualOrder.getPoints());
        if (!result){
            log.error("更新相机余额错误");
            throw new Exception("更新相机余额错误");
        }
        boolean result2 = virtualOrderService.paySuccessVirtualOrder(orderSn, tradeNo, paymentTypeName);
        if (!result2){
            log.error("更新充值订单失败");
            throw new Exception("更新充值订单失败");
        }
    }

    @Override
    public String getType() {
        return "recharge";
    }
}
