From aeea62d121a1a8fd4475da30f1ef8b44b4776c7b Mon Sep 17 00:00:00 2001
From: wl <173@qq.com>
Date: 星期三, 04 一月 2023 14:02:47 +0800
Subject: [PATCH] fix: 店铺消息发送

---
 ycl-platform/src/main/java/com/ycl/vo/message/MessageVO.java                    |   12 +++-
 ycl-platform/src/main/java/com/ycl/service/message/IMessageService.java         |    2 
 ycl-platform/src/main/resources/mapper/message/MessageMapper.xml                |    6 +
 ycl-platform/src/main/java/com/ycl/entity/message/Message.java                  |    3 +
 ycl-platform/src/main/java/com/ycl/dto/message/MessageParam.java                |    1 
 ycl-platform/src/main/resources/mapper/trend/TrendAnalysisMapper.xml            |    1 
 ycl-platform/src/main/java/com/ycl/controller/message/MessageController.java    |   11 +++
 ycl-platform/src/main/java/com/ycl/service/message/impl/MessageServiceImpl.java |   94 +++++++++++++++++++++++++-----
 8 files changed, 107 insertions(+), 23 deletions(-)

diff --git a/ycl-platform/src/main/java/com/ycl/controller/message/MessageController.java b/ycl-platform/src/main/java/com/ycl/controller/message/MessageController.java
index 2489f87..cfc396a 100644
--- a/ycl-platform/src/main/java/com/ycl/controller/message/MessageController.java
+++ b/ycl-platform/src/main/java/com/ycl/controller/message/MessageController.java
@@ -46,6 +46,17 @@
         return CommonResult.success(iMessageService.sendMessage(message));
     }
 
+
+    @ApiOperation(value = "鍙戦�佹秷鎭�氳繃鎵嬫満鍙风爜")
+    @RequestMapping(value = "/send_message_phone", method = RequestMethod.POST)
+    public CommonResult sendMessageByPhoneNumber( @RequestBody Message message) {
+        AdminUserDetails user = (AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+        //璁剧疆鍒涘缓浜�
+        message.setCreateUser(user.getUserId());
+        return CommonResult.success(iMessageService.sendMessageByPhone(message));
+    }
+
+
     @ApiOperation(value = "鏆傚瓨娑堟伅")
     @PostMapping("/saveMessage")
     @ResponseBody
diff --git a/ycl-platform/src/main/java/com/ycl/dto/message/MessageParam.java b/ycl-platform/src/main/java/com/ycl/dto/message/MessageParam.java
index e8a0ee4..fa5e76c 100644
--- a/ycl-platform/src/main/java/com/ycl/dto/message/MessageParam.java
+++ b/ycl-platform/src/main/java/com/ycl/dto/message/MessageParam.java
@@ -53,4 +53,5 @@
 
     @ApiModelProperty(value = "鍝嶅簲缁撴灉")
     private String respondResult;
+
 }
\ No newline at end of file
diff --git a/ycl-platform/src/main/java/com/ycl/entity/message/Message.java b/ycl-platform/src/main/java/com/ycl/entity/message/Message.java
index fcdf270..78b77ab 100644
--- a/ycl-platform/src/main/java/com/ycl/entity/message/Message.java
+++ b/ycl-platform/src/main/java/com/ycl/entity/message/Message.java
@@ -169,4 +169,7 @@
 
     @ApiModelProperty(value = "鍝嶅簲缁撴灉")
     private String respondResult;
+
+    @ApiModelProperty(value = "鐢佃瘽鍙风爜")
+    private String phoneNumber;
 }
diff --git a/ycl-platform/src/main/java/com/ycl/service/message/IMessageService.java b/ycl-platform/src/main/java/com/ycl/service/message/IMessageService.java
index 571a72e..0505805 100644
--- a/ycl-platform/src/main/java/com/ycl/service/message/IMessageService.java
+++ b/ycl-platform/src/main/java/com/ycl/service/message/IMessageService.java
@@ -23,4 +23,6 @@
     Page<MessageVO> list(MessageParam messageParam);
 
     Boolean deleteMessages(List<Long> ids);
+
+    String sendMessageByPhone(Message message);
 }
