ソースを参照

feats: add message send logic

张延森 5 年 前
コミット
1c5b6513ad

+ 29
- 3
src/main/java/com/huiju/estateagents/common/CommConstant.java ファイルの表示

@@ -445,19 +445,19 @@ public class CommConstant {
445 445
      * 消息通知
446 446
      * 必须存在表   td_miniapp_template_type 中
447 447
      */
448
-    public static final String MINIAPP_TPL_NOTICE = "notice";
448
+    public static final String MESSAGE_CONTENT_OF_NOTICE = "notice";
449 449
     
450 450
     /**
451 451
      * 助力通知
452 452
      * 必须存在表   td_miniapp_template_type 中
453 453
      */
454
-    public static final String MINIAPP_TPL_HELP = "help-result";
454
+    public static final String MESSAGE_CONTENT_OF_HELP = "help-result";
455 455
     
456 456
     /**
457 457
      * 拼团通知
458 458
      * 必须存在表   td_miniapp_template_type 中
459 459
      */
460
-    public static final String MINIAPP_TPL_GROUP = "group-result";
460
+    public static final String MESSAGE_CONTENT_OF_GROUP = "group-result";
461 461
     
462 462
     /**
463 463
      * 活动以结束
@@ -515,4 +515,30 @@ public class CommConstant {
515 515
      * 埋点类型h5
516 516
      */
517 517
     public static final String EVENT_H5 = "h5";
518
+
519
+    /**
520
+     * 系统参数  通知消息次时间间隔
521
+     */
522
+    public static final String SYSPARAM_NOTICE_MESSAGE_INTERVAL = "notice_message_interval";
523
+
524
+    /**
525
+     * 任一消息, 可以是下面的任意一种
526
+     */
527
+    public static final String MESSAGE_TYPE_OF_SOMEWAY = "someway";
528
+
529
+    /**
530
+     * 短信消息
531
+     */
532
+    public static final String MESSAGE_TYPE_OF_SMS = "sms";
533
+
534
+    /**
535
+     * 小程序订阅消息
536
+     */
537
+    public static final String MESSAGE_TYPE_OF_MINIAPP_SUBSCRIBE = "miniapp_subscribe";
538
+
539
+    /**
540
+     * 小程序模板消息
541
+     */
542
+    public static final String MESSAGE_TYPE_OF_MINIAPP_TEMPLATE = "miniapp_template";
543
+
518 544
 }

+ 4
- 0
src/main/java/com/huiju/estateagents/common/NumberUtils.java ファイルの表示

@@ -0,0 +1,4 @@
1
+package com.huiju.estateagents.common;
2
+
3
+public class NumberUtils {
4
+}

+ 78
- 0
src/main/java/com/huiju/estateagents/entity/TaPersonMessageRule.java ファイルの表示

@@ -0,0 +1,78 @@
1
+package com.huiju.estateagents.entity;
2
+
3
+import com.baomidou.mybatisplus.annotation.IdType;
4
+import com.baomidou.mybatisplus.annotation.TableId;
5
+import lombok.Data;
6
+import lombok.EqualsAndHashCode;
7
+import lombok.experimental.Accessors;
8
+
9
+import java.io.Serializable;
10
+import java.time.LocalDateTime;
11
+
12
+/**
13
+ * <p>
14
+ * 用户消息规则
15
+ * </p>
16
+ *
17
+ * @author jobob
18
+ * @since 2019-09-04
19
+ */
20
+@Data
21
+@EqualsAndHashCode(callSuper = false)
22
+@Accessors(chain = true)
23
+public class TaPersonMessageRule implements Serializable {
24
+
25
+    private static final long serialVersionUID = 1L;
26
+
27
+    /**
28
+     * 序号
29
+     */
30
+    @TableId(value = "serial_no", type = IdType.AUTO)
31
+    private Integer serialNo;
32
+
33
+    /**
34
+     * 人员ID
35
+     */
36
+    private String personId;
37
+
38
+    /**
39
+     * 消息类型
40
+     */
41
+    private String messageType;
42
+
43
+    /**
44
+     * 内容类型
45
+     */
46
+    private String contentType;
47
+
48
+    /**
49
+     * 限制条数
50
+     */
51
+    private Integer limitNum;
52
+
53
+    /**
54
+     * 当前使用
55
+     */
56
+    private Integer leftNum;
57
+
58
+    /**
59
+     * 上次发送时间
60
+     */
61
+    private LocalDateTime lastSendTime;
62
+
63
+    /**
64
+     * 上次消息ID
65
+     */
66
+    private String lastMessageId;
67
+
68
+    /**
69
+     * 公司id
70
+     */
71
+    private Integer orgId;
72
+
73
+    /**
74
+     * 楼盘id
75
+     */
76
+    private String buildingId;
77
+
78
+}

