package com.xydl.cac.controller;

import com.xydl.cac.entity.NPoint;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.NPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.repository.query.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Api(tags = {"监测设备属性相关接口"})
@RequestMapping("npoint")
@Slf4j
public class NPointController extends BasicController {

    @Resource
    NPointService service;

    @GetMapping("listAll")
    @ApiOperation("查询全部")
    public Response<List<NPoint>> listAll(@ApiParam("监测设备id") @RequestParam(value = "sensorId", required = true) Integer sensorId) throws Exception {
        List<NPoint> result = service.listAll(sensorId);
        return Response.success(result);
    }

    @PostMapping("add")
    @ApiOperation("新增")
    public Response<NPoint> add(@Validated @RequestBody NPoint item) throws Exception {
        NPoint result = service.add(item);
        return Response.success(result);
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public Response<String> delete(@Validated @NotNull(message = "id不能为空!") @Param("id") Integer id) throws Exception {
        if (id == null) {
            throw new BusinessException("id不能为空!");
        }
        service.delete(id);
        return Response.success("OK");
    }

}
