/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.youchain.jamod.rest;

import com.youchain.annotation.Log;
import com.youchain.jamod.domain.ModbusInfo;
import com.youchain.jamod.service.ModbusInfoService;
import com.youchain.jamod.service.dto.ModbusInfoQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @website https://eladmin.vip
* @author huojin
* @date 2025-03-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "modbusInfo管理")
@RequestMapping("/api/modbusInfo")
public class ModbusInfoController {

    private final ModbusInfoService modbusInfoService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('modbusInfo:list')")
    public void exportModbusInfo(HttpServletResponse response, ModbusInfoQueryCriteria criteria) throws IOException {
        modbusInfoService.download(modbusInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询modbusInfo")
    @ApiOperation("查询modbusInfo")
    @PreAuthorize("@el.check('modbusInfo:list')")
    public ResponseEntity<Object> queryModbusInfo(ModbusInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(modbusInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增modbusInfo")
    @ApiOperation("新增modbusInfo")
    @PreAuthorize("@el.check('modbusInfo:add')")
    public ResponseEntity<Object> createModbusInfo(@Validated @RequestBody ModbusInfo resources){
        return new ResponseEntity<>(modbusInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改modbusInfo")
    @ApiOperation("修改modbusInfo")
    @PreAuthorize("@el.check('modbusInfo:edit')")
    public ResponseEntity<Object> updateModbusInfo(@Validated @RequestBody ModbusInfo resources){
        modbusInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除modbusInfo")
    @ApiOperation("删除modbusInfo")
    @PreAuthorize("@el.check('modbusInfo:del')")
    public ResponseEntity<Object> deleteModbusInfo(@RequestBody Integer[] ids) {
        modbusInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}