mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-14 05:16:49 +08:00
409 lines
18 KiB
Java
409 lines
18 KiB
Java
package com.xboe.course;
|
||
|
||
import java.io.File;
|
||
import java.io.FileInputStream;
|
||
import java.io.InputStreamReader;
|
||
import java.io.Reader;
|
||
import java.time.LocalDateTime;
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.boot.system.ApplicationHome;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||
import com.xboe.common.utils.StringUtil;
|
||
import com.xboe.core.utils.OkHttpUtil;
|
||
import com.xboe.course.entity.old.Chapter;
|
||
import com.xboe.course.entity.old.CourseOld;
|
||
import com.xboe.course.entity.old.CourseOldTeacher;
|
||
import com.xboe.course.entity.old.Resource;
|
||
import com.xboe.course.newCourse.course.dao.CourseDao;
|
||
import com.xboe.course.newCourse.course.dao.UserDao;
|
||
import com.xboe.course.newCourse.course.dao.UserGroupDao;
|
||
import com.xboe.course.newCourse.course.entity.CourseImportLog;
|
||
import com.xboe.course.newCourse.course.entity.User;
|
||
import com.xboe.course.newCourse.course.service.ICourseOldService;
|
||
|
||
import lombok.extern.slf4j.Slf4j;
|
||
|
||
@Slf4j
|
||
@Component
|
||
public class CourseJsonRunner {
|
||
|
||
private static Map<String,String> CourseContentTypeMap=new HashMap<String,String>();
|
||
|
||
@Autowired
|
||
CourseDao courseDao;
|
||
|
||
@Autowired
|
||
UserDao userDao;
|
||
|
||
@Autowired
|
||
ICourseOldService courseOldService;
|
||
|
||
@Autowired
|
||
OkHttpUtil okHttpUtil;
|
||
|
||
@Autowired
|
||
UserGroupDao userGroupDao;
|
||
|
||
static{
|
||
CourseContentTypeMap.put("video","视频");
|
||
CourseContentTypeMap.put("pdf","pdf");
|
||
CourseContentTypeMap.put("audio","音频");
|
||
CourseContentTypeMap.put("office","office文件已转化为pdf");
|
||
CourseContentTypeMap.put("html-courseware","HTML非标课件");
|
||
//CourseContentTypeMap.put("html","html内容");
|
||
// CourseContentTypeMap.put("html","html内容");
|
||
CourseContentTypeMap.put("homework","作业");
|
||
CourseContentTypeMap.put("examination","考试");
|
||
}
|
||
|
||
|
||
public void run(String kid) throws Exception {
|
||
|
||
ApplicationHome home = new ApplicationHome(getClass());
|
||
|
||
File jarFile = home.getSource();
|
||
String dataFile = jarFile.getParentFile().toString() + "/course.json";
|
||
Reader reader = new InputStreamReader(new FileInputStream(dataFile),"utf-8");
|
||
|
||
int ch=0;
|
||
StringBuffer stringBuffer = new StringBuffer();
|
||
while ((ch=reader.read()) !=-1){
|
||
stringBuffer.append((char) ch);
|
||
}
|
||
reader.close();
|
||
|
||
//josn字符串
|
||
String json = stringBuffer.toString();
|
||
// System.out.println(json);
|
||
|
||
ObjectMapper mapper = new ObjectMapper();
|
||
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,true);
|
||
mapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT,true);
|
||
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
|
||
|
||
CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, CourseOld.class);
|
||
|
||
List<CourseOld> list=mapper.readValue(json,collectionType);
|
||
|
||
//单个导入
|
||
List<CourseOld> courseOlds = new ArrayList<>();
|
||
if(StringUtil.isNotBlank(kid)){
|
||
for (CourseOld c:list) {
|
||
if(c.getKid().equals(kid)){
|
||
courseOlds.add(c);
|
||
list=courseOlds;
|
||
}else{
|
||
log.error("未读取到本门课程"+kid);
|
||
list=courseOlds;
|
||
}
|
||
}
|
||
}
|
||
log.info("共读取【"+list.size()+"】门课程");
|
||
if (list != null && !list.isEmpty()) {
|
||
log.info("开始执行导入课程");
|
||
this.verify(list);
|
||
log.info("执行导入课程完成");
|
||
}else{
|
||
log.error("无任何课程信息");
|
||
System.out.println("无任何课程信息");
|
||
}
|
||
//List<CourseOld> verify = this.verify(list1);
|
||
|
||
|
||
}
|
||
|
||
private CourseImportLog addErrorLogs(CourseOld cold,String error) {
|
||
CourseImportLog courseImportLog = new CourseImportLog();
|
||
courseImportLog.setKid(cold.getKid());
|
||
courseImportLog.setName(cold.getName());
|
||
courseImportLog.setMess(error);
|
||
courseImportLog.setTime(LocalDateTime.now());
|
||
return courseImportLog;
|
||
}
|
||
|
||
// private String checkAudience(CourseOld cold) {
|
||
// String url = SysConstant.getConfigValue(BaseConstant.CONFIG_EXTERNALINTERFACE_URL_SYSTEM) + "/xboe/old/data/audience/detail";
|
||
// String error=null;
|
||
// if (cold.getAudienceId() != null && !cold.getAudienceId().isEmpty()) {
|
||
// try {
|
||
// for (String s : cold.getAudienceId()) {
|
||
// UserGroup userGroup = userGroupDao.findOne(FieldFilters.eq("kid", s));
|
||
// if(userGroup==null) {
|
||
// userGroup = new UserGroup();
|
||
// }else{
|
||
//// error="已有该受众";
|
||
// log.error("已存在受众,不在重复导入[kid:]"+userGroup.getKid()+"[name]"+userGroup.getName());
|
||
// continue;
|
||
// }
|
||
// Map<String, String> map = new HashMap<>();
|
||
// map.put("kid", s);
|
||
// String responseStr = okHttpUtil.doPost(url, map);
|
||
// if (StringUtils.isNotBlank(responseStr)) {
|
||
// ObjectMapper mapper = new ObjectMapper();
|
||
// JsonResponse<Audience> audience = mapper.readValue(responseStr, new TypeReference<JsonResponse<Audience>>() {
|
||
// });
|
||
// if (audience.getStatus() == HttpStatus.SC_OK && audience.getResult() != null) {
|
||
// Audience result = audience.getResult();
|
||
// if (result != null) {
|
||
// if (StringUtil.isBlank(result.getAudienceName())) {
|
||
// error = "受众名称为空,不能导入";
|
||
// break;
|
||
// }
|
||
// if (result.getType().equals('1')) {
|
||
// error = "受众类型为自由受众,不能导入";
|
||
// break;
|
||
// }
|
||
// if (StringUtil.isNotBlank(result.getAudienceName())) {
|
||
// if (StringUtil.isBlank(result.getCreatedBy())) {
|
||
// //赋予默认
|
||
//// result.setCreatedBy("965341999643234304");
|
||
// userGroup.setSysCreateBy("965341999643234304");
|
||
// userGroup.setSysCreateBy("董瑞华");
|
||
// userGroup.setKid(result.getKid());
|
||
// userGroup.setName(result.getAudienceName());
|
||
// userGroup.setGtype(1);
|
||
// userGroup.setDescription(result.getDescription());
|
||
// if (result.getStatus().equals('0')) {
|
||
// userGroup.setStatus(0);
|
||
// }
|
||
// if (result.getStatus().equals('1')) {
|
||
// userGroup.setStatus(1);
|
||
// }
|
||
// if (result.getStatus().equals('2')) {
|
||
// userGroup.setStatus(2);
|
||
// }
|
||
// if (cold.getUserGroupList() == null) {
|
||
// cold.setUserGroupList(new ArrayList<>());
|
||
// }
|
||
//
|
||
//
|
||
// } else {
|
||
// User user = userDao.getBySysId(result.getCreatedBy());
|
||
// if (user == null) {
|
||
//// result.setCreatedBy("965341999643234304");
|
||
// userGroup.setSysCreateBy("965341999643234304");
|
||
// userGroup.setSysCreateBy("董瑞华");
|
||
// userGroup.setKid(result.getKid());
|
||
// userGroup.setName(result.getAudienceName());
|
||
// userGroup.setGtype(1);
|
||
// userGroup.setDescription(result.getDescription());
|
||
// if (result.getStatus().equals('0')) {
|
||
// userGroup.setStatus(0);
|
||
// }
|
||
// if (result.getStatus().equals('1')) {
|
||
// userGroup.setStatus(1);
|
||
// }
|
||
// if (result.getStatus().equals('2')) {
|
||
// userGroup.setStatus(2);
|
||
// }
|
||
// if (cold.getUserGroupList() == null) {
|
||
// cold.setUserGroupList(new ArrayList<>());
|
||
// }
|
||
//// cold.getAudienceList().add(result);
|
||
// log.error("受众【" + result.getAudienceName() + "】的创建人未匹配到,系统设计为默认人员【965341999643234304】");
|
||
// } else {
|
||
// userGroup.setSysCreateBy(user.getId());
|
||
// userGroup.setSysCreateBy(user.getName());
|
||
// userGroup.setKid(result.getKid());
|
||
// userGroup.setName(result.getAudienceName());
|
||
// userGroup.setGtype(1);
|
||
// userGroup.setDescription(result.getDescription());
|
||
// if (result.getStatus().equals('0')) {
|
||
// userGroup.setStatus(0);
|
||
// }
|
||
// if (result.getStatus().equals('1')) {
|
||
// userGroup.setStatus(1);
|
||
// }
|
||
// if (result.getStatus().equals('2')) {
|
||
// userGroup.setStatus(2);
|
||
// }
|
||
// if (cold.getUserGroupList() == null) {
|
||
// cold.setUserGroupList(new ArrayList<>());
|
||
// }
|
||
// }
|
||
// }
|
||
// cold.getUserGroupList().add(userGroup);
|
||
// }
|
||
//
|
||
// }
|
||
// }
|
||
//
|
||
// }else {
|
||
// CourseImportLog courseImportLog = new CourseImportLog();
|
||
// courseImportLog.setKid(cold.getKid());
|
||
// courseImportLog.setName(cold.getName());
|
||
// courseImportLog.setMess("查询受众【"+s+"】失败,无返回结果");
|
||
// error="查询受众【"+s+"】失败,无返回结果";
|
||
// break;
|
||
// }
|
||
// }
|
||
// } catch (Exception e) {
|
||
// error="受众类型为自由受众,不能导入,详细错误查看日志";
|
||
// log.error("处理课程受众失败",e);
|
||
//
|
||
// }
|
||
//
|
||
// }
|
||
// return error;
|
||
// }
|
||
|
||
//入库前校验
|
||
private void verify(List<CourseOld> list) {
|
||
//可以导入的课程
|
||
List<CourseOld> courseOlds = new ArrayList<>();
|
||
//不能导放的课程
|
||
List<CourseImportLog> importLogs = new ArrayList<>();
|
||
List<String> strings = new ArrayList<>();
|
||
//自身过滤
|
||
for (CourseOld c : list) {
|
||
// if(StringUtil.isNotBlank(c.getCoverImg())){
|
||
// String substring=null;
|
||
// if(c.getCoverImg().contains("upload")) {
|
||
// substring = c.getCoverImg().substring(23, c.getCoverImg().length());
|
||
// }else{
|
||
// substring=c.getCoverImg().substring(16,c.getCoverImg().length());
|
||
// }
|
||
// String sql = "update boe_course set cover_img='" + substring + "' where kid='" + c.getKid() + "';";
|
||
// strings.add(sql);
|
||
// }
|
||
|
||
|
||
//判断数据库中是否已存在
|
||
String mess = courseDao.detailOld(c.getKid());
|
||
if (mess != null) {
|
||
importLogs.add(addErrorLogs(c,mess));
|
||
continue;
|
||
}
|
||
|
||
//课程内容的检查
|
||
boolean hasScorm=false;//是否包含score包
|
||
List<Chapter> impChapter=new ArrayList<>();//课程可以导入的章节
|
||
for (Chapter chapter : c.getChapterList()) {
|
||
|
||
if (chapter.getResourceList() != null && !chapter.getResourceList().isEmpty()) {
|
||
|
||
Chapter newChapter=new Chapter();
|
||
newChapter.setName(chapter.getName());
|
||
newChapter.setResourceList(new ArrayList<>());
|
||
|
||
for (Resource r : chapter.getResourceList()) {
|
||
if (r.getTypeCode().equals("scorm")) {
|
||
hasScorm=true;//只要有scorm就不导入
|
||
break;
|
||
}
|
||
else if(CourseContentTypeMap.containsKey(r.getTypeCode())){
|
||
newChapter.getResourceList().add(r);
|
||
}
|
||
}
|
||
//有可导入的资源的才可以添加到章中
|
||
if(!newChapter.getResourceList().isEmpty()) {
|
||
impChapter.add(newChapter);
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
if(hasScorm) {
|
||
importLogs.add(addErrorLogs(c,"该课程有scorm包"));
|
||
continue;//课程不能导入,验证下一个
|
||
}
|
||
if(impChapter.isEmpty()) {
|
||
importLogs.add(addErrorLogs(c,"该课程无可导入内容"));
|
||
continue;//课程不能导入,验证下一个
|
||
}
|
||
else {
|
||
c.setChapterList(impChapter);
|
||
}
|
||
|
||
//对教师的检查
|
||
boolean teacherError=false;
|
||
if(c.getTeachers()!=null && !c.getTeachers().isEmpty()){
|
||
for (CourseOldTeacher ct: c.getTeachers()){
|
||
User user = userDao.getBySysId(ct.getUser_id());
|
||
if(user==null){
|
||
log.error("新系统没有这个用户"+"kid:"+c.getKid()+"用户id:"+ct.getUser_id());
|
||
importLogs.add(addErrorLogs(c,"无课程教师,"+"kid:"+c.getKid()+"用户id:"+ct.getUser_id()));
|
||
teacherError=true;
|
||
break;
|
||
}else{
|
||
ct.setNewId(user.getId());//新的教师的id
|
||
}
|
||
}
|
||
}
|
||
// else { //无课程教师,指定默认教师
|
||
// importLogs.add(addErrorLogs(c,"无课程教师"));
|
||
// teacherError=true;
|
||
// }
|
||
if(teacherError) {
|
||
continue;
|
||
}
|
||
//对受众的检查,普通受众的课程添加,自动受众的课程不添加
|
||
|
||
// String checkRs=this.checkAudience(c);
|
||
// if(StringUtils.isNotBlank(checkRs)) {
|
||
// importLogs.add(addErrorLogs(c,checkRs));
|
||
// continue;
|
||
// }
|
||
|
||
//没有错误,再添加列表中
|
||
courseOlds.add(c);
|
||
}
|
||
courseOldService.saveList(importLogs);
|
||
|
||
// for (String s:strings) {
|
||
// System.out.println(s);
|
||
// }
|
||
//去除html标签
|
||
|
||
int success=0;
|
||
for (CourseOld c : courseOlds) {
|
||
String htmlStr = c.getSummary();
|
||
//定义script的正则表达式,去除js可以防止注入
|
||
String scriptRegex = "<script[^>]*?>[\\s\\S]*?<\\/script>";
|
||
//定义style的正则表达式,去除style样式,防止css代码过多时只截取到css样式代码
|
||
String styleRegex = "<style[^>]*?>[\\s\\S]*?<\\/style>";
|
||
//定义HTML标签的正则表达式,去除标签,只提取文字内容
|
||
String htmlRegex = "<[^>]+>";
|
||
//定义空格,回车,换行符,制表符
|
||
String spaceRegex = "\\s*|\t|\r|\n";
|
||
|
||
// 过滤script标签
|
||
htmlStr = htmlStr.replaceAll(scriptRegex, "");
|
||
// 过滤style标签
|
||
htmlStr = htmlStr.replaceAll(styleRegex, "");
|
||
// 过滤html标签
|
||
htmlStr = htmlStr.replaceAll(htmlRegex, "");
|
||
// 过滤空格等
|
||
htmlStr = htmlStr.replaceAll(spaceRegex, "");
|
||
// 过滤
|
||
htmlStr = htmlStr.replaceAll(" ", "");
|
||
// 过滤所有的空格
|
||
htmlStr = htmlStr.replaceAll(" ", "");
|
||
|
||
c.setSummary(htmlStr);
|
||
try {
|
||
courseOldService.save(c);
|
||
success++;
|
||
}catch(Exception e) {
|
||
CourseImportLog impError=addErrorLogs(c,"保存课程失败:"+e.getMessage());
|
||
courseOldService.saveError(impError);
|
||
log.error("保存课程信息失败",e);
|
||
}
|
||
}
|
||
|
||
|
||
log.info("共导入课程【"+success+"】 ,未导入【"+importLogs.size()+"】");
|
||
|
||
}
|
||
|
||
}
|