------------恢复内容开始------------
1.定义一个通用实体类
package com.cnqisoft.eagle.upload.entity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.Date;
import java.util.UUID;
@Getter
@Setter
@ToString
@MappedSuperclass
public abstract class Upload {
@Id
@GenericGenerator(name = "uuid-generator", strategy = "uuid2")
@GeneratedValue(generator = "uuid-generator")
@Column(length = 16, updatable = false)
private String id;
@CreationTimestamp
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;
}
2.定义一个拓展的实体类并继承通用实体类
package com.cnqisoft.eagle.upload.entity;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.persistence.*;
import java.io.Serializable;
@Getter
@Setter
@ToString
public class UploadRecord extends Upload implements Serializable {
private String uuid; // uuid
private String path;
private String name; // 原文件名
private String fileTag;
private Long size;
private Integer status;
}
3.将通用的方法放在一个抽象类,以此作为实现类继承的拓展
package com.cnqisoft.eagle.upload.service;
import com.cnqisoft.eagle.upload.entity.Upload;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
public abstract class AbstractUploadService<T extends Upload, R extends JpaRepository<T, String>> {
protected final R uploadRepository;
AbstractUploadService(R repository) {
this.uploadRepository = repository;
}
@Transactional
public List<T> queryAllEntity(){
return uploadRepository.findAll();
}
@Transactional
public T queryEntityById(String id) {
return uploadRepository.findById(id).orElse(null);
}
@Transactional
public T insert(T entity) {
return uploadRepository.save(entity);
}
@Transactional
public T updateEntity(T entity) {
return uploadRepository.save(entity);
}
@Transactional
public void deleteEntity(String id) {
uploadRepository.deleteById(id);
}
}
2.定义业务接口
package com.cnqisoft.eagle.upload.service;
import com.cnqisoft.eagle.upload.entity.UploadInfo;
import com.cnqisoft.eagle.upload.entity.UploadRecord;
import com.cnqisoft.eagle.upload.entity.UploadState;
public interface UploadRecordService {
UploadRecord getUploadRecordByFileTag(String fileTag);
UploadRecord insertUploadRecord(UploadInfo uploadInfo, String path);
UploadRecord updateUploadRecord(UploadRecord record);
void deleteUploadRecord(String id);
UploadRecord updateUploadStatus(String fileTag, Integer Status);
UploadState checkUploadedFile(String fileTag);
}
3.实现类继承通用方法并,并实现业务接口并注入JpaRepository
package com.cnqisoft.eagle.upload.service;
import com.cnqisoft.eagle.upload.entity.UploadInfo;
import com.cnqisoft.eagle.upload.entity.UploadRecord;
import com.cnqisoft.eagle.upload.entity.UploadState;
import com.cnqisoft.eagle.upload.repository.UploadRecordRepository;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.Optional;
import java.util.UUID;
@Service
public class UploadRecordServiceImpl extends AbstractUploadService<UploadRecord, UploadRecordRepository> implements UploadRecordService {
UploadRecordServiceImpl(UploadRecordRepository repository) { super(repository); }
@Override
@Transactional
public UploadRecord getUploadRecordByFileTag(String fileTag) {
return uploadRepository.findByFileTag(fileTag);
}
@Override
@Transactional
public UploadRecord insertUploadRecord(UploadInfo uploadInfo, String path) {
UploadRecord uploadRecord = new UploadRecord();
uploadRecord.setId(uploadInfo.getFileTag());
uploadRecord.setName(uploadInfo.getName());
uploadRecord.setUuid(uploadInfo.getUuid());
uploadRecord.setSize(uploadInfo.getSize());
uploadRecord.setFileTag(uploadInfo.getFileTag());
uploadRecord.setStatus(0);
uploadRecord.setPath(path);
return uploadRecord;
}
@Override
public UploadRecord updateUploadRecord(UploadRecord record) {
return uploadRepository.save(record);
}
@Override
public void deleteUploadRecord(String id) {
uploadRepository.deleteById(id);
}
@Override
public UploadRecord updateUploadStatus(String fileTag, Integer Status) {
UploadRecord record = uploadRepository.findByFileTag(fileTag);
if (record != null) {
record.setStatus(Status);
record = this.updateUploadRecord(record);
}
return record;
}
@Override
public UploadState checkUploadedFile(String tag) {
UploadRecord uploadRecord = uploadRepository.findByFileTag(tag);
UploadState uploadState = new UploadState();
if (uploadRecord != null) {
Integer state = Optional.ofNullable(uploadRecord.getStatus()).orElse(0);
uploadState.setUuid(uploadRecord.getUuid());
uploadState.setState(state);
} else {
uploadState.setUuid(UUID.randomUUID().toString());
uploadState.setState(2);
}
return uploadState;
}
}