派生自 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
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
package com.qxueyou.scc.courseware.service.impl;
 
import java.math.BigDecimal;
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.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
 
import com.qxueyou.scc.admin.classes.model.ClsClass;
import com.qxueyou.scc.base.model.Result;
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.base.util.UUIDUtils;
import com.qxueyou.scc.base.util.WordProcessUtils;
import com.qxueyou.scc.exercise.model.ExerciseGroup;
import com.qxueyou.scc.media.model.MediaVideo;
import com.qxueyou.scc.media.service.IMediaVideoService;
import com.qxueyou.scc.org.dao.OrgDAO;
import com.qxueyou.scc.org.model.OrgCategoryLevel;
import com.qxueyou.scc.org.model.OrgCollegeCourse;
import com.qxueyou.scc.org.model.OrgCourseCategory;
import com.qxueyou.scc.org.model.Organization;
import com.qxueyou.scc.courseware.service.ICourceCategoryService;
import com.qxueyou.scc.school.model.SchHandout;
import com.qxueyou.scc.school.service.IHandoutService;
import com.qxueyou.scc.sys.model.SysCondition;
 
@Service(value = "courceCategoryAppService")
public class CourceCategoryAppService  extends CommonAppService implements ICourceCategoryService {
    
    private OrgDAO orgDAO;
    
    /**
     * IHandoutService·þÎñ
     */
    @Autowired
    IHandoutService handoutService;
    
    @Autowired
    private IMediaVideoService mediaVideoService;
    
    public OrgDAO getOrgDAO() {
        return orgDAO;
    }
 
    /**
     * ÒÀÀµ×¢Èë
     *
     * @param commonDAO
     */
    @Autowired(required = false)
    public void setOrgDAO(@Qualifier("orgDAO")
        OrgDAO orgDAO) {
        this.orgDAO = orgDAO;
    }
 
    @Override
    public List<OrgCollegeCourse> queryOrgCourseList(String hql, List<Object> args) {
        return orgDAO.queryOrgCourseList(hql, args);
    }
 
    @Override
    public List<OrgCourseCategory> queryOrgCourseCategoryList(String hql,
            List<Object> args) {
        return orgDAO.queryOrgCourseCategoryList(hql, args);
    }
 
    @Override
    public List<OrgCollegeCourse> queryOrgCourseListNew(String hql, List<Object> args) {
        return orgDAO.queryOrgCourseListNew(hql, args);
    }
    
