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

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
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.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.module.trade.controller.admin.tradeOrderaudit.vo.TradeOrderAuditPageReqVO;
import com.yc.ship.module.trade.controller.admin.tradeOrderaudit.vo.TradeOrderAuditRespVO;
import com.yc.ship.module.trade.controller.admin.tradeOrderaudit.vo.TradeOrderAuditSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.tradeOrderaudit.TradeOrderAuditDO;
import com.yc.ship.module.trade.service.tradeOrderaudit.TradeOrderAuditService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
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.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

/**
 * @author qsl
 */
@Tag(name = "管理后台 - 订单审核记录")
@RestController
@RequestMapping("/trade/order-audit")
@Validated
public class TradeOrderAuditController {

    @Resource
    private TradeOrderAuditService orderAuditService;

    @PostMapping("/create")
    @Operation(summary = "创建订单审核记录")
    @PreAuthorize("@ss.hasPermission('trade:order-audit:create')")
    public CommonResult<Long> createOrderAudit(@Valid @RequestBody TradeOrderAuditSaveReqVO createReqVO) {
        return success(orderAuditService.createOrderAudit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单审核记录")
    @PreAuthorize("@ss.hasPermission('trade:order-audit:update')")
    public CommonResult<Boolean> updateOrderAudit(@Valid @RequestBody TradeOrderAuditSaveReqVO updateReqVO) {
        orderAuditService.updateOrderAudit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单审核记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-audit:delete')")
    public CommonResult<Boolean> deleteOrderAudit(@RequestParam("id") Long id) {
        orderAuditService.deleteOrderAudit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单审核记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-audit:query')")
    public CommonResult<TradeOrderAuditRespVO> getOrderAudit(@RequestParam("id") Long id) {
        TradeOrderAuditDO orderAudit = orderAuditService.getOrderAudit(id);
        return success(BeanUtils.toBean(orderAudit, TradeOrderAuditRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单审核记录分页")
    @PreAuthorize("@ss.hasPermission('trade:order-audit:query')")
    public CommonResult<PageResult<TradeOrderAuditRespVO>> getOrderAuditPage(@Valid TradeOrderAuditPageReqVO pageReqVO) {
        PageResult<TradeOrderAuditDO> pageResult = orderAuditService.getOrderAuditPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TradeOrderAuditRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单审核记录 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-audit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderAuditExcel(@Valid TradeOrderAuditPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TradeOrderAuditDO> list = orderAuditService.getOrderAuditPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单审核记录.xls", "数据", TradeOrderAuditRespVO.class,
                BeanUtils.toBean(list, TradeOrderAuditRespVO.class));
    }

}