派生自 projectDept/qhighschool

胡仁荣
2022-10-31 fe7381d6e8ec1f427408de0297ac7f41533202f6
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
package com.qxueyou.scc.exam.service.impl;
 
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import com.qxueyou.scc.base.model.Pager;
import com.qxueyou.scc.base.model.Result;
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.base.util.UUIDUtils;
import com.qxueyou.scc.exam.model.ExamPaperSectionInfo;
import com.qxueyou.scc.exam.service.IExamPaperSectionService;
import com.qxueyou.scc.exercise.model.ExerciseGroup;
import com.qxueyou.scc.exercise.model.ExerciseGroupItemRe;
import com.qxueyou.scc.exercise.model.ExerciseItem;
import com.qxueyou.scc.exercise.service.IExerciseGroupService;
import com.qxueyou.scc.exercise.service.IExerciseService;
 
/**
 * ÊÔ¾í²¿·Ö¹ÜÀí·þÎñ²ã
 *
 * @author kevin
 * @createTime 2017-11-1
 */
@Service
public class ExamPaperSectionService extends CommonAppService implements IExamPaperSectionService {
 
    @Autowired
    IExerciseGroupService exerciseGroupService;
 
    @Autowired
    IExerciseService exerciseService;
 
    @SuppressWarnings("rawtypes")
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Override
    public int queryExamPaperSectionCount(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return 0;
    }
 