    /**
     * ×é×°   È«²¿¿Î³Ì    ËùÓвéѯÌõ¼þ
     * 
     * @return
     * @version 2.0
     */
    public Map<String,Object> generateAllCourseCondition(){
        
        Map<String,Object> result = new HashMap<String,Object>(3);
        
        // 1.¿Î³Ì·ÖÀà
        String firstHql = "select c from OrgCourseCategory c where c.deleteFlag is false and c.categoryLevel = ? order by c.code asc ";
        List<OrgCourseCategory> lstFirstCategory = this.find(firstHql, 
                CollectionUtils.newList(OrgCourseCategory.CATEGORY_LEVEL_FIRST) , OrgCourseCategory.class);
        
        String secondHql = "select c from OrgCourseCategory c where c.deleteFlag is false and c.categoryLevel = ? and type <> ? order by c.code asc ";
        List<OrgCourseCategory> lstSecondCategory = this.find(secondHql, 
                CollectionUtils.newList(OrgCourseCategory.CATEGORY_LEVEL_SECOND,Organization.ORG_TYPE_DEGREE_EDUCATION) , OrgCourseCategory.class);
        
        
        // 1.1 µÚ¶þ²ã
        Map<String,List<OrgCourseCategory>> secondMap = new HashMap<String,List<OrgCourseCategory>>(lstFirstCategory.size());
        for(OrgCourseCategory second : lstSecondCategory ){
            if(StringUtils.isNotBlank(second.getParentCategoryId()) && null == secondMap.get(second.getParentCategoryId())){
                secondMap.put(second.getParentCategoryId(), CollectionUtils.newList(OrgCourseCategory.class, second));
            }else if(StringUtils.isNotBlank(second.getParentCategoryId())){
                secondMap.get(second.getParentCategoryId()).add(second);
            }
        }
        
        // ×é×°µÄÊý¾Ý¸ñʽ
        Map<String,Object> sMap;
        List<Map<String,Object>> lstS ;
        
        Map<String,Object> fMap;
        List<Map<String,Object>> lstF = new ArrayList<Map<String,Object>>();
        
        int fIndex = 0; 
        int sIndex = 0; 
        
        // Ñ­»·
        for(OrgCourseCategory first : lstFirstCategory){
            
            fIndex = fIndex + 1 ;
            sIndex = 0; 
            fMap = new HashMap<String,Object>();
            lstS = new ArrayList<Map<String,Object>>();
            
            fMap.put("index", fIndex );
            fMap.put("categoryId", first.getCategoryId());
            fMap.put("categoryName", first.getCategoryName());
            fMap.put("code", first.getCode());
            fMap.put("parentId", null);
            
            // µÚ¶þ²ã
            if(null != secondMap.get(first.getCategoryId()) ){
                for(OrgCourseCategory second : secondMap.get(first.getCategoryId())){
                    
                    sIndex = sIndex + 1 ;
                    sMap = new HashMap<String,Object>();
                    
                    sMap.put("index", sIndex );
                    sMap.put("categoryId", second.getCategoryId());
                    sMap.put("categoryName", second.getCategoryName());
                    sMap.put("code", second.getCode());
                    sMap.put("parentId", second.getParentCategoryId());
                    lstS.add(sMap);
                }
            }
            
            fMap.put("children", lstS);
            
            lstF.add(fMap);
        }
        
        result.put("course", lstF);
        
        // 2.ÅÅÐò
        List<Map<String,Object>> lstSort = new ArrayList<Map<String,Object>>(5);
        Map<String,Object> sort;
        
        String sortHql = "from SysCondition s where s.deleteFlag is false and s.module = ? order by s.order asc ";
        List<SysCondition> lstSorts = this.find(sortHql, CollectionUtils.newList(SysCondition.MODULE_COURSE_MARKET_SORT), SysCondition.class);
        
        for(SysCondition s : lstSorts ){
            sort = new HashMap<String,Object>(3);
            sort.put("index", s.getOrder());
            sort.put("code", s.getCode());
            sort.put("name", s.getName());
            lstSort.add(sort);
        }
        result.put("sort", lstSort);
        
        // 3.µØÇø
        List<Map<String,Object>> lstCity = new ArrayList<Map<String,Object>>(6);
        Map<String,Object> city;
        
        String cityHql = "from SysCondition s where s.deleteFlag is false and s.module = ? order by s.order asc ";
        List<SysCondition> lstCitys = this.find(cityHql, CollectionUtils.newList(SysCondition.MODULE_COURSE_MARKET_CITY), SysCondition.class);
        
        for(SysCondition s : lstCitys ){
            city = new HashMap<String,Object>(3);
            city.put("index", s.getOrder());
            city.put("code", s.getCode());
            city.put("name", s.getName());
            lstCity.add(city);
        }
        result.put("city", lstCity);
        
        
        return result ;
    }
    
    public Result insertLevelCategory(String id,String name,String code,String type){
        Organization org = read(Organization.class, ClientUtils.getOrgId());
        Result result = new Result(true);
        //±£´æ
        if("save".equals(type)){
            OrgCategoryLevel ocl = read(OrgCategoryLevel.class, id);
            ocl.setCategoryName(name);
            ocl.setCode(code);
            save(ocl);
            result.setData(ocl);
            return result;
        }
        //Ôö¼Óͬ¼¶
        if("add".equals(type)){
            OrgCategoryLevel ocl0 = read(OrgCategoryLevel.class, id);
            OrgCategoryLevel ocl = new OrgCategoryLevel();
            ocl.setCategoryName(name);
            ocl.setCode(code);
            ocl.setDeleteFlag(false);
            ocl.setFullName(name);
            ocl.setImgPath(null);
            ocl.setLevel(ocl0.getLevel());
            ocl.setParentId(ocl0.getParentId());
            OrgCategoryLevel parent = read(OrgCategoryLevel.class, ocl0.getParentId());
            ocl.setOrgId(ClientUtils.getOrgId());
            ocl.setTopOrgId(org.getTopOrganizationId());
            TraceUtils.setCreateTrace(ocl);
            
            result.setData(insertOcl(ocl,parent,name));
            return result;
        }
        
        //Ôö¼Óϼ¶
        if("addNext".equals(type)){
            OrgCategoryLevel parent = read(OrgCategoryLevel.class, id);
            OrgCategoryLevel ocl = new OrgCategoryLevel();
            ocl.setCategoryName(name);
            ocl.setCode(code);
            ocl.setDeleteFlag(false);
            ocl.setFullName(name);
            ocl.setImgPath(null);
            ocl.setParentId(id);
            
            ocl.setOrgId(ClientUtils.getOrgId());
            ocl.setTopOrgId(org.getTopOrganizationId());
            TraceUtils.setCreateTrace(ocl);
            
            result.setData(insertOcl(ocl,parent,name));
            return result;
        }
        
        return new Result(false,"²ÎÊý´íÎó");
        
    
    }
    
