/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved.
*
*/
package net.northking.cctp.device.api.pcDevice;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;

import net.northking.cctp.device.api.pcDevice.service.CdPcDeviceApiService;
import net.northking.cctp.device.db.entity.CdDeviceStatus;
import net.northking.cctp.device.db.entity.CdEngineInfo;
import net.northking.cctp.device.db.entity.CdPcDevice;
import net.northking.cctp.device.db.entity.CdPcUser;
import net.northking.cctp.device.db.service.CdDeviceStatusService;
import net.northking.cctp.device.dto.pcDevice.*;
import net.northking.cctp.device.util.DeviceStatusUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "PC设备管理")
@RequestMapping("/v1")
@RestController
public class CdPcDeviceQueryCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(CdPcDeviceQueryCtrl.class);

  @Autowired
  private CdPcDeviceApiService cdPcDeviceApiService;

  @Autowired
  private CdDeviceStatusService cdDeviceStatusService;

  @Autowired
  private RedisLockRegistry redisLockRegistry;

  @Autowired
  private DeviceStatusUtil statusUtil;

  @ApiOperation(value = "根据PC设备查询执行引擎信息")
  @GetMapping(value = "/device/pc/engine/{uuid}",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<CdEngineInfo> findEngineById(
          @PathVariable("uuid") String uuid)
  {
    ResultWrapper<CdEngineInfo> wrapper = new ResultWrapper<>();
    CdEngineInfo result = this.cdPcDeviceApiService.findEngineById(uuid);
    return wrapper.success(result);
  }


  @ApiOperation(value = "注册登记(PC端)")
  @PostMapping(value = "/device/register/pc",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> registerPC(
          @RequestBody CdPcDevice device){
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String result = this.cdPcDeviceApiService.registerPC(device);
    return wrapper.success(result);
  }

  @ApiOperation(value = "修改设备状态")
  @PostMapping(value = "/device/pc/update/status",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> updateStatus(@RequestBody CdPcDeviceStatusDto deviceStatusDto) {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    //修改设备状态
    CdDeviceStatus status = cdDeviceStatusService.findByPrimaryKey(deviceStatusDto.getId());
    status.setId(deviceStatusDto.getId());
    status.setDeviceStatus(deviceStatusDto.getDeviceStatus());
    if (status.getDeviceType().equals("1")){
      statusUtil.updateMobStatus(status);
    }else {
      statusUtil.updatePcStatus(status);
    }
    return wrapper.success(1);
  }

  @ApiOperation(value = "根据id查询PC设备用户")
  @GetMapping(value = "/query/pc/user/{uuid}",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<List<CdPcUser>> queryPcUser(@PathVariable("uuid") String uuid) {
    ResultWrapper<List<CdPcUser>> wrapper = new ResultWrapper<>();
    List<CdPcUser> users = this.cdPcDeviceApiService.queryDeviceUser(uuid);
    return wrapper.success(users);
  }

  @ApiOperation(value = "新增PC设备用户")
  @PostMapping(value = "/update/pc/user",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> updatePcUser(@RequestBody CdPcUserUpdateDto dto){
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer result = this.cdPcDeviceApiService.updatePcUser(dto);
    return wrapper.success(result);
  }


}


