package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.data.entity.DataLimitation;
import com.jiayue.ipfcst.common.data.entity.DataLimitationLog;
import com.jiayue.ipfcst.common.data.entity.ManualInterventionData;
import com.jiayue.ipfcst.common.data.entity.ManualInterventionLog;
import com.jiayue.ipfcst.common.data.repository.DataLimitationLogRepository;
import com.jiayue.ipfcst.common.data.repository.DataLimitationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class DataLimitationService {
  @Autowired
  private DataLimitationRepository dataLimitationRepository;
  @Autowired
  private DataLimitationLogRepository dataLimitationLogRepository;


  /**
   * 根据开始结束时间查询数据限制数据
   * @param startTime 开始时间
   * @param endTime 结束时间
   * @return
   */
  @Transactional(propagation = Propagation.REQUIRED)
  public List<DataLimitation> queryDataLimitation(Long startTime, Long endTime){
    List<DataLimitation> dataLimitationList = dataLimitationRepository.findByLimitTimeBetween(startTime,endTime);

    dataLimitationList.sort(Comparator.comparing(DataLimitation::getLimitTime));
    return dataLimitationList;
  }

  @Transactional(propagation = Propagation.REQUIRED)
  public boolean saveDataLimitation(List<Map<String,String>> list){
    Map<String,String> map1 = new HashMap<>();
    try{
      List<DataLimitation> lists =queryDataLimitation(Long.parseLong(list.get(0).get("lastModifyTime")),Long.parseLong(list.get(0).get("lastModifyTime"))+60*60*24*1000 -1000);
      dataLimitationRepository.deleteByLimitTimeBetween(Long.parseLong(list.get(0).get("lastModifyTime")),Long.parseLong(list.get(0).get("lastModifyTime"))+60*60*24*1000 -1000);
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      Date systemDate = new Date();
      List<DataLimitation> dataLimitationList = new ArrayList<>();
      for(int i = 0 ;i< list.size() ;i++){
        map1 = list.get(i);
        if(map1.get("dataMax1")!=""&&map1.get("dataMax1")!=null||map1.get("dataMin1")!=""&&map1.get("dataMin1")!=null){
          DataLimitation dataLimitation = new DataLimitation();
          String strDate = map1.get("time1");
          Date date =simpleDateFormat.parse(strDate);
          if(map1.get("dataMax1")!=""&&map1.get("dataMax1")!=null){
            dataLimitation.setUpperLimit(new BigDecimal(map1.get("dataMax1")));
          }
          if(map1.get("dataMin1")!=""&&map1.get("dataMin1")!=null ){
            dataLimitation.setLowerLimit(new BigDecimal(map1.get("dataMin1")));
          }

          dataLimitation.setLimitTime(date.getTime());
          dataLimitationList.add(dataLimitation);
        }
        if(map1.get("dataMax2")!=""&&map1.get("dataMax2")!=null||map1.get("dataMin2")!=""&&map1.get("dataMin2")!=null){
          DataLimitation dataLimitation = new DataLimitation();
          String strDate = map1.get("time2");
          Date date =simpleDateFormat.parse(strDate);
          if(map1.get("dataMax2")!="" &&map1.get("dataMax2")!=null ){
            dataLimitation.setUpperLimit(new BigDecimal(map1.get("dataMax2")));
          }
          if(map1.get("dataMin2")!="" &&map1.get("dataMin2")!=null ){
            dataLimitation.setLowerLimit(new BigDecimal(map1.get("dataMin2")));
          }
          dataLimitation.setLimitTime(date.getTime());
          dataLimitationList.add(dataLimitation);
        }
        if(map1.get("dataMax3")!=""&&map1.get("dataMax3")!=null||map1.get("dataMin3")!=""&&map1.get("dataMin3")!=null){
          DataLimitation dataLimitation = new DataLimitation();
          String strDate = map1.get("time3");
          Date date =simpleDateFormat.parse(strDate);
          if(map1.get("dataMax3")!="" &&map1.get("dataMax3")!=null ){
            dataLimitation.setUpperLimit(new BigDecimal(map1.get("dataMax3")));
          }
          if(map1.get("dataMin3")!="" &&map1.get("dataMin3")!=null ){
            dataLimitation.setLowerLimit(new BigDecimal(map1.get("dataMin3")));
          }
          dataLimitation.setLimitTime(date.getTime());
          dataLimitationList.add(dataLimitation);
        }
        if(map1.get("dataMax4")!=""&&map1.get("dataMax4")!=null||map1.get("dataMin4")!=""&&map1.get("dataMin4")!=null){
          DataLimitation dataLimitation = new DataLimitation();
          String strDate = map1.get("time4");
          Date date =simpleDateFormat.parse(strDate);
          if(map1.get("dataMax4")!="" &&map1.get("dataMax4")!=null ){
            dataLimitation.setUpperLimit(new BigDecimal(map1.get("dataMax4")));
          }
          if(map1.get("dataMin4")!="" &&map1.get("dataMin4")!=null ){
            dataLimitation.setLowerLimit(new BigDecimal(map1.get("dataMin4")));
          }
          dataLimitation.setLimitTime(date.getTime());
          dataLimitationList.add(dataLimitation);
        }
        dataLimitationRepository.saveAll(dataLimitationList);
      }
      List<DataLimitation> listss =queryDataLimitation(Long.parseLong(list.get(0).get("lastModifyTime")),Long.parseLong(list.get(0).get("lastModifyTime"))+60*60*24*1000 -1000);
      DataLimitationLog dataLimitationLog = new DataLimitationLog();
      List<String> strings = new ArrayList<>();
      String a;
      String b;

      String c;
      String d = new String();
      listss.sort(Comparator.comparing(DataLimitation::getLimitTime));
      if(lists.size() > 0&&listss.size() > 0){
        //保存前有数据 保存后有数据 修改
        for(int i = 0; i < listss.size(); i++){
          a = simpleDateFormat.format(listss.get(i).getLimitTime());


          if(listss.get(i).getUpperLimit() != null){
            b = listss.get(i).getUpperLimit().toString();
            c = "上限值";
            d= d + a+" "+c +" "+ b+System.getProperty("line.separator");
          }
          if(listss.get(i).getLowerLimit() != null){
            b = listss.get(i).getLowerLimit().toString();
            c = "下限值";
            d= d + a+" "+c +" "+ b+System.getProperty("line.separator");
          }

        }
        dataLimitationLog.setOperationalTime(systemDate.getTime());
        dataLimitationLog.setOperationalContext(d);
        dataLimitationLog.setOperationalStatus(1);
        dataLimitationLog.setLimitTime(Long.parseLong(list.get(0).get("lastModifyTime")));
        dataLimitationLogRepository.save(dataLimitationLog);
      }
      if(lists.size()==0&&listss.size() > 0){
        //保存前没数据 保存后有数据  新增
        for(int i = 0; i < listss.size(); i++){
          a = simpleDateFormat.format(listss.get(i).getLimitTime());


          if(listss.get(i).getUpperLimit() != null){
            b = listss.get(i).getUpperLimit().toString();
            c = "上限值";
            d= d + a+" "+c +" "+ b+System.getProperty("line.separator");
          }
          if(listss.get(i).getLowerLimit() != null){
            b = listss.get(i).getLowerLimit().toString();
            c = "下限值";
            d= d + a+" "+c +" "+ b+System.getProperty("line.separator");
          }
        }
        dataLimitationLog.setOperationalTime(systemDate.getTime());
        dataLimitationLog.setOperationalContext(d);
        dataLimitationLog.setOperationalStatus(0);
        dataLimitationLog.setLimitTime(Long.parseLong(list.get(0).get("lastModifyTime")));
        dataLimitationLogRepository.save(dataLimitationLog);
      }
      if(lists.size()>0&&listss.size() == 0){
        //保存前有数据 保存后没数据  删除
        dataLimitationLog.setOperationalTime(systemDate.getTime());
        dataLimitationLog.setOperationalContext(d);
        dataLimitationLog.setOperationalStatus(2);
        dataLimitationLog.setLimitTime(Long.parseLong(list.get(0).get("lastModifyTime")));
        dataLimitationLogRepository.save(dataLimitationLog);
      }

    }catch (Exception e){
      e.getStackTrace();
      return false;
    }
    return true;
  }

}
