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

import cn.iocoder.yudao.module.pms.controller.admin.maintain.material.vo.IotMaintainMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.material.vo.IotMaintainMaterialRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.material.vo.IotMaintainMaterialSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintain.material.IotMaintainMaterialDO;
import cn.iocoder.yudao.module.pms.service.maintain.material.IotMaintainMaterialService;
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-material")
@Validated
public class IotMaintainMaterialController {

    @Resource
    private IotMaintainMaterialService iotMaintainMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建工单物料")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-material:create')")
    public CommonResult<Long> createIotMaintainMaterial(@Valid @RequestBody IotMaintainMaterialSaveReqVO createReqVO) {
        return success(iotMaintainMaterialService.createIotMaintainMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单物料")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-material:update')")
    public CommonResult<Boolean> updateIotMaintainMaterial(@Valid @RequestBody IotMaintainMaterialSaveReqVO updateReqVO) {
        iotMaintainMaterialService.updateIotMaintainMaterial(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得工单物料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-material:query')")
    public CommonResult<IotMaintainMaterialRespVO> getIotMaintainMaterial(@RequestParam("id") Long id) {
        IotMaintainMaterialDO iotMaintainMaterial = iotMaintainMaterialService.getIotMaintainMaterial(id);
        return success(BeanUtils.toBean(iotMaintainMaterial, IotMaintainMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单物料分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-material:query')")
    public CommonResult<PageResult<IotMaintainMaterialRespVO>> getIotMaintainMaterialPage(@Valid IotMaintainMaterialPageReqVO pageReqVO) {
        PageResult<IotMaintainMaterialDO> pageResult = iotMaintainMaterialService.getIotMaintainMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMaintainMaterialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单物料 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMaintainMaterialExcel(@Valid IotMaintainMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMaintainMaterialDO> list = iotMaintainMaterialService.getIotMaintainMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工单物料.xls", "数据", IotMaintainMaterialRespVO.class,
                        BeanUtils.toBean(list, IotMaintainMaterialRespVO.class));
    }
    @GetMapping("/choose")
    @Operation(summary = "获得工单物料")
    @Parameter(name = "deptId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-maintain-material:query')")
    public CommonResult<PageResult<IotMaintainMaterialDO>> getIotMaintainMaterials(@Valid IotMaintainMaterialPageReqVO pageReqVO) {
        PageResult<IotMaintainMaterialDO> iotMaintainMaterials = iotMaintainMaterialService.getIotMaintainMaterials(pageReqVO);
        return success(iotMaintainMaterials);
    }
}
