package cn.newfeifan.mall.module.distri.controller.admin.ordersettlement;

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.distri.controller.admin.ordersettlement.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ordersettlement.OrderSettlementDO;
import cn.newfeifan.mall.module.distri.service.ordersettlement.OrderSettlementService;

@Tag(name = "管理后台 - 订单结算表,记录有那些订单参与了本次结算")
@RestController
@RequestMapping("/distri/order-settlement")
@Validated
public class OrderSettlementController {

    @Resource
    private OrderSettlementService orderSettlementService;

    @PostMapping("/create")
    @Operation(summary = "创建订单结算表,记录有那些订单参与了本次结算")
    @PreAuthorize("@ss.hasPermission('distri:order-settlement:create')")
    public CommonResult<Long> createOrderSettlement(@Valid @RequestBody OrderSettlementSaveReqVO createReqVO) {
        return success(orderSettlementService.createOrderSettlement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单结算表,记录有那些订单参与了本次结算")
    @PreAuthorize("@ss.hasPermission('distri:order-settlement:update')")
    public CommonResult<Boolean> updateOrderSettlement(@Valid @RequestBody OrderSettlementSaveReqVO updateReqVO) {
        orderSettlementService.updateOrderSettlement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单结算表,记录有那些订单参与了本次结算")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:order-settlement:delete')")
    public CommonResult<Boolean> deleteOrderSettlement(@RequestParam("id") Long id) {
        orderSettlementService.deleteOrderSettlement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单结算表,记录有那些订单参与了本次结算")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:order-settlement:query')")
    public CommonResult<OrderSettlementRespVO> getOrderSettlement(@RequestParam("id") Long id) {
        OrderSettlementDO orderSettlement = orderSettlementService.getOrderSettlement(id);
        return success(BeanUtils.toBean(orderSettlement, OrderSettlementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单结算表,记录有那些订单参与了本次结算分页")
    @PreAuthorize("@ss.hasPermission('distri:order-settlement:query')")
    public CommonResult<PageResult<OrderSettlementRespVO>> getOrderSettlementPage(@Valid OrderSettlementPageReqVO pageReqVO) {
        PageResult<OrderSettlementDO> pageResult = orderSettlementService.getOrderSettlementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderSettlementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单结算表,记录有那些订单参与了本次结算 Excel")
    @PreAuthorize("@ss.hasPermission('distri:order-settlement:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderSettlementExcel(@Valid OrderSettlementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderSettlementDO> list = orderSettlementService.getOrderSettlementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单结算表,记录有那些订单参与了本次结算.xls", "数据", OrderSettlementRespVO.class,
                        BeanUtils.toBean(list, OrderSettlementRespVO.class));
    }

}