    @Override
    public List<ExamPaperSectionInfo> queryExamPaperSectionList(Map<String, Object> param, Pager page) {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    public String addExamPaperSection(ExamPaperSectionInfo examPaperSectionInfo) {
        TraceUtils.setCreateTrace(examPaperSectionInfo);
        // ²éѯµ±Ç°×î´óµÄsectionOrder
        String hql = "select max(sectionOrder) from ExamPaperSectionInfo where examPaperId=? and deleteFlag is false";
        Integer maxOrder = this.findUnique(hql, CollectionUtils.newList(examPaperSectionInfo.getExamPaperId()),
                Integer.class);
        int beginOrder = maxOrder == null ? 0 : maxOrder.intValue();
        examPaperSectionInfo.setSectionOrder(++beginOrder);
 
        // »ñÈ¡µ±Ç°section¶ÔÓ¦µÄ¿ªÊ¼index
        int count = this.findCount("from ExerciseGroupItemRe r where r.deleteFlag is false and r.exerciseGroupId=?",
                CollectionUtils.newList(examPaperSectionInfo.getGroupId()));
 
        String sectionId = UUIDUtils.generateUUID().replace("-", "");
        examPaperSectionInfo.setSectionId(sectionId);
        examPaperSectionInfo.setItemStartOrder((short) (count));
        examPaperSectionInfo.setItemEndOrder((short) -1);
        this.insert(examPaperSectionInfo);
        return examPaperSectionInfo.getSectionId();
    }
 
    @Override
    public Result deleteExamPaperSection(String[] paperSectionIds) {
        Result result = new Result(true);
        if (paperSectionIds != null && paperSectionIds.length > 0) {
            String hql = "update ExamPaperSectionInfo set deleteFlag = true where sectionId=?";
            result = bulkUpdateInLoop(hql, paperSectionIds);
 
            for (String sectionId : paperSectionIds) {
                ExamPaperSectionInfo currentSectionInfo = this.read(ExamPaperSectionInfo.class, sectionId);
                this.deleteAllSectionItem(currentSectionInfo.getSectionId());
            }
        }
        return result;
    }
 
    @Override
    public Result updateExamPaperSection(ExamPaperSectionInfo param) {
        ExamPaperSectionInfo examPaperSectionInfo = read(ExamPaperSectionInfo.class, param.getSectionId());
        if (examPaperSectionInfo != null) {
            TraceUtils.setUpdateTrace(examPaperSectionInfo);
            examPaperSectionInfo.setSectionName(param.getSectionName());
            examPaperSectionInfo.setSectionDesc(param.getSectionDesc());
        }
        return save(examPaperSectionInfo);
    }
 
    @Deprecated
    @Override
    public Result saveExamPaperSectionGroup(String sectionId, String[] selectedGroupIds, Short[] itemTypes) throws Exception {
        ExamPaperSectionInfo examPaperSectionInfo = read(ExamPaperSectionInfo.class, sectionId);
        ExerciseGroup exerciseGroup = read(ExerciseGroup.class, examPaperSectionInfo.getGroupId());
 
        // »ñÈ¡ÊÔ¾í²¿·ÖÔ­¿ªÊ¼Î»ÖúͽáÊøÎ»ÖÃ
        int origStartIndex = examPaperSectionInfo.getItemStartOrder();
        int origEndIndex = examPaperSectionInfo.getItemEndOrder();
 
        if (origEndIndex > 0) {
            this.deleteAllSectionItem(examPaperSectionInfo.getSectionId());
        }
 
        // ½«Ìâ¿âµÄÌâÄ¿¸´ÖƵ½ÊÔ¾íÌâ¿â
        int itemIndex = origStartIndex;
        ExerciseGroup targetExerciseGroup = new ExerciseGroup();
        targetExerciseGroup.setGroupId(examPaperSectionInfo.getGroupId());
        if (selectedGroupIds != null && selectedGroupIds.length > 0) {
            for (int i = 0; i < selectedGroupIds.length; i++) {
                ExerciseGroup sourceExerciseGroup = this.read(ExerciseGroup.class, selectedGroupIds[i]);
                itemIndex += exerciseGroupService.doCopyExerciseGroupItem(sourceExerciseGroup, targetExerciseGroup, itemTypes,
                        itemIndex);
            }
 
            long allCount = exerciseGroup.getAllCount() == null ? 0
                    : exerciseGroup.getAllCount().longValue() + (itemIndex - origStartIndex);
            exerciseGroup.setAllCount(BigInteger.valueOf(allCount));
            if (itemIndex - origStartIndex > 0) {
                examPaperSectionInfo.setItemEndOrder((short) (itemIndex));
            }
            this.updateSectionRangeIndex(sectionId, itemIndex - origStartIndex);
            this.updatePaperScore(exerciseGroup.getGroupId());
        }
        return new Result(true);
    }
 
 
    @Override
    public Result saveExamPaperSectionItems(String sectionId, String sourceGroupId, String[] selectedItemIds) throws Exception {
        ExamPaperSectionInfo examPaperSectionInfo = read(ExamPaperSectionInfo.class, sectionId);
        ExerciseGroup exerciseGroup = read(ExerciseGroup.class, examPaperSectionInfo.getGroupId());
 
        // »ñÈ¡ÊÔ¾í²¿·ÖÔ­¿ªÊ¼Î»ÖúͽáÊøÎ»ÖÃ
        int origStartIndex = examPaperSectionInfo.getItemStartOrder();
        int origEndIndex = examPaperSectionInfo.getItemEndOrder();
 
        //Èç¹ûÊÔ¾íÆäËû²¿·ÖÖÐÒѾ­´æÔÚ¸ÃÌâÄ¿£¬Ôò·µ»Ø´íÎó£¬Öظ´µÄÌâÄ¿
        int count = this.findCountByComplexHql("from ExerciseGroupItemRe r where r.deleteFlag is false and r.exerciseGroupId=:paperGroupId and (r.itemOrder<:startIdx or r.itemOrder>:endIdx)  and r.exerciseItemId in (:exerciseItemIds)",
                CollectionUtils.newObjectMap("startIdx", origStartIndex, "endIdx", origEndIndex, "paperGroupId", examPaperSectionInfo.getGroupId(), "exerciseItemIds", selectedItemIds));
 
        if (count > 0) {
            return new Result(false, "Çë¼ì²éÑ¡ÔñµÄÌâÄ¿£¬Í¬Ò»·ÝÊÔ¾íÖв»ÔÊÐí´æÔÚÏàͬµÄÌâÄ¿£¡");
        }
 
        // ½«Ìâ¿âµÄÌâÄ¿¸´ÖƵ½ÊÔ¾íÌâ¿â
        int copyStartIndex = origEndIndex > 0 ? origEndIndex : origStartIndex;
        ExerciseGroup targetExerciseGroup = new ExerciseGroup();
        targetExerciseGroup.setGroupId(examPaperSectionInfo.getGroupId());
        ExerciseGroup sourceExerciseGroup = this.read(ExerciseGroup.class, sourceGroupId);
        int copyEndIndex = copyStartIndex + exerciseGroupService.doCopyExerciseGroupItem(sourceExerciseGroup, targetExerciseGroup, selectedItemIds,
                copyStartIndex);
        long allCount = exerciseGroup.getAllCount() == null ? 0 : exerciseGroup.getAllCount().longValue() + (copyEndIndex - copyStartIndex);
        exerciseGroup.setAllCount(BigInteger.valueOf(allCount));
        //Èç¹ûÓб£´æÌâÄ¿Ôò´¦ÀíË÷Òý£¬·ñÔò²»´¦Àí
        if (copyEndIndex - copyStartIndex > 0) {
            examPaperSectionInfo.setItemEndOrder((short) (copyEndIndex));
        }
        this.updateSectionRangeIndex(sectionId, copyEndIndex - copyStartIndex);
        this.updatePaperScore(exerciseGroup.getGroupId());
        return new Result(true);
    }
 
    @Override
    public ExamPaperSectionInfo queryExamPaperSectionDetail(String examPaperSectionId) {
        ExamPaperSectionInfo examPaperSectionInfo = this.read(ExamPaperSectionInfo.class, examPaperSectionId);
        ExerciseGroup exerciseGroup = exerciseGroupService.queryExerciseGroupDetail(examPaperSectionInfo.getGroupId());
        List<ExerciseItem> items = null;
        // ²éѯ³ösection ¶ÔÓ¦µÄÌâÄ¿ÐÅÏ¢
        if (examPaperSectionInfo.getItemEndOrder() > 0) {
            int itemCount = examPaperSectionInfo.getItemEndOrder() - examPaperSectionInfo.getItemStartOrder();
            items = new ArrayList<ExerciseItem>(itemCount);
            items.addAll(exerciseGroup.getItems().subList(examPaperSectionInfo.getItemStartOrder(),
                    examPaperSectionInfo.getItemEndOrder()));
        }
        examPaperSectionInfo.setLstExerciseItem(items);
        return examPaperSectionInfo;
    }
 
    @SuppressWarnings("unchecked")
    private Result updateSectionRangeIndex(String currentSectionid, int adjustNum) {
        ExamPaperSectionInfo currentSectionInfo = this.read(ExamPaperSectionInfo.class, currentSectionid);
        // Èç¹û²¿·Ö¶ÔÓ¦µÄendΪ-1£¬Ôò²»ÐèÒª¸üÐÂendorder
        String hql = "from ExamPaperSectionInfo where examPaperId=? and sectionOrder>? and deleteFlag is false";
        List<ExamPaperSectionInfo> lstExamPaperSectionInfo = this.find(hql,
                CollectionUtils.newList(currentSectionInfo.getExamPaperId(), currentSectionInfo.getSectionOrder()),
                ExamPaperSectionInfo.class);
        if (lstExamPaperSectionInfo != null && lstExamPaperSectionInfo.size() > 0) {
            for (ExamPaperSectionInfo sectionInfo : lstExamPaperSectionInfo) {
                if (sectionInfo.getItemEndOrder() > 0) {
                    sectionInfo.setItemStartOrder((short) (sectionInfo.getItemStartOrder() + adjustNum));
                    sectionInfo.setItemEndOrder((short) (sectionInfo.getItemEndOrder() + adjustNum));
                } else {
                    sectionInfo.setItemStartOrder((short) (sectionInfo.getItemStartOrder() + adjustNum));
                }
                this.saveOrUpdateAll(lstExamPaperSectionInfo);
            }
        }
        //ÇåÀíÊÔ¾í»º´æ
        this.redisTemplate.delete(currentSectionInfo.getGroupId());
        return new Result(true);
    }
 
    private void deleteAllSectionItem(String sectionId) {
        ExamPaperSectionInfo examPaperSectionInfo = this.read(ExamPaperSectionInfo.class, sectionId);
        int origStartIndex = examPaperSectionInfo.getItemStartOrder();
        int origEndIndex = examPaperSectionInfo.getItemEndOrder();
 
        if (origEndIndex > 0) {
            int itemCount = origEndIndex - origStartIndex;
            List<ExerciseGroupItemRe> lstItemRe = new ArrayList<ExerciseGroupItemRe>(itemCount);
            String hql = "from ExerciseGroupItemRe r where r.exerciseGroupId=? and r.deleteFlag is false order by r.itemOrder asc";
            List<ExerciseGroupItemRe> lstExerciseGroupItemRe = this.find(hql,
                    CollectionUtils.newList(examPaperSectionInfo.getGroupId()), ExerciseGroupItemRe.class);
            lstItemRe.addAll(lstExerciseGroupItemRe.subList(examPaperSectionInfo.getItemStartOrder(),
                    examPaperSectionInfo.getItemEndOrder()));
            String[] exerciseItemIds = new String[itemCount];
            for (int i = 0; i < lstItemRe.size(); i++) {
                exerciseItemIds[i] = lstItemRe.get(i).getExerciseItemId();
            }
            this.deleteSectionItem(examPaperSectionInfo.getSectionId(), examPaperSectionInfo.getGroupId(),
                    exerciseItemIds);
            // section×ø±êÖÿÕ
            examPaperSectionInfo.setItemEndOrder((short) -1);
            this.save(examPaperSectionInfo);
 
        }
    }
 
    @Override
    public Result deleteSectionItem(String sectionId, String groupId, String[] exerciseItemIds) {
        if (exerciseItemIds != null && exerciseItemIds.length > 0) {
            // ²éѯÌâ¿âÓëÌâÄ¿µÄ¹ØÏµ
            List<String> lstRelationIds = new ArrayList<String>(exerciseItemIds.length);
            ExamPaperSectionInfo currentSectionInfo = this.read(ExamPaperSectionInfo.class, sectionId);
 
            String hql = "from ExerciseGroupItemRe r where r.exerciseGroupId=? and r.deleteFlag is false order by r.itemOrder asc";
            List<ExerciseGroupItemRe> lstExerciseGroupItemRe = this.find(hql,
                    CollectionUtils.newList(currentSectionInfo.getGroupId()), ExerciseGroupItemRe.class);
            lstExerciseGroupItemRe = lstExerciseGroupItemRe.subList(currentSectionInfo.getItemStartOrder(),
                    currentSectionInfo.getItemEndOrder());
 
            Map<String, ExerciseGroupItemRe> itemReMap = new HashMap<String, ExerciseGroupItemRe>(
                    exerciseItemIds.length);
            for (ExerciseGroupItemRe itemRe : lstExerciseGroupItemRe) {
                itemReMap.put(itemRe.getExerciseItemId(), itemRe);
            }
 
            for (String strExerciseItemId : exerciseItemIds) {
                lstRelationIds.add(itemReMap.get(strExerciseItemId).getRelationId());
            }
 
            // Èç¹ûÌâĿɾ³ýÍêÁË£¬ÔòÉèÖÃendΪ-1
            if (currentSectionInfo.getItemEndOrder() - exerciseItemIds.length == currentSectionInfo
                    .getItemStartOrder()) {
                currentSectionInfo.setItemEndOrder((short) -1);
            } else {
                currentSectionInfo
                        .setItemEndOrder((short) (currentSectionInfo.getItemEndOrder() - exerciseItemIds.length));
            }
            this.save(currentSectionInfo);
 
            this.deleteExerciseItems(lstRelationIds.toArray(new String[]{}), groupId);
 
            this.updateSectionRangeIndex(sectionId, -exerciseItemIds.length);
 
            this.updatePaperScore(groupId);
        }
        return new Result(true);
    }
 
    /**
     * É¾³ýÊÔ¾íϰÌâ
     *
     *
     * @param reIds
     * @param groupId
     * @return
     */
    private Result deleteExerciseItems(String[] reIds, String groupId) {
 
        ExerciseGroup group = this.read(ExerciseGroup.class, groupId);
 
        // ¸üÐÂ×éÊÔ¾íϰÌâÊýÁ¿
        group.setAllCount(group.getAllCount().subtract(new BigInteger(String.valueOf(reIds.length))));
        TraceUtils.setUpdateTrace(group);
        this.save(group);
 
        // 2.ɾ³ýÊÔ¾íÌâÄ¿×鹨Áª±íÊý¾Ý
        this.bulkUpdateInLoop("delete from ExerciseGroupItemRe  where relationId=?", reIds);
 
        // ¸üÐÂitemµÄ˳ÐòºÅ
        String hql = "from ExerciseGroupItemRe re where re.exerciseGroupId=? and re.deleteFlag is false order by re.itemOrder asc ";
        List<ExerciseGroupItemRe> lstRe = this.find(hql, CollectionUtils.newList(groupId), ExerciseGroupItemRe.class);
        if (lstRe != null && lstRe.size() > 0) {
            for (int i = 0; i < lstRe.size(); i++) {
                lstRe.get(i).setItemOrder(i + 1);
            }
            this.saveOrUpdateAll(lstRe);
        }
        return new Result(true);
    }
 
    @Override
    public Result saveSectionItem(ExerciseGroup exerciseGroup) {
        //±£´æÌâÄ¿ÐÅÏ¢
        return exerciseService.saveExerciseItemBatch(exerciseGroup.getGroupId(), exerciseGroup.getItems());
    }
 
    @Override
    public BigDecimal updatePaperScore(String groupId) {
        String totalScore = this.findUnique("select sum(s.score) as totalScore from ExerciseGroupItemRe r, ExerciseItemScore s where r.exerciseGroupId = s.groupId and r.exerciseItemId = s.exerciseItemId "
                + " and s.groupId=? and r.deleteFlag is false and s.deleteFlag is false ", CollectionUtils.newList(groupId), String.class);
        totalScore = StringUtils.isEmpty(totalScore) ? "0.00" : totalScore;
        this.bulkUpdate("update ExamPaperInfo set totalScore=" + totalScore + " where deleteFlag is false and  groupId=?", new Object[]{groupId});
        return new BigDecimal(totalScore);
    }
 
}