package com.jiayue.ipfcst.dataexchange.service;

import com.jiayue.ipfcst.dataexchange.domain.TemplatePoint;
import com.jiayue.ipfcst.dataexchange.repository.TemplatePointRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 模版点位的操作类
 *
 * @author 修唯xiuwei
 * @version 3.0
 */
@Service
public class TemplatePointService {

  /**
   * Template point repository
   */
  @Autowired
  TemplatePointRepository templatePointRepository;


  /**
   * 新增一个模版实体
   *
   * @param templatePoint 模版点位实体
   * @return TemplatePoint template point
   */
  public TemplatePoint add(TemplatePoint templatePoint) {
    return templatePointRepository.save(templatePoint);
  }


  /**
   * 查询符合条件的模版实体
   *
   * @param templatePoint 模版点位实体
   * @return TemplatePoint list
   */
  public List<TemplatePoint> get(TemplatePoint templatePoint) {
    if (templatePoint == null) {
      return templatePointRepository.findAll();
    }
    return templatePointRepository.findAll(Example.of(templatePoint));
  }


  /**
   * 新增若干模版实体
   *
   * @param templatePoints 模版点位实体List
   * @return TemplatePoints list
   */
  public List<TemplatePoint> addAll(List<TemplatePoint> templatePoints) {
    return templatePointRepository.saveAll(templatePoints);
  }


  /**
   * 删除模版点位 by 模版id
   *
   * @param templatePointId template point id
   */
  public void deleteById(Integer templatePointId) {
    templatePointRepository.deleteById(templatePointId);
  }

  /**
   * 删除满足条件的模版点位
   *
   * @param templatePoint 模版点位实体
   */
  public void delete(TemplatePoint templatePoint) {
    templatePointRepository.delete(templatePoint);
  }


  /**
   * 修改模版点位
   *
   * @param templatePoint 模版点位实体
   * @return TemplatePoint template point
   */
  public TemplatePoint update(TemplatePoint templatePoint) {
    if (templatePoint.getId() == null) {
      return null;
    }
    return templatePointRepository.save(templatePoint);
  }


  /**
   * 通过绑定的模版id来修改点表绑定的通道
   *
   * @param tunnelId                通道id
   * @param equipmentDataTemplateId 设备数据模板id
   * @return int
   */
  public int updateTunnelByTemplateId(String tunnelId, Integer equipmentDataTemplateId) {
    return templatePointRepository.updateTunnelByTemplateId(tunnelId, equipmentDataTemplateId);
  }


}
