package com.xydl.cac.controller;

import com.xydl.cac.entity.Jg;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.JgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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("area")
@Slf4j
public class JgController extends BasicController {

    @Resource
    JgService service;

    @GetMapping("listAll")
    @ApiOperation("查询列表")
    public Response<List<Jg>> listAll(Integer bdzid) {
        List<Jg> result = service.listAll(bdzid);
        return Response.success(result);
    }

    @GetMapping("sensorCount")
    @ApiOperation("查询监测装置数量")
    public Response<List<Jg>> sensorCount(Integer bdzid) {
        List<Jg> result = service.sensorCount(bdzid);
        return Response.success(result);
    }

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

    @PostMapping("update")
    @ApiOperation("更新")
    public Response<String> update(@Validated @RequestBody Jg item) throws Exception {
        if (item.getId() == null) {
            throw new BusinessException("id不能为空!");
        }
        service.update(item);
        return Response.success("OK");
    }

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

}
