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 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 rankLst) { for (SchRank rank : rankLst) { updateRank(rank); } } /** * 更新排名 * * @param rank */ private void updateRank(SchRank rank) { // 查询所有待排名实时数据列表 List rankLst = querySchRankLstByRank(rank); // 按value排序 Collections.sort(rankLst, new Comparator() { @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 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 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 lstSchRank = dao.findList(hql, page, CollectionUtils.newList( rank.getRankType(), rank.getScopeId(), rank.getScopeType()), SchRank.class); return lstSchRank; } /** * 判断哪些需要更新排名 * * @param userId * @return */ @SuppressWarnings("unused") private List getRankLstNeedUpdate(String userId,String rankType,String scopeType,String scopeId) { StringBuffer sb = new StringBuffer("from SchRank where userId=? and rankType = ? and scopeType = ? "); // 参数集合 List 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 lstSchRank = dao.find(sb.toString(), args, SchRank.class); List result = new ArrayList(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 rankLst) { List hisLst = new ArrayList(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); } }