package com.yami.trading.service.system.impl; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.google.common.collect.Lists; import com.yami.trading.bean.Tip; import com.yami.trading.common.constants.RedisKeys; import com.yami.trading.common.constants.TipConstants; import com.yami.trading.dao.tip.TipMapper; import com.yami.trading.service.RealNameAuthRecordService; import com.yami.trading.service.syspara.SysparaService; import com.yami.trading.service.system.TipService; import com.yami.trading.service.tradeview.SymbolTradeViewData; import com.yami.trading.service.tradeview.SymbolTradeViewDataHolder; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; import java.util.*; @Service @Slf4j public class TipServiceImpl extends ServiceImpl implements TipService { private static Logger logger = LoggerFactory.getLogger(TipServiceImpl.class); public final static String TIPLIST = RedisKeys.TIPLIST; Map>> randomTip = new HashMap<>(); /** * key:业务id */ @Autowired private SysparaService sysparaService; @Autowired RedisTemplate redisTemplate; @Autowired RealNameAuthRecordService realNameAuthRecordService; /** * 初始化数据 */ public void init() { // HashOperations hashOps = redisTemplate.opsForHash(); redisTemplate.delete(TIPLIST); List list = this.list(); for (Tip tip : list) { // System.out.println(tip.getBusinessId()+"->"+tip.getModel()); // if (tip.getModel().equals(TipConstants.KYC)){ // RealNameAuthRecord realNameAuthRecord= realNameAuthRecordService.getById(tip.getBusinessId()); // if (realNameAuthRecord==null){ // deleteTip(tip.getBusinessId()); // } // else { // put(tip.getBusinessId(), tip); // } // }//else { put(tip.getBusinessId(), tip); //} } } /** * 新增通知 * * @param businessId 业务id(唯一性) * @param model 模块 */ public void saveTip(String businessId, String model) { try { Tip tip = get(businessId); if (null == tip) { tip = new Tip(); } tip.setBusinessId(businessId); tip.setCreateTime(new Date()); tip.setModel(model); tip.setTimeStamp(new Date().getTime()); saveOrUpdate(tip); put(businessId, tip); } catch (Exception e) { logger.error("fail put tip businessId:{" + businessId + "},model:{" + model + "},e:{}", e); } } public void saveTip(String businessId, String model,String userId) { try { Tip tip = get(businessId); if (null == tip) { tip = new Tip(); } tip.setBusinessId(businessId); tip.setCreateTime(new Date()); tip.setModel(model); tip.setTimeStamp(new Date().getTime()); tip.setSendUserId(userId); saveOrUpdate(tip); put(businessId, tip); } catch (Exception e) { logger.error("fail put tip businessId:{" + businessId + "},model:{" + model + "},e:{}", e); } } public void put(String businessId,Tip tip) { try { HashOperations hashOps = redisTemplate.opsForHash(); hashOps.put(TIPLIST, businessId, tip); // String json= (String) redisTemplate.opsForValue().get(TIPLIST); // if (StrUtil.isNotBlank(json)){ // Map tipMap= new Gson().fromJson(json,new TypeToken>(){}.getType()); // tipMap.put(businessId,tip); // redisTemplate.opsForValue().set(TIPLIST,new Gson().toJson(tipMap)); // // } } catch (Exception e) { } } public void remove(String businessId){ try { HashOperations hashOps = redisTemplate.opsForHash(); hashOps.delete(TIPLIST,businessId); // String json= (String) redisTemplate.opsForValue().get(TIPLIST); // if (StrUtil.isNotBlank(json)){ // Map tipMap= new Gson().fromJson(json,new TypeToken>(){}.getType()); // tipMap.remove(businessId); // redisTemplate.opsForValue().set(TIPLIST,new Gson().toJson(tipMap)); // // } } catch (Exception e) { } } public Tip get(String businessId) { try { HashOperations hashOps = redisTemplate.opsForHash(); return (Tip) hashOps.get(TIPLIST,businessId); // String json= (String) redisTemplate.opsForValue().get(TIPLIST); // if (StrUtil.isNotBlank(json)){ // Map tipMap= new Gson().fromJson(json,new TypeToken>(){}.getType()); // return tipMap.get(businessId); // } } catch (Exception e) { } return null; } public List values() { try { List list = redisTemplate.opsForHash().values(TIPLIST); return list; } catch (Exception e) { } return null; } /** * 新增通知 * * @param tip 消息通知 */ public void saveTip(Tip tip) { try { tip.setCreateTime(new Date()); tip.setTimeStamp(new Date().getTime()); save(tip); put(tip.getBusinessId(), tip); } catch (Exception e) { logger.error("fail put tip businessId:{" + tip.getBusinessId() + "},model:{" + tip.getModel() + "},e:{}", e); } } /** * 移除通知 * * @param businessId */ public void deleteTip(String businessId) { try { Tip tip = get(businessId); if (tip != null) { removeById(tip); remove(businessId); } } catch (Exception e) { logger.error("fail remove tip businessId:{" + businessId + "},e:{}", e); } } /** * 批量移除通知 * */ public void deleteTip(List businessIds) { deleteBatchTip(businessIds);// 解决幂等性性问题 for (String id : businessIds) { remove(id); } } /** * 批量更新订单收益 * */ protected void deleteBatchTip(final List idList) { remove(Wrappers.query().lambda().in(Tip::getBusinessId,idList)); } /** * 获取总数 数据 * * @param username * @return */ public List> cacheSumTips(String username,List userIds) { List> result = new LinkedList>(); // 根据权限列表,判定是否有模块的通知权限 List resourceIds = this.userAuth(username); List filterTips = filterTips(username, null); if (CollectionUtils.isEmpty(filterTips)) { // logger.info("根据用户名获取的通知数据:{}", filterTips); return result; } // 构建模块的通知数据 Map> modelMap = modelData(filterTips,userIds); //logger.info("构建模块的通知数据:{}", modelMap); for (Map.Entry> entry : modelMap.entrySet()) { // if(resourceIds==null){ // Map map = tipData(entry.getKey(), entry.getValue()); // result.add(map); // } else if (resourceIds.contains(entry.getKey())) {// 有权限则返回通知 // Map map = tipData(entry.getKey(), entry.getValue()); // result.add(map); // } Map map = tipData(entry.getKey(), entry.getValue()); result.add(map); } // logger.info("返回数据:{}", result); return result; } /** * 获取指定模块的新通知数据 * * @param username * @return */ public List> cacheNewTipsByModel(String username, Long lastTimeStamp, String model,List userIds) { List> result = new LinkedList>(); // 根据权限列表,判定是否有模块的通知权限 List resourceIds = this.userAuth(username); List filterNewTips = filterTips(username, lastTimeStamp); if (CollectionUtils.isEmpty(filterNewTips)) { return result; } // 构建模块的通知数据 Map> modelMap = modelData(filterNewTips,userIds); List tipList = modelMap.get(model); if (CollectionUtils.isEmpty(tipList)) { return result; } else { result.add(tipNewData(model, tipList,userIds)); } return result; } /** * 新增通知 * * @param businessId 业务id(唯一性) * @param model 模块 */ public void saveNewTip(String businessId, String model,String remark) { try { Tip tip = get(businessId); if (null == tip) { tip = new Tip(); } tip.setBusinessId(businessId); tip.setCreateTime(new Date()); tip.setModel(model); tip.setTimeStamp(new Date().getTime()); tip.setTargetUsername(remark); saveOrUpdate(tip); put(businessId, tip); } catch (Exception e) { logger.error("fail put tip businessId22:{" + businessId + "},model:{" + model + "},e:{}", e); } } /** * 获取新通知数据 * * @param username * @return */ public List> cacheNewTips(String username, Long lastTimeStamp,List userIds) { List> result = new LinkedList>(); // 根据权限列表,判定是否有模块的通知权限 List filterNewTips = filterTips(username, lastTimeStamp); // System.out.println("username = " + username); // System.out.println("resourceIds = " + resourceIds); // System.out.println("filterNewTips = " + filterNewTips); if (CollectionUtils.isEmpty(filterNewTips)) { return result; } // 构建模块的通知数据 Map> modelMap = modelData(filterNewTips,userIds); for (Map.Entry> entry : modelMap.entrySet()) { // if(resourceIds==null){ Map map = tipNewData(entry.getKey(), entry.getValue(),userIds); result.add(map); // } // else if (resourceIds.contains(entry.getKey())) {// 有权限则返回通知 // Map map = tipNewData(entry.getKey(), entry.getValue()); // result.add(map); // } } return result; } private List filterTips(final String username, final Long lastTimeStamp) { ArrayList tipList = new ArrayList(values()); CollectionUtils.filter(tipList, new Predicate() {// 过滤查找新生成的通知数据 @Override public boolean evaluate(Object paramObject) { // TODO Auto-generated method stub Tip tip = (Tip) paramObject; if (TipConstants.MUST_USERNAME_MODEL.containsKey(tip.getModel())) { if (StringUtils.isNotEmpty(tip.getTargetUsername()) && username.equals(tip.getTargetUsername())) { return lastTimeStamp == null || tip.getTimeStamp() > lastTimeStamp;// 时间戳为空则直接返回 } else { return false; } } else if (TipConstants.AGENT_MODEL.containsKey(tip.getModel())) { // if(ch){ // // } } return lastTimeStamp == null || ((Tip) paramObject).getTimeStamp() > lastTimeStamp; } }); return tipList; } private String getPath(HttpServletRequest request) { return String.format("%s://%s:%s%s", request.getScheme(), request.getServerName(), request.getServerPort(), request.getContextPath()); } /** * 用户权限列表,根据权限判定对应的模块提醒 * * @param username * @return */ private List userAuth(String username) { // 这块可做成缓存 // SecUser user = this.secUserService.findUserByLoginName(username); // Role role = user.getRoles().toArray(new Role[0])[0]; // Set resources = role.getResources(); // List resourceIds = new ArrayList(); // for (Resource r : resources) { // resourceIds.add(r.getId().toString()); // } // return resourceIds; return null; } /** * 每个模块对应的提醒数据 * * @return */ private Map> modelData(Collection tips,List userIds) { // 构建需要返回对应权限的数据 ArrayList tipList = new ArrayList(tips); Map> modelMap = new HashMap>(); // 构建各个模块的数据 for (Tip tip : tipList) { List list = modelMap.get(tip.getModel()); if (list == null) { list = new ArrayList(); } if (CollectionUtil.isNotEmpty(userIds)){ if (userIds.contains(tip.getSendUserId())){ list.add(tip); } } else { list.add(tip); } modelMap.put(tip.getModel(), list); } return modelMap; } /** * 构建模块的通知数据 * * @param model 模块 * @param list 模块列表数据 * @return */ private Map tipNewData(String model, List list, List userIds) { Map resultMap = new HashMap(); List tipList=new ArrayList<>(); if (CollectionUtil.isNotEmpty(userIds)){ for (Tip t:list){ log.info(userIds.contains(t.getSendUserId())+"======"+t.getSendUserId()); if (userIds.contains(t.getSendUserId())){ tipList.add(t); } } list=tipList; } // 模块新增数 resultMap.put("tip_content_num", list.size()); // 生成html通知 // String htmlMessage = MessageFormat.format(TipConstants.MESSAGE_MAP.get(model), // MessageFormat.format("{0}", list.size())); String htmlMessage = TipConstants.MESSAGE_MAP.get(model); if(list.size() > 0 && htmlMessage != null){ htmlMessage = htmlMessage.replace("{0}",""+list.size()+""); } // 模块提示消息 resultMap.put("tip_message", list.size() > 0 ? htmlMessage : ""); // 模块请求url resultMap.put("tip_url", list.size() > 0 ? TipConstants.ACTION_MAP.get(model) : ""); // 是否右下角提示 resultMap.put("tip_show",isShowTip(model)); // 提示类型 resultMap.put("tip_type",TipConstants.MESSAGE_TYPE.get(model)); return resultMap; } /** * 是否右下角提示,true:是,false:否 * @param model * @return */ private boolean isShowTip(String model) { String value = sysparaService.find("tip_noshow_models").getSvalue(); return value.indexOf(model)==-1; } private Map tipData(String model, final List list) { Map resultMap = new HashMap(); // 页面上对应的classname(可自定义) resultMap.put("tip_dom_name", TipConstants.DOM_MAP.get(model)); // 模块总数 resultMap.put("tip_content_sum", list.size()); // 模块新增数 return resultMap; } public List> getRandom() { String[] strName = { // Technology Services "ruanjianhefuwu", // Electronic Technology "kejishebei", // Finance "yinhanghetouzifuwu", // Health Technology "yiliaofuwu", // Industrial Services, "gongyepin", // Consumer Durables "baihuolingshou", // Finance "yinhanghetouzifuwu", "shiwuheyinliao", "zhouqixingxiaofeipin", "baoxian", "qichejilingjian", "gngyeheshangyefuwu", "huashilianliao", "zouqixingfuwu", "dianxinfuwu", "gongyongshiye", "fangdichan", "kuangchanziyuan", "huaxuepin", "xiaofeipin" }; randomTip.clear(); List> arr = new ArrayList<>(); String[] strValues = new String[strName.length]; strValues[0] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Technology Services", "-") + "%"; strValues[1] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Electronic Technology", "-") + "%"; strValues[2] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Finance", "-") + "%"; strValues[3] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Health Technology", "-") + "%"; strValues[4] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Industrial Services", "-") + "%"; strValues[5] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Consumer Durables", "-") + "%"; strValues[6] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Finance", "-") + "%"; strValues[7] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Energy Minerals", "-") + "%"; strValues[8] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Producer Manufacturing", "-") + "%"; strValues[9] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Commercial Services", "-") + "%"; strValues[10] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Utilities", "-") + "%"; strValues[11] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Non-Energy Minerals", "-") + "%"; strValues[12] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Transportation", "-") + "%"; strValues[13] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Distribution Services", "-") + "%"; strValues[14] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Process Industries", "-") + "%"; strValues[15] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Communications", "-") + "%"; strValues[16] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Communications", "-") + "%"; strValues[17] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Communications", "-") + "%"; strValues[18] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Retail Trade", "-") + "%"; strValues[19] = SymbolTradeViewDataHolder.industryMap.getOrDefault("Communications", "-") + "%"; for (int i = 0; i < strName.length; i++) { Map map1 = new HashMap<>(); map1.put("key", strName[i]); map1.put("value", strValues[i]); arr.add(map1); } return arr; } @Override public List> getRandom(String type) { List symbolTradeViewData = SymbolTradeViewDataHolder.typeList.get(type); if(CollectionUtils.isEmpty(symbolTradeViewData)){ return Lists.newArrayList(); } int size = symbolTradeViewData.size(); List> arr = new ArrayList<>(); for (int i = 0; i < size; i++) { Map map1 = new HashMap<>(); map1.put("key", symbolTradeViewData.get(i).getSymbol()); map1.put("value", symbolTradeViewData.get(i).getRate().toString() +"%"); arr.add(map1); } return arr; } }