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

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.orderitemsettlement.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.orderitemsettlement.OrderItemSettlementDO;
import cn.newfeifan.mall.module.distri.service.orderitemsettlement.OrderItemSettlementService;

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

    @Resource
    private OrderItemSettlementService orderItemSettlementService;

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

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

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

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

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

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

}