package com.ylx.giftCard.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.common.exception.ServiceException;
import com.ylx.common.utils.poi.ExcelUtil;
import com.ylx.giftCard.domain.dto.GiftCardOrderQueryDTO;
import com.ylx.giftCard.domain.vo.GiftCardOrderExportVO;
import com.ylx.giftCard.domain.vo.GiftCardOrderPageVO;
import com.ylx.giftCard.service.IGiftCardOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/gift/card/order")
@Api(tags = {"购物卡订单"})
@Slf4j
public class GiftCardOrderController {

    @Resource
    private IGiftCardOrderService giftCardOrderService;

    /**
     * 分页查询购物卡订单
     *
     * @param page 分页参数
     * @param dto 查询条件
     * @return 购物卡订单分页列表
     */
    @GetMapping("/page")
    @ApiOperation("分页查询购物卡订单")
    public R<Page<GiftCardOrderPageVO>> page(Page<GiftCardOrderPageVO> page, GiftCardOrderQueryDTO dto) {
        try {
            return R.ok(giftCardOrderService.getAdminGiftCardOrderPage(page, dto));
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("分页查询购物卡订单异常", e);
            return R.fail("分页查询购物卡订单失败");
        }
    }

    /**
     * 导出购物卡订单
     *
     * @param response 响应对象
     * @param dto 查询条件
     */
    @PostMapping("/export")
    @ApiOperation("导出购物卡订单")
    @Log(title = "导出购物卡订单", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, GiftCardOrderQueryDTO dto) {
        try {
            List<GiftCardOrderExportVO> list = giftCardOrderService.getAdminGiftCardOrderExportList(dto);
            ExcelUtil<GiftCardOrderExportVO> util = new ExcelUtil<>(GiftCardOrderExportVO.class);
            util.exportExcel(response, list, "购物卡订单");
        } catch (ServiceException e) {
            log.error("导出购物卡订单参数异常", e);
            throw e;
        } catch (Exception e) {
            log.error("导出购物卡订单异常", e);
            throw new RuntimeException("导出购物卡订单失败", e);
        }
    }
}
