package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.ucenter.constant.OrderConstant;
import com.fdkankan.ucenter.entity.Product;
import com.fdkankan.ucenter.entity.ProductOrder;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.httpClient.param.PayGoods;
import com.fdkankan.ucenter.httpClient.service.PayService;
import com.fdkankan.ucenter.httpClient.vo.PayOrderVo;
import com.fdkankan.ucenter.mapper.IProductOrderMapper;
import com.fdkankan.ucenter.service.IProductCooperationService;
import com.fdkankan.ucenter.service.IProductOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-03-13
 */
@Service
public class ProductOrderServiceImpl extends ServiceImpl<IProductOrderMapper, ProductOrder> implements IProductOrderService {

    @Autowired
    IProductService productService;
    @Autowired
    PayService payService;


    @Override
    public ProductOrder createOrder(Integer totalCount, String key, User user,Integer payType, Integer timeZone,Long cameraId,String lang,String sceneType) {
        ProductOrder productOrder = new ProductOrder();

        Product product = productService.getByKey(key);
        BigDecimal price = BigDecimal.ZERO;
        if(product != null){
            price = product.getPrice();
            productOrder.setProductId(product.getId());
        }
        productOrder.setAmount(price);
        productOrder.setPayType(payType);
        productOrder.setUserId(user.getId());
        productOrder.setCount(totalCount);
        productOrder.setTimeZoneOff(timeZone);
        productOrder.setCameraId(cameraId);
        productOrder.setLang(lang);
        productOrder.setSceneType(sceneType);

        PayGoods payGoods = new PayGoods(product.getName(),totalCount,4,1);
        PayOrderVo payOrderVo = payService.downOrder(price.multiply(new BigDecimal(totalCount)), "cooperationOrder",user.getUserName(),user.getNickName(), Arrays.asList(payGoods),null);
        if(payOrderVo == null){
            throw new BusinessException(OrderConstant.FAILURE_CODE_8005,OrderConstant.FAILURE_MSG_8005);
        }
        productOrder.setOrderSn(payOrderVo.getOrderSn());
        this.save(productOrder);

        return productOrder;
    }

    @Autowired
    IProductCooperationService productCooperationService;

    @Override
    public void handleOrder(String orderSn, String tradeNo, String openId, Integer payType) {
        LambdaUpdateWrapper<ProductOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ProductOrder::getOrderSn,orderSn);
        wrapper.set(ProductOrder::getTradeTime,new Date());
        wrapper.set(ProductOrder::getNumber,tradeNo);
        wrapper.set(ProductOrder::getPayStatus,1);
        wrapper.set(ProductOrder::getPayType,payType);
        this.update(wrapper);

        productCooperationService.paySuccess(orderSn);
    }

    @Override
    public ProductOrder getByOrderSn(String orderSn) {
        LambdaQueryWrapper<ProductOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductOrder::getOrderSn,orderSn);
        List<ProductOrder> list = this.list(wrapper);
        if(list.isEmpty() ){
            return null;
        }
        return list.get(0);
    }
}
