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

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.barcode.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.barcode.BarCodeDO;
import cn.iocoder.yudao.module.wms.service.barcode.BarCodeService;

@Tag(name = "管理后台 - 条码管理")
@RestController
@RequestMapping("/wms/bar-code")
@Validated
public class BarCodeController {

    @Resource
    private BarCodeService barCodeService;

    @PostMapping("/create")
    @Operation(summary = "创建条码管理")
    @PreAuthorize("@ss.hasPermission('wms:bar-code:create')")
    public CommonResult<Long> createBarCode(@Valid @RequestBody BarCodeSaveReqVO createReqVO) {
        return success(barCodeService.createBarCode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新条码管理")
    @PreAuthorize("@ss.hasPermission('wms:bar-code:update')")
    public CommonResult<Boolean> updateBarCode(@Valid @RequestBody BarCodeSaveReqVO updateReqVO) {
        barCodeService.updateBarCode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除条码管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:bar-code:delete')")
    public CommonResult<Boolean> deleteBarCode(@RequestParam("id") Long id) {
        barCodeService.deleteBarCode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得条码管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:bar-code:query')")
    public CommonResult<BarCodeRespVO> getBarCode(@RequestParam("id") Long id) {
        BarCodeDO barCode = barCodeService.getBarCode(id);
        return success(BeanUtils.toBean(barCode, BarCodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得条码管理分页")
    @PreAuthorize("@ss.hasPermission('wms:bar-code:query')")
    public CommonResult<PageResult<BarCodeRespVO>> getBarCodePage(@Valid BarCodePageReqVO pageReqVO) {
        PageResult<BarCodeDO> pageResult = barCodeService.getBarCodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BarCodeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出条码管理 Excel")
    @PreAuthorize("@ss.hasPermission('wms:bar-code:export')")
    @OperateLog(type = EXPORT)
    public void exportBarCodeExcel(@Valid BarCodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BarCodeDO> list = barCodeService.getBarCodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "条码管理.xls", "数据", BarCodeRespVO.class,
                        BeanUtils.toBean(list, BarCodeRespVO.class));
    }

}