package com.fzm.mall.server.front.goods.controller;


import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.front.base.PageVO;
import com.fzm.mall.server.front.base.ResponseVO;
import com.fzm.mall.server.front.constant.MallResponseEnum;
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.model.Deposit;
import com.fzm.mall.server.front.order.model.vo.DeliveryVO;
import com.fzm.mall.server.front.order.service.IBiddingRecordService;
import com.fzm.mall.server.front.order.service.IDepositService;
import com.fzm.mall.server.front.user.model.Header;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

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

/**
 * <p>
 * 商品拍卖 前端控制器
 * </p>
 *
 * @author fzm
 * @since 2021-09-02
 */
@Api(tags = "拍卖商品接口", description = "拍卖商品信息等")
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping(value = "/mall/auction", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class AuctionController {
    private final IAuctionService auctionService;
    private final IDepositService depositService;
    private final IGoodSpuService goodSpuService;
    private final IBiddingRecordService biddingRecordService;

    @ApiOperation(value = "我的拍卖")
    @PostMapping("/getBiddingRecordsByUid")
    public ResponseVO<PageVO<UserAuctionVO>> getBiddingRecordsByUid(@ApiIgnore @RequestAttribute Header header, @RequestBody UserAuctionQO qo) {
        String uid = header.getUid();
        qo.setUid(uid);
        PageVO<UserAuctionVO> merchantCouponSettles = auctionService.getMyAuction(qo);
        return new ResponseVO(MallResponseEnum.SUCCESS, merchantCouponSettles);
    }

    @ApiOperation(value = "拍卖详情")
    @PostMapping("/getByAuctionId")
    public ResponseVO<UserAuctionVO> getByAuctionId(@RequestBody JSONObject json, @ApiIgnore @RequestAttribute Header header) {
        String uid = header.getUid();
        String auctionId = json.getString("auctionId");
        Auction auction = auctionService.getByAuctionId(auctionId);
        UserAuctionVO userAuctionVO = new UserAuctionVO();
        BeanUtils.copyProperties(auction,userAuctionVO);
        Deposit deposit = depositService.getDepositByUid(uid, auctionId);
        userAuctionVO.setIsDeposit(deposit.getStatus());
        String goodsId = auction.getGoodsId();
        GoodSpu spu = goodSpuService.goodSpu(goodsId);
        userAuctionVO.setThumb(spu.getThumb());
        userAuctionVO.setGoodsName(spu.getName());
        userAuctionVO.setGoodsId(goodsId);
        //取当前最大值
        List<BiddingRecord> biddingRecords = biddingRecordService.queryBiddingRecordsByAuctionId(uid, auctionId);
        BiddingRecord biddingRecord = biddingRecords.stream().max(Comparator.comparing(BiddingRecord -> BiddingRecord.getCurrentPrice())).get();
        BigDecimal currentPrice = BigDecimal.ZERO;
        if (biddingRecord != null) {
            currentPrice = biddingRecord.getCurrentPrice();
        }
        userAuctionVO.setCurrentPrice(currentPrice);
        return new ResponseVO<>(MallResponseEnum.SUCCESS, userAuctionVO);
    }

    @ApiOperation(value = "拍卖商品列表")
    @PostMapping("/goodsAuctionList")
    public ResponseVO<PageVO<UserAuctionVO>> goodsAuctionList(@RequestBody JSONObject json, @RequestBody PageVo pageVo) {
        String status = json.getString("status");//1.进行中 2.已结束 3.未开始
        PageVO<UserAuctionVO> merchantCouponSettles = auctionService.goodsAuctionList(status, pageVo);
        return new ResponseVO(MallResponseEnum.SUCCESS, merchantCouponSettles);
    }

}
