package org.nl.acs.opc.rest;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.nl.acs.opc.service.OpcService;
import org.nl.acs.opc.service.dto.OpcDto;
import org.nl.common.logging.annotation.Log;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author jiaolm
 * @date 2023-05-09
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/opc")
public class OpcController {

    private final OpcService opcService;

    @GetMapping
    @Log("查询opc")
    public ResponseEntity<Object> query(@RequestParam Map whereJson, Pageable page) {
        return new ResponseEntity<>(opcService.queryAll(whereJson, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增opc")
    //@PreAuthorize("@el.check('opc:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody OpcDto dto) {
        opcService.insert(dto);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改opc")
    //@PreAuthorize("@el.check('opc:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody OpcDto dto) {
        opcService.update(dto);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除opc")
    //@PreAuthorize("@el.check('opc:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        opcService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出opc")
    @GetMapping(value = "/download")
    //@PreAuthorize("@el.check('opc:list')")
    public void download(HttpServletResponse response, Map whereJson) throws IOException {
        opcService.download(opcService.queryAll(whereJson), response);
    }

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

    @Log("导出opc")
    @PostMapping(value = "/getmeteal")
    //@PreAuthorize("@el.check('opc:list')")
    public ResponseEntity<Object> getmeteal(@RequestBody Map whereJson) throws IOException {
        return new ResponseEntity<>(opcService.getmeteal(whereJson), HttpStatus.OK);
    }

    @Log("新增PLC")
    @PostMapping(value = "/addPLC")
    //@PreAuthorize("@el.check('opc:list')")
    public ResponseEntity<Object> createPlc(@RequestBody Map whereJson) throws IOException {
        opcService.createPLC(whereJson);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }


    @GetMapping(value = "/queryServerPlc")
    //@PreAuthorize("@el.ceck('menu:list','roles:list')")
    public ResponseEntity<Object> query(@RequestParam String id) {
        return new ResponseEntity<>(opcService.queryServerPlc(id), HttpStatus.OK);
    }

    @Log("删除PLC")
    @PostMapping(value = "/delPLC")
    //@PreAuthorize("@el.check('opc:list')")
    public ResponseEntity<Object> delPLC(@RequestBody Map whereJson) throws IOException {
        opcService.delPLC(whereJson);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("编辑PLC")
    @PostMapping(value = "/editPLC")
    //@PreAuthorize("@el.check('opc:list')")
    public ResponseEntity<Object> editPLC(@RequestBody JSONObject json) throws IOException {
        opcService.editPLC(json);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
