package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.util.SecurityUtil;
import com.fdkankan.ucenter.common.PageInfo;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.entity.VirtualOrder;
import com.fdkankan.ucenter.mapper.IVirtualOrderMapper;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.service.IVirtualOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.vo.request.IncrementOrderParam;
import com.fdkankan.ucenter.vo.request.VirtualOrderParam;
import com.fdkankan.ucenter.vo.response.VirtualOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 充值订单表（双目相机点数充值、消费） 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-07-13
 */
@Service
public class VirtualOrderServiceImpl extends ServiceImpl<IVirtualOrderMapper, VirtualOrder> implements IVirtualOrderService {

    @Autowired
    IUserService userService;

    @Override
    public PageInfo pageList(VirtualOrderParam param) {
        User user = userService.getByUserName(param.getUserName());
        param.setUserId(user.getId());
        Page<VirtualOrderVo> page = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
        return PageInfo.PageInfo(page);
    }

    @Override
    public VirtualOrder getByOrderSn(String orderSn) {
        QueryWrapper<VirtualOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VirtualOrder::getOrderSn,orderSn);
        List<VirtualOrder> list = this.list(queryWrapper);
        if(list == null || list.size()<=0){
            return null;
        }
        return list.get(0);
    }

    @Override
    public VirtualOrder getByOrderSnNoPay(String orderSn) {
        QueryWrapper<VirtualOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(VirtualOrder::getOrderSn,orderSn);
        queryWrapper.lambda().ne(VirtualOrder::getPayStatus,1);
        List<VirtualOrder> list = this.list(queryWrapper);
        if(list == null || list.size()<=0){
            return null;
        }
        return list.get(0);
    }

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

    @Override
    public List<VirtualOrder> getByCameraId(Long cameraId) {
        LambdaQueryWrapper<VirtualOrder> vWr = new LambdaQueryWrapper<>();
        vWr.eq(VirtualOrder::getCameraId,cameraId);
        return this.list(vWr);
    }
}
