派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
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
package com.qxueyou.scc.school.service.impl;
 
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.imageio.ImageIO;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
 
import com.alibaba.fastjson.JSON;
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.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.config.AliOssConfig;
import com.qxueyou.scc.school.model.SchStudyReport;
import com.qxueyou.scc.school.service.IStudyReportService;
import com.qxueyou.scc.school.util.StudyReportUtils;
//import com.qxueyou.scc.sys.service.IOssService;
import com.qxueyou.scc.user.model.User;
import com.qxueyou.scc.user.model.UserRegistration;
 
/**
 * 学习报告查询及生成接口,如果存在,则直接查询,否则根据各类数据生成
 * @author xiadehu
 *
 */
@Service("StudyReportService")
@EnableConfigurationProperties(AliOssConfig.class)
public class StudyReportService extends CommonAppService implements
        IStudyReportService {
//    @Autowired
//    ICacheService cache;
//    
//    @Autowired
//    IOssService oss;
    
    @Autowired
    AliOssConfig aliOssConfig;
    
    private static final Logger log = LogManager.getLogger("StudyReportService");
 
    @Override
    public Result doGetOrCreateStudyReport() {
 
        String hql = "from SchStudyReport where userId=? and deleteFlag is false";
 
        SchStudyReport report = findUnique(hql,
                CollectionUtils.newList(ClientUtils.getUserId()),
                SchStudyReport.class);
 
        if (report == null) {
            report = createReport();
        }
 
        return new Result(true, null, toMapByReport(report));
    }
 
    /**
     * 创建新的学习报告
     * 汇聚各类数据
     * 
     * @return
     */
    private SchStudyReport createReport() {
 
        SchStudyReport report = new SchStudyReport();
        TraceUtils.setCreateTrace(report);
 
        User user = read(User.class, ClientUtils.getUserId());
 
        report.setUserId(user.getUserId());
        report.setName(user.getName());
        report.setStudentOrder(getStudyOrder(user.getCreateTime()));
        report.setStudyPeriod(getStudyPeriod(user.getCreateTime()));
        report.setCourseCount(getCourseCount(user.getUserId()));
        setFirstCourseAndClass(report);
        report.setStudyHandoutCount(getStudyHandoutCount(user.getUserId()));
        report.setStudyVideoCount(getStudyVideoCount(user.getUserId()));
        report.setViewLiveCount(getViewLiveCount(user.getUserId()));
        setExerciseData(report);
        setAtsData(report);
        setOrgs(report);
        setRecCourses(report);
        setStudyTime(report);
        
        save(report);
 
        return report;
    }
    
 
    private int getStudyPeriod(Date createTime) {
        Calendar cal = Calendar.getInstance();    
        cal.setTime(createTime);
        long time1 = cal.getTimeInMillis();   
        long between_days=(System.currentTimeMillis()-time1)/(1000*3600*24);  
        return Integer.parseInt(String.valueOf(between_days));
    }
 
    /**
     * 统计出用户集中学习的时间段,选取排名靠前的三个时间段(每个时间段1小时)进行显示
     * @param report
     */
    private void setStudyTime(SchStudyReport report) {
        
        List<Map<String, Object>> result = queryStudyTime();
        
        //从0点到23点,统计每个时间段学习的次数
        int[] hours = new int[24];
        
        int h=0;
        int num = 0;
        int count = 0;
        for(Map<String,Object> item:result){
            h = (Integer)item.get("h");
            num = ((Long)item.get("num")).intValue();
            hours[h]=hours[h]+num;
            count = count + num;
        }
        
        //计算出前三
        int[][] most3 = new int[][]{{0,0},{0,0},{0,0}};
        
        for(int i=0;i<24;i++){
            for(int j=0;j<3;j++){
                if(hours[i]>most3[j][0]){
                    most3[j][0]=hours[i];
                    most3[j][1]=i;
                    break;
                }
            }
        }
        
        
        List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
        
        //返回前三(非零)的数据
        for(int j=0;j<3;j++){
            if(most3[j][0]>0){
                data.add(CollectionUtils.newObjectMap(
                        "name",getTimeAlias(most3[j][1]),
                        "timeFrom",most3[j][1],
                        "timeTo",most3[j][1]+1,
                        "percent",String.format("%.1f%%",(double)most3[j][0]/count*100)));
            }
        }
        
        report.setStudyTime(JSON.toJSONString(data));
        report.setStudyTimeImg(createImg(data));
    }
 
    private String createImg(List<Map<String, Object>> data) {
        ByteArrayOutputStream os=null;
        InputStream is=null;
        try {
            BufferedImage img = StudyReportUtils.drawPng(180, 180, data);
            os = new ByteArrayOutputStream();  
            ImageIO.write(img, "png", os);
            is = new ByteArrayInputStream(os.toByteArray());
            String destPath = "/web/res/img/studyreport/"+ClientUtils.getUserId()+"/"+UUIDUtils.generateUUID()+".png";
//            oss.uplpadImage(is, "PNG", destPath);
            return destPath;
        } catch (IOException e) {
            log.error(e,e);
        }  finally{
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);            
        }
        return null;
    }
 
    /**
     * 查询练习、讲义、视频学习的时间统计数据
     * @return
     */
    private List<Map<String, Object>> queryStudyTime() {
        String hql1 = " select hour(createTime) as h,count(1) as num from ExerciseItemAnalisiU where userId=:userId group by hour(createTime) ";
        String hql2 = " select hour(createTime) as h,count(1) as num from MediaVideoLiveRecord where userId=:userId group by hour(createTime) ";
        String hql3 = " select hour(createTime) as h,count(1) as num from MediaVideoRecord where userId=:userId group by hour(createTime) ";
        
        List<Map<String,Object>> r1 = findListWithMapByHql(hql1, CollectionUtils.newObjectMap("userId",ClientUtils.getUserId()));
        List<Map<String,Object>> r2 = findListWithMapByHql(hql2, CollectionUtils.newObjectMap("userId",ClientUtils.getUserId()));
        List<Map<String,Object>> r3 = findListWithMapByHql(hql3, CollectionUtils.newObjectMap("userId",ClientUtils.getUserId()));
        r1.addAll(r2);
        r1.addAll(r3);
        return r1;
    }
    
    private String getTimeAlias(int hour){
        if(hour<3){
            return "深夜";
        }
        if(hour<6){
            return "凌晨";
        }
        if(hour<12){
            return "早上";
        }
        if(hour<14){
            return "中午";
        }
        if(hour<18){
            return "下午";
        }
        if(hour<24){
            return "晚上";
        }
        return null;
    }
 
    private void setRecCourses(SchStudyReport report) {
        
        String hql = " select cls.orgId from UserRegistration reg,ClsClass cls where reg.userId=? and reg.status=? and reg.deleteFlag is false " +
                " and reg.classId=cls.classId order by reg.activationTime desc";
        
        String orgId = findUnique(hql, CollectionUtils.newList(
                ClientUtils.getUserId(),UserRegistration.STATUS_ACTIVE),
                String.class);
        
        hql = " select code from Organization where organizationId=?";
        
        String code = findUnique(hql, CollectionUtils.newList(
                orgId),
                String.class);
        
        hql = "select org.logoPath as imgPath,cls.name as name, org.name as orgName,concat('/transact/class/view/',cls.classId) as clickUrl from ClsClass cls,Organization org "+ 
                "where cls.deleteFlag is false and cls.recommend is true and cls.orgId=:orgId and org.organizationId=cls.orgId " +
                "order by cls.studentCount desc,cls.clickNum desc";    
        
        List<Map<String,Object>> data = findListWithMapByHql(hql, 
                CollectionUtils.newObjectMap(
                        "orgId",orgId),new Pager(3,1));
        
        report.setRecCourses(JSON.toJSONString(data));
        report.setMoreRecUrl("/weixin/qxueyou/redirect?orgCode="+code+"&routerUrl=/course/market");
    }
 
    private void setOrgs(SchStudyReport report) {
        String hql = " select distinct org.name as name,org.logoPath as logoUrl from UserRegistration reg,ClsClass cls,Organization org " +
                "where reg.classId=cls.classId and cls.orgId=org.organizationId " +
                "and reg.userId=:userId and reg.deleteFlag is false and reg.status=:status";
        
        List<Map<String,Object>> data = findListWithMapByHql(hql, 
                CollectionUtils.newObjectMap(
                        "userId",ClientUtils.getUserId(),
                        "status",UserRegistration.STATUS_ACTIVE),new Pager(10,1));
        
        report.setOrgs(JSON.toJSONString(data));
    }
 
    /**
     * 设置传播大使数据
     * @param report
     */
    @SuppressWarnings("unchecked")
    private void setAtsData(SchStudyReport report) {
        String hql = "select className from AtsShare where userId=? and deleteFlag is false order by shareTime";
 
        String className = findUnique(hql, CollectionUtils.newList(
                ClientUtils.getUserId()),
                String.class);
        report.setFirstShareCourse(className);
        
        hql = "select sum(viewCount) as c from AtsShare where userId=? and deleteFlag is false";
 
        BigDecimal count= findUnique(hql, CollectionUtils.newList(ClientUtils.getUserId()), BigDecimal.class);
        report.setViewCountOfShare(count==null?0:count.intValue());
        
        hql = "select allBalance from TransAccount where userId=? and deleteFlag is false and accountTypeId=4";
 
        BigDecimal allBalance= findUnique(hql, CollectionUtils.newList(ClientUtils.getUserId()), BigDecimal.class);
        report.setAwardAmountOfShare(allBalance);
        
        hql = "select count(1) from TransAccount where deleteFlag is false and accountTypeId=3";
 
        int accountTotal= ((Number) findUnique(hql, Collections.EMPTY_LIST, Number.class)).intValue();
        
        hql = "select his.rank from SchRankHis his,SchRankVer ver where his.versionId=ver.versionId and ver.rankType='ATS_SCORE' and his.userId=?";
        Number rankN = findUnique(hql, CollectionUtils.newList(ClientUtils.getUserId()), Number.class);
        int rank= rankN==null?0:rankN.intValue();
        report.setAtsOrder(BigDecimal.valueOf((double)(accountTotal-rank)/accountTotal));
    }
 
    private void setExerciseData(SchStudyReport report) {
        
        String hql = "select count(distinct exerciseItemId) as c,coalesce(avg(accuracy),0) as accuracy from ExerciseItemAnalisiU where userId=:userId and deleteFlag is false";
 
        List<Map<String,Object>> data = findListWithMapByHql(hql, CollectionUtils.newObjectMap("userId",ClientUtils.getUserId()));
        
        if(data.isEmpty()){
            report.setDoExerciseCount(0);
            report.setExerciseAccuracy(BigDecimal.ZERO);
            report.setAccuracyOrder(BigDecimal.valueOf(800000+(int)Math.random()*400000));
            return;
        }
        
        report.setDoExerciseCount(((Long)(data.get(0).get("c"))).intValue());
        report.setExerciseAccuracy(BigDecimal.valueOf((Double)data.get(0).get("accuracy")));
        
        hql = "select count(1) from SchStudyReport where exerciseAccuracy>? and deleteFlag is false";
 
        int order= findUnique(hql, CollectionUtils.newList(report.getExerciseAccuracy()), Number.class).intValue();
        report.setAccuracyOrder(BigDecimal.valueOf(order));
    }
 
    private int getViewLiveCount(String userId) {
 
        String hql = "select count(liveId) from MediaVideoLiveRecord where userId=? and deleteFlag is false";
 
        return findUnique(hql, CollectionUtils.newList(userId), Number.class).intValue();
    }
 
    private int getStudyVideoCount(String userId) {
        String hql = "select count(videoId) from MediaVideoRecord where userId=? and deleteFlag is false";
 
        return findUnique(hql, CollectionUtils.newList(userId), Number.class).intValue();
    }
 
    private int getStudyHandoutCount(String userId) {
        String hql = "select count(handoutId) from SchHandoutRecord where userId=? and deleteFlag is false";
 
        return findUnique(hql, CollectionUtils.newList(userId), Number.class).intValue();
    }
 
    private int getCourseCount(String userId) {
 
        String hql = "select count(classId) from UserRegistration where userId=? and deleteFlag is false and status=?";
 
        return findUnique(
                hql,
                CollectionUtils.newList(userId, UserRegistration.STATUS_ACTIVE),
                Number.class).intValue();
 
    }
 
    private void setFirstCourseAndClass(SchStudyReport report) {
 
        String hql = "select classId from UserRegistration where userId=? and deleteFlag is false and status=? order by activationTime";
 
        String classId = findUnique(hql, CollectionUtils.newList(
                ClientUtils.getUserId(), UserRegistration.STATUS_ACTIVE),
                String.class);
 
        hql = "select coalesce(cls.name,'') as className,coalesce(cls.collegeCourseName,'') as courseName from ClsClass cls " +
                "where cls.classId=:classId and cls.deleteFlag is false";
 
        List<Map<String,Object>> cou = findListWithMapByHql(hql, CollectionUtils.newObjectMap("classId",classId));
        
        if(cou==null||cou.isEmpty()){
            return;
        }
 
        report.setFirstClass((String)cou.get(0).get("className"));
        report.setFirstCourse((String)cou.get(0).get("courseName"));
 
    }
 
    private int getStudyOrder(Date createTime) {
 
        String hql = "select count(1) from User where createTime<? and deleteFlag is false";
 
        return findUnique(hql, CollectionUtils.newList(createTime),
                Number.class).intValue();
 
    }
 
    /**
     * 将report转换为返回机构数据结果
     * 
     * @param report
     * @return
     */
    private Map<String, Object> toMapByReport(SchStudyReport report) {
        String resDomain = aliOssConfig.getDomain();
        
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("resDomain", resDomain);
        result.put("name", report.getName());
        result.put("studentOrder", report.getStudentOrder());
        result.put("studyPeriod", report.getStudyPeriod());
        result.put("courseCount", report.getCourseCount());
        result.put("firstCourse", report.getFirstCourse());
        result.put("firstClass", report.getFirstClass());
        result.put("studyHandoutCount", report.getStudyHandoutCount());
        result.put("studyVideoCount", report.getStudyVideoCount());
        result.put("viewLiveCount", report.getViewLiveCount());
        result.put("doExerciseCount", report.getDoExerciseCount());
        result.put("exerciseAccuracy", report.getExerciseAccuracy());
        result.put("accuracyOrder", report.getAccuracyOrder());
        result.put("firstShareCourse", report.getFirstShareCourse());
        result.put("viewCountOfShare", report.getViewCountOfShare());
        result.put("awardAmountOfShare", report.getAwardAmountOfShare());
        result.put("atsOrder", report.getAtsOrder());
        result.put("studyTime", JSON.parse(report.getStudyTime()));
        result.put("setStudyTimeImg",report.getStudyTimeImg());
        result.put("orgs", JSON.parse(report.getOrgs()));
        result.put("recCourses", JSON.parse(report.getRecCourses()));
        result.put("moreRecUrl", report.getMoreRecUrl());
        result.put("atsWithAwardCount", getatsWithAwardCount());
        
        return result;
    }
 
    private Integer getatsWithAwardCount() {
        
//        Long atsWithAwardCount = cache.get("school/studyReport/atsWithAwardCount", Long.class);
//        if(atsWithAwardCount!=null){
//            return atsWithAwardCount.intValue();
//        }
        
        String hql = "select count(1) from TransAccount where userId=? and allBalance>0 and deleteFlag is false and accountTypeId=4";
 
        Number c= findUnique(hql, CollectionUtils.newList(ClientUtils.getUserId()), BigDecimal.class);
//        cache.set("school/studyReport/atsWithAwardCount",3600*3, c);
        
        return c.intValue();
    }
 
}