package easydo.technology.controller;

import easydo.technology.model.Device;
import easydo.technology.service.DeviceService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/device")
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @PostMapping(value = "/getPage")
    public Object getPage(@RequestBody(required = false) Map<String, Object> map) throws Exception {
        if (map == null) {
            map = new HashMap<>();
        }
        Object page = deviceService.getPage(map);
        return new ResponseEntity<>(page, HttpStatus.OK);
    }

    @PostMapping(value = "/getList")
    public Object getList(@RequestBody(required = false) Map<String, Object> map) throws Exception {
        if (map == null) {
            map = new HashMap<>();
        }
        Object list = deviceService.getList(map);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @PostMapping(value = "/save")
    public Object save(@RequestBody Device model) throws Exception {
        Object result = deviceService.save(model);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @PostMapping(value = "/update")
    public Object update(@RequestBody Device model) throws Exception {
        Object result = deviceService.update(model);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @PostMapping(value = "/remove")
    public Object remove(@RequestBody Device model) throws Exception {
        Object result = deviceService.remove(model);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
