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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;

import net.northking.cctp.device.api.engineInfo.service.CdEngineInfoApiService;
import net.northking.cctp.device.db.entity.CdEngineInfo;
import net.northking.cctp.device.dto.engineInfo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "执行引擎信息表")
@RequestMapping("/v1")
@RestController
public class CdEngineInfoQueryCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(CdEngineInfoQueryCtrl.class);

  @Autowired
  private CdEngineInfoApiService apiService;

  /**
   * 查询用户列表
   *
   * @param queryByPage  查询条件
   * @return 数据列表
   */
  @ApiOperation(value = "分页查询列表")
  @RequiredPermissions("engineManage")
  @PostMapping(value = "/paging/query/CdEngineInfos",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Pagination<CdEngineInfoDetailDto>> pagingQuery(
      @RequestBody QueryByPage<CdEngineInfoQueryDto> queryByPage)
  {
    ResultWrapper<Pagination<CdEngineInfoDetailDto>> wrapper = new ResultWrapper<>();
    Pagination<CdEngineInfoDetailDto> page = apiService.pagingQuery(queryByPage);

    return wrapper.success(page);
  }



  @ApiOperation(value = "重启引擎")
  @RequiredPermissions({"ui-sassEnRestart","ui-sassEnRestarts"})
  @PostMapping(value = "/engine/restart",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> restart(@RequestBody List<String> list){
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String restart = this.apiService.restart(list);
    return wrapper.success(restart);
  }

  @ApiOperation(value = "更新引擎版本")
  @RequiredPermissions({"ui-sassEnUpdate","ui-sassEnUpdates"})
  @PostMapping(value = "/engine/update-version",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> updateVersion(@RequestBody List<String> ids){
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer result = this.apiService.updateVersion(ids);
    return wrapper.success(result);
  }

  @ApiOperation(value = "获取一个可用执行引擎")
  @GetMapping(
          value = "/find/engine/{proId}",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<CdEngineInfo> getEngineInfo( @PathVariable("proId") String proId){
    ResultWrapper<CdEngineInfo> wrapper = new ResultWrapper<>();
    CdEngineInfo engineInfo = this.apiService.getEngineInfo(proId);
    return wrapper.success(engineInfo);
  }

  @ApiOperation(value = "根据引擎id释放设备")
  @GetMapping(
          value = "/release/engine/{engineId}",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> releaseDevice( @PathVariable("engineId") String engineId){
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String result = this.apiService.releaseDevice(engineId);
    return wrapper.success(result);
  }


}