    private OrgCategoryLevel insertOcl(OrgCategoryLevel ocl,OrgCategoryLevel parent,String name){
        int loop = 0;
        String levelCode = "";
        String origLevelCode = "";
        OrgCategoryLevel origCateLevel ;
        //BeanUtils.copyProperties(ocl, origCateLevel);
        if(parent==null){
            // Êײ㣺ֱ½ÓÉú³É
            levelCode = UUIDUtils.generateUUID();
            origLevelCode = levelCode;
        }else{
            // Ï²㣺¸ù¾ÝparentÉú³É
            levelCode = parent.getLevelCode()+"00";
        }
        do {
 
            try {
 
                origCateLevel = new OrgCategoryLevel();
                BeanUtils.copyProperties(ocl ,  origCateLevel);
                if(parent==null){ // Êײ㣺ֱ½ÓÉú³É
                    origCateLevel.setLevel(BigDecimal.ONE.shortValue());
                    origCateLevel.setLevelCode(levelCode+"_00");
                }else{ // Ï²㣺¸ù¾ÝparentÉú³É
                    origCateLevel.setLevel(new BigDecimal(parent.getLevel()).add(BigDecimal.ONE).shortValue());
                    origCateLevel.setLevelCode(levelCode);
                }
                save(origCateLevel);
                flush();
                
                return origCateLevel ;
                // break;
 
            } catch (Exception e) {
                
                clearCache();
                
                loop++;
                if(parent==null){
                    // Êײ㣺ֱ½ÓÉú³É
                    levelCode = origLevelCode.concat(String.valueOf(loop));
                }else{
                    // Ï²㣺¸ù¾ÝparentÉú³É
                    String h = String.valueOf(loop);
                    if(loop<10){
                        h = "0"+loop;
                    }
                    levelCode = parent.getLevelCode().concat(h);
                }
                
            }
 
        } while (loop < 1000);
        
        return null ;
    }
    
    @SuppressWarnings("unused")
    private String generateLevelCode(String name) {
 
        // ×ªÆ´Òô
        //ʹÓÃ΢Èí×Ô´øÖÐÎÄÊäÈë·¨ÓÃÊó±ê·­Ò³£¬Ñ¡ÔñµÚÒ»Ò³Ö®ºóµÄÊý¾Ýʱ£¬»áÉú³É(li'zhi.dr)ÕâÖÖÕ˺Å
        String[] arrUnidecode = WordProcessUtils.toPinyin(name," ").replaceAll("'", "").split(" ");
        
        String orgCode = "";
        if(null != arrUnidecode){
            for(String str : arrUnidecode){
                orgCode = orgCode.concat(str.substring(0,1));
            }
        }
        
        
        return orgCode;
    }
    
    public Result doDeleteCate(String id){
        boolean flag = false;
        String hql = " from OrgCategoryLevel where deleteFlag is false and (categoryLevelId = ? or parentId = ?)";
        List<OrgCategoryLevel> lstCate = find(hql, CollectionUtils.newList(id,id), OrgCategoryLevel.class);
        for (OrgCategoryLevel orgCategoryLevel : lstCate) {
            hql = " from OrgCollegeCourse where deleteFlag is false and courseCategoryId = ?";
            List<OrgCollegeCourse> course = find(hql, CollectionUtils.newList(orgCategoryLevel.getCategoryLevelId()), OrgCollegeCourse.class);
            for (OrgCollegeCourse orgCollegeCourse : course) {
                hql = " from ClsClass where collegeCourseId = ? and deleteFlag is false";
                List<ClsClass> classes = find(hql, CollectionUtils.newList(orgCollegeCourse.getCollegeCourseId()), ClsClass.class);
                if(!classes.isEmpty()){
                    flag = true;
                    break;
                }
                
            }
            if(flag){
                break;
            }
            
        }
        if(flag){
            return new Result(false,"¸Ã·ÖÀàϵĿÆÄ¿Ï´æÔڰ༶,ÇëÏÈɾ³ý°à¼¶");
        }
        for (OrgCategoryLevel orgCategoryLevel : lstCate) {
            hql = " from OrgCollegeCourse where deleteFlag is false and courseCategoryId = ?";
            List<OrgCollegeCourse> course = find(hql, CollectionUtils.newList(orgCategoryLevel.getCategoryLevelId()), OrgCollegeCourse.class);
            for (OrgCollegeCourse orgCollegeCourse : course) {
                doDeleteCourse(orgCollegeCourse.getCollegeCourseId());
            }
            orgCategoryLevel.setDeleteFlag(true);
            TraceUtils.setUpdateTrace(orgCategoryLevel);
            save(orgCategoryLevel);
            
        }
        return new Result(true);
    }
    
