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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.bean.ExcelData;
import com.gree.mall.manager.bean.coupon.CouponDetailBean;
import com.gree.mall.manager.bean.exchange.GiftExchangeCodeBean;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.exchange.ExchangeLogic;
import com.gree.mall.manager.plus.entity.GiftExchangeCode;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@Api(value = "兑换码API", tags ={"兑换码API"} )
@RequestMapping(value = "/exchange/code", produces = "application/json; charset=utf-8")
public class ExchangeCodeController {

    @Autowired
    ExchangeLogic exchangeLogic;

    @GetMapping("/count/list")
    @ApiOperation(value = "统计列表")
    public ResponseHelper<IPage<GiftExchangeCodeBean>> countList(
            @ApiParam(value = "昵称/电话",required = false) @RequestParam(required = false) String keyword,
            @ApiParam(value = "网点主键id",required = false) @RequestParam(required = false) String adminWebsitId,
            @ApiParam(value = "pageNo",required = false) @RequestParam(required = false) Integer pageNo,
            @ApiParam(value = "pageSize",required = false) @RequestParam(required = false) Integer pageSize,
            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<GiftExchangeCodeBean> giftExchangeCodeBeanIPage = exchangeLogic.queryExchangeCodeCountList(keyword, adminWebsitId, pageNo, pageSize, request);
        return ResponseHelper.success(giftExchangeCodeBeanIPage);
    }

    @GetMapping("/count/export")
    @ApiOperation(value = "统计列表导出")
    public void exportData(
            @ApiParam(value = "昵称/电话",required = false) @RequestParam(required = false) String keyword,
            @ApiParam(value = "网点主键id",required = false) @RequestParam(required = false) String adminWebsitId,
            HttpServletRequest request,HttpServletResponse response
    ) throws Exception {
        IPage<GiftExchangeCodeBean> giftExchangeCodeBeanIPage = exchangeLogic.queryExchangeCodeCountList(keyword, adminWebsitId, 1, -1, request);
        ExcelData excelData = exchangeLogic.exportCountData(giftExchangeCodeBeanIPage.getRecords());
        ExcelUtils.exportExcel(request,response,"record.xlsx",excelData);
    }


    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<GiftExchangeCode>> list(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "用户id",required = true) @RequestParam(required = true) String userId,
            @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "状态 1=已使用 0=未使用",required = false) @RequestParam(required = false) Integer status,
            @ApiParam(value = "pageNo",required = false) @RequestParam(required = false) Integer pageNo,
            @ApiParam(value = "pageSize",required = false) @RequestParam(required = false) Integer pageSize,
            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<GiftExchangeCode> list = exchangeLogic.list(id,userId, orderId, status, pageNo, pageSize, request);
        return ResponseHelper.success(list);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public void exportData(
            @ApiParam(value = "id",required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "用户id",required = true) @RequestParam(required = true) String userId,
            @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String orderId,
            @ApiParam(value = "状态 1=已使用 0=未使用",required = false) @RequestParam(required = false) Integer status,
            HttpServletRequest request,HttpServletResponse response
    ) throws Exception {
        IPage<GiftExchangeCode> list = exchangeLogic.list(id,userId, orderId, status, 1, -1, request);
        ExcelData excelData = exchangeLogic.exportData(list.getRecords());
        ExcelUtils.exportExcel(request,response,"record.xlsx",excelData);
    }




}
