package com.gree.mall.miniapp.controller.seckill;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.annotation.ApiNotAuth;
import com.gree.mall.miniapp.bean.PayDetail;
import com.gree.mall.miniapp.bean.order.OrderBuyBean;
import com.gree.mall.miniapp.bean.seckill.SecKillExtends;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.seckill.SecKillLogic;
import com.gree.mall.miniapp.plus.entity.SecKillActivity;
import com.gree.mall.miniapp.plus.entity.SecKillActivitySpec;
import com.gree.mall.miniapp.utils.IpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@Api(value = "小程序秒杀", tags ={"小程序秒杀"} )
@RequestMapping(value = "/goods/sec/", produces = "application/json; charset=utf-8")
public class SecKillController {

    @Autowired
    private SecKillLogic secKillLogic;


    @ApiNotAuth
    @GetMapping("/time")
    @ApiOperation(value = "秒杀时间段")
    public ResponseHelper<List<SecKillExtends>> time(HttpServletRequest request) throws Exception{

        List<SecKillExtends> secKillExtends = secKillLogic.queryTimeList(request);
        return ResponseHelper.success(secKillExtends);
    }


    @ApiNotAuth
    @GetMapping("/goods/list")
    @ApiOperation(value = "秒杀商品列表")
    public ResponseHelper<Page<SecKillActivitySpec>> page(
            @ApiParam(value = "秒杀id",required = true) @RequestParam String secKillId,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception{

        IPage<SecKillActivitySpec> page = secKillLogic.page(secKillId,pageNum,pageSize);
        return ResponseHelper.success(page);
    }


    @PostMapping("/kill/start")
    @ApiOperation(value = "秒杀开启,秒杀前先判断秒杀是否开启,开启:返回一个token,秒杀时需要带上,未开启,返回空字串")
    public ResponseHelper<String> secKillStart(
            @ApiParam(value = "秒杀id",required = true) @RequestParam String secKillId
    ) throws Exception{
        return ResponseHelper.success(secKillLogic.secKillStart(secKillId));
    }


//    @PostMapping("/buy")
//    @ApiOperation("秒杀购买")
//    public ResponseHelper buy(
//            @RequestBody OrderBuyBean orderBuyBean,
//            HttpServletRequest request
//    ) throws Exception {
//        String ip = IpUtil.getIpAddr(request);
//        return ResponseHelper.success(secKillLogic.buy(orderBuyBean,ip));
//    }

}
