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.ucenter.common.PageInfo;
import com.fdkankan.ucenter.entity.ExpansionOrder;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.mapper.IExpansionOrderMapper;
import com.fdkankan.ucenter.service.IExpansionOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IUserService;
import com.fdkankan.ucenter.vo.request.VirtualOrderParam;
import com.fdkankan.ucenter.vo.response.ExpansionOrderVo;
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-14
 */
@Service
public class ExpansionOrderServiceImpl extends ServiceImpl<IExpansionOrderMapper, ExpansionOrder> implements IExpansionOrderService {

    @Autowired
    IUserService userService;


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

    @Override
    public Long getCount(Long userId) {
        return this.getBaseMapper().getCount(userId);
    }

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

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

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

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