integrated_traffic/src/main/java/com/zcloud/util/archivesUtil/SignAbsHandler.java

173 lines
7.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.zcloud.util.archivesUtil;
import com.zcloud.service.eduArchives.TrainingScheduleService;
import com.zcloud.entity.PageData;
import com.zcloud.service.eduArchives.ArchivesReviewService;
import com.zcloud.service.eduArchives.ArchivesService;
import com.zcloud.service.eduStudy.ClassPostService;
import com.zcloud.service.eduStudy.ClassService;
import com.zcloud.service.eduStudy.TrainLevelTypeService;
import com.zcloud.service.corp.CorpInfoService;
import com.zcloud.service.system.PostService;
import com.zcloud.util.Const;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Tools;
import com.zcloud.util.WordToPdfUtil;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.images.ByteArrayImageProvider;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import fr.opensagres.xdocreport.template.formatter.NullImageBehaviour;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 说明:安全培训教育记录及签字表
* 作者wangxuan
* 官网www.zcloudchina.com
*/
@Component
public class SignAbsHandler extends AbsHandlerBatchExport {
@Autowired
private ArchivesService archivesService;
@Resource
private ClassService classService;
@Autowired
private TrainLevelTypeService trainLevelTypeService;
@Resource
private CorpInfoService corpInfoService;
@Resource
private ArchivesReviewService archivesReviewService;
@Autowired
private TrainingScheduleService trainingscheduleService;
@Autowired
private PostService postService;
@Autowired
private ClassPostService classPostService;
/*
* 同一前缀 archives101
* 同一前缀 archives102
* varList: [{ NAME: '安全培训教材会审表', TYPE: 100 },
{ NAME: '培训日程安排通知/也可称为培训计划', TYPE: 101 },
{ NAME: '安全培训教材或课程讲义', TYPE: 106 },
{ NAME: '安全培训教育记录及签字表', TYPE: 102 },
{ NAME: '培训影像资料', TYPE: 107 },
{ NAME: '培训考核试卷', TYPE: 103 },
{ NAME: '学员考核成绩统计表', TYPE: 104 },
{ NAME: '本期综合考评报告', TYPE: 105 },
{ NAME: '与培训机构签订的服务合同或者协议复印件', TYPE: 6 }],
* */
@Override
public File batchExport(PageData pd, String nowZip) throws Exception {
String projectPath = pd.getString("projectPath");
PageData clazz = classService.findById(pd);
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy年MM月dd日"); //日期格式
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String start_time = dateformat.format(formatter.parse(clazz.getString("START_TIME")));
String now = DateUtil.getSdfTimesSSS();
PageData curpd = new PageData();
curpd.put("CLASS_ID",pd.getString("CLASS_ID"));
List<PageData> students = archivesService.getStudents(curpd);
students.forEach(x -> {
String path = Const.HTTPFILEURL + x.getString("SIGNATURE_PATH");
ByteArrayImageProvider byteArrayImageProvider;
URL url;
try {
url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();
int state = connection.getResponseCode();
if (state == 200) {
byteArrayImageProvider = new ByteArrayImageProvider(url.openStream());//照片 读取网络路径
if (byteArrayImageProvider != null) {
x.put("pic", byteArrayImageProvider);
} else {
x.put("pic", " ");
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
List<PageData> curList = archivesService.getCurriculums(curpd);
List<String> ArrayDATA_IDS = new ArrayList<String>();
for (PageData c : curList) {
ArrayDATA_IDS.add(c.getString("CURRICULUM_ID"));
}
curpd.put("ArrayDATA_IDS", ArrayDATA_IDS);
List<PageData> coursewaresList = archivesService.getCoursewares(curpd);
PageData rpd = new PageData();
double classhour = 0.0;
Set<String> teacherSet = new HashSet<String>();
for (PageData c : coursewaresList) {
if (!Tools.isEmpty(c.get("TEACHERNAME"))) {
teacherSet.add(c.get("TEACHERNAME").toString());
}
if (!Tools.isEmpty(c.get("CLASSHOUR"))) {
classhour += Double.parseDouble(c.get("CLASSHOUR").toString());
}
}
StringBuffer stringBuilder = new StringBuffer();
if (teacherSet != null){
String[] split = teacherSet.toString().replaceAll("\\[|\\]","").split(",");
for (int i = 0; i < split.length ; i++) {
if (i >= 5){
stringBuilder.append("等...");
break;
}
stringBuilder.append(split[i]);
if (i != split.length - 1 && i < 4) {
stringBuilder.append(",");
}
}
rpd.put("TEACHERS", stringBuilder.toString());
}
File file = new File(projectPath + Const.FILEPATHWORDTEMPLATE + "qz.docx");
InputStream mbwj = new FileInputStream(file);
//注册xdocreport实例并加载FreeMarker模板引擎
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(mbwj, TemplateEngineKind.Freemarker);
//创建xdocreport上下文对象
IContext context = report.createContext();
context.put("TEACHERS", rpd.getString("TEACHERS")); // 教师
context.put("START_TIME", start_time); // 培训教师
context.put("TRAINING_ADDRESS", clazz.getString("CORP_NAME")); // 培训地点
context.put("var2", students.size()); // 人数
context.put("CLASS_NAME", clazz.getString("NAME")); // 培训内容
context.put("SUMCLASSHOUR", classhour); // 学时
context.put("studentList", students); // 学员
FieldsMetadata fm = report.createFieldsMetadata();
fm.addFieldAsList("studentList ");
fm.addFieldAsImage("pic", "student.pic", NullImageBehaviour.RemoveImageTemplate);
FileOutputStream out = new FileOutputStream(projectPath + Const.TEMPZIPFILES + "安全培训教育记录及签字表-" + now + ".docx");
report.process(context, out);
out.close();
WordToPdfUtil.docToPdf(projectPath + Const.TEMPZIPFILES + "安全培训教育记录及签字表-" + now + ".docx",
projectPath + Const.TEMPZIPFILES + "安全培训教育记录及签字表-" + now + ".pdf");
//删除本地word pdf
File word = new File(projectPath + Const.TEMPZIPFILES + "安全培训教育记录及签字表-" + now + ".docx");
word.delete();
File pdf = new File(projectPath + Const.TEMPZIPFILES + "安全培训教育记录及签字表-" + now + ".pdf");
return pdf;
}
@Override
public void afterPropertiesSet() throws Exception {
BatchExportFactory.register("archives102", this);
}
}