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

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.depotlocationitem.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.depotlocationitem.DepotLocationItemDO;
import cn.iocoder.yudao.module.wms.service.depotlocationitem.DepotLocationItemService;

@Tag(name = "管理后台 - 库位子表管理")
@RestController
@RequestMapping("/wms/depot-location-item")
@Validated
public class DepotLocationItemController {

    @Resource
    private DepotLocationItemService depotLocationItemService;

    @PostMapping("/create")
    @Operation(summary = "创建库位子表管理")
    @PreAuthorize("@ss.hasPermission('wms:depot-location-item:create')")
    public CommonResult<Long> createDepotLocationItem(@Valid @RequestBody DepotLocationItemSaveReqVO createReqVO) {
        return success(depotLocationItemService.createDepotLocationItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库位子表管理")
    @PreAuthorize("@ss.hasPermission('wms:depot-location-item:update')")
    public CommonResult<Boolean> updateDepotLocationItem(@Valid @RequestBody DepotLocationItemSaveReqVO updateReqVO) {
        depotLocationItemService.updateDepotLocationItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库位子表管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:depot-location-item:delete')")
    public CommonResult<Boolean> deleteDepotLocationItem(@RequestParam("id") Long id) {
        depotLocationItemService.deleteDepotLocationItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库位子表管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:depot-location-item:query')")
    public CommonResult<DepotLocationItemRespVO> getDepotLocationItem(@RequestParam("id") Long id) {
        DepotLocationItemDO depotLocationItem = depotLocationItemService.getDepotLocationItem(id);
        return success(BeanUtils.toBean(depotLocationItem, DepotLocationItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库位子表管理分页")
    @PreAuthorize("@ss.hasPermission('wms:depot-location-item:query')")
    public CommonResult<PageResult<DepotLocationItemRespVO>> getDepotLocationItemPage(@Valid DepotLocationItemPageReqVO pageReqVO) {
        PageResult<DepotLocationItemDO> pageResult = depotLocationItemService.getDepotLocationItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepotLocationItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库位子表管理 Excel")
    @PreAuthorize("@ss.hasPermission('wms:depot-location-item:export')")
    @OperateLog(type = EXPORT)
    public void exportDepotLocationItemExcel(@Valid DepotLocationItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepotLocationItemDO> list = depotLocationItemService.getDepotLocationItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库位子表管理.xls", "数据", DepotLocationItemRespVO.class,
                        BeanUtils.toBean(list, DepotLocationItemRespVO.class));
    }

}