package com.qxueyou.scc.teach.res.service.impl; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import com.alibaba.druid.util.StringUtils; import com.alibaba.fastjson.JSONObject; import com.qxueyou.scc.admin.classes.service.IClassLectureService; import com.qxueyou.scc.base.model.FileMeta; import com.qxueyou.scc.base.model.Pager; 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.ClientUtils; import com.qxueyou.scc.base.util.CollectionUtils; import com.qxueyou.scc.base.util.TraceUtils; import com.qxueyou.scc.teach.res.model.Res; import com.qxueyou.scc.teach.res.model.ResDir; import com.qxueyou.scc.teach.res.model.ResLib; import com.qxueyou.scc.teach.res.service.IResItemService; import com.qxueyou.scc.teach.res.service.IResService; import com.qxueyou.scc.teach.subject.service.impl.SubjectLectureService; /** * ×ÊÔ´·þÎñ * * @author Ïĵ»¢ */ @Service public class ResService extends CommonAppService implements IResService { public static final String RES_TOUPDATE_CONVER_STATUS_LST = "RES_TOUPDATE_CONVER_STATUS_LST"; private static final String RES_DIR_ROOT = "0"; @Autowired ICacheService cacheService; @Autowired private ApplicationContext appContext; @Autowired private IClassLectureService classLectureService; /** * ¸÷ÀàÐÍ×ÊÔ´·þÎñʵÏÖ */ private Map resItemServiceMap = new HashMap(6); @PostConstruct private void init() { registerResItemService(appContext.getBean("ResItemVideoService", IResItemService.class)); registerResItemService(appContext.getBean("ResItemAudioService", IResItemService.class)); registerResItemService(appContext.getBean("ResItemDocService", IResItemService.class)); registerResItemService(appContext.getBean("ResItemExerciseService", IResItemService.class)); registerResItemService(appContext.getBean("ResItemArticleService", IResItemService.class)); } private void registerResItemService(IResItemService service) { resItemServiceMap.put(service.getResItemType(), service); } /* * (non-Javadoc) * * @see com.qxueyou.scc.teach.res.service.IResService#add(java.lang.String, * java.lang.String, java.lang.String, java.lang.String) */ @Override public Result add(String dirId, String itemDestId, String name, String remark, String type, String coverPageUrl) { ResDir dir = read(ResDir.class, dirId); String resItemId = resItemServiceMap.get(type).add(itemDestId, name); Res res = new Res(); TraceUtils.setCreateTrace(res); res.setCoverPageUrl(coverPageUrl); res.setItemDestId(resItemId); res.setName(name); res.setStatus(Res.STATUS_CONVER); res.setType(type); res.setResDirId(dirId); res.setRemark(remark); res.setLibId(dir.getLibId()); save(res); return new Result(true, "success", CollectionUtils.newStringMap("resId", res.getResId())); } @Override public Result addBatchRes(String dirId, String type, List items) { ResDir dir = read(ResDir.class, dirId); Res res = null; for (FileMeta fileMeta : items) { String name = fileMeta.getFileName(); String resItemId = resItemServiceMap.get(type).add(fileMeta.getFileId(), name); res = new Res(); TraceUtils.setCreateTrace(res); res.setItemDestId(resItemId); res.setName(name); res.setStatus(Res.STATUS_CONVER); res.setType(type); res.setLibId(dir.getLibId()); res.setResDirId(dirId); save(res); } return Result.SUCCESS; } /* * (non-Javadoc) * * @see com.qxueyou.scc.teach.res.service.IResService#update(java.lang.String, * java.lang.String, java.lang.String) */ @Override public Result update(String resId, String name, String remark, String coverPageUrl) { Res res = read(Res.class, resId); resItemServiceMap.get(res.getType()).update(res.getItemDestId(), name); TraceUtils.setUpdateTrace(res); res.setName(name); res.setCoverPageUrl(coverPageUrl); res.setRemark(remark); res.setStatus(Res.STATUS_DRAFT); save(res); return new Result(true, "success"); } /* * (non-Javadoc) * * @see com.qxueyou.scc.teach.res.service.IResService#delete(java.lang.String[]) */ @Override public Result delete(String[] resIds) { for (String resId : resIds) { delete(resId); } return new Result(true, "success"); } /** * @param resId ×ÊÔ´id * @return */ private Result delete(String resId) { Res res = read(Res.class, resId); TraceUtils.setUpdateTrace(res); res.setDeleteFlag(true); save(res); return new Result(true, "success"); } @Override public Result doCopy(String resId, String destDirId, String name) { // TODO Auto-generated method stub return null; } @Override public Result doMove(String resId, String destDirId, String name) { // TODO Auto-generated method stub return null; } @Override public List listRes(String dirId, String text, Integer pageSize, Integer pageNum, String type) { StringBuffer hql = new StringBuffer("from Res where deleteFlag is false and name like ? and resDirId=? "); List args = CollectionUtils.newList(text + "%", dirId); if (!StringUtils.isEmpty(type)) { hql.append(" and type=?"); args.add(type); } hql.append(" order by createTime desc"); List result = findList(hql.toString(), new Pager(pageSize, pageNum), args, Res.class); return result; } @Override public List listMyRes(String keyword, Integer pageSize, Integer pageNum, String type) { ResLib lib = readUserLib(); StringBuffer hql = new StringBuffer("from Res where deleteFlag is false and libId=? and status ='draft'"); List args = CollectionUtils.newList(lib.getLibId()); if (!StringUtils.isEmpty(type)) { hql.append(" and type=?"); args.add(type); } if (!StringUtils.isEmpty(keyword)) { hql.append(" and name like ?"); args.add("%" + keyword + "%"); } hql.append(" order by updateTime desc"); List result = findList(hql.toString(), new Pager(pageSize, pageNum), args, Res.class); return result; } @Override public int listMyResCount(String text, String type) { ResLib lib = readUserLib(); StringBuffer hql = new StringBuffer("from Res where deleteFlag is false and libId=? and status ='draft'"); List args = CollectionUtils.newList(lib.getLibId()); if (!StringUtils.isEmpty(type)) { hql.append(" and type=?"); args.add(type); } if (!StringUtils.isEmpty(text)) { hql.append(" and name like ?"); args.add("%" + text + "%"); } hql.append(" order by updateTime desc"); return findCount(hql.toString(), args); } @Override public List listResByLib(String libType, String ownerId, String keyword, Integer pageSize, Integer pageNum, String resType) { ResLib lib = this.getResLib(libType, ownerId); StringBuffer hql = new StringBuffer( "from Res where deleteFlag is false and name like ? and libId=? and status=? order by updateTime desc"); List args = CollectionUtils.newList(keyword + "%", lib == null ? null : lib.getLibId(),Res.STATUS_DRAFT); if (!StringUtils.isEmpty(resType)) { hql.append(" and type=?"); args.add(resType); } List result = findList(hql.toString(), new Pager(pageSize, pageNum), args, Res.class); return result; } @Override public int listResCountByLib(String libType, String ownerId, String text, String resType) { ResLib lib = this.getResLib(libType, ownerId); StringBuffer hql = new StringBuffer("from Res where deleteFlag is false and libId=? order by updateTime desc"); List args = CollectionUtils.newList(null == lib ? null : lib.getLibId()); if (!StringUtils.isEmpty(resType)) { hql.append(" and type=?"); args.add(resType); } return findCount(hql.toString(), args); } @Override public int listResCount(String dirId, String text, String type) { String hql = "from Res where resDirId=? and deleteFlag is false and name like ? "; List args = CollectionUtils.newList(dirId, text + "%"); if (!StringUtils.isEmpty(type)) { hql = hql.concat(" and type = ?"); args.add(type); } return findCount(hql, args); } @Override public Result readAccessPath(String resId, String attribute) { Res res = read(Res.class, resId); Object path = Res.FILE_TYPE_VIDEO.equals(res.getType())?JSONObject.parse(resItemServiceMap.get(res.getType()).readAccessPath(res.getItemDestId(), attribute)):CollectionUtils.newObjectMap("path", resItemServiceMap.get(res.getType()).readAccessPath(res.getItemDestId(), attribute)); if(Res.FILE_TYPE_DOC.equals(res.getType())) { return new Result(true, "success", CollectionUtils.newObjectMap("path", path, "name", res.getName(), "coverPageUrl", res.getCoverPageUrl(), "size", this.classLectureService.readDocPageCount(res.getItemDestId())));//ÎļþµÄÒ³Êý }else { return new Result(true, "success", CollectionUtils.newObjectMap("path", path, "name", res.getName(), "coverPageUrl", res.getCoverPageUrl(), "size", 0)); } } @Override public Result addDir(String parentDirId, String name) { ResDir parent = read(ResDir.class, parentDirId); ResDir resDir = new ResDir(); TraceUtils.setCreateTrace(resDir); resDir.setName(name); resDir.setParentDirId(parentDirId); resDir.setLibId(null == parent ? null : parent.getLibId()); save(resDir); return new Result(true, "success", CollectionUtils.newObjectMap("dirId", resDir.getDirId(), "name", name)); } @Override public ResDir readDir(String dirId) { return read(ResDir.class, dirId); } @Override public Result updateDir(String dirId, String name) { ResDir resDir = read(ResDir.class, dirId); TraceUtils.setUpdateTrace(resDir); resDir.setName(name); save(resDir); return new Result(true, "success", CollectionUtils.newObjectMap("dirId", resDir.getDirId(), "name", name)); } @Override public Result deleteDir(String[] dirIds) { for (String dirId : dirIds) { deleteFile4Dir(dirId);// ɾ³ý¶ÔÓ¦µÄÎļþ deleteDir(dirId); } return new Result(true, "success"); } private void deleteDir(String dirId) { ResDir resDir = read(ResDir.class, dirId); TraceUtils.setUpdateTrace(resDir); resDir.setDeleteFlag(true); save(resDir); } /** * ɾ³ý×ÊÔ´¿âÎļþ¼Ð¶ÔӦɾ³ýÎļþ * * @param dirId Îļþ¼Ðid */ @Override public void deleteFile4Dir(String dirId) { String[] dirIds = dirId.split(","); this.bulkUpdateInLoop("update Res set deleteFlag = true where deleteFlag is false and resDirId = ?", dirIds); } @Override public ResDir doGetRootDir(String type, String ownerId) { ResLib lib = getResLib(type, ownerId); // ÎÞ×ÊÔ´¿âÔò³õʼ»¯,²¢·µ»Ø¸ù½Úµã if (lib == null) { Result result = initResLib(type, ownerId); return read(ResDir.class, result.getDataT("rootDirId")); } // System.out.println(lib.getRootDirId()); return read(ResDir.class, lib.getRootDirId()); } @Override public List doGetMyLibChildDirs(String parentDirId) { return doGetChildDirsInner(parentDirId, ResLib.OWNNER_TYPE_USER); } private List doGetChildDirsInner(String parentDirId, String type) { // ¸ù½ÚµãµÄ´¦Àí if (StringUtils.isEmpty(parentDirId)) { ResLib lib = getResLib(type, ClientUtils.getUserId()); // ÎÞ×ÊÔ´¿âÔò³õʼ»¯,²¢·µ»Ø¸ù½Úµã if (lib == null) { Result result = initUserLib(); return CollectionUtils.newList(ResDir.class, read(ResDir.class, result.getDataT("rootDirId"))); } return CollectionUtils.newList(ResDir.class, read(ResDir.class, lib.getRootDirId())); } return listDir(parentDirId); } private List listDir(String parentDirId) { String hql = "from ResDir where parentDirId=? and deleteFlag is false"; List result = find(hql, CollectionUtils.newList(parentDirId), ResDir.class); return result; } @Override public Res read(String resId) { return read(Res.class, resId); } @Override public Result initUserLib() { return initResLib(ResLib.OWNNER_TYPE_USER, ClientUtils.getUserId()); } private Result initResLib(String type, String ownerId) { String hql = "from ResLib where ownerId=? and ownerType=? and deleteFlag is false"; ResLib lib = findUnique(hql, CollectionUtils.newList(ownerId, type), ResLib.class); if (lib == null) { Result result = addDir(RES_DIR_ROOT, "¸ùĿ¼"); lib = new ResLib(); TraceUtils.setCreateTrace(lib); lib.setOwnerId(ownerId); lib.setOwnerType(type); lib.setRootDirId(result.getDataT("dirId")); save(lib); ResDir root = read(ResDir.class, result.getDataT("dirId")); root.setLibId(lib.getLibId()); save(root); return new Result(true, "success", CollectionUtils.newStringMap("rootDirId", lib.getRootDirId(), "libId", lib.getLibId())); } return new Result(true, "success", CollectionUtils.newStringMap("rootDirId", lib.getRootDirId(), "libId", lib.getLibId())); } @Override public ResLib readUserLib() { String hql = "from ResLib where ownerId=? and ownerType=? and deleteFlag is false"; ResLib lib = findUnique(hql, CollectionUtils.newList(ClientUtils.getUserId(), ResLib.OWNNER_TYPE_USER), ResLib.class); return lib; } @Override public ResLib getResLib(String type, String ownerId) { String hql = "from ResLib where ownerId=? and ownerType=? and deleteFlag is false"; return findUnique(hql, CollectionUtils.newList(ownerId, type), ResLib.class); } @Override public List doGetChildDirs(String parentDirId, String type) { return doGetChildDirsInner(parentDirId, type); } /** * ¶¨Ê±Æ÷¼à¿Ø×ªÂëÍê³ÉºóÉèÖÃͬ²½res±íµÄ״̬ */ @Scheduled(cron = "0/2 * * * * ?") public void doTimer() { String itemDestId = cacheService.lstLeftPop(RES_TOUPDATE_CONVER_STATUS_LST); if (StringUtils.isEmpty(itemDestId)) { return; } Res res = findUnique("from Res where deleteFlag is false and itemDestId = ?", CollectionUtils.newList(itemDestId), Res.class); res.setStatus(Res.STATUS_DRAFT); TraceUtils.setUpdateTrace(res); save(res); cacheService.lstRightPush(SubjectLectureService.RES_LECTURE_CONVER_LST, res.getResId()); } }