package com.jiayue.ipfcst.controller;

import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.DisplayConfig;
import com.jiayue.ipfcst.service.DisplayConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 展示配置restful接口
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/7 10:12
 */
@RestController
@Slf4j
public class DisplayConfigController {
  private final DisplayConfigService displayConfigService;

  @Autowired
  public DisplayConfigController(DisplayConfigService displayConfigService){
    this.displayConfigService = displayConfigService;


  }

  /**
   * 展示编码查询
   * @param displayCode 展示编码
   * @return result
   */
  @GetMapping(value="displayConfig/{displayCode}")
  public ResponseVO displayConfigQuery(@PathVariable("displayCode") String displayCode) {
    DisplayConfig displayConfig;
    try{
      displayConfig = displayConfigService.findByDisplayCode(displayCode);
      return ResponseVO.success(displayConfig);
    }catch(Exception e){
      e.printStackTrace();
      log.error("查询展示编码错误");
      return ResponseVO.fail(e.toString());
    }
  }
  /**
   * 保存展示编码查询
   * @param displayCode 展示编码
   * @return result
   */
  @PostMapping(value="displayConfig")
  public ResponseVO displayConfigSave(@RequestBody DisplayConfig displayCode) {
    DisplayConfig displayConfig;
    try {
      if(displayCode.getHiddenCode().equals("")||displayCode.getHiddenCode()==""){
        displayCode.setHiddenCode(",");
      }
      displayConfig = displayConfigService.save(displayCode);
      return ResponseVO.success(displayConfig);
    } catch (Exception e) {
      e.printStackTrace();
      log.error("保存展示编码错误");
      return ResponseVO.fail(e.toString());
    }
  }

  /**
   * 查询数据时间间隔设置
   */
  @GetMapping("displayConfig/timeInterval")
  public ResponseVO displayConfigTimeInterval() {
    DisplayConfig displayConfig;
    try {
      displayConfig = displayConfigService.displayConfigTimeInterval();
      return ResponseVO.success(displayConfig);
    } catch (Exception e) {
      e.printStackTrace();
      log.error("查询数据时间间隔设置错误");
      return ResponseVO.fail(e.toString());
    }
  }


  /**
   * 查询分页固定设置
   */
  @GetMapping("displayConfig/pageSize")
  public ResponseVO displayConfigPageSize() {
    DisplayConfig displayConfig;
    try {
      displayConfig = displayConfigService.displayConfigPageSize();
      return ResponseVO.success(displayConfig);
    } catch (Exception e) {
      e.printStackTrace();
      log.error("查询数据时间间隔设置错误");
      return ResponseVO.fail(e.toString());
    }
  }

  @GetMapping("displayConfig/eqTimeInterval")
  public ResponseVO displayConfigEqTimeInterval(){
    DisplayConfig displayConfig;
    try {
      displayConfig = displayConfigService.displayConfigEqTimeInterval();
      return ResponseVO.success(displayConfig);
    } catch (Exception e) {
      e.printStackTrace();
      log.error("查询设备数据时间间隔设置错误");
      return ResponseVO.fail(e.toString());
    }
  }


  @DeleteMapping("displayConfig/deleteById/{id}")
  public ResponseVO displayConfigEqTimeInterval(@PathVariable Integer id){
    try {
      displayConfigService.deleteById(id);
      return ResponseVO.success();
    } catch (Exception e) {
      e.printStackTrace();
      log.error("删除显示设置信息出错");
      return ResponseVO.fail(e.toString());
    }
  }
}