+ 28
- 0
src/main/java/com/huiju/estateagents/mapper/TaPersonMessageRuleMapper.java ファイルの表示

@@ -0,0 +1,28 @@
1
+package com.huiju.estateagents.mapper;
2
+
3
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4
+import com.huiju.estateagents.entity.TaPersonMessageRule;
5
+import org.apache.ibatis.annotations.Mapper;
6
+import org.apache.ibatis.annotations.Param;
7
+
8
+/**
9
+ * <p>
10
+ *  Mapper 接口
11
+ * </p>
12
+ *
13
+ * @author jobob
14
+ * @since 2019-11-13
15
+ */
16
+@Mapper
17
+public interface TaPersonMessageRuleMapper extends BaseMapper<TaPersonMessageRule> {
18
+
19
+    /**
20
+     * 依据人员ID, 消息类型, 内容类型, 等获取人员消息规则
21
+     * @param personId
22
+     * @param messageType
23
+     * @param contentType
24
+     * @param orgId
25
+     * @return
26
+     */
27
+    TaPersonMessageRule getByPersonAndType(@Param("personId") String personId, @Param("messageType") String messageType, @Param("contentType") String contentType, @Param("orgId") Integer orgId);
28
+}

+ 0
- 4
src/main/java/com/huiju/estateagents/service/IMiniAppService.java ファイルの表示

