派生自 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
package com.qxueyou.scc.school.service.impl;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
 
import javax.annotation.PostConstruct;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
//import com.aliyun.openservices.ons.api.ConsumeContext;
//import com.aliyun.openservices.ons.api.Message;
import com.qxueyou.scc.base.dao.CommonDAO;
import com.qxueyou.scc.base.model.Pager;
import com.qxueyou.scc.base.service.IONSExceptionLogService;
//import com.qxueyou.scc.base.service.IONSMsgDealService;
//import com.qxueyou.scc.base.service.impl.CommonONSConsumer;
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.school.model.SchRank;
import com.qxueyou.scc.school.model.SchRankHis;
import com.qxueyou.scc.school.model.SchRankVer;
 
@Service("RankMsgDealService")
public class RankMsgDealService{
//public class RankMsgDealService implements IONSMsgDealService {
    
    @Autowired
    CommonDAO dao;
    
//    @Autowired
//    ICacheService cache;
    
    @Autowired
    IONSExceptionLogService onsExceptionLogService;
    
 
    
    @SuppressWarnings("unused")
    private static Logger log = LogManager.getLogger("ONSExceptionLogService");
    
    /**
     * ÅÅÃû¸üÐÂʱ¼ä,ĬÈÏ60Ãë
     */
    @SuppressWarnings("unused")
    private int updateRankMinutes = 60;
    
    @PostConstruct
    void init() {
        
//        CommonONSConsumer.registerHandler("SCH_RANK", "RankMsgDealService");
        
//        updateRankMinutes = Integer.parseInt(cfg.getConfigByEnv("ons-rank-update-minutes"));
    }
 
//    @Override
//    public void doHandle(Message msg, ConsumeContext context) {
//        
//        try {
//            
//            String userId = msg.getUserProperties("userId");
//            String rankType = msg.getUserProperties("rankType");
//            String scopeType = msg.getUserProperties("scopeType");
//            String scopeId = msg.getUserProperties("scopeId");
//            
//            // ÅжÏÄÄЩÐèÒª¸üÐÂÅÅÃû
//            List<SchRank> rankLst = getRankLstNeedUpdate(userId,rankType,scopeType,scopeId);
//            
//            // Èç¹ûÓУ¬Ôò¸üÐÂÅÅÃû
//            updateRankLst(rankLst);
//            
//        }catch(Exception e){
//            
//            String businessId = cfg.getConfigByEnv("ons-common-topic") + "-" + UUIDUtils.generateUUID();
//            String desp = "userId:" + msg.getUserProperties("userId")+ 
//                    ";rankType:" + msg.getUserProperties("rankType") +
//                    ";scopeType:" + msg.getUserProperties("scopeType") + 
//                    ";scopeId:" + msg.getUserProperties("scopeId") ;
//
//            log.error( businessId + " :ÅÅÃûÏûÏ¢·¢ËÍʧ°Ü:" + e , e);
//            onsExceptionLogService.logSaveExceptionLog(cfg.getConfigByEnv("ons-common-topic"), "SCH_RANK", businessId , desp );
//        }
//        
//        
//    }
 
    /**
     * ¸üÐÂÅÅÃû
     * 
     * @param rankLst
     */
    @SuppressWarnings("unused")
    private void updateRankLst(List<SchRank> rankLst) {
 
        for (SchRank rank : rankLst) {
            updateRank(rank);
        }
 
    }
 
    /**
     * ¸üÐÂÅÅÃû
     * 
     * @param rank
     */
    private void updateRank(SchRank rank) {
 
        // ²éѯËùÓдýÅÅÃûʵʱÊý¾ÝÁбí
        List<SchRank> rankLst = querySchRankLstByRank(rank);
 
        // °´valueÅÅÐò
        Collections.sort(rankLst, new Comparator<SchRank>() {
            @Override
            public int compare(SchRank r1, SchRank r2) {
                if(r1.getValue()==null){
                    return -1;
                }
                if(r2.getValue()==null){
                    return 1;
                }
                return r2.getValue().compareTo(r1.getValue());
            }
        });
 
        // Ð½¨°æ±¾¼Ç¼
        SchRankVer version = insertRankVersion(rank);
 
        // ²åÈërank_hisÊý¾Ý
        saveRankHisData(version, rankLst);
 
        // ²åÈ뻺´æ¼Ç¼£¬±ê¼Ç±¾´Î¸üÐÂÅÅÃûʱ¼ä
        updateUpdateTime(rank);
 
    }
 
    /**
     * ²åÈ뻺´æ¼Ç¼£¬±ê¼Ç±¾´Î¸üÐÂÅÅÃûʱ¼ä
     * 
     * @param rank
     */
    private void updateUpdateTime(SchRank rank) {
//        cache.set(getCacheIdFromRank(rank), 60 * updateRankMinutes, 1);
    }
 
