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

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.qrcode.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.qrcode.QrCodeDO;
import cn.iocoder.yudao.module.wms.service.qrcode.QrCodeService;

@Tag(name = "管理后台 - 二维码管理")
@RestController
@RequestMapping("/wms/qr-code")
@Validated
public class QrCodeController {

    @Resource
    private QrCodeService qrCodeService;

    @PostMapping("/create")
    @Operation(summary = "创建二维码管理")
    @PreAuthorize("@ss.hasPermission('wms:qr-code:create')")
    public CommonResult<Long> createQrCode(@Valid @RequestBody QrCodeSaveReqVO createReqVO) {
        return success(qrCodeService.createQrCode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新二维码管理")
    @PreAuthorize("@ss.hasPermission('wms:qr-code:update')")
    public CommonResult<Boolean> updateQrCode(@Valid @RequestBody QrCodeSaveReqVO updateReqVO) {
        qrCodeService.updateQrCode(updateReqVO);
        return success(true);
    }

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

    @DeleteMapping("/batchDelete")
    @Operation(summary = "批量删除二维码管理")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> batchDeleteQrCode(@RequestBody List<Long> ids) {
        qrCodeService.batchDeleteQrCode(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得二维码管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:qr-code:query')")
    public CommonResult<QrCodeRespVO> getQrCode(@RequestParam("id") Long id) {
        QrCodeDO qrCode = qrCodeService.getQrCode(id);
        return success(BeanUtils.toBean(qrCode, QrCodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得二维码管理分页")
    @PreAuthorize("@ss.hasPermission('wms:qr-code:query')")
    public CommonResult<PageResult<QrCodeRespVO>> getQrCodePage(@Valid QrCodePageReqVO pageReqVO) {
        PageResult<QrCodeDO> pageResult = qrCodeService.getQrCodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QrCodeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出二维码管理 Excel")
    @PreAuthorize("@ss.hasPermission('wms:qr-code:export')")
    @OperateLog(type = EXPORT)
    public void exportQrCodeExcel(@Valid QrCodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QrCodeDO> list = qrCodeService.getQrCodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "二维码管理.xls", "数据", QrCodeRespVO.class,
                        BeanUtils.toBean(list, QrCodeRespVO.class));
    }

}
