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

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.depot.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.depot.DepotDO;
import cn.iocoder.yudao.module.wms.service.depot.DepotService;

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

    @Resource
    private DepotService depotService;

    @PostMapping("/create")
    @Operation(summary = "创建仓库管理")
    @PreAuthorize("@ss.hasPermission('wms:depot:create')")
    public CommonResult<Long> createDepot(@Valid @RequestBody DepotSaveReqVO createReqVO) {
        return success(depotService.createDepot(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓库管理")
    @PreAuthorize("@ss.hasPermission('wms:depot:update')")
    public CommonResult<Boolean> updateDepot(@Valid @RequestBody DepotSaveReqVO updateReqVO) {
        depotService.updateDepot(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得仓库管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:depot:query')")
    public CommonResult<DepotRespVO> getDepot(@RequestParam("id") Long id) {
        DepotDO depot = depotService.getDepot(id);
        return success(BeanUtils.toBean(depot, DepotRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仓库管理分页")
    @PreAuthorize("@ss.hasPermission('wms:depot:query')")
    public CommonResult<PageResult<DepotRespVO>> getDepotPage(@Valid DepotPageReqVO pageReqVO) {
        PageResult<DepotDO> pageResult = depotService.getDepotPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepotRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得仓库管理列表")
    public CommonResult<List<DepotRespVO>> getDepotList(@Valid DepotPageReqVO pageReqVO) {
        List<DepotRespVO> list = depotService.getDepotList(pageReqVO);
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓库管理 Excel")
    @PreAuthorize("@ss.hasPermission('wms:depot:export')")
    @OperateLog(type = EXPORT)
    public void exportDepotExcel(@Valid DepotPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepotDO> list = depotService.getDepotPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仓库管理.xls", "数据", DepotRespVO.class,
                        BeanUtils.toBean(list, DepotRespVO.class));
    }

}