    /**
     * »ñÈ¡cacheId
     * 
     * @param rank
     * @return
     */
    @SuppressWarnings("unused")
    private String getCacheIdFromRank(SchRank rank) {
        return rank.getScopeType()
                .concat(rank.getScopeId() != null ? rank.getScopeId() : "")
                .concat(rank.getRankType());
    }
 
    /**
     * ´´½¨Ðµİ汾¼Ç¼
     * 
     * @param rank
     * @return
     */
    private SchRankVer insertRankVersion(SchRank rank) {
 
        // ¸üоɵÄ×îа汾
        String hql = "from SchRankVer where rankType=? and scopeId=? and scopeType=? and latest is true order by version desc";
        List<SchRankVer> oldLastestVerLst = dao.find(hql, CollectionUtils.newList(
                rank.getRankType(), rank.getScopeId(), rank.getScopeType()),
                SchRankVer.class);
 
        SchRankVer lastestVer = oldLastestVerLst.isEmpty()?null:oldLastestVerLst.get(0);
        
        for(SchRankVer oldVer:oldLastestVerLst){
            TraceUtils.setUpdateTrace(oldVer);
            oldVer.setLatest(false);
            dao.saveOrUpdate(oldVer);
        }
 
        // ´´½¨Ðµİ汾
        SchRankVer ver = new SchRankVer();
 
        TraceUtils.setCreateTrace(ver);
        ver.setDeleteFlag(false);
 
        ver.setLatest(true);
        ver.setRankType(rank.getRankType());
        ver.setScopeId(rank.getScopeId());
        ver.setScopeType(rank.getScopeType());
        ver.setVersion(lastestVer == null ? 1
                : lastestVer.getVersion() + 1);
        ver.setVersionTime(new Date());
 
        dao.saveOrUpdate(ver);
 
        return ver;
    }
 
    /**
     * ¸ù¾ÝÈκÎÒ»ÌõÅÅÃûÊý¾Ý²éѯËùÓдýÅÅÃûʵʱÊý¾ÝÁбí
     * 
     * @param rank
     * @return
     */
    private List<SchRank> querySchRankLstByRank(SchRank rank) {
        Pager page = new Pager();
        page.setPageNum(1);
        page.setPageSize(200);
        String hql = "from SchRank where rankType=? and scopeId=? and scopeType=? order by value desc";
        List<SchRank> lstSchRank = dao.findList(hql, page, CollectionUtils.newList(
                rank.getRankType(), rank.getScopeId(), rank.getScopeType()),
                SchRank.class);
 
        return lstSchRank;
    }
 
    /**
     * ÅжÏÄÄЩÐèÒª¸üÐÂÅÅÃû
     * 
     * @param userId
     * @return
     */
    @SuppressWarnings("unused")
    private List<SchRank> getRankLstNeedUpdate(String userId,String rankType,String scopeType,String scopeId) {
 
        StringBuffer sb = new StringBuffer("from SchRank where userId=? and rankType = ? and scopeType = ? ");
        // ²ÎÊý¼¯ºÏ
        List<Object> args = CollectionUtils.newList(userId,rankType,scopeType);
        
        if(!SchRank.SCOPE_TYPE_QXUEYOU.equals(scopeType) 
                && StringUtils.isNotBlank(scopeId)){// qxueyouÅÅÃû ²»ÐèÒª
            sb.append(" and scopeId = ?");
            args.add(scopeId);
        }
        
        List<SchRank> lstSchRank = dao.find(sb.toString(), args, SchRank.class);
 
        List<SchRank> result = new ArrayList<SchRank>(5);
        
        String cacheId = null;
 
//        for (SchRank rank : lstSchRank) {
//            cacheId = getCacheIdFromRank(rank);
//            if (cache.get(cacheId, Integer.class) == null) {
//                result.add(rank);
//            }
//        }
 
        return result;
    }
 
    /**
     * ²åÈërank_hisÊý¾Ý
     * 
     * @param version
     * @param rankLst
     */
    private void saveRankHisData(SchRankVer version, List<SchRank> rankLst) {
 
        List<SchRankHis> hisLst = new ArrayList<SchRankHis>(rankLst.size());
 
        int i = 1;
        Date now = new Date();
        SchRankHis his = null;
 
        for (SchRank rank : rankLst) {
 
            // QѧÓÑÖ»´¦Àíǰһ°ÙÃû
            if (i > 100
                    && SchRank.SCOPE_TYPE_QXUEYOU.equals(rank.getScopeType())) {
                break;
            }
 
            his = new SchRankHis();
 
            TraceUtils.setCreateTrace(his);
            his.setDeleteFlag(false);
 
            his.setRank(i++);
            his.setRankId(rank.getRankId());
            his.setRankTime(now);
            his.setUserId(rank.getUserId());
            his.setValue(rank.getValue());
            his.setVersionId(version.getVersionId());
 
            hisLst.add(his);
        }
 
        dao.saveOrUpdateAll(hisLst);
    }
    
}