package cn.iocoder.yudao.module.system.controller.admin.housearea;

import cn.iocoder.yudao.module.system.controller.admin.housearea.vo.HouseAreaPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.housearea.vo.HouseAreaRespVO;
import cn.iocoder.yudao.module.system.controller.admin.housearea.vo.HouseAreaSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.housearea.vo.HouseAreaVO;
import cn.iocoder.yudao.module.system.controller.admin.houselane.vo.WareHouseLanePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.houselane.vo.WareHouseLaneRespVO;
import cn.iocoder.yudao.module.system.controller.admin.houselane.vo.WareHouseLaneSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.housearea.HouseAreaDO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselane.WareHouseLaneDO;
import cn.iocoder.yudao.module.system.service.housearea.HouseAreaService;
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.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 库区")
@RestController
@RequestMapping("/system/ware/house-area")
@Validated
public class HouseAreaController {

    @Resource
    private HouseAreaService houseAreaService;

    @PostMapping("/createOrEditOrDel")
    @Operation(summary = "创建修改删除库区")
    @PreAuthorize("@ss.hasPermission('ware:house-area:create')")
    public CommonResult<HouseAreaVO> createOrEditOrDel(@Valid @RequestBody HouseAreaSaveReqVO createReqVO) {
        HouseAreaVO vo = houseAreaService.createOrEditOrDel(createReqVO);
        return success(vo);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库区")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ware:house-area:query')")
    public CommonResult<HouseAreaRespVO> getHouseArea(@RequestParam("id") Long id) {
        HouseAreaDO houseArea = houseAreaService.getHouseArea(id);
        return success(BeanUtils.toBean(houseArea, HouseAreaRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库区分页")
    @PreAuthorize("@ss.hasPermission('ware:house-area:query')")
    public CommonResult<PageResult<HouseAreaRespVO>> getHouseAreaPage(@Valid HouseAreaPageReqVO pageReqVO) {
        PageResult<HouseAreaDO> pageResult = houseAreaService.getHouseAreaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HouseAreaRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库区 Excel")
    @PreAuthorize("@ss.hasPermission('ware:house-area:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHouseAreaExcel(@Valid HouseAreaPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HouseAreaDO> list = houseAreaService.getHouseAreaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库区.xls", "数据", HouseAreaRespVO.class,
                        BeanUtils.toBean(list, HouseAreaRespVO.class));
    }

    @GetMapping("/getHouseAreaList")
    @Operation(summary = "获得全部库区")
    @PreAuthorize("@ss.hasPermission('ware:house-area:query')")
    public CommonResult<List<HouseAreaRespVO>> getHouseAreaList(@RequestParam Long positionMapId) {
        List<HouseAreaDO> list = houseAreaService.getHouseAreaList(positionMapId);
        return success(BeanUtils.toBean(list, HouseAreaRespVO.class));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库区")
    @Parameter(name = "id", description = "菜单编号", required= true, example = "1024")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        houseAreaService.delete(id);
        return success(true);
    }

}
