package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.data.constant.enums.AlarmTypeEnum;
import com.jiayue.ipfcst.common.data.entity.SysAlarm;
import com.jiayue.ipfcst.common.data.entity.WindTurbineStatusData;
import com.jiayue.ipfcst.common.data.repository.SysAlarmRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import java.util.*;

@Service
@Slf4j
public class SysAlarmService {
    private final SysAlarmRepository sysAlarmRepository;

    @Autowired
    public SysAlarmService(SysAlarmRepository sysAlarmRepository) {
        this.sysAlarmRepository = sysAlarmRepository;
    }

    /**
     * 插入告警信息
     *
     * @param
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void save(SysAlarm sysAlarm) {

        this.sysAlarmRepository.save(sysAlarm);
    }

    /**
     * 删除告警信息
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    public void delete(Integer id) {
        sysAlarmRepository.deleteById(id);
    }

    /**
     * 查询告警信息
     *
     * @return 告警信息
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Page<SysAlarm> get(
            final SysAlarm sysAlarm, final Integer page, final Integer size) {
      List<Sort.Order> orders = new ArrayList<Sort.Order>();

      orders.add(new Sort.Order(Sort.Direction.DESC, "createTime"));


      Sort sort = Sort.by(orders);
      Specification<SysAlarm> specification = this.specification();
      Pageable pageable = PageRequest.of(page - 1, size, sort);
      Page windTurbineStatusDatas = sysAlarmRepository.findAll(specification,pageable);

      return windTurbineStatusDatas;
    }

  /**
   * 根据时间和编号 分页查询条件 yh
   *
   * @return 过滤条件
   */
  Specification<SysAlarm> specification() {
    return (Specification<SysAlarm>) (root, criteriaQuery, cb) -> {
      List<Predicate> predicates = new ArrayList<>();
      //添加排序的功能
      return cb.and(predicates.toArray(new Predicate[predicates.size()]));

    };
  }

    /**
      * 查询告警信息
     *
       * @return 告警信息
     */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public List<SysAlarm> getAll(){
    return this.sysAlarmRepository.findAll();
  }

    public static void main(String[] args) {
        String val = "http://www.163.com?id=";
        for (int i = 1; i < 97; i++) {
            val = val + i + ":20.22|";
        }
        System.out.println(val);
    }

  /**
   * 根据描述查询告警信息
   *
   * @return 告警信息
   */
  @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
  public Page<SysAlarm> getForNameAndAlarmTypeEnum(SysAlarm sysAlarm, Integer page, Integer size, AlarmTypeEnum alarmTypeEnum){
    sysAlarm.setAlarmTypeEnum(alarmTypeEnum);
    ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("modelNumber", ExampleMatcher.GenericPropertyMatchers.contains())
      .withMatcher("alarmTypeEnum", ExampleMatcher.GenericPropertyMatchers.contains());
    Example<SysAlarm> example = Example.of(sysAlarm, matcher);
    Pageable pageable = PageRequest.of(page - 1, size);
    return this.sysAlarmRepository.findAll(example, pageable);
  }


}
