派生自 projectDept/qhighschool

EricsHu
2023-03-30 d552378a0fbc97a530e8d7601ca874551ced5d6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
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<String, IResItemService> resItemServiceMap = new HashMap<String, IResItemService>(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_DRAFT);
        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<FileMeta> 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_DRAFT);
            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<Res> 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<Object> args = CollectionUtils.newList(text + "%", dirId);
 
        if (!StringUtils.isEmpty(type)) {
            hql.append(" and type=?");
            args.add(type);
        }
        hql.append(" order by createTime desc");
        List<Res> result = findList(hql.toString(), new Pager(pageSize, pageNum), args, Res.class);
        return result;
    }
 
    @Override
    public List<Res> 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<Object> 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<Res> 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<Object> 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<Res> 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 like ? and status=? order by updateTime desc");
 
        List<Object> args = CollectionUtils.newList(keyword + "%", lib == null ? "%" : lib.getLibId() + "%",Res.STATUS_DRAFT);
 
        if (!StringUtils.isEmpty(resType)) {
            hql.append(" and type=?");
            args.add(resType);
        }
 
        List<Res> 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 like ? order by updateTime desc");
 
        List<Object> args = CollectionUtils.newList(null == lib ? "%" : 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<Object> 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);
        String type="0";
//        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));
        Object path = Res.FILE_TYPE_VIDEO.equals(type)? resItemServiceMap.get(type).readAccessPath(res.getItemDestId(), attribute):resItemServiceMap.get(type).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<ResDir> doGetMyLibChildDirs(String parentDirId) {
 
        return doGetChildDirsInner(parentDirId, ResLib.OWNNER_TYPE_USER);
 
    }
 
    private List<ResDir> 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<ResDir> listDir(String parentDirId) {
 
        String hql = "from ResDir where parentDirId=? and deleteFlag is false";
 
        List<ResDir> 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<ResDir> 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());
    }
 
}