package com.jiayue.ipfcst.common.data.repository;


import com.jiayue.ipfcst.common.data.constant.enums.FileStatusEnum;
import com.jiayue.ipfcst.common.data.constant.enums.FileTypeEnum;
import com.jiayue.ipfcst.common.data.constant.enums.UploadProtocolEnum;
import com.jiayue.ipfcst.common.data.entity.UploadFileLog;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 上报文件日志表仓储
 *
 * @author xsl
 * @version 3.0
 */
public interface UploadFileLogRepository extends BaseRepository<UploadFileLog, Integer> {
	/**
	 * 查詢文件是否生成
	 *
	 * @param fileName
	 * @return
	 */
	List<UploadFileLog> findByFileNameAndFileTypeEnumAndCreateTimeBetween(String fileName,FileTypeEnum fileTypeEnum, Date jt, Date mt);

	/**
	 * 根据上报对象及文件名更新上报次数
	 *
	 * @param fileStatusEnum
	 * @param uploadCounter
	 * @param uploadObjectId
	 * @param fileName
	 */
	@Modifying
	@Query(value = "update UploadFileLog t set t.fileStatusEnum=?1,t.uploadCounter=?2 where t.uploadObjectId = ?3 and t.fileName = ?4")
	void updateFileUploadCount(FileStatusEnum fileStatusEnum, Integer uploadCounter, Integer uploadObjectId, String fileName);

	/**
	 * 根据上报对象ID和文件名称查找文件日志
	 *
	 * @param uploadObjectId
	 * @param fileName
	 * @param st
	 * @param et
	 * @return
	 */
	UploadFileLog findByUploadObjectIdAndFileNameAndCreateTimeBetween(Integer uploadObjectId, String fileName,Date st,Date et);
	/**
	 * 查询上报成功文件
	 * @param id 上报对象ID
	 * @param fileTypeEnum 文件类型
	 * @param fileStatusEnum 文件状态
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return 上报成功结果集
	 */
	List<UploadFileLog> findByUploadObjectIdAndFileTypeEnumAndFileStatusEnumAndCreateTimeBetween(Integer id, FileTypeEnum fileTypeEnum,FileStatusEnum fileStatusEnum, Date startTime, Date endTime);

	/**
	 * 删除指定日期之前的日志
	 */
	@Modifying
	@Query(value = "delete from UploadFileLog t where t.createTime < ?1")
	void deleteLessCreateTime(Date date);

	/**
	 * console启动获取未上报的文件
	 */
	@Query(value = "from UploadFileLog where fileStatusEnum=?1")
	List<UploadFileLog> queryFileUploadCache(FileStatusEnum fileStatusEnum);


	List<UploadFileLog> findByFileTypeEnumAndFileStatusEnumAndCreateTimeBetween(FileTypeEnum fileTypeEnum,FileStatusEnum fileStatusEnum, Date startTime, Date endTime);

	List<UploadFileLog> findByFileTypeEnumAndBackupCAndCreateTimeBetween(FileTypeEnum fileTypeEnum,String backupC,Date startTime,Date endTime);

	List<UploadFileLog> findByFileNameAndCreateTimeAfterAndFileTypeEnum(String fileName,Date creatTime,FileTypeEnum fileTypeEnum);


	@Modifying
	@Query(value = "update UploadFileLog t set t.fileStatusEnum=?1,t.uploadCounter=?2 where t.id = ?3")
	void uploadFileUpdate(FileStatusEnum fileStatusEnum,Integer uploadCounter,Integer id);

	/**
	 * 根据id更新文件已经生成了日志的标识
	 *
	 * @param backupC
	 * @param id
	 */
	@Modifying
	@Query(value = "update UploadFileLog t set t.backupC=?1 where t.id = ?2")
	void updateBackupC(String backupC, Integer id);
}
