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

import com.yc.ship.framework.common.pojo.CommonResult;
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.check.*;
import com.yc.ship.module.trade.enums.CheckTypeEnum;
import com.yc.ship.module.trade.framework.annotation.PlatTenantEnv;
import com.yc.ship.module.trade.service.order.TradeCheckService;
import com.yc.ship.module.trade.service.pda.PdaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
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.operatelog.core.enums.OperateTypeEnum.*;


/**
 * @author qsl
 */
@Slf4j
@Validated
@RequestMapping("/trade")
@RestController
@Tag(name = "核销服务（带token）")
public class TradeCheckController {
    @Resource
    private PdaService pdaService;
    @Resource
    private TradeCheckService tradeCheckService;

    @PostMapping("/check/system")
    @Operation(summary = "系统核销")
    @OperateLog(type = CHECK)
    public CommonResult<List<TradeCheckRespVO>> check(@Valid @RequestBody TradeCheckReqVO checkReqVO) {
        checkReqVO.setType(CheckTypeEnum.System.getType());
        return tradeCheckService.checkCommon(checkReqVO);
    }

    @PostMapping("/check/pda")
    @Operation(summary = "PDA核销(MachineId、OperatorId 必传)")
    @OperateLog(type = CHECK)
    public CommonResult<List<TradeCheckRespVO>> checkPda(@Valid @RequestBody TradeCheckReqVO checkReqVO) {
        checkReqVO.setType(CheckTypeEnum.PDA.getType());
        return tradeCheckService.checkPda(checkReqVO);
    }

    @PostMapping("/check/applet")
    @Operation(summary = "小程序核销(导游)")
    @OperateLog(type = CHECK)
    public CommonResult<List<TradeCheckRespVO>> checkApplet(@Valid @RequestBody TradeCheckReqVO checkReqVO) {
        checkReqVO.setType(CheckTypeEnum.Applet.getType());
        return tradeCheckService.checkApplet(checkReqVO);
    }

    @PostMapping("/check/other")
    @Operation(summary = "非平台产品核销(导游核销旅行社订单)")
    @OperateLog(type = CHECK)
    public CommonResult<List<TradeCheckRespVO>> checkOther(@Valid @RequestBody TradeCheckReqVO checkReqVO) {
        checkReqVO.setType(CheckTypeEnum.Applet.getType());
        return tradeCheckService.checkOther(checkReqVO);
    }

    @PostMapping("/check-record/page")
    @Operation(summary = "核销记录列表")
    @OperateLog(type = CHECK)
    public CommonResult<?> page(@Valid @RequestBody TradeCheckPageReqVO pageReqVO) {
        return tradeCheckService.querySimpleCheckPage(pageReqVO, true);
    }

    @GetMapping("/exportCheckRecordList")
    @Operation(summary = "导出核销列表 Excel")
    @OperateLog(type = EXPORT, enable = false)
    @PlatTenantEnv
    public void exportCheckRecordList(@Valid TradeCheckPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(10000);
        List<CheckRespVO> list = tradeCheckService.querySimpleCheckList(pageReqVO, false).getList();
        // 导出 Excel
        ExcelUtils.write(response, "核销列表.xls", "数据", CheckRespVO.class, list);
    }


    @PostMapping("/check/query-order-info")
    @Operation(summary = "查询订单信息")
    @OperateLog(type = API)
    public CommonResult<?> queryOrderInfo(@Valid @RequestBody CheckOrderPageVO reqVO) {
        return pdaService.queryOrderWithoutSiteNum(reqVO);
    }

    @PostMapping("/check/queryGuideOrderInfo")
    @Operation(summary = "查询订单信息")
    @OperateLog(type = API)
    public CommonResult<?> queryGuideOrderInfo(@Valid @RequestBody CheckOrderPageVO reqVO) {
        return pdaService.queryOrderGuideWithoutSiteNum(reqVO);
    }

}
