admin
2022-05-12 fa705507ba574c857b1667553737d23b1b7ff495
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
package com.ks.app.service.manager;
 
import com.ks.app.entity.SystemEnum;
import com.ks.app.entity.config.AppJumpType;
import com.ks.app.entity.config.SystemConfigKey;
import com.ks.app.service.inter.config.SystemConfigService;
import com.ks.app.vo.AcceptData;
import com.ks.lib.common.exception.ParamsException;
import com.ks.push.exception.BPushDeviceTokenException;
import com.ks.push.exception.BPushTaskException;
import com.ks.push.pojo.DO.*;
import com.ks.push.service.BDeviceTokenService;
import com.ks.push.service.BPushTaskService;
import com.ks.push.utils.JPushUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author hxh
 * @title: PushManager
 * @description: 推送管理器
 * @date 2022/4/29 17:33
 */
@Component
public class PushManager {
 
    @Reference(version = "1.0", check = false)
    private BDeviceTokenService bdeviceTokenService;
 
    @Reference(version = "1.0", check = false)
    private BPushTaskService bPushTaskService;
 
    @Resource
    private SystemConfigService systemConfigService;
 
    /**
     * @return void
     * @author hxh
     * @description 保存token
     * @date 17:36 2022/4/29
     * @param: acceptData
     * @param: uid
     * @param: regId
     **/
    public void saveToken(AcceptData acceptData, Long uid, String regId) throws BPushDeviceTokenException, ParamsException {
        BPushDeviceToken token = new BPushDeviceToken();
        if (uid != null) {
            token.setUid(uid + "");
        }
        token.setType(PushPlatform.jpush);
        token.setAppCode(systemConfigService.getValueCache(acceptData.getSystem(), SystemConfigKey.androidPushAppCode));
        token.setVersionCode(acceptData.getVersion());
        token.setToken(regId);
        token.setDeviceId(acceptData.getUtdId());
        token.setBuildVersion(acceptData.getOsVersion());
        token.setBuildModel(acceptData.getDeviceType());
        token.setCreateTime(new Date());
        bdeviceTokenService.save(token);
    }
 
 
    /**
     * @return void
     * @author hxh
     * @description 绑定用户ID
     * @date 17:40 2022/4/29
     * @param: system
     * @param: uid
     * @param: deviceId
     **/
    public void bindUid(SystemEnum system, Long uid, String deviceId) {
        bdeviceTokenService.bindUid(systemConfigService.getValueCache(system, SystemConfigKey.androidPushAppCode), deviceId, uid + "");
    }
 
    /**
     * @return void
     * @author hxh
     * @description 解绑用户ID
     * @date 17:41 2022/4/29
     * @param: system
     * @param: deviceId
     **/
    public void unBind(SystemEnum system, String deviceId) {
        bdeviceTokenService.unBindUid(systemConfigService.getValueCache(system, SystemConfigKey.androidPushAppCode), deviceId);
    }
 
 
    /**
     * @return void
     * @author hxh
     * @description 推送签到
     * @date 16:40 2022/5/7
     * @param: system
     * @param: uid
     **/
    public void pushSignIn(SystemEnum system, List<Long> uidList) throws BPushTaskException {
        if (uidList == null || uidList.size() == 0) {
            return;
        }
        Map<String, String> extras = new HashMap<>();
        extras.put("type", AppJumpType.signIn.name());
        String title = "签到提醒";
        String content = "今日你还未签到,签到最高可领150金币!";
        push(system, uidList, title, content, extras);
    }
 
    /**
     * @return void
     * @author hxh
     * @description 推送金币结算
     * @date 16:41 2022/5/7
     * @param: system
     * @param: uid
     * @param: goldCorn
     * @param: money
     **/
    public void pushGoldCornSettle(SystemEnum system, Long uid, int goldCorn, BigDecimal money) throws BPushTaskException {
        Map<String, String> extras = new HashMap<>();
        extras.put("type", AppJumpType.extract.name());
        String title = "到账提醒";
        String content = String.format("昨日金币折算成功,入账%s元", money.setScale(2).toString());
        push(system, Arrays.asList(new Long[]{uid}), title, content, extras);
    }
 
    /**
     * @return void
     * @author hxh
     * @description 推送内容
     * @date 16:55 2022/5/7
     * @param: system
     * @param: uid
     * @param: title
     * @param: content
     * @param: extras
     **/
    private void push(SystemEnum system, List<Long> uidList, String title, String content, Map<String, String> extras) throws BPushTaskException {
        BPushTask bPushTask = new BPushTask();
        BPushMessage message = new BPushMessage();
        message.setMessage(false);
        message.setExtras(extras);
        message.setAndroidActivity(getPushIntent(system));
        message.setTitle(title);
        message.setContent(content);
 
 
        bPushTask.setMessage(message);
        bPushTask.setAppCode(systemConfigService.getValueCache(system, SystemConfigKey.androidPushAppCode));
 
        BPushFilter filter = new BPushFilter();
        if (uidList == null || uidList.size() == 0) {
            //推送30天内还活跃的用户
            filter.setMinActiveTime(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24L * 30));
        } else {
            filter.setUidList(uidList.stream().map(item -> item + "").collect(Collectors.toList()));
        }
        bPushTask.setFilter(filter);
        String taskId = bPushTaskService.createTask(bPushTask);
        bPushTaskService.startPush(taskId);
    }
 
    private String getPushIntent(SystemEnum system) {
        return JPushUtil.createIntent(systemConfigService.getValueCache(system, SystemConfigKey.androidPushActivity), system.getPackageName());
    }
 
 
}