@ -1,13 +1,35 @@
@@ -1,13 +1,35 @@
package com.cloud.kicc.commonbiz.service.impl ;
import cn.hutool.core.date.LocalDateTimeUtil ;
import cn.hutool.core.util.BooleanUtil ;
import cn.hutool.core.util.ObjectUtil ;
import cn.hutool.core.util.StrUtil ;
import com.alibaba.fastjson.JSONObject ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import com.baomidou.mybatisplus.core.toolkit.Wrappers ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.cloud.kicc.common.core.exception.CheckedException ;
import com.cloud.kicc.commonbiz.api.entity.PushBlacklist ;
import com.cloud.kicc.commonbiz.api.entity.PushChatMessage ;
import com.cloud.kicc.commonbiz.api.entity.PushConcernFan ;
import com.cloud.kicc.commonbiz.api.entity.PushConcernFanType ;
import com.cloud.kicc.commonbiz.api.enums.PushAuditStatusEnum ;
import com.cloud.kicc.commonbiz.mapper.PushConcernFanMapper ;
import com.cloud.kicc.commonbiz.service.IPushBlacklistService ;
import com.cloud.kicc.commonbiz.service.IPushChatMessageService ;
import com.cloud.kicc.commonbiz.service.IPushConcernFanService ;
import com.cloud.kicc.commonbiz.service.IPushConcernFanTypeService ;
import com.cloud.kicc.commonbiz.util.PushClientUtil ;
import lombok.RequiredArgsConstructor ;
import okhttp3.OkHttpClient ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import java.time.format.DateTimeFormatter ;
import java.util.List ;
import java.util.Map ;
import java.util.Optional ;
import java.util.stream.Collectors ;
/ * *
* < p >
@ -18,8 +40,16 @@ import java.util.Map;
@@ -18,8 +40,16 @@ import java.util.Map;
* @since 2023 - 03 - 24
* /
@Service
@RequiredArgsConstructor
@Transactional ( rollbackFor = Exception . class )
public class PushConcernFanServiceImpl extends ServiceImpl < PushConcernFanMapper , PushConcernFan > implements IPushConcernFanService {
private final OkHttpClient okHttpClient ;
private final IPushConcernFanService iPushConcernFanService ;
private final IPushBlacklistService iPushBlacklistService ;
private final IPushChatMessageService iPushChatMessageService ;
private final IPushConcernFanTypeService iPushConcernFanTypeService ;
@Override
public IPage < Map < String , Object > > selectPushConcernFanList ( IPage < Map < String , Object > > page , Map < String , Object > map ) {
return baseMapper . selectPushConcernFanList ( page , map ) ;
@ -34,4 +64,125 @@ public class PushConcernFanServiceImpl extends ServiceImpl<PushConcernFanMapper,
@@ -34,4 +64,125 @@ public class PushConcernFanServiceImpl extends ServiceImpl<PushConcernFanMapper,
public IPage < Map < String , Object > > selectPushFriendList ( IPage < Map < String , Object > > page , Map < String , Object > map ) {
return baseMapper . selectPushFriendList ( page , map ) ;
}
@Override
public void concernFanMessageSend ( PushChatMessage pushChatMessage ) {
if ( StrUtil . isBlank ( pushChatMessage . getUserId ( ) ) ) {
throw new CheckedException ( "当前推送方用户ID必填!" ) ;
}
List < PushConcernFanType > pushConcernFanTypeList ;
// 是否为关注推送
if ( StrUtil . equals ( pushChatMessage . getConcernFanStatus ( ) , "1" ) ) {
if ( StrUtil . isBlank ( pushChatMessage . getAlias ( ) ) ) throw new CheckedException ( "当前推送用户别名ID必填!" ) ;
// 查询对方是否有关注推送用户
PushConcernFan pushConcernFan = iPushConcernFanService . getOne ( Wrappers . < PushConcernFan > lambdaQuery ( )
. eq ( PushConcernFan : : getConcernUserId , pushChatMessage . getUserId ( ) )
. eq ( PushConcernFan : : getFanUserId , pushChatMessage . getAlias ( ) )
. eq ( PushConcernFan : : getStatus , PushAuditStatusEnum . APPROVED ) ) ;
// 推送限制关注用户只能推送一条消息,需要双向关注后才能解锁限制
if ( ObjectUtil . isEmpty ( pushConcernFan ) ) {
Optional < PushChatMessage > optionalPushChatMessage = iPushChatMessageService . list ( Wrappers . < PushChatMessage > lambdaQuery ( )
. eq ( PushChatMessage : : getConcernFanStatus , "1" )
. eq ( PushChatMessage : : getUserId , pushChatMessage . getUserId ( ) )
. eq ( PushChatMessage : : getAlias , pushChatMessage . getAlias ( ) ) ) . stream ( ) . findFirst ( ) ;
optionalPushChatMessage . ifPresent ( value - > { throw new CheckedException ( "对方还未关注你,你只能发送一条消息,发送失败!" ) ; } ) ;
}
// 查询关注设置的推送类型,如果参数中没有指定则采用默认的推送类型
pushConcernFanTypeList = iPushConcernFanTypeService . list ( Wrappers . < PushConcernFanType > lambdaQuery ( )
. eq ( PushConcernFanType : : getFanUserId , pushChatMessage . getUserId ( ) )
. eq ( StrUtil . isNotBlank ( pushChatMessage . getPushTypeId ( ) ) , PushConcernFanType : : getTypeId , pushChatMessage . getPushTypeId ( ) )
. eq ( StrUtil . isBlank ( pushChatMessage . getPushTypeId ( ) ) , PushConcernFanType : : getDefaultType , "1" ) ) ;
} else {
// 配置推送用户,针对粉丝支持多人推送
if ( StrUtil . isBlank ( pushChatMessage . getAlias ( ) ) ) {
List < PushConcernFan > pushConcernFanList = iPushConcernFanService . list ( Wrappers . < PushConcernFan > lambdaQuery ( )
. eq ( PushConcernFan : : getConcernUserId , pushChatMessage . getUserId ( ) )
. eq ( PushConcernFan : : getStatus , PushAuditStatusEnum . APPROVED ) ) ;
pushChatMessage . setAlias ( pushConcernFanList . stream ( ) . map ( item - > item . getConcernUserId ( ) ) . collect ( Collectors . joining ( ) ) ) ;
}
// 查询粉丝设置的推送类型,如果参数中没有指定则采用默认的推送类型
pushConcernFanTypeList = iPushConcernFanTypeService . list ( Wrappers . < PushConcernFanType > lambdaQuery ( )
. eq ( PushConcernFanType : : getConcernFanId , pushChatMessage . getUserId ( ) )
. eq ( StrUtil . isNotBlank ( pushChatMessage . getPushTypeId ( ) ) , PushConcernFanType : : getTypeId , pushChatMessage . getPushTypeId ( ) )
. eq ( StrUtil . isBlank ( pushChatMessage . getPushTypeId ( ) ) , PushConcernFanType : : getDefaultType , "1" ) ) ;
}
// 过滤黑名单,根据谁拉黑了推送方用户进行过滤推送用户
List < PushBlacklist > pushBlacklists = iPushBlacklistService . list ( Wrappers . < PushBlacklist > lambdaQuery ( ) . eq ( PushBlacklist : : getFanUserId , pushChatMessage . getUserId ( ) ) ) ;
pushBlacklists . forEach ( item - > pushChatMessage . setAlias ( pushChatMessage . getAlias ( ) . replaceAll ( item . getConcernUserId ( ) + ",|$" , "" ) ) ) ;
// 推送消息
StrUtil . split ( pushChatMessage . getAlias ( ) , "," ) . forEach ( alias - > {
Optional < PushConcernFanType > pushConcernFanTypeOptional = pushConcernFanTypeList . stream ( ) . filter ( item - >
StrUtil . equals ( pushChatMessage . getConcernFanStatus ( ) , "1" ) ? item . getConcernUserId ( ) . equals ( alias ) : item . getFanUserId ( ) . equals ( alias ) ) . findFirst ( ) ;
if ( pushConcernFanTypeOptional . isPresent ( ) ) {
PushConcernFanType pushConcernFanType = pushConcernFanTypeOptional . get ( ) ;
pushChatMessage . setPushTypeId ( pushConcernFanType . getTypeId ( ) ) ;
pushChatMessage . setPlayLights ( pushConcernFanType . getPlayLights ( ) ) ;
pushChatMessage . setPlaySound ( pushConcernFanType . getPlaySound ( ) ) ;
pushChatMessage . setPlayVibrate ( pushConcernFanType . getPlayVibrate ( ) ) ;
pushChatMessage . setPlayToText ( pushConcernFanType . getPlayToText ( ) ) ;
pushChatMessage . setSound ( pushConcernFanType . getOfflineRingtone ( ) ) ;
pushChatMessage . setOnlineRingtone ( pushConcernFanType . getOnlineRingtone ( ) ) ;
}
pushChatMessage . setType ( "customizedcast" ) ;
pushChatMessage . setDisplayType ( "notification" ) ;
pushChatMessage . setAliasType ( PushClientUtil . uPushUserAliasType ) ;
pushChatMessage . setAlias ( alias ) ;
JSONObject custom = new JSONObject ( ) ;
custom . put ( "fanUserId" , pushChatMessage . getUserId ( ) ) ;
custom . put ( "offlineRingtone" , pushChatMessage . getSound ( ) ) ;
custom . put ( "onlineRingtone" , pushChatMessage . getOnlineRingtone ( ) ) ;
custom . put ( "playToText" , BooleanUtil . toBoolean ( pushChatMessage . getPlayToText ( ) ) ) ;
custom . put ( "playSound" , BooleanUtil . toBoolean ( pushChatMessage . getPlaySound ( ) ) ) ;
custom . put ( "playVibrate" , BooleanUtil . toBoolean ( pushChatMessage . getPlayVibrate ( ) ) ) ;
custom . put ( "playLights" , BooleanUtil . toBoolean ( pushChatMessage . getPlayLights ( ) ) ) ;
pushChatMessage . setCustom ( custom . toString ( ) ) ;
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject . put ( "production_mode" , PushClientUtil . PRODUCTION_MODE ) ;
jsonObject . put ( "type" , pushChatMessage . getType ( ) ) ;
jsonObject . put ( "description" , pushChatMessage . getRemarks ( ) ) ;
jsonObject . put ( "alias_type" , pushChatMessage . getAliasType ( ) ) ;
jsonObject . put ( "alias" , pushChatMessage . getAlias ( ) ) ;
JSONObject payload = new JSONObject ( ) ;
payload . put ( "display_type" , pushChatMessage . getDisplayType ( ) ) ;
JSONObject body = new JSONObject ( ) ;
body . put ( "ticker" , pushChatMessage . getTitle ( ) ) ;
body . put ( "text" , pushChatMessage . getText ( ) ) ;
body . put ( "title" , pushChatMessage . getTitle ( ) ) ;
body . put ( "sound" , pushChatMessage . getSound ( ) ) ;
body . put ( "after_open" , "go_custom" ) ;
body . put ( "custom" , pushChatMessage . getCustom ( ) ) ;
if ( BooleanUtil . toBoolean ( pushChatMessage . getPlaySound ( ) ) ) {
body . put ( "play_sound" , true ) ;
}
if ( BooleanUtil . toBoolean ( pushChatMessage . getPlayVibrate ( ) ) ) {
body . put ( "play_vibrate" , true ) ;
}
if ( BooleanUtil . toBoolean ( pushChatMessage . getPlayLights ( ) ) ) {
body . put ( "play_lights" , true ) ;
}
payload . put ( "body" , body ) ;
jsonObject . put ( "payload" , payload ) ;
JSONObject policy = new JSONObject ( ) ;
policy . put ( "expire_time" , LocalDateTimeUtil . now ( ) . plusDays ( 3 ) . format ( DateTimeFormatter . ofPattern ( "yyyy-MM-dd HH:mm:ss" ) ) ) ;
policy . put ( "out_biz_no" , LocalDateTimeUtil . now ( ) . format ( DateTimeFormatter . ofPattern ( "yyyy-MM-dd HH:mm:ss" ) ) ) ;
jsonObject . put ( "policy" , policy ) ;
try {
PushClientUtil pushClientUtil = new PushClientUtil ( okHttpClient ) ;
pushClientUtil . send ( jsonObject ) ;
iPushChatMessageService . save ( pushChatMessage ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
} ) ;
}
}