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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.bean.ExcelData;
import com.gree.mall.manager.bean.activity.luckdraw.PromotionLuckDrawBean;
import com.gree.mall.manager.bean.activity.luckdraw.PromotionLuckDrawCodeBean;
import com.gree.mall.manager.bean.activity.luckdraw.PromotionLuckDrawList;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.activity.PromotionLuckDrawLogic;
import com.gree.mall.manager.utils.excel.ExcelUtils;
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 javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Slf4j
@RestController
@Api(value = "抽奖活动API", tags ={"抽奖活动API"} )
@RequestMapping(value = "/promotion/luck/draw", produces = "application/json; charset=utf-8")
public class PromotionLuckDrawController {

    @Autowired
    PromotionLuckDrawLogic promotionLuckDrawLogic;

    @GetMapping("/list")
    @ApiOperation("抽奖活动列表")
    public ResponseHelper<IPage<PromotionLuckDrawList>> list(
         @ApiParam(value = "活动名称",required = false) @RequestParam(required = false) String name,
         @ApiParam(value = "状态",required = false) @RequestParam(required = false) Boolean status,
         @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
         @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize
    ){
        IPage<PromotionLuckDrawList> list = promotionLuckDrawLogic.list(name, status, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @PostMapping("/add")
    @ApiOperation("新增抽奖活动")
    public ResponseHelper add( @Valid @RequestBody PromotionLuckDrawBean promotionLuckDrawBean){
        promotionLuckDrawLogic.add(promotionLuckDrawBean);
        return ResponseHelper.success();
    }

    @GetMapping("/detail")
    @ApiOperation(value = "抽奖活动详情")
    public ResponseHelper<PromotionLuckDrawBean> detail(
            @ApiParam(value = "抽奖活动id",required = true) @RequestParam(required = true) String promotionLuckDrawId
    ) throws Exception {
        PromotionLuckDrawBean detail = promotionLuckDrawLogic.detail(promotionLuckDrawId);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/updateStatus")
    @ApiOperation("修改状态")
    public ResponseHelper updateStatus(
            @ApiParam(value = "抽奖活动id",required = true) @RequestParam(required = true) String promotionLuckDrawId,
            @ApiParam(value = "状态true=进行中 false=已结束",required = true) @RequestParam(required = true) Boolean status
    ){
        promotionLuckDrawLogic.updateStatus(promotionLuckDrawId,status);
        return ResponseHelper.success();
    }


    @GetMapping("/exchange/list")
    @ApiOperation("兑换记录")
    public ResponseHelper<IPage<PromotionLuckDrawCodeBean>> exchangeList(
            @ApiParam(value = "活动id",required = false) @RequestParam(required = false) String promotionLuckDrawId,
            @ApiParam(value = "优惠券id",required = false) @RequestParam(required = false) String promotionLuckDrawCouponId,
            @ApiParam(value = "兑换码",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "昵称",required = false) @RequestParam(required = false) String name,
            @ApiParam(value = "电话",required = false) @RequestParam(required = false) String mobile,
            @ApiParam(value = "兑换开始时间",required = false) @RequestParam(required = false) String exchangeStartTime,
            @ApiParam(value = "兑换结束时间",required = false) @RequestParam(required = false) String exchangeEndTime,
            @ApiParam(value = "使用开始时间",required = false) @RequestParam(required = false) String useStartTime,
            @ApiParam(value = "使用结束时间",required = false) @RequestParam(required = false) String useEndTime,
            @ApiParam(value = "订单id",required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "优惠券名称",required = false) @RequestParam(required = false) String couponName,
            @ApiParam(value = "状态 0=未兑换 1=已兑换未使用 2=已使用",required = false) @RequestParam(required = false) Integer status2,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize
    ){
        IPage<PromotionLuckDrawCodeBean> page = promotionLuckDrawLogic
                .exchangeList(promotionLuckDrawId,promotionLuckDrawCouponId, code, name, mobile, exchangeStartTime, exchangeEndTime, useStartTime,
                        useEndTime, orderId, couponName, status2, pageNum, pageSize);
        return ResponseHelper.success(page);
    }

    @GetMapping("/exchange/export")
    @ApiOperation("兑换记录")
    public void exportExchange(
            @ApiParam(value = "活动id",required = false) @RequestParam(required = false) String promotionLuckDrawId,
            @ApiParam(value = "优惠券id",required = false) @RequestParam(required = false) String promotionLuckDrawCouponId,
            @ApiParam(value = "兑换码",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "昵称",required = false) @RequestParam(required = false) String name,
            @ApiParam(value = "电话",required = false) @RequestParam(required = false) String mobile,
            @ApiParam(value = "兑换开始时间",required = false) @RequestParam(required = false) String exchangeStartTime,
            @ApiParam(value = "兑换结束时间",required = false) @RequestParam(required = false) String exchangeEndTime,
            @ApiParam(value = "使用开始时间",required = false) @RequestParam(required = false) String useStartTime,
            @ApiParam(value = "使用结束时间",required = false) @RequestParam(required = false) String useEndTime,
            @ApiParam(value = "订单id",required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "优惠券名称",required = false) @RequestParam(required = false) String couponName,
            @ApiParam(value = "状态 0=未兑换 1=已兑换未使用 2=已使用",required = false) @RequestParam(required = false) Integer status2,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<PromotionLuckDrawCodeBean> page = promotionLuckDrawLogic
                .exchangeList(promotionLuckDrawId,promotionLuckDrawCouponId, code, name, mobile, exchangeStartTime, exchangeEndTime, useStartTime,
                        useEndTime, orderId, couponName, status2, 1, -1);
        ExcelData excelData = promotionLuckDrawLogic.exportData(page.getRecords());
        ExcelUtils.exportExcel(request,response,"兑换记录.xlsx",excelData);
    }

    @GetMapping("/code/export")
    @ApiOperation("导出兑换码")
    public void exportCode(
            @RequestParam(required = false) String promotionLuckDrawId,
            @RequestParam(required = false) String promotionLuckDrawCouponId,
                           HttpServletRequest request,
                           HttpServletResponse response) throws Exception {
        ExcelData excelData = promotionLuckDrawLogic.exportCode(promotionLuckDrawId,promotionLuckDrawCouponId);
        ExcelUtils.exportExcel(request,response,"兑换码.xlsx",excelData);
    }
}
