package cn.iocoder.yudao.module.member.controller.admin.storeorderdetail;

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 cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.storeorderdetail.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storeorderdetail.StoreOrderDetailDO;
import cn.iocoder.yudao.module.member.service.storeorderdetail.StoreOrderDetailService;

@Tag(name = "管理后台 - 商品订单详情")
@RestController
@RequestMapping("/member/store-order-detail")
@Validated
public class StoreOrderDetailController {

    @Resource
    private StoreOrderDetailService storeOrderDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建商品订单详情")
    @PreAuthorize("@ss.hasPermission('member:store-order-detail:create')")
    public CommonResult<Integer> createStoreOrderDetail(@Valid @RequestBody StoreOrderDetailSaveReqVO createReqVO) {
        return success(storeOrderDetailService.createStoreOrderDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品订单详情")
    @PreAuthorize("@ss.hasPermission('member:store-order-detail:update')")
    public CommonResult<Boolean> updateStoreOrderDetail(@Valid @RequestBody StoreOrderDetailSaveReqVO updateReqVO) {
        storeOrderDetailService.updateStoreOrderDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品订单详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-order-detail:delete')")
    public CommonResult<Boolean> deleteStoreOrderDetail(@RequestParam("id") Integer id) {
        storeOrderDetailService.deleteStoreOrderDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品订单详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-order-detail:query')")
    public CommonResult<StoreOrderDetailRespVO> getStoreOrderDetail(@RequestParam("id") Integer id) {
        StoreOrderDetailDO storeOrderDetail = storeOrderDetailService.getStoreOrderDetail(id);
        return success(BeanUtils.toBean(storeOrderDetail, StoreOrderDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品订单详情分页")
    @PreAuthorize("@ss.hasPermission('member:store-order-detail:query')")
    public CommonResult<PageResult<StoreOrderDetailRespVO>> getStoreOrderDetailPage(@Valid StoreOrderDetailPageReqVO pageReqVO) {
        PageResult<StoreOrderDetailDO> pageResult = storeOrderDetailService.getStoreOrderDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreOrderDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品订单详情 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-order-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreOrderDetailExcel(@Valid StoreOrderDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreOrderDetailDO> list = storeOrderDetailService.getStoreOrderDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品订单详情.xls", "数据", StoreOrderDetailRespVO.class,
                        BeanUtils.toBean(list, StoreOrderDetailRespVO.class));
    }

}