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

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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.yc.ship.framework.excel.core.util.ExcelUtils;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yc.ship.module.trade.controller.admin.orderpolicy.vo.*;
import com.yc.ship.module.trade.dal.dataobject.orderpolicy.OrderPolicyDO;
import com.yc.ship.module.trade.service.orderpolicy.OrderPolicyService;

@Tag(name = "管理后台 - 订单使用的优惠政策")
@RestController
@RequestMapping("/trade/order-policy")
@Validated
public class OrderPolicyController {

    @Resource
    private OrderPolicyService orderPolicyService;

    @PostMapping("/create")
    @Operation(summary = "创建订单使用的优惠政策")
    @PreAuthorize("@ss.hasPermission('trade:order-policy:create')")
    public CommonResult<Long> createOrderPolicy(@Valid @RequestBody OrderPolicySaveReqVO createReqVO) {
        return success(orderPolicyService.createOrderPolicy(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单使用的优惠政策")
    @PreAuthorize("@ss.hasPermission('trade:order-policy:update')")
    public CommonResult<Boolean> updateOrderPolicy(@Valid @RequestBody OrderPolicySaveReqVO updateReqVO) {
        orderPolicyService.updateOrderPolicy(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单使用的优惠政策")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-policy:delete')")
    public CommonResult<Boolean> deleteOrderPolicy(@RequestParam("id") Long id) {
        orderPolicyService.deleteOrderPolicy(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单使用的优惠政策")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-policy:query')")
    public CommonResult<OrderPolicyRespVO> getOrderPolicy(@RequestParam("id") Long id) {
        OrderPolicyDO orderPolicy = orderPolicyService.getOrderPolicy(id);
        return success(BeanUtils.toBean(orderPolicy, OrderPolicyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单使用的优惠政策分页")
    @PreAuthorize("@ss.hasPermission('trade:order-policy:query')")
    public CommonResult<PageResult<OrderPolicyRespVO>> getOrderPolicyPage(@Valid OrderPolicyPageReqVO pageReqVO) {
        PageResult<OrderPolicyDO> pageResult = orderPolicyService.getOrderPolicyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderPolicyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单使用的优惠政策 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-policy:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderPolicyExcel(@Valid OrderPolicyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderPolicyDO> list = orderPolicyService.getOrderPolicyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单使用的优惠政策.xls", "数据", OrderPolicyRespVO.class,
                        BeanUtils.toBean(list, OrderPolicyRespVO.class));
    }

}