Survey/src/main/java/com/example/survey/util/ThreadWordUtil.java
2021-05-23 14:57:31 +08:00

61 lines
1.6 KiB
Java

package com.example.survey.util;
import java.util.HashMap;
import java.util.Map;
import com.example.survey.dao.RecordDao;
import com.example.survey.entity.Record;
import com.example.survey.enumeration.RecordStateEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.Data;
@Data
@Component
public class ThreadWordUtil implements Runnable {
private Record record;
private Thread t;
private String threadName;
@Value("${file.path}")
private String path;
@Autowired
private RecordDao recordDao;
@Override
public void run() {
if(record != null) {
String templatePath = this.path + record.getMetaData().getName() + ".docx";
String outputPath = this.path + record.getUuid() + ".docx";
try{
Map<String, Object> values = record.getValues();
values.put("submit", record.getOperationList().get(0).getUser().getUsername());
values.put("review", record.getOperationList().get(0).getUser().getUsername());
WordUtil wordUtil = new WordUtil(values, templatePath, outputPath);
wordUtil.export2word();
record.setState(RecordStateEnum.REVIEWED.getValue());
recordDao.saveRecord(record);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public void start() {
if (t == null) {
t = new Thread(this, threadName);
t.start();
}
}
}