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

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.inouttype.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.inouttype.InOutTypeDO;
import cn.iocoder.yudao.module.wms.service.inouttype.InOutTypeService;

@Tag(name = "管理后台 - 出入库类型管理")
@RestController
@RequestMapping("/wms/in-out-type")
@Validated
public class InOutTypeController {

    @Resource
    private InOutTypeService inOutTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建出入库类型管理")
    @PreAuthorize("@ss.hasPermission('wms:in-out-type:create')")
    public CommonResult<Long> createInOutType(@Valid @RequestBody InOutTypeSaveReqVO createReqVO) {
        return success(inOutTypeService.createInOutType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新出入库类型管理")
    @PreAuthorize("@ss.hasPermission('wms:in-out-type:update')")
    public CommonResult<Boolean> updateInOutType(@Valid @RequestBody InOutTypeSaveReqVO updateReqVO) {
        inOutTypeService.updateInOutType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除出入库类型管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:in-out-type:delete')")
    public CommonResult<Boolean> deleteInOutType(@RequestParam("id") Long id) {
        inOutTypeService.deleteInOutType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得出入库类型管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:in-out-type:query')")
    public CommonResult<InOutTypeRespVO> getInOutType(@RequestParam("id") Long id) {
        InOutTypeDO inOutType = inOutTypeService.getInOutType(id);
        return success(BeanUtils.toBean(inOutType, InOutTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得出入库类型管理分页")
    @PreAuthorize("@ss.hasPermission('wms:in-out-type:query')")
    public CommonResult<PageResult<InOutTypeRespVO>> getInOutTypePage(@Valid InOutTypePageReqVO pageReqVO) {
        PageResult<InOutTypeDO> pageResult = inOutTypeService.getInOutTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InOutTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出出入库类型管理 Excel")
    @PreAuthorize("@ss.hasPermission('wms:in-out-type:export')")
    @OperateLog(type = EXPORT)
    public void exportInOutTypeExcel(@Valid InOutTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InOutTypeDO> list = inOutTypeService.getInOutTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "出入库类型管理.xls", "数据", InOutTypeRespVO.class,
                        BeanUtils.toBean(list, InOutTypeRespVO.class));
    }

}