package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.core.exception.BusinessException;
import com.jiayue.ipfcst.common.data.entity.HolidayCalendar;
import com.jiayue.ipfcst.common.data.repository.HolidayCalendarRepository;
import com.jiayue.ipfcst.common.data.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 假期日历业务类
 *
 * @author tl
 * @version 1.0
 * @since 2020/03/11 9:49
 */
@Service
@Slf4j
public class HolidayCalendarService extends BaseService {
  private final HolidayCalendarRepository holidayCalendarRepository;

  @Autowired
  public HolidayCalendarService(HolidayCalendarRepository holidayCalendarRepository) {
    this.holidayCalendarRepository = holidayCalendarRepository;
  }

  /**
   * 新增假期日历信息
   *
   * @param holidayCalendar 假期日历信息
   */
  public void save(HolidayCalendar holidayCalendar)
    throws BusinessException {
    this.holidayCalendarRepository.save(holidayCalendar);
  }

  /**
   * 删除假期日历信息
   */
  @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
  public void delete(final Integer id) throws BusinessException {
    this.holidayCalendarRepository.deleteById(id);
  }

  /**
   * 修改假期日历信息
   */
  @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
  public void update(HolidayCalendar holidayCalendar)
    throws BusinessException {
    if (holidayCalendar.getId() == null) {
      throw new BusinessException("日历编号不能为空");
    } else {
      this.holidayCalendarRepository.save(holidayCalendar);
    }
  }

  /**
   * 分页查询
   */
  @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS, readOnly = true)
  public Page<HolidayCalendar> get(
    final HolidayCalendar holidayCalendar, final Integer page, final Integer size) {
    ExampleMatcher matcher =
      ExampleMatcher.matching().withMatcher("id", ExampleMatcher.GenericPropertyMatchers.contains());
    Example<HolidayCalendar> example = Example.of(holidayCalendar, matcher);
    Pageable pageable = PageRequest.of(page - 1, size);
    return this.holidayCalendarRepository.findAll(example, pageable);

  }


  @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS, readOnly = true)
  public List<HolidayCalendar> getAll() {

    return this.holidayCalendarRepository.findAll();

  }

  /**
   * 假期数据直接清空原数据，保存新数据
   * @param beans
   */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCloud(List<HolidayCalendar> beans) {
        holidayCalendarRepository.deleteAll();
        holidayCalendarRepository.saveAll(beans);
    }

  /**
   * 计算出距离当前最近且在当前时间之后的时间
   * @return
   */
  public HolidayCalendar findByNextTime(){

    List<HolidayCalendar> holidayCalendarList = holidayCalendarRepository.findAll();
    HolidayCalendar holidayCalendar1 = new HolidayCalendar();
    holidayCalendar1.setStartTime(0l);
    if(holidayCalendarList.size()>0){

      Long time = System.currentTimeMillis();
      for(HolidayCalendar holidayCalendar:holidayCalendarList){
        Long difference = holidayCalendar.getStartTime() - time;
        if(holidayCalendar.getStartTime()>time){
          if(holidayCalendar1.getStartTime()==0){
//          holidayCalendar1.setStartTime(difference);
            holidayCalendar1 = holidayCalendar;
          }
          if(difference>0l&&difference<holidayCalendar1.getStartTime()){
            holidayCalendar1 = holidayCalendar;
          }
        }

      }
    }

    return holidayCalendar1;
  }
}
