package cn.iocoder.yudao.module.wms.controller.admin.businessordernumber;

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.wms.controller.admin.businessordernumber.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.businessordernumber.BusinessOrderNumberDO;
import cn.iocoder.yudao.module.wms.service.businessordernumber.BusinessOrderNumberService;

@Tag(name = "管理后台 - 业务单号管理")
@RestController
@RequestMapping("/wms/business-order-number")
@Validated
public class BusinessOrderNumberController {

    @Resource
    private BusinessOrderNumberService businessOrderNumberService;

    @PostMapping("/create")
    @Operation(summary = "创建业务单号管理")
    @PreAuthorize("@ss.hasPermission('wms:business-order-number:create')")
    public CommonResult<Long> createBusinessOrderNumber(@Valid @RequestBody BusinessOrderNumberSaveReqVO createReqVO) {
        return success(businessOrderNumberService.createBusinessOrderNumber(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务单号管理")
    @PreAuthorize("@ss.hasPermission('wms:business-order-number:update')")
    public CommonResult<Boolean> updateBusinessOrderNumber(@Valid @RequestBody BusinessOrderNumberSaveReqVO updateReqVO) {
        businessOrderNumberService.updateBusinessOrderNumber(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务单号管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:business-order-number:delete')")
    public CommonResult<Boolean> deleteBusinessOrderNumber(@RequestParam("id") Long id) {
        businessOrderNumberService.deleteBusinessOrderNumber(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务单号管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:business-order-number:query')")
    public CommonResult<BusinessOrderNumberRespVO> getBusinessOrderNumber(@RequestParam("id") Long id) {
        BusinessOrderNumberDO businessOrderNumber = businessOrderNumberService.getBusinessOrderNumber(id);
        return success(BeanUtils.toBean(businessOrderNumber, BusinessOrderNumberRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务单号管理分页")
    @PreAuthorize("@ss.hasPermission('wms:business-order-number:query')")
    public CommonResult<PageResult<BusinessOrderNumberRespVO>> getBusinessOrderNumberPage(@Valid BusinessOrderNumberPageReqVO pageReqVO) {
        PageResult<BusinessOrderNumberDO> pageResult = businessOrderNumberService.getBusinessOrderNumberPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessOrderNumberRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务单号管理 Excel")
    @PreAuthorize("@ss.hasPermission('wms:business-order-number:export')")
    @OperateLog(type = EXPORT)
    public void exportBusinessOrderNumberExcel(@Valid BusinessOrderNumberPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessOrderNumberDO> list = businessOrderNumberService.getBusinessOrderNumberPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "业务单号管理.xls", "数据", BusinessOrderNumberRespVO.class,
                        BeanUtils.toBean(list, BusinessOrderNumberRespVO.class));
    }

}