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

import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseLocationTypePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseLocationTypeRespVO;
import cn.iocoder.yudao.module.system.controller.admin.houselocation.vo.WareHouseLocationTypeSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.houselocation.WareHouseLocationTypeDO;
import cn.iocoder.yudao.module.system.service.houselocation.WareHouseLocationTypeService;
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-location-type")
@Validated
public class WareHouseLocationTypeController {

    @Resource
    private WareHouseLocationTypeService houseLocationTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建库位类型")
    @PreAuthorize("@ss.hasPermission('ware:house-location-type:create')")
    public CommonResult<Long> createHouseLocationType(@Valid @RequestBody WareHouseLocationTypeSaveReqVO createReqVO) {
        return success(houseLocationTypeService.createHouseLocationType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库位类型")
    @PreAuthorize("@ss.hasPermission('ware:house-location-type:update')")
    public CommonResult<Boolean> updateHouseLocationType(@Valid @RequestBody WareHouseLocationTypeSaveReqVO updateReqVO) {
        houseLocationTypeService.updateHouseLocationType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库位类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ware:house-location-type:delete')")
    public CommonResult<Boolean> deleteHouseLocationType(@RequestParam("id") Long id) {
        houseLocationTypeService.deleteHouseLocationType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库位类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ware:house-location-type:query')")
    public CommonResult<WareHouseLocationTypeRespVO> getHouseLocationType(@RequestParam("id") Long id) {
        WareHouseLocationTypeDO houseLocationType = houseLocationTypeService.getHouseLocationType(id);
        return success(BeanUtils.toBean(houseLocationType, WareHouseLocationTypeRespVO.class));
    }

    @GetMapping("/getList")
    @Operation(summary = "获得库位类型集合")
    @PreAuthorize("@ss.hasPermission('ware:house-location-type:getList')")
    public CommonResult<List<WareHouseLocationTypeRespVO>> getHouseLocationTypeList() {
        List<WareHouseLocationTypeDO> list = houseLocationTypeService.getHouseLocationTypeList();
        return success(BeanUtils.toBean(list, WareHouseLocationTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库位类型分页")
    @PreAuthorize("@ss.hasPermission('ware:house-location-type:query')")
    public CommonResult<PageResult<WareHouseLocationTypeRespVO>> getHouseLocationTypePage(@Valid WareHouseLocationTypePageReqVO pageReqVO) {
        PageResult<WareHouseLocationTypeDO> pageResult = houseLocationTypeService.getHouseLocationTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WareHouseLocationTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库位类型 Excel")
    @PreAuthorize("@ss.hasPermission('ware:house-location-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHouseLocationTypeExcel(@Valid WareHouseLocationTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WareHouseLocationTypeDO> list = houseLocationTypeService.getHouseLocationTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库位类型.xls", "数据", WareHouseLocationTypeRespVO.class,
                        BeanUtils.toBean(list, WareHouseLocationTypeRespVO.class));
    }

}