package com.sv.oms.controller;
import com.sv.service.oms.DeviceService;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.oms.controller.OmsController;
import com.sv.entity.Device;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Controller - 门禁设备
 *
 * @author zhaoziyu
 * @since 2018-08-09
 */
@RestController
public class DeviceController extends OmsController {

	private final Logger logger = LoggerFactory.getLogger(DeviceController.class);
	
	@Resource
	private DeviceService deviceService;

	/**
	 * 分页查询门禁设备
	 */
	@RequestMapping(value = "/devices", method = RequestMethod.GET)
	public ResponseDTO findPage(Pagination pagination) {
		return ResponseDTO.ok()
				.setPagination(deviceService.findPage(pagination));
	}

	/**
	 * 查询门禁设备信息
	 */
	@RequestMapping(value = "/device/{id}", method = RequestMethod.GET)
	public ResponseDTO findById(@PathVariable("id") Integer id) {
		return ResponseDTO.ok()
				.addAttribute("device", deviceService.findById(id));
	}

	/**
	 * 保存门禁设备
	 */
	@RequestMapping(value = "/device", method = RequestMethod.POST)
	public ResponseDTO save(Device device) {
		deviceService.save(device);
		return ResponseDTO.ok("保存成功");
	}

	/**
	 * 删除门禁设备
	 */
	@RequestMapping(value = "/device/delete", method = RequestMethod.POST)
	public ResponseDTO delete(@RequestParam("id[]") Integer[] ids) {
		deviceService.deleteByIds(ids);
		return ResponseDTO.ok("删除成功");
	}

	/**
	 * 重新连接
	 * @param deviceId
	 * @return
	 */
	@RequestMapping("/device/reconnect/{id}")
	public ResponseDTO reconnect(@PathVariable("id") Integer deviceId){
		deviceService.reconnect(deviceId);
		return ResponseDTO.ok("开始连接，请等待连接完成");
	}

}
