package com.fdkankan.ucenter.pay.strategy.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fdkankan.ucenter.common.constants.NacosProperty;
import com.fdkankan.ucenter.entity.*;
import com.fdkankan.ucenter.pay.strategy.OrderStrategy;
import com.fdkankan.ucenter.service.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Log4j2
@Component
public class DownloadOrderImpl implements OrderStrategy {
    @Autowired
    private IDownloadOrderService downloadOrderService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IAgentNewLogService agentNewLogService;
    @Autowired
    ICameraTypeService cameraTypeService;
    @Override
    public void handleOrder(String orderSn, String tradeNo, String openId, int paymentTypeName, Long spaceId) throws Exception {
        DownloadOrder downloadOrder = downloadOrderService.getByOrderSnNoPay(orderSn);
        if (downloadOrder == null) {
            log.error("找不到订单：" + orderSn);
            throw new Exception("找不到订单，out_trade_no错误");
        }
        // 更新场景下载订单
        boolean result = downloadOrderService.paySuccessDownloadOrder(orderSn, tradeNo, paymentTypeName);
        if (!result){
            log.error("更新增值权益订单失败");
            throw new Exception("更新增值权益订单失败");
        }
        agentNewLogService.addByOrder(downloadOrder);

        //更新用户下载次数
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getId,downloadOrder.getUserId());
        wrapper.setSql("download_num_total = download_num_total + " + downloadOrder.getCount());
        userService.update(wrapper);
    }

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