diff --git a/ycl-platform/src/main/java/com/ycl/service/message/impl/MessageServiceImpl.java b/ycl-platform/src/main/java/com/ycl/service/message/impl/MessageServiceImpl.java
index 04e0ea9..e0c1a53 100644
--- a/ycl-platform/src/main/java/com/ycl/service/message/impl/MessageServiceImpl.java
+++ b/ycl-platform/src/main/java/com/ycl/service/message/impl/MessageServiceImpl.java
@@ -82,26 +82,29 @@
     @Transactional(rollbackFor = Exception.class)
     public String sendMessage(Message message) {
 
-        Message savedMessage = messageMapper.selectOne(new LambdaQueryWrapper<Message>()
-                .eq(Message::getTargetTo, message.getTargetTo())
-                .eq(Message::getTargetFrom, message.getTargetFrom())
-                .eq(Message::getBody, message.getBody())
-                .eq(Message::getHead, message.getHead()));
-        if (savedMessage != null) {
-            messageMapper.deleteById(savedMessage);
+        if (!StringUtils.equals(message.getChannelCode(),ChannelCode.SMS)){
+            Message savedMessage = messageMapper.selectOne(new LambdaQueryWrapper<Message>()
+                    .eq(Message::getTargetTo, message.getTargetTo())
+                    .eq(Message::getTargetFrom, message.getTargetFrom())
+                    .eq(Message::getBody, message.getBody())
+                    .eq(Message::getHead, message.getHead()));
+            if (savedMessage != null) {
+                messageMapper.deleteById(savedMessage);
+            }
         }
-
         ArrayList<Long> ids = new ArrayList<>();
         message.setTargetFrom(umsAdminMapper.selectById(message.getCreateUser()).getUsername());
-        for (String receive : message.getTargetTo().split(",")) {
-            Message sendMessage = new Message();
-            BeanUtils.copyProperties(message, sendMessage);
-            sendMessage.setCreateTime(new Date());
-            sendMessage.setSendTime(new Date());
-            sendMessage.setTargetTo(receive);
-            sendMessage.setSendTime(new Date());
-            this.save(sendMessage);
-            ids.add(sendMessage.getId());
+        if (message.getId()==null) {
+            for (String receive : message.getTargetTo().split(",")) {
+                Message sendMessage = new Message();
+                BeanUtils.copyProperties(message, sendMessage);
+                sendMessage.setCreateTime(new Date());
+                sendMessage.setSendTime(new Date());
+                sendMessage.setTargetTo(receive);
+                sendMessage.setSendTime(new Date());
+                this.save(sendMessage);
+                ids.add(sendMessage.getId());
+            }
         }
         switch (message.getChannelCode()) {
             //閭欢鍙戦��
@@ -176,6 +179,63 @@
         }
     }
 
+    @Override
+    @SneakyThrows
+    public String sendMessageByPhone(Message message) {
+        Message sendMessage = new Message();
+        BeanUtils.copyProperties(message, sendMessage);
+        sendMessage.setCreateTime(new Date());
+        sendMessage.setSendTime(new Date());
+        sendMessage.setSendTime(new Date());
+        this.save(sendMessage);
+
+        String context = "銆�" + message.getHead() + "銆�" + message.getBody();
+        String addSerial = "";
+        SendReq sendReq = new SendReq();
+        sendReq.setEcName(ecName);
+        sendReq.setApId(apId);
+        sendReq.setSign(sign);
+        sendReq.setContext(context);
+        sendReq.setMobiles(message.getPhoneNumber());
+        sendReq.setAddSerial(addSerial);
+
+        StringBuffer mac = new StringBuffer();
+        mac.append(ecName);
+        mac.append(apId);
+        mac.append(sendReq.getMobiles());
+        mac.append(context);
+        mac.append(sign);
+        mac.append(addSerial);
+        sendReq.setMac(MD5Util.md5Encrypt32Lower(mac.toString()));
+
+        HttpHeaders httpHeaders = new HttpHeaders();
+        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
+
+        ObjectMapper objectMapper = new ObjectMapper();
+
+        HttpEntity<String> stringHttpEntity = new HttpEntity<>(Base64Utils.encodeToString(objectMapper.writeValueAsBytes(mac)), httpHeaders);
+
+        SendRes sendRes = objectMapper.readValue(restTemplate.postForEntity(url, stringHttpEntity, String.class).getBody(), SendRes.class);
+        Message sendedMessage = new Message();
+        if (sendRes.getRspcod().equals("success")) {
+            sendedMessage.setStatus(2);
+            sendedMessage.setRespondResult("鍙戦�佹垚鍔�");
+            sendedMessage.setId(sendMessage.getId());
+            sendedMessage.setUpdateTime(new Date());
+            sendedMessage.setSendTime(new Date());
+            messageMapper.updateById(sendedMessage);
+            return "send message success!";
+        } else {
+            sendedMessage.setStatus(3);
+            sendedMessage.setRespondResult("鍙戦�佸け璐�");
+            sendedMessage.setSendTime(new Date());
+            sendedMessage.setId(sendedMessage.getId());
+            sendedMessage.setUpdateTime(new Date());
+            messageMapper.updateById(sendedMessage);
+            return "send message fail!";
+        }
+    }
+
     //鍙戦�侀偖浠�
     @SneakyThrows
     private void sendMail(Message message) {
diff --git a/ycl-platform/src/main/java/com/ycl/vo/message/MessageVO.java b/ycl-platform/src/main/java/com/ycl/vo/message/MessageVO.java
index be9d22f..a86f6cf 100644
--- a/ycl-platform/src/main/java/com/ycl/vo/message/MessageVO.java
+++ b/ycl-platform/src/main/java/com/ycl/vo/message/MessageVO.java
@@ -64,6 +64,9 @@
     @ApiModelProperty(value = "鍙戦�佽��")
     private String targetFrom;
 
+    @ApiModelProperty(value = "鎺ユ敹鑰�")
+    private Integer targetTo;
+
     /**
      * 鍙戦�佹椂闂�
      */
@@ -76,9 +79,6 @@
     @ApiModelProperty(value = "鐘舵��0-鏈彂甯�1-宸插彂甯�")
     private Integer status;
 
-    @ApiModelProperty(value = "鍒涘缓浜�")
-    private String createUser;
-
     /**
      * 鍒涘缓鏃堕棿
      */
@@ -88,4 +88,10 @@
 
     @ApiModelProperty(value = "鍝嶅簲缁撴灉")
     private String respondResult;
+
+    @ApiModelProperty(value = "浠g爜")
+    private String channelCode;
+
+    @ApiModelProperty(value = "娑堟伅绫诲瀷")
+    private Integer messageType;
 }
diff --git a/ycl-platform/src/main/resources/mapper/message/MessageMapper.xml b/ycl-platform/src/main/resources/mapper/message/MessageMapper.xml
index f1b0a73..be8b16f 100644
--- a/ycl-platform/src/main/resources/mapper/message/MessageMapper.xml
+++ b/ycl-platform/src/main/resources/mapper/message/MessageMapper.xml
@@ -37,7 +37,7 @@
             resultType="com.ycl.vo.message.MessageVO">
         SELECT
         t1.id,
-        t2.username as targetFrom,
+        t1.target_from,
         t3.column_name as messageTypeName,
         t1.`status`,
         t1.body,
@@ -45,10 +45,12 @@
         t1.create_time,
         t1.respond_result,
         t1.head,
+        t1.target_to,
+        t1.message_type,
+        t1.channel_code,
         t4.username as createUser
         FROM
         `ums_message` t1
-        LEFT JOIN ums_admin t2 ON t1.target_from = t2.id
         left join ums_admin t4 on t1.create_user = t4.id
         LEFT JOIN ums_message_column t3 ON t3.id = t1.message_type
         <where>
diff --git a/ycl-platform/src/main/resources/mapper/trend/TrendAnalysisMapper.xml b/ycl-platform/src/main/resources/mapper/trend/TrendAnalysisMapper.xml
index 8a74e7e..db8e345 100644
--- a/ycl-platform/src/main/resources/mapper/trend/TrendAnalysisMapper.xml
+++ b/ycl-platform/src/main/resources/mapper/trend/TrendAnalysisMapper.xml
@@ -21,7 +21,6 @@
         </where>
         GROUP BY
         t1.video_point_id
-        ORDER BY
         count DESC
     </select>
 

--
Gitblit v1.8.0