package com.yc.ship.module.trade.controller.admin.order;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.trade.controller.admin.order.vo.order.TradeDetailPageReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.TradeDetailRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.*;
import com.yc.ship.module.trade.framework.annotation.PlatTenantEnv;
import com.yc.ship.module.trade.service.otc.OtcTradeOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.API;


/**
 * @author qsl
 */
@Validated
@RestController
@Tag(name = "旅行社交易服务-退款")
public class OtcTradeRefundController {

    @Resource
    private OtcTradeOrderService otcTradeOrderService;

    @PostMapping("/otc/trade/window/refundOrderList")
    @Operation(summary = "退单列表",description = "根据条件查询退单列表")
    @OperateLog(type = API)
    @PlatTenantEnv
    public CommonResult<PageResult<RefundRespVO>> getRefundRecordPage(@Valid @RequestBody RefundPageReqVO pageReqVO) {
        return success(otcTradeOrderService.getRefundRecordPage(pageReqVO));
    }

    @GetMapping("/otc/trade/window/refundDetailList")
    @Operation(summary = "退单批次详情",description = "根据退单id查询退单批次详情")
    @OperateLog(type = API)
    @PlatTenantEnv
    public CommonResult<List<RefundDetailRespVO>> getRefundBatchDetails(@Valid @RequestParam("id") Long id) {
        return success(otcTradeOrderService.getRefundBatchDetails(id));
    }

    @GetMapping("/otc/trade/window/refundOrderDetailList")
    @Operation(summary = "退单门票详情[分页]",description = "根据退单id查询退单详情")
    @OperateLog(type = API)
    public CommonResult<PageResult<TradeDetailRespVO>> getRefundDetailPage(@Valid TradeDetailPageReqVO pageReqVO) {
        return success(otcTradeOrderService.getRefundDetailPage(pageReqVO));
    }

    @GetMapping("/otc/trade/window/refundOrderInfo")
    @Operation(summary = "退单申请详情",description = "根据退单id查询退单详情")
    @OperateLog(type = API)
    public CommonResult<TradeOrderRespVO> getRefundOrderInfo(@Valid @RequestParam("id") Long id) {
        return success(otcTradeOrderService.getRefundOrderInfo(id));
    }

    @GetMapping("/otc/trade/window/refundRecordList")
    @Operation(summary = "退款记录[分页]",description = "根据条件查询退款记录")
    @OperateLog(type = API)
    @PlatTenantEnv
    public CommonResult<PageResult<RefundRecordRespVO>> getPageRefundRecord(@Valid RefundRecordPageReqVO pageReqVO) {
        return success(otcTradeOrderService.getPageRefundRecord(pageReqVO));
    }

    @GetMapping("/otc/trade/window/export-refundexcel")
    @Operation(summary = "导出退单列表 Excel")
    @OperateLog(type = API)
    @PlatTenantEnv
    public void exportRefundexcel(@Valid RefundRecordPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RefundRecordRespVO> list = otcTradeOrderService.getPageRefundRecord(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "退单列表.xls", "数据", RefundRecordRespVO.class, list);
    }

}
