Browse Source

消息推送添加返回内容

lighter 10 months ago
parent
commit
2bc86e2a66

+ 2 - 2
src/main/java/thyyxxk/wxservice_server/controller/WxApiController.java

@@ -85,8 +85,8 @@ public class WxApiController {
 
     @PassToken
     @PostMapping("/pushMessage")
-    public void pushMessage(@RequestBody @Validated PushMessageParam param) {
-        pushWxMessageService.pushMessage2(param);
+    public ResultVo<String> pushMessage(@RequestBody @Validated PushMessageParam param) {
+        return ResultVoUtil.success(pushWxMessageService.pushMessage2(param));
     }
 
     @PassToken

+ 3 - 5
src/main/java/thyyxxk/wxservice_server/dao/WxApiDao.java

@@ -121,11 +121,9 @@ public interface WxApiDao {
             "and sgl_check_notified=0 ")
     List<TradeNo> selectTradeNosForScheduleTask();
 
-    @Select("select openid from t_wechat_patient_bind with(nolock) where patient_id=#{cardNo} and del_flag=0")
-    List<String> selectOpenidByPatientId(String cardNo);
-
-    @Select("select openid from t_wechat_patient_bind with(nolock) where ic_card_no=#{cardNo} and del_flag=0")
-    List<String> selectOpenidByIcCardNo(String cardNo);
+    @Select("select openid from t_wechat_patient_bind with(nolock) " +
+            "where (patient_id=#{cardNo} or ic_card_no=#{cardNo}) and del_flag=0")
+    List<String> selectOpenidByCardNo(String cardNo);
 
     @Select("select rtrim(a.code) doctorCode,rtrim(a.name) doctorName,rtrim(b.name) doctorTitle,rtrim(a.dept_code) deptCode, " +
             "deptName=(select rtrim(name) from zd_unit_code with(nolock) where code=a.dept_code), " +

+ 1 - 2
src/main/java/thyyxxk/wxservice_server/entity/wxapi/PushMessageParam.java

@@ -11,9 +11,8 @@ import javax.validation.constraints.NotNull;
  */
 @Data
 public class PushMessageParam {
-    @NotBlank(message = "诊疗卡号不能为空。")
+    @NotBlank(message = "诊疗卡号/门诊号不能为空。")
     private String cardNo;
     @NotNull(message = "消息内容不能为空。")
     private JSONObject msgContext;
-    private boolean isCardNoPatientId = false;
 }

+ 13 - 9
src/main/java/thyyxxk/wxservice_server/service/PushWxMessageService.java

@@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
+import thyyxxk.wxservice_server.config.exception.BizException;
 import thyyxxk.wxservice_server.dao.WxApiDao;
 import thyyxxk.wxservice_server.entity.wxapi.PushMessageParam;
 import thyyxxk.wxservice_server.utils.PropertiesUtil;
@@ -27,25 +28,28 @@ public class PushWxMessageService {
         this.dao = dao;
     }
 
-    public void pushMessage2(PushMessageParam param) {
+    public String pushMessage2(PushMessageParam param) {
         if (StringUtil.isBlank(param.getCardNo())) {
-            return;
+            throw new BizException("患者就诊卡号不能为空");
         }
-        List<String> openidList = param.isCardNoPatientId() ?
-                dao.selectOpenidByPatientId(param.getCardNo()) : dao.selectOpenidByIcCardNo(param.getCardNo());
+        List<String> openidList = dao.selectOpenidByCardNo(param.getCardNo());
         if (null == openidList || openidList.isEmpty()) {
-            log.info("未找到卡号【{}】的openid。", param.getCardNo());
-            return;
+            throw new BizException("未找到卡号【" + param.getCardNo() + "】的openid");
         }
         RestTemplate template = new RestTemplate();
         JSONObject content = param.getMsgContext();
         String wxUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" +
                 PropertiesUtil.getLocalProperty("access_token");
+        String res = null;
         for (String openid : openidList) {
             content.replace("touser", openid);
-            String res = template.postForObject(wxUrl, content, String.class);
-            log.info("推送消息:内容:{},结果:{}", content, res);
+            res = template.postForObject(wxUrl, content, String.class);
+            log.info("推送消息1:内容:{},结果:{}", content, res);
         }
+        if (null == res) {
+            throw new BizException("推送失败,网络异常");
+        }
+        return res;
     }
 
     public String pushMessage2(JSONObject msgContent) {
@@ -53,7 +57,7 @@ public class PushWxMessageService {
         String wxUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" +
                 PropertiesUtil.getLocalProperty("access_token");
         String res = template.postForObject(wxUrl, msgContent, String.class);
-        log.info("推送消息:内容:{},结果:{}", msgContent, res);
+        log.info("推送消息2:内容:{},结果:{}", msgContent, res);
         if (StringUtil.isBlank(res)) {
             return "推送失败,未知的异常。";
         }