leomon
2025-07-12 0233d941ae15db1a089bdb633f76d67be6a15ed3
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.oying.modules.message.rest;
 
import com.alibaba.fastjson2.JSONObject;
import com.oying.annotation.Log;
import com.oying.modules.message.domain.MessageSystem;
import com.oying.modules.message.domain.dto.SendMessageRequestDto;
import com.oying.modules.message.service.MessageSystemService;
import com.oying.modules.message.domain.dto.MessageSystemQueryCriteria;
import com.oying.modules.system.domain.User;
import com.oying.modules.system.domain.UserSubscribe;
import com.oying.utils.HttpRequest;
import lombok.Data;
import lombok.RequiredArgsConstructor;
 
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oying.utils.PageResult;
import com.oying.modules.security.service.WeiXinService;
import com.oying.utils.R;
/**
 * @author 李萌
 * @date 2025-05-20
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "消息_系统")
@RequestMapping("/api/message/messageSystem")
public class MessageSystemController {
 
    private static final Logger log = LoggerFactory.getLogger(MessageSystemController.class);
    private final MessageSystemService messageSystemService;
    private final WeiXinService weiXinService;
 
 
 
//    @ApiOperation("导出数据")
//    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('messageSystem:list')")
//    public void exportMessageSystem(HttpServletResponse response, MessageSystemQueryCriteria criteria) throws IOException {
//        messageSystemService.download(messageSystemService.queryAll(criteria), response);
//    }
 
    @GetMapping
    @ApiOperation("查询消息_系统")
    @PreAuthorize("@el.check('messageSystem:list')")
    public R<PageResult<MessageSystem>> queryMessageSystem(MessageSystemQueryCriteria criteria){
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
//        return new ResponseEntity<>(messageSystemService.queryAll(criteria,page),HttpStatus.OK);
        return R.success(messageSystemService.queryAll(criteria,page));
    }
 
//    @PostMapping
//    @Log("新增消息_系统")
//    @ApiOperation("新增消息_系统")
//    @PreAuthorize("@el.check('messageSystem:add')")
//    public ResponseEntity<Object> createMessageSystem(@Validated @RequestBody MessageSystem resources){
//        messageSystemService.create(resources);
//        return new ResponseEntity<>(HttpStatus.CREATED);
//    }
 
//    @PutMapping
//    @Log("修改消息_系统")
//    @ApiOperation("修改消息_系统")
//    @PreAuthorize("@el.check('messageSystem:edit')")
//    public ResponseEntity<Object> updateMessageSystem(@Validated @RequestBody MessageSystem resources){
//        messageSystemService.update(resources);
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }
 
//    @DeleteMapping
//    @Log("删除消息_系统")
//    @ApiOperation("删除消息_系统")
//    @PreAuthorize("@el.check('messageSystem:del')")
//    public ResponseEntity<Object> deleteMessageSystem(@ApiParam(value = "传ID数组[]") @RequestBody List<Integer> ids) {
//        messageSystemService.deleteAll(ids);
//        return new ResponseEntity<>(HttpStatus.OK);
//    }
 
    //查询一条系统消息
    @GetMapping("/{id}")
    @ApiOperation("查询一条系统消息")
    public R<MessageSystem> getMessageSystem(@PathVariable Integer id){
        MessageSystem messageSystem = messageSystemService.getById(id);
//        return new ResponseEntity<>(messageSystem,HttpStatus.OK);
        return R.success(messageSystem);
    }
    //插入一条系统消息 带两个参数start与end
    @PostMapping()
    @ApiOperation("插入一条系统消息")
    public R<Object> insertMessageSystem(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) {
 
        // 创建 MessageSystem 对象
        MessageSystem messageSystem = new MessageSystem();
        messageSystem.setStartTime(new Timestamp(start.getTime()));
        messageSystem.setEndTime(new Timestamp(end.getTime()));
 
        // 调用服务层保存数据
        messageSystemService.save(messageSystem);
 
//        return new ResponseEntity<>(HttpStatus.OK);
        return R.success("插入成功");
    }
 
    /**
     * POST 该接口用于发送订阅消息。
     *  data       请求参数
     * openid     用户openId
     * templateId 订阅模板id
     * 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);
//            jsonObject = HttpRequest.exchangeJsonObject(HttpMethod.POST, url, map);
//        } else {
//            jsonObject.put("message", "测试环境");
//        }
//        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);
//    }
    //调用微信发送消息接口sendMessage
 
    //示例
//    {
//        "touser": "OPENID",
//            "template_id": "TEMPLATE_ID",
//            "page": "index",
//            "data":{
//        "phrase3": {
//            "value": "审核通过"
//        },
//        "name1": {
//            "value": "订阅"
//        },
//        "date2": {
//            "value": "2019-12-25 09:42"
//        }
//    }
//    }
//todo  发送订阅消息   数据库暂未存储
    @PostMapping("/sendMessage")
    @ApiOperation("发送订阅消息")
    public R<SendMessageRequestDto> sendMessage(@RequestBody SendMessageRequestDto request){
        //打印request
        //SendMessageRequestDto(touser=OPENID, template_id=TEMPLATE_ID, page=index, data={date2={"value":"2019-12-25 09:42"}, phrase3={"value":"审核通过"}, name1={"value":"订阅"}})
        log.info("request:{}", request);
        try {
            weiXinService.sendMessage(request.getData(), request.getTouser(), request.getTemplate_id(), request.getPage());
            return R.success(request);
        } catch (Exception e) {
            // 如果捕获到异常,返回错误信息
            log.error("发送订阅消息失败", e);
            return R.fail(request,"发送订阅消息失败 用真实数据:" + e.getMessage());
        }
    }
 
 
 
}