package com.iformall.controller.device;

import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;

import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxDevice;
import com.iformall.enums.EnumDeviceOnlineStatus;
import com.iformall.enums.EnumDeviceStatus;
import com.iformall.service.WxDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("wxDevice")
@Api(description="IOT设备")
public class WxDeviceController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private WxDeviceService wxDeviceService;


	@ApiOperation("设备列表")
	@GetMapping("list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "IOT设备-设备列表")
	public Result list(@ModelAttribute WxDevice wxDevice, Integer pageNum, Integer pageSize) {
		wxDevice.setTenantId(getTenantId());
		wxDevice.setStatus(EnumDeviceStatus.VALID.getCode());
		return new ResultData(wxDeviceService.listAsPage(wxDevice, pageNum,pageSize));
	}

    @ApiOperation("添加设备")
    @PostMapping("add")
    @SystemControllerLog(description = "IOT设备-添加设备")
    public Result add(@RequestBody WxDevice wxDevice) {

		WxDevice record = new WxDevice();
		record.setDeviceId(wxDevice.getDeviceId());
		record.setStatus(EnumDeviceStatus.VALID.getCode());
		List<WxDevice> list = wxDeviceService.findList(record);
		if (list.size() > 0)
			return new ResultData(ErrorCode.DEVICE_ALREADY_EXIST);
		wxDevice.setStatus(EnumDeviceStatus.VALID.getCode());
		wxDevice.setOnlineStatus(EnumDeviceOnlineStatus.OFFLINE.getCode());
		wxDevice.setTenantId(getTenantId());

    	return wxDeviceService.saveOrUpdate(wxDevice);
    }

	@ApiOperation("更新设备")
	@PostMapping("update")
    @SystemControllerLog(description = "IOT设备-更新设备")
	public Result update(@RequestBody WxDevice wxDevice) {
    	if (wxDevice == null)
    		return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		if (wxDevice.getId() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		wxDevice.setTenantId(getTenantId());

		return wxDeviceService.saveOrUpdate(wxDevice);
	}

	@ApiOperation("设备详情")
	@GetMapping("detail")
    @SystemControllerLog(description = "IOT设备-设备详情")
	public Result detail(@RequestParam Long id) {
		if (id == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		WxDevice wxDevice = wxDeviceService.getById(id);
		if (wxDevice == null)
			return new ResultData(ErrorCode.SCREENAD_NOT_FOUND);

		return new ResultData(wxDevice);
	}

	@ApiOperation("删除设备")
	@PostMapping("delete")
    @SystemControllerLog(description = "IOT设备-删除设备")
	public Result delete(@RequestParam Long id) {
		if (id == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		WxDevice wxDevice = new WxDevice();
		wxDevice.setId(id);
		wxDevice.setTenantId(getTenantId());
		wxDevice.setStatus(EnumDeviceStatus.INVALID.getCode());

		return wxDeviceService.saveOrUpdate(wxDevice);

	}

}
