package com.xydl.cac.controller;

import com.xydl.cac.entity.Zsb;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.ZsbService;
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("zsb")
@Slf4j
public class ZsbController extends BasicController {

    @Resource
    ZsbService service;

    @GetMapping("listAll")
    @ApiOperation("查询全部列表")
    public Response<List<Zsb>> listAll(Integer jgid) throws Exception {
        List<Zsb> result = service.listAll(jgid);
        return Response.success(result);
    }

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

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

}