    public Result doDeleteCourse(String id){
        String hql = " from ClsClass where deleteFlag is false and collegeCourseId = ?";
        ClsClass cls = findUnique(hql, CollectionUtils.newList(id), ClsClass.class);
        if(cls!=null){
            return new Result(false,"¸Ã¿ÆÄ¿Ï´æÔڰ༶,ÇëÏÈɾ³ý°à¼¶");
        }
        //ɾ³ýËùÓÐÊÓÆµ
        doDeleteVideo(id);
        //ɾ³ýËùÓÐÁ·Ï°
        doDeleteExe(id);
        //ɾ³ýËùÓн²Òå
        doDeleteHandout(id);
        
        hql = " from OrgCollegeCourse where deleteFlag is false and collegeCourseId = ?";
        OrgCollegeCourse course = findUnique(hql, CollectionUtils.newList(id), OrgCollegeCourse.class);
        course.setDeleteFlag(true);
        TraceUtils.setUpdateTrace(course);
        return save(course);
    }
    
    private void doDeleteVideo(String id){
        String hql = " from MediaVideo where (videoId in "
                + " (select videoId from MediaVideoReCourse where collegeCourseId = ? and deleteFlag is false)"
                + " or originVideoId in (select videoId from MediaVideoReCourse where collegeCourseId = ? and deleteFlag is false))"
                + " and deleteFlag is false";
        List<MediaVideo> lstVideo = find(hql, CollectionUtils.newList(id,id), MediaVideo.class);
        for (MediaVideo mv : lstVideo) {
            mv.setDeleteFlag(true);
            TraceUtils.setUpdateTrace(mv);
            this.mediaVideoService.saveVideo(mv);
        }
    }
    private void doDeleteExe(String id){
        String hql = " from ExerciseGroup where (groupId in (select groupId from ExerciseReCourse where collegeCourseId = ? and deleteFlag is false)"
                + " or originExerciseId in (select groupId from ExerciseReCourse where collegeCourseId = ? and deleteFlag is false))"
                + " and deleteFlag is false";
        List<ExerciseGroup> groups = find(hql,CollectionUtils.newList(id,id),ExerciseGroup.class);
        for (ExerciseGroup g : groups){
            g.setDeleteFlag(true);
            TraceUtils.setUpdateTrace(g);
            save(g);
        }
    }
    
    private void doDeleteHandout(String id){
        String hql = " from SchHandout where (handoutId in "
                + "(select handoutId from SchHandoutReCourse where collegeCourseId = ? and deleteFlag is false"
                + ") or originHandoutId in (select handoutId from SchHandoutReCourse where collegeCourseId = ? and deleteFlag is false ))"
                + "and deleteFlag is false";
        List<SchHandout> handouts = find(hql,CollectionUtils.newList(id,id),SchHandout.class);
        for(SchHandout handout : handouts){
            handout.setDeleteFlag(true);
            TraceUtils.setUpdateTrace(handout);
            this.handoutService.saveHandout(handout);
        }
    }
    
    /**
     * ÐÂÔö¿ÆÄ¿
     * @param id
     * @param name
     * @param code
     * @param type  
     * @return
     */
    @Override
    public Result addCategory(Organization org, String id, String name, String code, String type) {
        Result result = new Result(true);
        //±£´æ
        if("save".equals(type)){
            OrgCategoryLevel ocl = this.read(OrgCategoryLevel.class, id);
            ocl.setCategoryName(name);
            ocl.setCode(code);
            this.save(ocl);
            result.setData(ocl);
            return result;
        }
        //Ôö¼Óͬ¼¶
        if("add".equals(type)){
            OrgCategoryLevel ocl0 = this.read(OrgCategoryLevel.class, id);
            OrgCategoryLevel ocl = new OrgCategoryLevel();
            ocl.setCategoryName(name);
            ocl.setCode(code);
            ocl.setDeleteFlag(false);
            ocl.setFullName(name);
            ocl.setImgPath(null);
            ocl.setLevel(ocl0.getLevel());
            ocl.setParentId(ocl0.getParentId());
            OrgCategoryLevel parent = this.read(OrgCategoryLevel.class, ocl0.getParentId());
            TraceUtils.setCreateTrace(ocl);
            
            result.setData(insertOcl(ocl,parent,name));//²åÈëocl
            return result;
        }
        
        //Ôö¼Óϼ¶
        if("addNext".equals(type)){
            OrgCategoryLevel parent = this.read(OrgCategoryLevel.class, id);
            OrgCategoryLevel ocl = new OrgCategoryLevel();
            ocl.setCategoryName(name);
            ocl.setCode(code);
            ocl.setDeleteFlag(false);
            ocl.setFullName(name);
            ocl.setImgPath(null);
            ocl.setParentId(id);
            TraceUtils.setCreateTrace(ocl);
            
            result.setData(insertOcl(ocl,parent,name));
            return result;
        }
        
        return new Result(false);
    }
    
}