package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.data.constant.enums.ElectricFieldStatrsEnum;
import com.jiayue.ipfcst.common.data.entity.ElectricField;
import com.jiayue.ipfcst.common.data.entity.UploadFileChannel;
import com.jiayue.ipfcst.common.data.repository.ElectricFieldRepository;
import com.jiayue.ipfcst.common.data.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import net.sf.ehcache.search.Attribute;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Result;
import net.sf.ehcache.search.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 场站信息业务层
 *
 * @author zzy
 * @version 1.0
 * @since 2019/8/7 9:53
 */
@Service
@Slf4j
public class ElectricFieldService extends BaseService {
  @Autowired
  EhCacheCacheManager ehCacheCacheManager;

  private final ElectricFieldRepository electricFieldRepository;

  @Autowired
  public ElectricFieldService(ElectricFieldRepository electricFieldRepository) {
    this.electricFieldRepository = electricFieldRepository;
  }

  /**
   * 修改场站信息
   *
   * @param electricField 场站信息
   */
  @Transactional(propagation = Propagation.SUPPORTS)
  public void update(ElectricField electricField) {
    clearElectricFieldCache();
    electricFieldRepository.deleteAll();


    this.electricFieldRepository.save(electricField);
  }

  /**
   * 保存场站信息
   *
   * @param electricField 场站信息
   */
  @Transactional(propagation = Propagation.SUPPORTS)
  public void add(ElectricField electricField) {
    clearElectricFieldCache();
    electricField.setStationStatusEnum(ElectricFieldStatrsEnum.E1);
    this.electricFieldRepository.save(electricField);
  }


  /**
   * 删除场站信息
   */
  @Transactional(propagation = Propagation.SUPPORTS)
  public void delete(String stationCode){
    clearElectricFieldCache();
    electricFieldRepository.deleteById(stationCode);
  }

  /**
   * 移除缓存中的场站信息
   */
  public void clearElectricFieldCache(){
    ElectricField electricField = this.get();
    if(electricField!=null){
      Cache cache = ehCacheCacheManager.getCacheManager().getCache("searchablecache");
      cache.remove("efi" + electricField.getStationCode());
    }

  }

  /**
   * 查询场站信息
   *
   * @return 场站信息
   */
  public ElectricField get(){
    Cache cache = ehCacheCacheManager.getCacheManager().getCache("searchablecache");
    Query query = cache.createQuery();
    //查询结果中包含Key和value
    query.includeKeys().includeValues();
    Attribute<String> keyName = cache.getSearchAttribute("key");
    query.addCriteria(keyName.ilike("*efi*"));
    Results results = query.execute();
    List<Result> resultList = results.all();

    ElectricField electricField = null;
    if (resultList != null && !resultList.isEmpty()) {
      for (Result result : resultList) {
        electricField = (ElectricField) result.getValue();
        break;
      }
      results.discard();
    } else {
      //查询数据库
      try {
        electricField = super.getElectricField();
      } catch (BusinessException e) {
        log.info("查找场站信息出错");
      }
      if (electricField!=null) {
        //将结果存入缓存
        cache.put(new Element("efi" + electricField.getStationCode(), electricField));
      }
    }
    return electricField;
  }
}
