Map<Long, String> wwUserId4PoiId = entityList.stream() .filter(e -> e.getFriendStatus()…
- User
Map<Long, String> wwUserId4PoiId = entityList.stream() .filter(e -> e.getFriendStatus() == 1) .collect(Collectors.toMap(WmAdMarketingWxUserInfoEntity::getWmPoiId, WmAdMarketingWxUserInfoEntity::getWwUserId, (v1, v2) -> v1));
换一个变量名
- Assistant
Map<Long, String> poiIdToUserId = entityList.stream() .filter(e -> e.getFriendStatus() == 1) .collect(Collectors.toMap(WmAdMarketingWxUserInfoEntity::getWmPoiId, WmAdMarketingWxUserInfoEntity::getWwUserId, (v1, v2) -> v1));
- User
public Map<Long, String> batchSendMsgToFriends(List wmPoiIdList, MsgContent msgContent) throws TException { Map<Long, String> failMap = Maps.newHashMap();
codeList<List<Long>> partition = Lists.partition(wmPoiIdList, 500); for (List<Long> p : partition) { List<WmAdMarketingWxUserInfoEntity> entityList = wmAdMarketingWxUserInfoRepository.selectByPoiIds(p); Map<Long, WmAdMarketingWxUserInfoEntity> poiIdToUserId = entityList.stream() .filter(e -> e.getFriendStatus() == 1) .collect(Collectors.toMap(WmAdMarketingWxUserInfoEntity::getWmPoiId, v -> v, (v1, v2) -> v1)); for (Long poi : p) { WmAdMarketingWxUserInfoEntity entity = poiIdToUserId.get(poi); if (entity != null) { String businessCode = entity.getBusinessCode(); String unionId = entity.getWwUnionId(); SendMsgToFriendsResponse resp = sendMsgToFriends(businessCode, 0L, unionId, msgContent); if (resp != null && resp.getCode() != 0) { failMap.put(poi, resp.getMsg()); } } else { failMap.put(poi, "该门店还没有绑定微信,无法发送微信消息"); } } } return failMap; } - Assistant
可以优化。
首先,可以将获取
poiIdToUserId的部分抽象出来,单独写一个方法,这样可以降低方法嵌套的层数,提高代码的可读性和可维护性。另外,可以使用Java8的
forEach方法代替传统的for循环,更加简洁。可以优化为:
code