package com.fzm.mall.server.front.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.asset.model.UserAssetRecord;
import com.fzm.mall.server.front.asset.model.po.qo.OrderQO;
import com.fzm.mall.server.front.asset.model.vo.order.OrderVO;
import com.fzm.mall.server.front.base.PageVO;
import com.fzm.mall.server.front.goods.model.Auction;
import com.fzm.mall.server.front.goods.model.GoodSpu;
import com.fzm.mall.server.front.goods.model.qo.UserAuctionQO;
import com.fzm.mall.server.front.goods.model.vo.PageVo;
import com.fzm.mall.server.front.goods.model.vo.UserAuctionVO;
import com.fzm.mall.server.front.goods.service.IAuctionService;
import com.fzm.mall.server.front.goods.service.IGoodSpuService;
import com.fzm.mall.server.front.order.model.BiddingRecord;
import com.fzm.mall.server.front.order.mapper.BiddingRecordMapper;
import com.fzm.mall.server.front.order.model.Deposit;
import com.fzm.mall.server.front.order.service.IBiddingRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.order.service.IDepositService;
import io.netty.util.internal.StringUtil;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户竞拍记录表 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-09-02
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BiddingRecordServiceImpl extends ServiceImpl<BiddingRecordMapper, BiddingRecord> implements IBiddingRecordService {
    private final BiddingRecordMapper biddingRecordMapper;

    public List<BiddingRecord> getBiddingRecordsByUid(QueryWrapper<BiddingRecord> q, String uid, Integer start, Integer size) {
        q.last("limit " + start + "," + size);
        List<BiddingRecord> list = list(q);

        return list;
    }

    public List<BiddingRecord> queryBiddingRecordsByAuctionId(String uid, String auctionId) {
        QueryWrapper<BiddingRecord> q = new QueryWrapper<>();
        q.eq("uid", uid);
        q.eq("auction_id", auctionId);
        q.orderByDesc("create_time");
        return list(q);
    }


    public PageVO<BiddingRecord> getBiddingRecordsByAuctionId(String auctionId, PageVo pageVo) {
        Integer page = pageVo.getPage();
        Integer size = pageVo.getSize();
        int start = (page - 1) * size;
        QueryWrapper<BiddingRecord> q = new QueryWrapper<>();
        q.eq("auction_id", auctionId);
        q.orderByDesc("create_time");
        int count = count(q);
        q.last("limit " + start + "," + size);
        List<BiddingRecord> list = list(q);
        PageVO<BiddingRecord> pageVO = new PageVO<>();
        pageVO.init(page, size, count, list);
        return pageVO;
    }

    /**
     * 分页查询用户拍卖订单
     */
    @Override
    public PageVO<BiddingRecord> listUserAuctionOrder(final UserAuctionQO qo) {
        List<BiddingRecord> list = biddingRecordMapper.listUserAuctionOrder(qo);
        Integer totalSize = biddingRecordMapper.countUserAuctionOrder(qo);
        PageVO<BiddingRecord> pageVO = new PageVO<>();
        pageVO.init(qo.getPage(), qo.getSize(), totalSize, list);
        return pageVO;
    }
}
