package com.jiayue.ipfcst.console.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.jiayue.ipfcst.common.core.util.DateTimeUtil;
import com.jiayue.ipfcst.common.core.util.JsonBeanUtil;
import com.jiayue.ipfcst.common.core.util.NumberUtils;
import com.jiayue.ipfcst.common.data.constant.enums.HolidayTypeEnum;
import com.jiayue.ipfcst.common.data.constant.enums.PredictionModelEnum;
import com.jiayue.ipfcst.common.data.entity.*;
import com.jiayue.ipfcst.common.data.entity.gathertunnelinfo.*;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.Sender104TcpTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.SenderCdtRtuTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.SenderModbusRtuTunnelInfo;
import com.jiayue.ipfcst.common.data.entity.sendertunnelinfo.SenderModbusTcpTunnelInfo;
import com.jiayue.ipfcst.common.data.repository.*;
import com.jiayue.ipfcst.common.data.service.BaseService;
import com.jiayue.ipfcst.dataexchange.service.ImpactOnContainerOperation;
import com.jiayue.ipfcst.dataexchange.service.ProtocolGatherDataPointService;
import com.jiayue.ipfcst.dataexchange.service.ProtocolSenderDataPointService;
import com.jiayue.ipfcst.dataexchange.service.TunnelInfoService;
import com.jiayue.ipfcst.fileupload.service.UploadFileChannelService;
import com.jiayue.ipfcst.fileupload.service.UploadFileCodeService;
import com.jiayue.ipfcst.fileupload.service.UploadObjectService;
import com.jiayue.ipfcst.fileupload.util.FileUtil;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
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.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 清理上报日志
 *
 * @author xsl
 * @version 3.0
 */
@Service
@Slf4j
public class DeleteUploadLogService extends BaseService {

  @Autowired
  UploadFileLogRepository uploadFileLogRepository;
  @Autowired
  UploadFileLogDetailRepository uploadFileLogDetailRepository;


  @Transactional(propagation = Propagation.REQUIRED)
  public void executeDelete() {
    log.info("-----------------开始执行清理上报日志任务----------------------");
    // 日志保留60天
    int keepDays = -60;
    // 删除到截止日期
    DateTime dateTime = DateUtil.offsetDay(new Date(), keepDays);
    String deleteEndTime = dateTime.toString(DatePattern.NORM_DATE_FORMAT);
    uploadFileLogRepository.deleteLessCreateTime(DateUtil.parse(deleteEndTime));
    uploadFileLogDetailRepository.deleteLessCreateTime(DateUtil.parse(deleteEndTime));
    log.info("-----------------执行清理上报日志完成----------------------");
  }
}
