package com.qxueyou.scc.teach.res.service.impl.resitem;
|
|
import java.util.Date;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Service;
|
|
import com.qxueyou.scc.base.model.Result;
|
import com.qxueyou.scc.base.service.ICacheService;
|
import com.qxueyou.scc.base.service.impl.CommonAppService;
|
import com.qxueyou.scc.base.util.CollectionUtils;
|
import com.qxueyou.scc.base.util.TraceUtils;
|
import com.qxueyou.scc.school.model.HandoutConverTask;
|
import com.qxueyou.scc.school.model.SchHandout;
|
import com.qxueyou.scc.teach.res.model.Res;
|
import com.qxueyou.scc.teach.res.model.ResFile;
|
import com.qxueyou.scc.teach.res.model.ResItemDoc;
|
import com.qxueyou.scc.teach.res.service.IFileService;
|
import com.qxueyou.scc.teach.res.service.IResItemService;
|
import com.qxueyou.scc.teach.res.service.impl.ResService;
|
|
@Service(value = "ResItemDocService")
|
public class ResItemDocService extends CommonAppService implements IResItemService {
|
|
private static final String RES_DOC_CONVER_LST = "RES_DOC_CONVER_LST";
|
|
@Autowired
|
IFileService fileService;
|
@Autowired
|
ICacheService cacheService;
|
|
@Override
|
public String add(String fileId, String name) {
|
|
ResFile file = fileService.read(fileId);
|
String handoutId = addDocConverTask(file);
|
ResItemDoc doc = new ResItemDoc();
|
TraceUtils.setCreateTrace(doc);
|
doc.setOrigFileId(fileId);
|
doc.setName(name);
|
doc.setHandoutId(handoutId);
|
doc.setStatus(ResItemDoc.STATUS_LINEUP);
|
save(doc);
|
// 将文档id放进缓存
|
cacheService.lstRightPush(RES_DOC_CONVER_LST, handoutId);
|
return doc.getDocId();
|
}
|
|
private String addDocConverTask(ResFile file) {
|
|
SchHandout handout = insertHandout(file);
|
insertHandoutConverTask(handout);
|
|
return handout.getHandoutId();
|
|
}
|
|
private SchHandout insertHandout(ResFile file) {
|
|
SchHandout handout = new SchHandout();
|
|
TraceUtils.setCreateTrace(handout);
|
|
handout.setDeleteFlag(false);
|
handout.setName(file.getFileName());
|
handout.setStatus(SchHandout.STATUS_PROCESSING);
|
handout.setFullPath(file.getPath());
|
handout.setDeleteFlag(false);
|
|
save(handout);
|
|
return handout;
|
}
|
|
private Result insertHandoutConverTask(SchHandout handout) {
|
|
HandoutConverTask task = new HandoutConverTask();
|
|
task.setCreateTime(new Date());
|
task.setUpdateTime(new Date());
|
task.setHandoutId(handout.getHandoutId());
|
task.setHandoutUrl(handout.getFullPath());
|
task.setStatus(HandoutConverTask.STATUS_DRAFT);
|
task.setDeleteFlag(false);
|
|
save(task);
|
|
return new Result(true);
|
}
|
|
@Override
|
public Result update(String id, String name) {
|
|
ResItemDoc doc = read(ResItemDoc.class, id);
|
|
TraceUtils.setUpdateTrace(doc);
|
|
doc.setName(name);
|
|
save(doc);
|
|
return new Result(true, "success");
|
}
|
|
@Override
|
public String readAccessPath(String resId, String attribute) {
|
ResItemDoc resItemDoc = read(ResItemDoc.class, resId);
|
if (ResItemDoc.STATUS_DRAFT != resItemDoc.getStatus()) {
|
ResFile resFile = read(ResFile.class,resItemDoc.getOrigFileId());
|
return resFile.getPath();
|
// return null;
|
}
|
SchHandout schHandout = read(SchHandout.class, resItemDoc.getHandoutId());
|
|
if (schHandout == null) {
|
return null;
|
}
|
|
return schHandout.getFullPath();
|
}
|
|
@Override
|
public String getResItemType() {
|
return Res.FILE_TYPE_DOC;
|
}
|
|
@Scheduled(cron = "0/2 * * * * ?") // 每10秒执行一次
|
public void doTimer() {
|
|
String id = cacheService.lstLeftPop(RES_DOC_CONVER_LST);
|
|
if (StringUtils.isBlank(id)) {
|
return;
|
}
|
|
SchHandout schHandout = read(SchHandout.class, id);
|
|
if (schHandout.getStatus() == SchHandout.STATUS_DRAFT) {
|
|
ResItemDoc resItemDoc = findUnique("from ResItemDoc where deleteFlag is false and handoutId = ?",
|
CollectionUtils.newList(id), ResItemDoc.class);
|
resItemDoc.setStatus(SchHandout.STATUS_DRAFT);
|
TraceUtils.setUpdateTrace(resItemDoc);
|
save(resItemDoc);
|
cacheService.lstRightPush(ResService.RES_TOUPDATE_CONVER_STATUS_LST, resItemDoc.getDocId());
|
return;
|
}
|
cacheService.lstRightPush(RES_DOC_CONVER_LST, id);
|
}
|
}
|