From 9931d6f56816aecb09333cef2d12777c08793547 Mon Sep 17 00:00:00 2001
From: xin <1099200748@qq.com>
Date: Wed, 04 Jun 2025 21:41:48 +0800
Subject: [PATCH] 系统:订阅消息列表

---
 oying-system/src/main/java/com/oying/modules/system/service/impl/UserServiceImpl.java          |    5 
 oying-system/src/main/java/com/oying/modules/system/domain/UserSubscribe.java                  |   57 ++++++++
 oying-system/src/main/java/com/oying/modules/system/domain/dto/UserSubscribeQueryCriteria.java |   30 ++++
 oying-system/src/main/java/com/oying/modules/system/service/UserService.java                   |    2 
 oying-system/src/main/resources/mapper/system/UserSubscribeMapper.xml                          |   40 +++++
 oying-system/src/main/java/com/oying/modules/system/mapper/UserSubscribeMapper.java            |   22 +++
 oying-system/src/main/java/com/oying/modules/system/mapper/UserMapper.java                     |    2 
 oying-system/src/main/java/com/oying/modules/system/rest/UserSubscribeController.java          |   54 +++++++
 oying-system/src/main/java/com/oying/modules/system/service/UserSubscribeService.java          |   53 +++++++
 oying-system/src/main/java/com/oying/modules/system/service/impl/UserSubscribeServiceImpl.java |   74 ++++++++++
 oying-system/src/main/java/com/oying/modules/security/service/WeiXinService.java               |   36 +++-
 oying-system/src/main/resources/mapper/system/UserMapper.xml                                   |    9 +
 12 files changed, 374 insertions(+), 10 deletions(-)

diff --git a/oying-system/src/main/java/com/oying/modules/security/service/WeiXinService.java b/oying-system/src/main/java/com/oying/modules/security/service/WeiXinService.java
index 2826e98..be3fee0 100644
--- a/oying-system/src/main/java/com/oying/modules/security/service/WeiXinService.java
+++ b/oying-system/src/main/java/com/oying/modules/security/service/WeiXinService.java
@@ -2,6 +2,10 @@
 
 import com.alibaba.fastjson2.JSONObject;
 import com.oying.modules.security.config.WeiXinProperties;
+import com.oying.modules.system.domain.User;
+import com.oying.modules.system.domain.UserSubscribe;
+import com.oying.modules.system.service.UserService;
+import com.oying.modules.system.service.UserSubscribeService;
 import com.oying.utils.HttpRequest;
 import com.oying.utils.RedisUtils;
 import lombok.extern.slf4j.Slf4j;
@@ -24,6 +28,10 @@
     private RedisUtils redisUtils;
     @Value("${wx.enabled}")
     private Boolean wxEnabled;
+    @Resource
+    private UserSubscribeService subscribeService;
+    @Resource
+    private UserService userService;
 
     /**
      * POST 获取稳定版接口调用凭据 获取小程序全局唯一后台接口调用凭据,token有效期为7200s,开发者需要进行妥善保存。
@@ -81,22 +89,30 @@
     /**
      * POST 该接口用于发送订阅消息。
      *
-     * @param data 请求参数
-     * @param openId 用户openId
+     * @param data       请求参数
+     * @param openid     用户openId
      * @param templateId 订阅模板id
-     * @param page 小程序跳转链接
-     * @return JSONObject
+     * @param page       小程序跳转链接
      */
