package com.jiayue.ipfcst.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.data.entity.*;
import com.jiayue.ipfcst.common.data.repository.*;
import com.jiayue.ipfcst.common.data.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 展示配置业务层
 *
 * @author yh
 * @version 1.0
 * @since 2019/8/5 16:02
 */
@Service
public class DisplayConfigService extends BaseService {
  private final DisplayConfigRepository displayConfigRepository;

  @Autowired
  public DisplayConfigService(DisplayConfigRepository displayConfigRepository){
    this.displayConfigRepository = displayConfigRepository;
  }

  /**
   *  按编码查询数据
   * @param displayCode 展示编码
   * @return 符合条件对象
   */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public DisplayConfig findByDisplayCode(String displayCode)throws BusinessException {
    return displayConfigRepository.findByDisplayCode(displayCode);
  }

  /**
   *  保存展示配置
   * @param displayConfig 保存实体
   * @return 符合条件对象
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public DisplayConfig save(DisplayConfig displayConfig)throws BusinessException {
    return displayConfigRepository.save(displayConfig);
  }

  /**
   *  查询时间间隔设置，如果没有就新增
   * @return 符合条件对象
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public DisplayConfig displayConfigTimeInterval()throws BusinessException {

    DisplayConfig displayConfig = displayConfigRepository.findByDisplayCode("timeInterval");
    if(displayConfig == null){
      DisplayConfig displayConfig1 = new DisplayConfig();
      displayConfig1.setDisplayCode("timeInterval");
      displayConfig1.setShowCode("900000");
      displayConfig1.setHiddenCode("0");
      displayConfigRepository.save(displayConfig1);
      return displayConfig1;
    }

    return displayConfig;
  }


  /**
   *  查询分页page，如果没有就新增
   * @return 符合条件对象
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public DisplayConfig displayConfigPageSize()throws BusinessException {

    DisplayConfig displayConfig = displayConfigRepository.findByDisplayCode("pageSize");
    if(displayConfig == null){
      DisplayConfig displayConfig1 = new DisplayConfig();
      displayConfig1.setDisplayCode("pageSize");
      displayConfig1.setShowCode("10");
      displayConfig1.setHiddenCode("0");
      displayConfigRepository.save(displayConfig1);
      return displayConfig1;
    }

    return displayConfig;
  }


  /**
   *  查询分页page，如果没有就新增
   * @return 符合条件对象
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public DisplayConfig displayConfigEqTimeInterval()throws BusinessException {

    DisplayConfig displayConfig = displayConfigRepository.findByDisplayCode("eqTimeInterval");
    if(displayConfig == null){
      DisplayConfig displayConfig1 = new DisplayConfig();
      displayConfig1.setDisplayCode("eqTimeInterval");
      displayConfig1.setShowCode("60000");
      displayConfig1.setHiddenCode("0");
      displayConfigRepository.save(displayConfig1);
      return displayConfig1;
    }

    return displayConfig;
  }


  /**
   *  删除
   * @return 符合条件对象
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public void deleteById(Integer id)throws BusinessException {

    displayConfigRepository.deleteById(id);

  }




}
