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

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

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

    @Resource
    private DepotLocationService depotLocationService;

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

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

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

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

    @GetMapping("/page")
    @Operation(summary = "获得库位管理分页")
    @PreAuthorize("@ss.hasPermission('wms:depot-location:query')")
    public CommonResult<PageResult<DepotLocationRespVO>> getDepotLocationPage(@Valid DepotLocationPageReqVO pageReqVO) {
        PageResult<DepotLocationDO> pageResult = depotLocationService.getDepotLocationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepotLocationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库位管理 Excel")
    @PreAuthorize("@ss.hasPermission('wms:depot-location:export')")
    @OperateLog(type = EXPORT)
    public void exportDepotLocationExcel(@Valid DepotLocationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepotLocationDO> list = depotLocationService.getDepotLocationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库位管理.xls", "数据", DepotLocationRespVO.class,
                        BeanUtils.toBean(list, DepotLocationRespVO.class));
    }

}
