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

import com.fdkankan.platform.order.entity.Order;
import com.fdkankan.platform.order.entity.TradeLog;
import com.fdkankan.platform.order.service.IOrderService;
import com.fdkankan.platform.order.service.ITradeLogService;
import com.fdkankan.platform.user.strategy.OrderItemStrategy;
import com.fdkankan.platform.user.vo.ResponseOrderItem;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 购买八月相机升级专业会员
 */
@Log4j2
@Component
public class Camera8EyesOrderImpl implements OrderItemStrategy {

    @Autowired
    private ITradeLogService tradeLogService;
    @Autowired
    private IOrderService orderService;

    @Override
    public void handleOrderItem(Long userId, ResponseOrderItem orderItem) throws Exception {
        log.warn("八目相机购买支付成功，生成交易记录");
        Order orderEntity = orderService.getById(orderItem.getOrderId());
        TradeLog tradeLogEntity = tradeLogService.getByOrderSn(orderEntity.getOrderSn());
        if (tradeLogEntity == null){
            tradeLogEntity = new TradeLog();
            tradeLogEntity.setOrderSn(orderEntity.getOrderSn());
            tradeLogEntity.setAmount(orderItem.getGoodsPrice().multiply(new BigDecimal(orderItem.getGoodsCount())));
            tradeLogEntity.setNumber(orderEntity.getTradeNum());
//            tradeLogEntity.setTradeTime(new Date());
            tradeLogEntity.setPayType(1);
            tradeLogEntity.setPayStatus(1);
            tradeLogEntity.setOpenid(orderItem.getOpenid());
            tradeLogEntity.setUserId(orderEntity.getUserId());
            tradeLogEntity.setAbroad(orderEntity.getAbroad());
            tradeLogService.save(tradeLogEntity);
        }else{
            tradeLogEntity.setAmount(orderItem.getGoodsPrice().multiply(new BigDecimal(orderItem.getGoodsCount())));
            tradeLogEntity.setNumber(orderEntity.getTradeNum());
//            tradeLogEntity.setTradeTime(new Date());
            tradeLogEntity.setOpenid(orderItem.getOpenid());
            tradeLogEntity.setTradeTime(new Date());
            tradeLogService.updateById(tradeLogEntity);
        }
    }

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