@@ -30,10 +30,6 @@ public interface IMiniAppService {
30 30
     
31 31
     void sendGroupMessage(TaPerson toUser, String link, String result, String activityName, LocalDateTime dt);
32 32
 
33
-    void sendMainBizMessage(TaPerson toUser, String link, String custName, String phone, String sex, String fromName, String bizType, String content);
34
-
35
-    void sendNewCustomerMessage(TaPerson toUser, String link, String custName, String phone, String sex, String fromName);
36
-
37 33
     ResponseBean getQrCode(String paramsStr, String appid);
38 34
 
39 35
     String createQrCode(WxMaService service, String path) throws Exception;

+ 35
- 0
src/main/java/com/huiju/estateagents/service/ITaPersonMessageRuleService.java ファイルの表示

@@ -0,0 +1,35 @@
1
+package com.huiju.estateagents.service;
2
+
3
+import com.baomidou.mybatisplus.extension.service.IService;
4
+import com.huiju.estateagents.entity.TaPersonMessageRule;
5
+import org.apache.ibatis.annotations.Param;
6
+
7
+/**
8
+ * <p>
9
+ * 人员消息规则 服务类
10
+ * </p>
11
+ *
12
+ * @author jobob
13
+ * @since 2019-05-10
14
+ */
15
+public interface ITaPersonMessageRuleService extends IService<TaPersonMessageRule> {
16
+    /**
17
+     * 依据 人员ID, 消息类型等查询人员消息规则
18
+     * @param personId
19
+     * @param messageType
20
+     * @param contentType
21
+     * @param orgId
22
+     * @return
23
+     */
24
+    TaPersonMessageRule getByPersonAndType(String personId, String messageType, String contentType, Integer orgId);
25
+
26
+    /**
27
+     * 依据 人员ID, 消息类型等更新或者插入人员消息规则
28
+     * @param personId
29
+     * @param messageType
30
+     * @param contentType
31
+     * @param orgId
32
+     * @return
33
+     */
34
+    TaPersonMessageRule saveOrUpdateByPersonAndMessage(String personId, String messageType, String contentType, Integer orgId);
35
+}

+ 154
- 113
src/main/java/com/huiju/estateagents/service/impl/MiniAppServiceImpl.java ファイルの表示

@@ -9,13 +9,16 @@ import com.huiju.estateagents.common.*;
9 9
 import com.huiju.estateagents.entity.*;
10 10
 import com.huiju.estateagents.mapper.*;
11 11
 import com.huiju.estateagents.service.IMiniAppService;
12
+import com.huiju.estateagents.service.ISysOrgParamsService;
12 13
 import com.huiju.estateagents.service.ITaMiniFormidsService;
14
+import com.huiju.estateagents.service.ITaPersonMessageRuleService;
13 15
 import lombok.extern.slf4j.Slf4j;
14 16
 import me.chanjar.weixin.common.error.WxErrorException;
15 17
 import org.springframework.beans.factory.annotation.Autowired;
16 18
 import org.springframework.stereotype.Service;
17 19
 
18 20
 import java.io.File;
21
+import java.time.Duration;
19 22
 import java.time.LocalDateTime;
20 23
 import java.util.ArrayList;
21 24
 import java.util.List;
@@ -42,6 +45,12 @@ public class MiniAppServiceImpl implements IMiniAppService {
42 45
     @Autowired
43 46
     SysSmsSettingMapper sysSmsSettingMapper;
44 47
 
48
+    @Autowired
49
+    ITaPersonMessageRuleService iTaPersonMessageRuleService;
50
+
51
+    @Autowired
52
+    ISysOrgParamsService iSysOrgParamsService;
53
+
45 54
     @Autowired
46 55
     WxUtils wxUtils;
47 56
 
@@ -81,33 +90,76 @@ public class MiniAppServiceImpl implements IMiniAppService {
81 90
             log.error("小程序 {} 不存在", toUser.getMiniappId());
82 91
             return ;
83 92
         }
84
-        String[] smsParams = { name, miniapp.getName() + "小程序" };
85 93
 
86
-        TaMiniappTemplate tpl = getTplByType(CommConstant.MINIAPP_TPL_NOTICE, toUser.getMiniappId());
94
+        // 消息内容类型
95
+        String contentType = CommConstant.MESSAGE_CONTENT_OF_NOTICE;
96
+
97
+        // 查询消息类型, 优先选择订阅消息发送
98
+        TaMiniappTemplate tpl = getTplByType(contentType, toUser.getMiniappId());
99
+
100
+        boolean isSubscribe = null != tpl && (null == tpl.getIsSubscribe() ? false : tpl.getIsSubscribe());
101
+
102
+        // 入库的消息类型使用的是 MESSAGE_TYPE_OF_SOMEWAY, 不能使用真正的类型
103
+        // 因为 通知消息 只注重内容, 不在乎 发送方式
104
+        String messageType = CommConstant.MESSAGE_TYPE_OF_SOMEWAY;
105
+
106
+        // 消息发送间隔, -1 代表不限制
107
+        float interval = -1f;
108
+
109
+        // 获取系统设置, 单位分钟
110
+        SysOrgParams sysOrgParams = iSysOrgParamsService.getParamByCode(CommConstant.SYSPARAM_NOTICE_MESSAGE_INTERVAL, toUser.getOrgId());
111
+        if (null != sysOrgParams && !StringUtils.isEmpty(sysOrgParams.getParamValue())) {
112
+            interval = Float.parseFloat(sysOrgParams.getParamValue());
113
+        }
114
+
115
+        // 获取用户消息发送记录
116
+        TaPersonMessageRule taPersonMessageRule = iTaPersonMessageRuleService.getByPersonAndType(toUser.getPersonId(), messageType, contentType, toUser.getOrgId());
117
+        if (null == taPersonMessageRule) {
118
+            interval = -1f;
119
+        }
120
+
121
+        // 如果需要判断间隔
122
+        if (interval > 0 && null != taPersonMessageRule.getLastSendTime()) {
123
+            LocalDateTime now = LocalDateTime.now();
124
+            // 间隔时间
125
+            Duration diff = Duration.between(taPersonMessageRule.getLastSendTime(), now);
126
+
127
+            // 统一转换为 秒 比较
128
+            // 如果间隔时间小于设定时间, 就不发送消息
129
+            if ((diff.toMillis() / 1000) < (interval * 60)) {
130
+                log.info("通知消息间隔小于设定值, 不变发送消息");
131
+                return;
132
+            }
133
+        }
134
+
135
+        String[] smsParams = { name, miniapp.getName() + "小程序" };
87 136
         if (null == tpl) {
88
-            log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), CommConstant.MINIAPP_TPL_NOTICE);
137
+            log.error("小程序 {} 不存在 或者 无通知消息模板 {}", toUser.getMiniappId(), contentType);
89 138
 
90 139
             // 短信发送
91
-            sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_NOTICE, smsParams);
92
-        };
93
-
94
-        if (((Boolean) true).equals(tpl.getIsSubscribe())) {
95
-            String[] data = new String[] { name, phone, content, DateUtils.cutSecond(dt) };
96
-            ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
97
-            if (res.getCode() != ResponseBean.CODE_SUCCESS) {
98
-                // 使用短信发送
99
-                sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_NOTICE, smsParams);
100
-            }
140
+            sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), contentType, smsParams);
101 141
         } else {
102
-            List<WxMaTemplateData> data = new ArrayList<>();
103
-            data.add(new WxMaTemplateData("keyword1", name, "#fffff"));
104
-            data.add(new WxMaTemplateData("keyword2", phone, "#fffff"));
105
-            data.add(new WxMaTemplateData("keyword3", content, "#fffff"));
106
-            data.add(new WxMaTemplateData("keyword4", DateUtils.cutSecond(dt), "#fffff"));
107 142
 
108
-            sendMessage(CommConstant.MINIAPP_TPL_NOTICE, toUser, link, data);
109
-            return;
143
+            if (isSubscribe) {
144
+                String[] data = new String[] { name, phone, content, DateUtils.cutSecond(dt) };
145
+                ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
146
+                if (res.getCode() != ResponseBean.CODE_SUCCESS) {
147
+                    // 使用短信发送
148
+                    sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), contentType, smsParams);
149
+                }
150
+            } else {
151
+                List<WxMaTemplateData> data = new ArrayList<>();
152
+                data.add(new WxMaTemplateData("keyword1", name, "#fffff"));
153
+                data.add(new WxMaTemplateData("keyword2", phone, "#fffff"));
154
+                data.add(new WxMaTemplateData("keyword3", content, "#fffff"));
155
+                data.add(new WxMaTemplateData("keyword4", DateUtils.cutSecond(dt), "#fffff"));
156
+
157
+                sendMessage(contentType, toUser, link, data);
158
+            }
110 159
         }