-    public JSONObject sendMessage(Map<String, Object> data, String openId, String templateId, String page) {
+    public void sendMessage(Map<String, Object> data, String openid, String templateId, String page) {
+        JSONObject jsonObject = new JSONObject();
         if (wxEnabled) {
             String url = weiXinProperties.getSendMessage();
             url = url.replace("{accessToken}", getStableAccessToken());
-            Map<String, Object> map = getSendMessageDto(data, openId, templateId, page);
-            return HttpRequest.exchangeJsonObject(HttpMethod.POST, url, map);
+            Map<String, Object> map = getSendMessageDto(data, openid, templateId, page);
+            jsonObject = HttpRequest.exchangeJsonObject(HttpMethod.POST, url, map);
+        } else {
+            jsonObject.put("message", "测试环境");
         }
-        JSONObject jsonObject = new JSONObject();
-        jsonObject.put("message", "测试环境");
-        return jsonObject;
+        User user = userService.findByOpenid(openid);
+        UserSubscribe sub = new UserSubscribe();
+        sub.setSubType(templateId);
+        sub.setUserId(user.getId());
+        sub.setOpenid(openid);
+        sub.setUsername(user.getUsername());
+        sub.setSendMessage(JSONObject.toJSONString(data));
+        sub.setSubMessage(jsonObject.toJSONString());
+        subscribeService.save(sub);
     }
 
     private Map<String, Object> getSendMessageDto(Map<String, Object> data, String openId, String templateId, String page) {
diff --git a/oying-system/src/main/java/com/oying/modules/system/domain/UserSubscribe.java b/oying-system/src/main/java/com/oying/modules/system/domain/UserSubscribe.java
new file mode 100644
index 0000000..8ebe509
--- /dev/null
+++ b/oying-system/src/main/java/com/oying/modules/system/domain/UserSubscribe.java
@@ -0,0 +1,57 @@
+package com.oying.modules.system.domain;
+
+import com.oying.base.BaseEntity;
+import cn.hutool.core.bean.BeanUtil;
+import io.swagger.annotations.ApiModelProperty;
+import cn.hutool.core.bean.copier.CopyOptions;
+import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
+import java.io.Serializable;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+* @description /
+* @author lixin
+* @date 2025-06-04
+**/
+@Getter
+@Setter
+@TableName("sys_user_subscribe")
+public class UserSubscribe extends BaseEntity implements Serializable {
+
+    @TableId(value = "sub_id", type = IdType.AUTO)
+    @ApiModelProperty(value = "主键")
+    private Long subId;
+
+    @NotBlank
+    @ApiModelProperty(value = "消息类型")
+    private String subType;
+
+    @NotNull
+    @ApiModelProperty(value = "用户ID")
+    private Long userId;
+
+    @NotBlank
+    @ApiModelProperty(value = "openid")
+    private String openid;
+
+    @NotBlank
+    @ApiModelProperty(value = "账号")
+    private String username;
+
+    @NotBlank
+    @ApiModelProperty(value = "发送消息")
+    private String sendMessage;
+
+    @NotBlank
+    @ApiModelProperty(value = "返回消息")
+    private String subMessage;
+
+    public void copy(UserSubscribe source){
+        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
+    }
+}
diff --git a/oying-system/src/main/java/com/oying/modules/system/domain/dto/UserSubscribeQueryCriteria.java b/oying-system/src/main/java/com/oying/modules/system/domain/dto/UserSubscribeQueryCriteria.java
new file mode 100644
index 0000000..b86a481
--- /dev/null
+++ b/oying-system/src/main/java/com/oying/modules/system/domain/dto/UserSubscribeQueryCriteria.java
@@ -0,0 +1,30 @@
+package com.oying.modules.system.domain.dto;
+
+import lombok.Data;
+import java.sql.Timestamp;
+import java.util.List;
+import io.swagger.annotations.ApiModelProperty;
+
+/**
+* @author lixin
+* @date 2025-06-04
+**/
+@Data
+public class UserSubscribeQueryCriteria{
+
+    @ApiModelProperty(value = "页码", example = "1")
+    private Integer page = 1;
+
+    @ApiModelProperty(value = "每页数据量", example = "10")
+    private Integer size = 10;
+
+    @ApiModelProperty(value = "消息类型")
+    private String subType;
+
+    @ApiModelProperty(value = "openid")
+    private String openid;
+
+    @ApiModelProperty(value = "手机号")
+    private String phone;
+    private List<Timestamp> createTime;
+}
diff --git a/oying-system/src/main/java/com/oying/modules/system/mapper/UserMapper.java b/oying-system/src/main/java/com/oying/modules/system/mapper/UserMapper.java
index d506a8c..4598cee 100644
--- a/oying-system/src/main/java/com/oying/modules/system/mapper/UserMapper.java
+++ b/oying-system/src/main/java/com/oying/modules/system/mapper/UserMapper.java
@@ -27,6 +27,8 @@
 
     User findByUsername(@Param("username") String username);
 
+    User findByOpenid(@Param("openid") String openid);
+
     User findByEmail(@Param("email") String email);
 
     User findByPhone(@Param("phone") String phone);
diff --git a/oying-system/src/main/java/com/oying/modules/system/mapper/UserSubscribeMapper.java b/oying-system/src/main/java/com/oying/modules/system/mapper/UserSubscribeMapper.java
new file mode 100644
index 0000000..fee44b5
--- /dev/null
+++ b/oying-system/src/main/java/com/oying/modules/system/mapper/UserSubscribeMapper.java
@@ -0,0 +1,22 @@
+package com.oying.modules.system.mapper;
+
+import com.oying.modules.system.domain.UserSubscribe;
+import com.oying.modules.system.domain.dto.UserSubscribeQueryCriteria;
+import java.util.List;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Mapper;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+
+/**
+* @author lixin
+* @date 2025-06-04
+**/
+@Mapper
+public interface UserSubscribeMapper extends BaseMapper<UserSubscribe> {
+
+    IPage<UserSubscribe> findAll(@Param("criteria") UserSubscribeQueryCriteria criteria, Page<Object> page);
+
+    List<UserSubscribe> findAll(@Param("criteria") UserSubscribeQueryCriteria criteria);
+}
diff --git a/oying-system/src/main/java/com/oying/modules/system/rest/UserSubscribeController.java b/oying-system/src/main/java/com/oying/modules/system/rest/UserSubscribeController.java
new file mode 100644
index 0000000..8c3aa2e
--- /dev/null
+++ b/oying-system/src/main/java/com/oying/modules/system/rest/UserSubscribeController.java
@@ -0,0 +1,54 @@
+package com.oying.modules.system.rest;
+
+import com.oying.annotation.Log;
+import com.oying.modules.system.domain.UserSubscribe;
+import com.oying.modules.system.service.UserSubscribeService;
+import com.oying.modules.system.domain.dto.UserSubscribeQueryCriteria;
+import lombok.RequiredArgsConstructor;
+import java.util.List;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+import io.swagger.annotations.*;
+import java.io.IOException;
+import javax.servlet.http.HttpServletResponse;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.oying.utils.PageResult;
+
+/**
+* @author lixin
+* @date 2025-06-04
+**/
+@RestController
+@RequiredArgsConstructor
+@Api(tags = "系统:订阅消息列表")
+@RequestMapping("/api/userSubscribe")
+public class UserSubscribeController {
+
+    private final UserSubscribeService userSubscribeService;
+
+    @ApiOperation("导出数据")
+    @GetMapping(value = "/download")
+    @PreAuthorize("@el.check('userSubscribe:list')")
+    public void exportUserSubscribe(HttpServletResponse response, UserSubscribeQueryCriteria criteria) throws IOException {
+        userSubscribeService.download(userSubscribeService.queryAll(criteria), response);
+    }
+
+    @GetMapping
+    @ApiOperation("查询订阅消息")
+    @PreAuthorize("@el.check('userSubscribe:list')")
+    public ResponseEntity<PageResult<UserSubscribe>> queryUserSubscribe(UserSubscribeQueryCriteria criteria){
+        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
+        return new ResponseEntity<>(userSubscribeService.queryAll(criteria,page),HttpStatus.OK);
+    }
+
+    @DeleteMapping
+    @Log("删除订阅消息")
+    @ApiOperation("删除订阅消息")
+    @PreAuthorize("@el.check('userSubscribe:del')")
+    public ResponseEntity<Object> deleteUserSubscribe(@ApiParam(value = "传ID数组[]") @RequestBody List<Long> ids) {
+        userSubscribeService.deleteAll(ids);
+        return new ResponseEntity<>(HttpStatus.OK);
+    }
+}
diff --git a/oying-system/src/main/java/com/oying/modules/system/service/UserService.java b/oying-system/src/main/java/com/oying/modules/system/service/UserService.java
index cfdeb44..20807ba 100644
--- a/oying-system/src/main/java/com/oying/modules/system/service/UserService.java
+++ b/oying-system/src/main/java/com/oying/modules/system/service/UserService.java
@@ -53,6 +53,8 @@
      */
     User findByName(String userName);
 
+    User findByOpenid(String openId);
+
     /**
      * 根据用户名查询
      * @param userName /
diff --git a/oying-system/src/main/java/com/oying/modules/system/service/UserSubscribeService.java b/oying-system/src/main/java/com/oying/modules/system/service/UserSubscribeService.java
new file mode 100644
index 0000000..cf02062
--- /dev/null
+++ b/oying-system/src/main/java/com/oying/modules/system/service/UserSubscribeService.java
@@ -0,0 +1,53 @@
+package com.oying.modules.system.service;
+
+import com.oying.modules.system.domain.UserSubscribe;
+import com.oying.modules.system.domain.dto.UserSubscribeQueryCriteria;
+import java.util.List;
+import java.io.IOException;
+import javax.servlet.http.HttpServletResponse;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.oying.utils.PageResult;
+
+/**
+* @description 服务接口
+* @author lixin
+* @date 2025-06-04
+**/
+public interface UserSubscribeService extends IService<UserSubscribe> {
+
+    /**
+    * 查询数据分页
+    * @param criteria 条件
+    * @param page 分页参数
+    * @return PageResult
+    */
+    PageResult<UserSubscribe> queryAll(UserSubscribeQueryCriteria criteria, Page<Object> page);
+
+    /**
+    * 查询所有数据不分页
+    * @param criteria 条件参数
+    * @return List<UserSubscribeDto>
+    */
+    List<UserSubscribe> queryAll(UserSubscribeQueryCriteria criteria);
+
+    /**
+    * 创建
+    * @param resources /
+    */
+    void create(UserSubscribe resources);
+
+    /**
+    * 多选删除
+    * @param ids /
+    */
+    void deleteAll(List<Long> ids);
+
+    /**
+    * 导出数据
+    * @param all 待导出的数据
+    * @param response /
+    * @throws IOException /
+    */
+    void download(List<UserSubscribe> all, HttpServletResponse response) throws IOException;
+}
diff --git a/oying-system/src/main/java/com/oying/modules/system/service/impl/UserServiceImpl.java b/oying-system/src/main/java/com/oying/modules/system/service/impl/UserServiceImpl.java
index 2a8fe44..0790373 100644
--- a/oying-system/src/main/java/com/oying/modules/system/service/impl/UserServiceImpl.java
+++ b/oying-system/src/main/java/com/oying/modules/system/service/impl/UserServiceImpl.java
@@ -178,6 +178,11 @@
     }
 
     @Override
+    public User findByOpenid(String openid) {
+        return userMapper.findByOpenid(openid);
+    }
+
+    @Override
     public User getLoginData(String userName) {
         return userMapper.findByUsername(userName);
     }
diff --git a/oying-system/src/main/java/com/oying/modules/system/service/impl/UserSubscribeServiceImpl.java b/oying-system/src/main/java/com/oying/modules/system/service/impl/UserSubscribeServiceImpl.java
new file mode 100644
index 0000000..90a4809
--- /dev/null
+++ b/oying-system/src/main/java/com/oying/modules/system/service/impl/UserSubscribeServiceImpl.java
@@ -0,0 +1,74 @@
+package com.oying.modules.system.service.impl;
+
+import com.oying.modules.system.domain.UserSubscribe;
+import com.oying.utils.FileUtil;
+import lombok.RequiredArgsConstructor;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.oying.modules.system.service.UserSubscribeService;
+import com.oying.modules.system.domain.dto.UserSubscribeQueryCriteria;
+import com.oying.modules.system.mapper.UserSubscribeMapper;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import com.oying.utils.PageUtil;
+import java.util.List;
+import java.util.Map;
+import java.io.IOException;
+import javax.servlet.http.HttpServletResponse;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import com.oying.utils.PageResult;
+
+/**
+* @description 服务实现
+* @author lixin
+* @date 2025-06-04
+**/
+@Service
+@RequiredArgsConstructor
+public class UserSubscribeServiceImpl extends ServiceImpl<UserSubscribeMapper, UserSubscribe> implements UserSubscribeService {
+
+    private final UserSubscribeMapper userSubscribeMapper;
+
+    @Override
+    public PageResult<UserSubscribe> queryAll(UserSubscribeQueryCriteria criteria, Page<Object> page){
+        return PageUtil.toPage(userSubscribeMapper.findAll(criteria, page));
+    }
+
+    @Override
+    public List<UserSubscribe> queryAll(UserSubscribeQueryCriteria criteria){
+        return userSubscribeMapper.findAll(criteria);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void create(UserSubscribe resources) {
+        userSubscribeMapper.insert(resources);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void deleteAll(List<Long> ids) {
+        userSubscribeMapper.deleteBatchIds(ids);
+    }
+
+    @Override
+    public void download(List<UserSubscribe> all, HttpServletResponse response) throws IOException {
+        List<Map<String, Object>> list = new ArrayList<>();
+        for (UserSubscribe userSubscribe : all) {
+            Map<String, Object> map = new LinkedHashMap<>();
+            map.put("消息类型", userSubscribe.getSubType());
+            map.put("用户ID", userSubscribe.getUserId());
+            map.put("openid", userSubscribe.getOpenid());
+            map.put("手机号", userSubscribe.getUsername());
+            map.put("发送消息", userSubscribe.getSendMessage());
+            map.put("返回消息", userSubscribe.getSubMessage());
+            map.put("创建者", userSubscribe.getCreateBy());
+            map.put("创建时间", userSubscribe.getCreateTime());
+            map.put("修改者", userSubscribe.getUpdateBy());
+            map.put("修改时间", userSubscribe.getUpdateTime());
+            list.add(map);
+        }
+        FileUtil.downloadExcel(list, response);
+    }
+}
diff --git a/oying-system/src/main/resources/mapper/system/UserMapper.xml b/oying-system/src/main/resources/mapper/system/UserMapper.xml
index a71e0a3..93bd442 100644
--- a/oying-system/src/main/resources/mapper/system/UserMapper.xml
+++ b/oying-system/src/main/resources/mapper/system/UserMapper.xml
@@ -116,6 +116,15 @@
         where u.username = #{username}
     </select>
 
+    <select id="findByOpenid" resultMap="BaseResultMap">
+        select
+        u.password user_password, u.is_admin user_is_admin,
+        <include refid="Base_Column_List"/>
+        from sys_user u
+        left join sys_dept d on u.dept_id = d.dept_id
+        where u.openid = #{openid}
+    </select>
+
     <select id="findByEmail" resultType="com.oying.modules.system.domain.User">
         select user_id as id, username from sys_user
         where email = #{email}
diff --git a/oying-system/src/main/resources/mapper/system/UserSubscribeMapper.xml b/oying-system/src/main/resources/mapper/system/UserSubscribeMapper.xml
new file mode 100644
index 0000000..04e95e2
--- /dev/null
+++ b/oying-system/src/main/resources/mapper/system/UserSubscribeMapper.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.oying.modules.system.mapper.UserSubscribeMapper">
+    <resultMap id="BaseResultMap" type="com.oying.modules.system.domain.UserSubscribe">
+        <id column="sub_id" property="subId"/>
+        <result column="sub_type" property="subType"/>
+        <result column="user_id" property="userId"/>
+        <result column="openid" property="openid"/>
+        <result column="username" property="username"/>
+        <result column="send_message" property="sendMessage"/>
+        <result column="sub_message" property="subMessage"/>
+        <result column="create_by" property="createBy"/>
+        <result column="create_time" property="createTime"/>
+        <result column="update_by" property="updateBy"/>
+        <result column="update_time" property="updateTime"/>
+    </resultMap>
+
+    <sql id="Base_Column_List">
+        sub_id, sub_type, user_id, openid, username, send_message, sub_message, create_by, create_time, update_by, update_time
+    </sql>
+
+    <select id="findAll" resultMap="BaseResultMap">
+        select
+        <include refid="Base_Column_List"/>
+        from sys_user_subscribe
+        <where>
+            <if test="criteria.subType != null">
+                and sub_type = #{criteria.subType}
+            </if>
+            <if test="criteria.blurry != null and criteria.blurry != ''">
+                and (openid like concat('%',#{criteria.openid},'%')
+                or username like concat('%',#{criteria.username},'%'))
+            </if>
+            <if test="criteria.createTime != null and criteria.createTime.size() > 0">
+                AND create_time BETWEEN #{criteria.createTime[0]} AND #{criteria.createTime[1]}
+            </if>
+        </where>
+        order by sub_id desc
+    </select>
+</mapper>

--
Gitblit v1.9.3