package com.xydl.cac.controller;

import com.xydl.cac.entity.Lx;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.Response;
import com.xydl.cac.service.LxService;
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("lx")
@Slf4j
public class LxController extends BasicController {

    @Resource
    LxService service;

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

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

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

}
