package org.nl.acs.plc.rest;

import org.nl.common.logging.annotation.Log;
import org.nl.acs.plc.service.OpcPlcService;
import org.nl.acs.opc.service.dto.OpcPlcDto;
import org.nl.acs.opc.service.dto.OpcPlcQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.util.Set;

/**
* @author jiaolm
* @date 2023-05-09
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "plc服务管理")
@RequestMapping("/api/opcPlc")
public class OpcPlcController {

    private final OpcPlcService opcPlcService;

    @GetMapping
    @Log("查询plc服务")
    @ApiOperation("查询plc服务")
    //@PreAuthorize("@el.check('opcPlc:list')")
    public ResponseEntity query(OpcPlcQueryParam query, Pageable pageable){
        return new ResponseEntity<>(opcPlcService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增plc服务")
    @ApiOperation("新增plc服务")
    //@PreAuthorize("@el.check('opcPlc:add')")
    public ResponseEntity create(@Validated @RequestBody OpcPlcDto resources){
        return new ResponseEntity<>(opcPlcService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改plc服务")
    @ApiOperation("修改plc服务")
    //@PreAuthorize("@el.check('opcPlc:edit')")
    public ResponseEntity update(@Validated @RequestBody OpcPlcDto resources){
        opcPlcService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除plc服务")
    @ApiOperation("删除plc服务")
    //@PreAuthorize("@el.check('opcPlc:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        opcPlcService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/selectList")
    @Log("下拉选PLC")
    @ApiOperation("下拉选PLC")
    //@PreAuthorize("@el.check('routePlan:list')")
    public ResponseEntity<Object> selectList() {
        return new ResponseEntity<>(opcPlcService.selectList(), HttpStatus.OK);
    }

    @GetMapping("/selectList/{opc_uuid}")
    @Log("下拉选PLC")
    @ApiOperation("下拉选PLC")
    //@PreAuthorize("@el.check('routePlan:list')")
    public ResponseEntity<Object> selectListByOpcID(@PathVariable String opc_uuid) {
        return new ResponseEntity<>(opcPlcService.selectListByOpcID(opc_uuid), HttpStatus.OK);
    }

    /*
    @Log("导出plc服务")
    @ApiOperation("导出plc服务")
    @GetMapping(value = "/download")
    //@PreAuthorize("@el.check('opcPlc:list')")
    public void download(HttpServletResponse response, OpcPlcQueryParam query) throws IOException {
        opcPlcService.download(opcPlcService.queryAll(query), response);
    }*/

}