160
+
161
+        // 更新消息发送时间
162
+        iTaPersonMessageRuleService.saveOrUpdateByPersonAndMessage(toUser.getPersonId(), messageType, contentType, toUser.getOrgId());
111 163
     }
112 164
     
113 165
     @Override
@@ -117,31 +169,49 @@ public class MiniAppServiceImpl implements IMiniAppService {
117 169
             log.error("小程序 {} 不存在", toUser.getMiniappId());
118 170
             return ;
119 171
         }
172
+
173
+        // 消息内容类型
174
+        String contentType = CommConstant.MESSAGE_CONTENT_OF_HELP;
175
+
176
+        TaMiniappTemplate tpl = getTplByType(contentType, toUser.getMiniappId());
177
+
178
+        boolean isSubscribe = null != tpl && (null == tpl.getIsSubscribe() ? false : tpl.getIsSubscribe());
179
+
180
+        // 消息发送类型
181
+        String messageType = null == tpl ? CommConstant.MESSAGE_TYPE_OF_SMS :
182
+                isSubscribe ? CommConstant.MESSAGE_TYPE_OF_MINIAPP_SUBSCRIBE : CommConstant.MESSAGE_TYPE_OF_MINIAPP_TEMPLATE;
183
+
120 184
         String[] smsParams = { miniapp.getName() + "小程序-" + activityName };
