package com.xydl.cac.controller;

import com.xydl.cac.entity.ModevType;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.ModevTypeService;
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("modevtype")
@Slf4j
public class ModevTypeController extends BasicController {

    @Resource
    ModevTypeService service;

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

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

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

    @PostMapping("update")
    @ApiOperation("更新")
    public Response<String> update(@Validated @RequestBody ModevType 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");
    }

    @PostMapping("detail")
    @ApiOperation("详情")
    public Response<ModevType> detail(@Validated @NotNull(message = "id不能为空!") Integer id) throws Exception {
        if (id == null) {
            throw new BusinessException("id不能为空!");
        }
        ModevType detail = service.detail(id);
        return Response.success(detail);
    }

}
