package com.gree.mall.manager.controller.activity;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.activity.CustomSecSpecBean;
import com.gree.mall.manager.bean.activity.SecKillActivityBean;
import com.gree.mall.manager.bean.activity.SecKillActivityStageBean;
import com.gree.mall.manager.bean.goods.CustomSecSpecBeanVO;
import com.gree.mall.manager.bean.goods.SecKillActivityStageBeanVO;
import com.gree.mall.manager.bean.order.OrderShareVO;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.activity.SecKillLogic;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Slf4j
@RestController
@Api(value = "秒杀商品列表", tags ={"秒杀商品列表"} )
@RequestMapping(value = "/sec-kill/goods/", produces = "application/json; charset=utf-8")
public class SecKillController {

    @Resource
    SecKillLogic secKillLogic;

    @ZfireList
    @PostMapping("/spec/list/pageV2")
    @ApiOperation(value = "秒杀商品列表V2")
    public ResponseHelper<IPage<CustomSecSpecBeanVO>> pageV2Spec(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<CustomSecSpecBeanVO> page = secKillLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<CustomSecSpecBeanVO>() {});
    }

    @PostMapping("/spec/list/pageV2Export")
    @ApiOperation(value = "秒杀商品列表V2导出")
    public void pageV2Export(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<CustomSecSpecBeanVO> page = secKillLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @ZfireList
    @PostMapping("/activity/list/pageV2")
    @ApiOperation(value = "秒杀活动列表V2")
    public ResponseHelper<IPage<SecKillActivityStageBeanVO>> pageV2Activity(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<SecKillActivityStageBeanVO> page = secKillLogic.pageV2Activity(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<SecKillActivityStageBeanVO>() {});
    }

    @PostMapping("/activity/list/pageV2Export")
    @ApiOperation(value = "秒杀活动列表V2导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<SecKillActivityStageBeanVO> page = secKillLogic.pageV2Activity(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }

    @GetMapping("/list/spec/page")
    @ApiOperation(value = "秒杀商品列表")
    public ResponseHelper<IPage<CustomSecSpecBean>> page(
            @ApiParam(value = "秒杀状态,0全部,1未开始,2进行中,3已结束,4已关闭") @RequestParam(required = false) Integer status,
            @ApiParam(value = "商品名称") @RequestParam(required = false) String keyword,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        return ResponseHelper.success(secKillLogic.pageSpec(status,keyword, pageNum, pageSize,request));
    }

    @GetMapping("/list/activity/page")
    @ApiOperation(value = "秒杀活动列表")
    public ResponseHelper pageActivity(
            @ApiParam(value = "秒杀状态,0全部,1未开始,2进行中,3已结束") @RequestParam(required = false) Integer status,
            @ApiParam(value = "商品名称") @RequestParam(required = false) String keyword,
            @ApiParam(value = "开始时间") @RequestParam(required = false) Date startTime,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        return ResponseHelper.success(secKillLogic.pageActivity(status,keyword,startTime,pageNum, pageSize,request));
    }


    @GetMapping("/list/spec/select")
    @ApiOperation(value = "秒杀选择活动商品列表")
    public ResponseHelper pageActivityGoods(
            @ApiParam(value = "商品名称") @RequestParam(required = false) String keyword,
            @ApiParam(value = "商品分类id") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws Exception {
        return ResponseHelper.success(secKillLogic.goodsSpecList(categoryId,keyword,pageNum, pageSize,request));
    }


    @PostMapping("/activity/add")
    @ApiOperation(value = "新增秒杀活动")
    public ResponseHelper add(
            @ApiParam(value = "object",required = true) @RequestBody SecKillActivityBean secKillActivityBean,
            HttpServletRequest request
    ) throws RemoteServiceException {
        secKillLogic.add(secKillActivityBean,request);
        return ResponseHelper.success();
    }

    @PostMapping("/activity/update")
    @ApiOperation(value = "编辑秒杀活动")
    public ResponseHelper update(
            HttpServletRequest request,
            @ApiParam(value = "object",required = true) @RequestBody SecKillActivityBean secKillActivityBean)
            throws RemoteServiceException {
        secKillLogic.update(request, secKillActivityBean);
        return ResponseHelper.success();
    }

    @GetMapping("/activity/detail")
    @ApiOperation(value = "秒杀活动详情")
    public ResponseHelper activityDetail(
            @ApiParam(value = "秒杀活动secKillId",required = true) @RequestParam String secKillId
    ) {
        return ResponseHelper.success(secKillLogic.detail(secKillId));
    }
    @PostMapping("/activity/close")
    @ApiOperation(value = "关闭秒杀活动")
    public ResponseHelper activityClose(
            @ApiParam(value = "秒杀活动secKillId",required = true) @RequestParam String secKillId
    ) {
        secKillLogic.updateStatus(secKillId,false);
        return ResponseHelper.success();
    }

    @PostMapping("/activity/open")
    @ApiOperation(value = "重新开启秒杀活动")
    public ResponseHelper activityOpen(
            @ApiParam(value = "秒杀活动secKillId",required = true) @RequestParam String secKillId
    ) {
        secKillLogic.updateStatus(secKillId,true);
        return ResponseHelper.success();
    }


    @PostMapping("/spec/delete")
    @ApiOperation(value = "删除秒杀商品")
    public ResponseHelper specDelete(
            @ApiParam(value = "secKillSpecId",required = true) @RequestParam String secKillSpecId
    ) throws RemoteServiceException {
        secKillLogic.specDelete(secKillSpecId);
        return ResponseHelper.success();
    }
    @PostMapping("/spec/close")
    @ApiOperation(value = "关闭秒杀商品")
    public ResponseHelper close(
            @ApiParam(value = "secKillSpecId",required = true) @RequestParam String secKillSpecId
    ) throws RemoteServiceException {
        secKillLogic.specClose(secKillSpecId);
        return ResponseHelper.success();
    }




//    @PostMapping("/updateStockNum")
//    @ApiOperation(value = "秒杀活动开始后增加或减少库存")
//    public ResponseHelper updateStockNum(
//            @ApiParam(value = "secKillSpecId",required = true) @RequestParam String secKillSpecId,
//            @ApiParam(value = "stockNum,增加为正数,减少为负",required = true) @RequestParam Integer stockNum
//    ) throws RemoteServiceException {
//        secKillLogic.updateStockNum(secKillSpecId,stockNum);
//        return ResponseHelper.success();
//    }
}
