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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.wms.controller.admin.depot.vo.DepotRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.depotarea.vo.DepotAreaPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.depotarea.vo.DepotAreaRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.depotarea.vo.DepotAreaSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.depotarea.DepotAreaDO;
import cn.iocoder.yudao.module.wms.service.depotarea.DepotAreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

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

    @Resource
    private DepotAreaService depotAreaService;

    @PostMapping("/create")
    @Operation(summary = "创建库区管理")
    @PreAuthorize("@ss.hasPermission('wms:depot-area:create')")
    public CommonResult<Long> createDepotArea(@Valid @RequestBody DepotAreaSaveReqVO createReqVO) {
        return success(depotAreaService.createDepotArea(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库区管理")
    @PreAuthorize("@ss.hasPermission('wms:depot-area:update')")
    public CommonResult<Boolean> updateDepotArea(@Valid @RequestBody DepotAreaSaveReqVO updateReqVO) {
        depotAreaService.updateDepotArea(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得库区管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:depot-area:query')")
    public CommonResult<DepotAreaRespVO> getDepotArea(@RequestParam("id") Long id) {
        DepotAreaDO depotArea = depotAreaService.getDepotArea(id);
        return success(BeanUtils.toBean(depotArea, DepotAreaRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库区管理分页")
    @PreAuthorize("@ss.hasPermission('wms:depot-area:query')")
    public CommonResult<PageResult<DepotAreaRespVO>> getDepotAreaPage(@Valid DepotAreaPageReqVO pageReqVO) {
        PageResult<DepotAreaDO> pageResult = depotAreaService.getDepotAreaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepotAreaRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得库区管理列表")
    public CommonResult<List<DepotAreaRespVO>> getDepotAreaList(@Valid DepotAreaPageReqVO pageReqVO) {
        List<DepotAreaRespVO> list = depotAreaService.getDepotAreaList(pageReqVO);
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库区管理 Excel")
    @PreAuthorize("@ss.hasPermission('wms:depot-area:export')")
    @OperateLog(type = EXPORT)
    public void exportDepotAreaExcel(@Valid DepotAreaPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepotAreaDO> list = depotAreaService.getDepotAreaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库区管理.xls", "数据", DepotAreaRespVO.class,
                BeanUtils.toBean(list, DepotAreaRespVO.class));
    }

}