121 185
 
122
-        TaMiniappTemplate tpl = getTplByType(CommConstant.MINIAPP_TPL_HELP, toUser.getMiniappId());
123 186
         if (null == tpl) {
124
-            log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), CommConstant.MINIAPP_TPL_HELP);
187
+            log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), contentType);
125 188
 
126 189
             // 短信发送
127
-            sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_HELP, smsParams);
128
-        };
129
-
130
-        if (((Boolean) true).equals(tpl.getIsSubscribe())) {
131
-            String[] data = new String[] { activityName, result };
132
-            ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
133
-            if (res.getCode() != ResponseBean.CODE_SUCCESS) {
134
-                // 使用短信发送
135
-                sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_HELP, smsParams);
136
-            }
190
+            sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), contentType, smsParams);
137 191
         } else {
138
-            List<WxMaTemplateData> data = new ArrayList<>();
139
-            data.add(new WxMaTemplateData("keyword1", result, "#fffff"));
140
-            data.add(new WxMaTemplateData("keyword2", activityName, "#fffff"));
141
-            data.add(new WxMaTemplateData("keyword3", DateUtils.cutSecond(dt), "#fffff"));
142
-            sendMessage(CommConstant.MINIAPP_TPL_HELP, toUser, link, data);
143
-            return;
192
+
193
+            if (isSubscribe) {
194
+                String[] data = new String[] { activityName, result };
195
+                ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
196
+                if (res.getCode() != ResponseBean.CODE_SUCCESS) {
197
+                    // 使用短信发送
198
+                    sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), contentType, smsParams);
199
+
200
+                    messageType = CommConstant.MESSAGE_TYPE_OF_SMS;
201
+                }
202
+            } else {
203
+                List<WxMaTemplateData> data = new ArrayList<>();
204
+                data.add(new WxMaTemplateData("keyword1", result, "#fffff"));
205
+                data.add(new WxMaTemplateData("keyword2", activityName, "#fffff"));
206
+                data.add(new WxMaTemplateData("keyword3", DateUtils.cutSecond(dt), "#fffff"));
207
+                sendMessage(contentType, toUser, link, data);
208
+            }
144 209
         }
210
+
211
+        // TODO 写消息发送日志
212
+
213
+        // 更新消息发送时间
214
+        iTaPersonMessageRuleService.saveOrUpdateByPersonAndMessage(toUser.getPersonId(), messageType, contentType, toUser.getOrgId());
145 215
     }
146 216
 
147 217
     @Override
@@ -151,91 +221,49 @@ public class MiniAppServiceImpl implements IMiniAppService {
151 221
             log.error("小程序 {} 不存在", toUser.getMiniappId());
152 222
             return ;
153 223
         }
154
-        String[] smsParams = { miniapp.getName() + "小程序-" + activityName };
155 224
 
156
-        TaMiniappTemplate tpl = getTplByType(CommConstant.MINIAPP_TPL_GROUP, toUser.getMiniappId());
157
-        if (null == tpl) {
158
-            log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), CommConstant.MINIAPP_TPL_GROUP);
225
+        // 消息内容类型
226
+        String contentType = CommConstant.MESSAGE_CONTENT_OF_GROUP;
159 227
 
160
-            // 短信发送
161
-            sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_GROUP, smsParams);
162
-        };
163
-
164
-        if (((Boolean) true).equals(tpl.getIsSubscribe())) {
165
-            String[] data = new String[] { activityName, result };
166
-            ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
167
-            if (res.getCode() != ResponseBean.CODE_SUCCESS) {
168
-                // 使用短信发送
169
-                sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_GROUP, smsParams);
170
-            }
171
-        } else {
172
-            List<WxMaTemplateData> data = new ArrayList<>();
173
-            data.add(new WxMaTemplateData("keyword1", result, "#fffff"));
174
-            data.add(new WxMaTemplateData("keyword2", activityName, "#fffff"));
175
-            data.add(new WxMaTemplateData("keyword3", DateUtils.cutSecond(dt), "#fffff"));
176
-            sendMessage(CommConstant.MINIAPP_TPL_GROUP, toUser, link, data);
177
-            return;
178
-        }
179
-    }
228
+        TaMiniappTemplate tpl = getTplByType(contentType, toUser.getMiniappId());
180 229
 
