package com.fdkankan.platform.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.util.NumberUtils;
import com.fdkankan.platform.order.entity.CommerceOrder;
import com.fdkankan.platform.order.mapper.ICommerceOrderMapper;
import com.fdkankan.platform.order.service.ICommerceOrderService;
import com.fdkankan.platform.order.vo.ResponseCommerceOrder;
import com.fdkankan.platform.user.request.RequestCommerceOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 商业订单表（八目相机） 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Service
public class CommerceOrderServiceImpl extends ServiceImpl<ICommerceOrderMapper, CommerceOrder> implements ICommerceOrderService {

    @Override
    public ResponseCommerceOrder saveByOrderParam(Long userId, RequestCommerceOrder order, BigDecimal price) {
        BigDecimal total = price.multiply(new BigDecimal(order.getCount()));
        CommerceOrder commerceOrderEntity = new CommerceOrder();
        commerceOrderEntity.setOrderSn(NumberUtils.getOrderSn());
        commerceOrderEntity.setAmount(total);
        commerceOrderEntity.setPayType(order.getPayType());
        commerceOrderEntity.setPayStatus(-1);
        commerceOrderEntity.setUserId(userId);
        commerceOrderEntity.setCommerceType(order.getCommerceType());
        commerceOrderEntity.setCount(order.getCount());
        commerceOrderEntity.setCameraId(order.getCameraId());
        commerceOrderEntity.setAbroad(order.getAbroad());
        this.save(commerceOrderEntity);
        ResponseCommerceOrder commerceOrder = new ResponseCommerceOrder();
        BeanUtils.copyProperties(commerceOrderEntity, commerceOrder);
        return commerceOrder;
    }

    @Override
    public CommerceOrder getByOrderSn(String orderSn) {
        LambdaQueryWrapper<CommerceOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommerceOrder::getTbStatus, TbStatus.VALID.code());
        queryWrapper.eq(CommerceOrder::getOrderSn,orderSn);
        List<CommerceOrder> list = this.list(queryWrapper);
        if(list == null || list.size() <= 0){
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean paySuccessCommerceOrder(String orderSn, String tradeNo, int paymentTypeName) {
        LambdaUpdateWrapper<CommerceOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(CommerceOrder::getPayType,paymentTypeName)
                .set(CommerceOrder::getNumber,tradeNo)
                .set(CommerceOrder::getPayStatus,1)
                .set(CommerceOrder::getTradeTime,new Date())
                .eq(CommerceOrder::getTbStatus,TbStatus.VALID.code())
                .eq(CommerceOrder::getOrderSn,orderSn);
        return this.update(updateWrapper);
    }
}
