派生自 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
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);
    }
}