181
-    @Override
182
-    public void sendMainBizMessage(TaPerson toUser, String link, String custName, String phone, String sex, String fromName, String bizType, String content) {
183
-        TaMiniappTemplate tpl = getTplByType("mainbiz", toUser.getMiniappId());
184
-        if (null == tpl) {
185
-            log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), "mainbiz");
230
+        boolean isSubscribe = null != tpl && (null == tpl.getIsSubscribe() ? false : tpl.getIsSubscribe());
186 231
 
187
-            // TODO
188
-            // 短信发送
189
-        };
190
-
191
-        if (((Boolean) true).equals(tpl.getIsSubscribe())) {
192
-            String[] data = new String[] { custName, phone, sex, fromName, bizType, content };
193
-            ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
194
-            if (res.getCode() != ResponseBean.CODE_SUCCESS) {
195
-                // TODO
196
-                // 使用短信发送
197
-            }
198
-        } else {
199
-            List<WxMaTemplateData> data = new ArrayList<>();
200
-            data.add(new WxMaTemplateData("keyword1", custName, "#fffff"));
201
-            data.add(new WxMaTemplateData("keyword2", StringUtils.ifNull(phone, "未知"), "#fffff"));
202
-            data.add(new WxMaTemplateData("keyword3",StringUtils.ifNull(sex, "未知"), "#fffff"));
203
-            data.add(new WxMaTemplateData("keyword4", fromName, "#fffff"));
204
-            data.add(new WxMaTemplateData("keyword5", bizType, "#fffff"));
205
-            data.add(new WxMaTemplateData("keyword6", content, "#fffff"));
206
-
207
-            sendMessage("mainbiz", toUser, link, data);
208
-            return;
209
-        }
210
-    }
232
+        // 消息发送类型
233
+        String messageType = null == tpl ? CommConstant.MESSAGE_TYPE_OF_SMS :
234
+                isSubscribe ? CommConstant.MESSAGE_TYPE_OF_MINIAPP_SUBSCRIBE : CommConstant.MESSAGE_TYPE_OF_MINIAPP_TEMPLATE;
235
+
236
+        String[] smsParams = { miniapp.getName() + "小程序-" + activityName };
211 237
 
212
-    @Override
213
-    public void sendNewCustomerMessage(TaPerson toUser, String link, String custName, String phone, String sex, String fromName) {
214
-        TaMiniappTemplate tpl = getTplByType("newCustomer", toUser.getMiniappId());
215 238
         if (null == tpl) {
216
-            log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), "newCustomer");
239
+            log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), contentType);
217 240
 
218
-            // TODO
219 241
             // 短信发送
220
-        };
221
-
222
-        if (((Boolean) true).equals(tpl.getIsSubscribe())) {
223
-            String[] data = new String[] { custName, phone, sex, fromName };
224
-            ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
225
-            if (res.getCode() != ResponseBean.CODE_SUCCESS) {
226
-                // TODO
227
-                // 使用短信发送
228
-            }
242
+            sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), contentType, smsParams);
229 243
         } else {
230
-            List<WxMaTemplateData> data = new ArrayList<>();
231
-            data.add(new WxMaTemplateData("keyword1", custName, "#fffff"));
232
-            data.add(new WxMaTemplateData("keyword2", StringUtils.ifNull(phone, "未知"), "#fffff"));
233
-            data.add(new WxMaTemplateData("keyword3",StringUtils.ifNull(sex, "未知"), "#fffff"));
234
-            data.add(new WxMaTemplateData("keyword4", fromName, "#fffff"));
235 244
 
236
-            sendMessage("newCustomer", toUser, link, data);
237
-            return;
245
+            if (isSubscribe) {
246
+                String[] data = new String[] { activityName, result };
247
+                ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
248
+                if (res.getCode() != ResponseBean.CODE_SUCCESS) {
249
+                    // 使用短信发送
250
+                    sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), contentType, smsParams);
251
+
252
+                    messageType = CommConstant.MESSAGE_TYPE_OF_SMS;
253
+                }
254
+            } else {
255
+                List<WxMaTemplateData> data = new ArrayList<>();
256
+                data.add(new WxMaTemplateData("keyword1", result, "#fffff"));
257
+                data.add(new WxMaTemplateData("keyword2", activityName, "#fffff"));
258
+                data.add(new WxMaTemplateData("keyword3", DateUtils.cutSecond(dt), "#fffff"));
259
+                sendMessage(contentType, toUser, link, data);
260
+            }
238 261
         }
