package cn.iocoder.yudao.module.pms.controller.admin.maintain.materials;

import cn.iocoder.yudao.module.pms.controller.admin.maintain.materials.vo.IotMaintainMaterialsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.materials.vo.IotMaintainMaterialsRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.materials.vo.IotMaintainMaterialsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.IotMaintainMaterialsDO;
import cn.iocoder.yudao.module.pms.service.maintain.IotMaintainMaterialsService;
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("/rq/iot-maintain-materials")
@Validated
public class IotMaintainMaterialsController {

    @Resource
    private IotMaintainMaterialsService iotMaintainMaterialsService;

    @PostMapping("/create")
    @Operation(summary = "创建维修工单对应物料")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-materials:create')")
    public CommonResult<Long> createIotMaintainMaterials(@Valid @RequestBody IotMaintainMaterialsSaveReqVO createReqVO) {
        return success(iotMaintainMaterialsService.createIotMaintainMaterials(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维修工单对应物料")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-materials:update')")
    public CommonResult<Boolean> updateIotMaintainMaterials(@Valid @RequestBody IotMaintainMaterialsSaveReqVO updateReqVO) {
        iotMaintainMaterialsService.updateIotMaintainMaterials(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除维修工单对应物料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-materials:delete')")
    public CommonResult<Boolean> deleteIotMaintainMaterials(@RequestParam("id") Long id) {
        iotMaintainMaterialsService.deleteIotMaintainMaterials(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维修工单对应物料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-materials:query')")
    public CommonResult<IotMaintainMaterialsRespVO> getIotMaintainMaterials(@RequestParam("id") Long id) {
        IotMaintainMaterialsDO iotMaintainMaterials = iotMaintainMaterialsService.getIotMaintainMaterials(id);
        return success(BeanUtils.toBean(iotMaintainMaterials, IotMaintainMaterialsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得维修工单对应物料分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-materials:query')")
    public CommonResult<PageResult<IotMaintainMaterialsRespVO>> getIotMaintainMaterialsPage(@Valid IotMaintainMaterialsPageReqVO pageReqVO) {
        PageResult<IotMaintainMaterialsDO> pageResult = iotMaintainMaterialsService.getIotMaintainMaterialsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMaintainMaterialsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维修工单对应物料 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-materials:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMaintainMaterialsExcel(@Valid IotMaintainMaterialsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMaintainMaterialsDO> list = iotMaintainMaterialsService.getIotMaintainMaterialsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维修工单对应物料.xls", "数据", IotMaintainMaterialsRespVO.class,
                        BeanUtils.toBean(list, IotMaintainMaterialsRespVO.class));
    }

}
