package com.jiayue.ipfcst.console.service;


import com.jiayue.ipfcst.common.data.entity.ManualInterventionData;
import com.jiayue.ipfcst.common.data.entity.ManualInterventionLog;
import com.jiayue.ipfcst.common.data.entity.ManualInterventionRatioData;
import com.jiayue.ipfcst.common.data.repository.*;
import com.jiayue.ipfcst.fileupload.service.BaseUploadFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 人工干预数据逻辑类
 *
 * @author whc
 * @version 2.0
 * @since 2020/08/24 16:53
 */
@Service
@Slf4j
public class ManualInterventionRatioService extends BaseUploadFileService {

    private final ManualInterventionDataRepository manualInterventionDataRepository;
    private final ManualInterventionRatioDataRepository manualInterventionRatioDataRepository;
    private final ManualInterventionService manualInterventionService;
    private final ManualInterventionLogRepository manualInterventionLogRepository;

  public ManualInterventionRatioService(ManualInterventionDataRepository manualInterventionDataRepository, ManualInterventionRatioDataRepository manualInterventionRatioDataRepository, ManualInterventionService manualInterventionService, ManualInterventionLogRepository manualInterventionLogRepository) {
    this.manualInterventionDataRepository = manualInterventionDataRepository;
    this.manualInterventionRatioDataRepository = manualInterventionRatioDataRepository;
    this.manualInterventionService = manualInterventionService;
    this.manualInterventionLogRepository = manualInterventionLogRepository;
  }

  /**
   * 根据创建时间
   * @param startDate
   * @param endDate
   * @param fileType
   * @return
   */
  public List<ManualInterventionRatioData> queryManualInterventionRatio(Long startDate, Long endDate, String fileType) {
    return manualInterventionRatioDataRepository.findByCreateTimeBetweenAndInterveneType(startDate, endDate, fileType);
  }


  /**
   * 保存人工干预系数设置 系数直接保存，生成的时候才会乘系数
   * @param manualInterventionRatioData
   * @return
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public boolean saveManualInterventionRatio(ManualInterventionRatioData manualInterventionRatioData){
    List<ManualInterventionData> lists = manualInterventionService.queryManualIntervention(manualInterventionRatioData.getInterveneStartTime(),manualInterventionRatioData.getInterveneEndTime(),manualInterventionRatioData.getInterveneType());
    //如果该时间设置过点位则不可以保存
    if(lists.size()>0){
      return false;
    }
    try{
      //插入操作日志
      /*List<ManualInterventionRatioData> list =  queryManualInterventionRatio(manualInterventionRatioData.getInterveneStartTime(), manualInterventionRatioData.getInterveneEndTime(), manualInterventionRatioData.getInterveneType());
      for(ManualInterventionRatioData m : list){

      }*/
      manualInterventionRatioDataRepository.save(manualInterventionRatioData);
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String a = simpleDateFormat.format(manualInterventionRatioData.getInterveneStartTime()) + "-" + simpleDateFormat.format(manualInterventionRatioData.getInterveneEndTime()) + " " + manualInterventionRatioData.getRatio().toString();
      ManualInterventionLog manualInterventionLog = new ManualInterventionLog();
      manualInterventionLog.setInterveneTime(manualInterventionRatioData.getInterveneStartTime());
      manualInterventionLog.setOperationalContext(a);
      manualInterventionLog.setOperationalStatus(3);
      manualInterventionLog.setFileType(manualInterventionRatioData.getInterveneType());
      manualInterventionLog.setOperationalTime(new Date().getTime());
      manualInterventionLogRepository.save(manualInterventionLog);
      return true;
    }catch (Exception e){
      e.printStackTrace();
      log.info("保存人工干预系数设置失败"+e);
      return false;
    }

  }
  public Boolean delete(Integer id){
    try{
      ManualInterventionRatioData m = manualInterventionRatioDataRepository.queryById(id);
      ManualInterventionLog manualInterventionLog = new ManualInterventionLog();
      manualInterventionLog.setInterveneTime(m.getInterveneStartTime());
      manualInterventionLog.setOperationalContext("");
      manualInterventionLog.setOperationalStatus(4);
      manualInterventionLog.setFileType(m.getInterveneType());
      manualInterventionLog.setOperationalTime(new Date().getTime());
      manualInterventionLogRepository.save(manualInterventionLog);
      manualInterventionRatioDataRepository.deleteById(id);
      return true;
    }catch (Exception e){
      e.printStackTrace();
      log.info("删除人工干预系数设置失败"+e);
      return false;
    }
  }

}