262
+
263
+        // TODO 写消息发送日志
264
+
265
+        // 更新消息发送时间
266
+        iTaPersonMessageRuleService.saveOrUpdateByPersonAndMessage(toUser.getPersonId(), messageType, contentType, toUser.getOrgId());
239 267
     }
240 268
 
241 269
     @Override
@@ -358,6 +386,8 @@ public class MiniAppServiceImpl implements IMiniAppService {
358 386
         } catch (WxErrorException e) {
359 387
             e.printStackTrace();
360 388
         }
389
+
390
+        // TODO 写消息发送日志
361 391
     }
362 392
 
363 393
     private ResponseBean sendSubscribeMessage(TaMiniappTemplate tpl, TaPerson toUser, String link, String[] data) {
@@ -388,9 +418,14 @@ public class MiniAppServiceImpl implements IMiniAppService {
388 418
             WxMaService wxService = wxUtils.getMiniApp(appid);
389 419
             wxService.getMsgService().sendSubscribeMsg(message);
390 420
 
421
+            // TODO 写消息发送日志
422
+
391 423
             return ResponseBean.success("消息发送成功");
392 424
         } catch (WxErrorException e) {
393 425
             e.printStackTrace();
426
+
427
+            // TODO 写消息发送日志
428
+
394 429
             return ResponseBean.error(e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
395 430
         }
396 431
     }
@@ -407,10 +442,16 @@ public class MiniAppServiceImpl implements IMiniAppService {
407 442
 
408 443
         SysSmsSetting smsSetting = sysSmsSettingMapper.selectOne(query);
409 444
         if (null == smsSetting) {
445
+
446
+            // TODO 写消息发送日志
447
+
410 448
             return "没有设置有效的【" + type + "】短信模板";
411 449
         }
412 450
 
413 451
         boolean success = smsUtils.sendMessage(smsSetting.getTplCode(), tel, params);
452
+
453
+        // TODO 写消息发送日志
454
+
414 455
         return success ? null : "短信发送失败";
415 456
     }
416 457
 
@@ -420,7 +461,7 @@ public class MiniAppServiceImpl implements IMiniAppService {
420 461
 
421 462
         String[] fileds = fieldsStr.split("\\|");
422 463
 
423
-        int length = fileds.length > list.length ? list.length : fileds.length;
464
+        int length = Math.min(fileds.length, list.length);
424 465
         for (int i = 0; i < length; i++) {
425 466
             data.add(new WxMaSubscribeData(fileds[i], list[i], "#fffff"));
426 467
         }

+ 67
- 0
src/main/java/com/huiju/estateagents/service/impl/TaPersonMessageRuleImpl.java ファイルの表示

@@ -0,0 +1,67 @@
1
+package com.huiju.estateagents.service.impl;
2
+
3
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
4
+import com.huiju.estateagents.entity.TaPersonMessageRule;
5
+import com.huiju.estateagents.mapper.TaPersonMessageRuleMapper;
6
+import com.huiju.estateagents.service.ITaPersonMessageRuleService;
7
+import io.swagger.models.auth.In;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.stereotype.Service;
10
+
11
+import java.time.LocalDateTime;
12
+
13
+/**
14
+ * <p>
15
+ * 楼盘户型对应图片表 服务实现类
16
+ * </p>
17
+ *
18
+ * @author jobob
19
+ * @since 2019-05-10
20
+ */
21
+@Service
22
+public class TaPersonMessageRuleImpl extends ServiceImpl<TaPersonMessageRuleMapper, TaPersonMessageRule> implements ITaPersonMessageRuleService {
23
+    @Autowired
24
+    private TaPersonMessageRuleMapper taPersonMessageRuleMapper;
25
+
26
+    @Override
27
+    public TaPersonMessageRule getByPersonAndType(String personId, String messageType, String contentType, Integer orgId) {
28
+        return taPersonMessageRuleMapper.getByPersonAndType(personId, messageType, contentType, orgId);
29
+    }
30
+
31
+    @Override
32
+    public TaPersonMessageRule saveOrUpdateByPersonAndMessage(String personId, String messageType, String contentType, Integer orgId) {
33
+        boolean success = false;
34
+        LocalDateTime now = LocalDateTime.now();
35
+        TaPersonMessageRule rule = getByPersonAndType(personId, messageType, contentType, orgId);
36
+
37
+        if (null == rule) {
38
+            // 没有信息则插入新记录
39
+            TaPersonMessageRule data = new TaPersonMessageRule();
40
+            data.setPersonId(personId);
41
+            data.setMessageType(messageType);
42
+            data.setContentType(contentType);
43
+            data.setOrgId(orgId);
44
+            data.setLimitNum(-1);   // 暂定 -1 是不限制
45
+            data.setLastSendTime(now);
46
+
47
+            success = save(data);
48
+            return success ? data : null;
49
+        } else {
50
+            // 有的话则更新记录
51
+            rule.setLastSendTime(now);
52
+
53
+            if (rule.getLimitNum() > -1) {
54
+                Integer left = rule.getLeftNum();
55
+                if (null == left || left <= 0) {
56
+                    left = 0;
57
+                } else {
58
+                    left -= 1;
59
+                }
60
+                rule.setLeftNum(left);
61
+            }
62
+
63
+            success = updateById(rule);
64
+            return success ? rule : null;
65
+        }
66
+    }
67
+}

+ 5
- 5
src/main/java/com/huiju/estateagents/service/impl/TaRecommendCustomerServiceImpl.java ファイルの表示

@@ -400,11 +400,11 @@ public class TaRecommendCustomerServiceImpl extends ServiceImpl<TaRecommendCusto
400 400
         }
401 401
 
402 402
         // 找到销售经理,并推送消息
403
-        TaPerson saleMan = taPersonMapper.getSalesExecutive();
404
-        if (null != saleMan && !StringUtils.isEmpty(saleMan.getMiniOpenid())) {
405
-            // 发送消息
406
-            iMiniAppService.sendNewCustomerMessage(saleMan, CommConstant.MINIAPP_INDEX, taRecommendCustomer.getName(), taRecommendCustomer.getPhone(), "", taPerson.getName());
407
-        }
403
+//        TaPerson saleMan = taPersonMapper.getSalesExecutive();
404
+//        if (null != saleMan && !StringUtils.isEmpty(saleMan.getMiniOpenid())) {
405
+//            // 发送消息
406
+//            iMiniAppService.sendNewCustomerMessage(saleMan, CommConstant.MINIAPP_INDEX, taRecommendCustomer.getName(), taRecommendCustomer.getPhone(), "", taPerson.getName());
407
+//        }
408 408
 
409 409
         return ResponseBean.success(taRecommendCustomer);
410 410
     }

+ 15
- 0
src/main/resources/mapper/TaPersonMessageRuleMapper.xml ファイルの表示

@@ -0,0 +1,15 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
+<mapper namespace="com.huiju.estateagents.mapper.TaPersonMessageRuleMapper">
4
+    <select id="getByPersonAndType" resultType="com.huiju.estateagents.entity.TaPersonMessageRule">
5
+        SELECT
6
+            *
7
+        FROM
8
+            ta_person_message_rule t
9
+        WHERE t.person_id = #{personId}
10
+        AND t.message_type = #{messageType}
11
+        AND t.content_type = #{contentType}
12
+        AND t.org_id = #{orgId}
13
+        limit 1
14
+    </select>
15
+</mapper>