package cn.newfeifan.mall.module.trade.controller.admin.ordercallbacklogs;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.trade.controller.admin.ordercallbacklogs.vo.*;
import cn.newfeifan.mall.module.trade.dal.dataobject.ordercallbacklogs.OrderCallbackLogsDO;
import cn.newfeifan.mall.module.trade.service.ordercallbacklogs.OrderCallbackLogsService;

@Tag(name = "管理后台 - 第三方回调日志")
@RestController
@RequestMapping("/trade/order-callback-logs")
@Validated
public class OrderCallbackLogsController {

    @Resource
    private OrderCallbackLogsService orderCallbackLogsService;

    @PostMapping("/create")
    @Operation(summary = "创建第三方回调日志")
    @PreAuthorize("@ss.hasPermission('trade:order-callback-logs:create')")
    public CommonResult<Long> createOrderCallbackLogs(@Valid @RequestBody OrderCallbackLogsSaveReqVO createReqVO) {
        return success(orderCallbackLogsService.createOrderCallbackLogs(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新第三方回调日志")
    @PreAuthorize("@ss.hasPermission('trade:order-callback-logs:update')")
    public CommonResult<Boolean> updateOrderCallbackLogs(@Valid @RequestBody OrderCallbackLogsSaveReqVO updateReqVO) {
        orderCallbackLogsService.updateOrderCallbackLogs(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除第三方回调日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-callback-logs:delete')")
    public CommonResult<Boolean> deleteOrderCallbackLogs(@RequestParam("id") Long id) {
        orderCallbackLogsService.deleteOrderCallbackLogs(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得第三方回调日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-callback-logs:query')")
    public CommonResult<OrderCallbackLogsRespVO> getOrderCallbackLogs(@RequestParam("id") Long id) {
        OrderCallbackLogsDO orderCallbackLogs = orderCallbackLogsService.getOrderCallbackLogs(id);
        return success(BeanUtils.toBean(orderCallbackLogs, OrderCallbackLogsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得第三方回调日志分页")
    @PreAuthorize("@ss.hasPermission('trade:order-callback-logs:query')")
    public CommonResult<PageResult<OrderCallbackLogsRespVO>> getOrderCallbackLogsPage(@Valid OrderCallbackLogsPageReqVO pageReqVO) {
        PageResult<OrderCallbackLogsDO> pageResult = orderCallbackLogsService.getOrderCallbackLogsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderCallbackLogsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出第三方回调日志 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-callback-logs:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderCallbackLogsExcel(@Valid OrderCallbackLogsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderCallbackLogsDO> list = orderCallbackLogsService.getOrderCallbackLogsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "第三方回调日志.xls", "数据", OrderCallbackLogsRespVO.class,
                        BeanUtils.toBean(list, OrderCallbackLogsRespVO.class));
    }

}