fix(platform): implement short message send

This commit is contained in:
2026-06-18 01:19:15 +08:00
parent f690f30552
commit 723443ea1e
4 changed files with 373 additions and 0 deletions

View File

@@ -1,17 +1,23 @@
package com.eip4.platform.metadata.web;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
@@ -50,6 +56,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
* <li>{@code GET /api/notice/unread-count} — 轻量未读数计数(顶栏轮询用)</li>
* <li>{@code GET /api/notice/{id}/detail} — 已发送/可访问消息详情</li>
* <li>{@code POST /api/notice/received/{id}/open} — 收到消息打开详情并置已读</li>
* <li>{@code POST /api/notice/send} — 发送短消息(写 sys_message + sys_messageuser)</li>
* <li>{@code POST /api/notice/{id}/read} — 单条置已读</li>
* <li>{@code POST /api/notice/read-all} — 当前用户全部已读</li>
* </ul>
@@ -196,6 +203,69 @@ public class NoticeController {
}
}
// ---------------------------------------------------------------- 发送短消息
/**
* 发送短消息。
*
* <p>复刻旧 {@code MessageSendForm -> SYS_MESSAGE_ADD} 的核心写库语义:写一条
* {@code SYS_MESSAGE(TYPE=2)},并按 {@code TOUSERS} 为每个收件人写 {@code SYS_MESSAGEUSER}。
* 收件人输入兼容标准 amis picker 的多种形态:逗号串、字符串数组、行对象数组(取
* {@code _id/value/SYS_USER_ID/VIEW_SYS_USER_ID})。
*/
@Operation(summary = "发送短消息",
description = "写 SYS_MESSAGE(TYPE=2) + SYS_MESSAGEUSER;TOUSERS 支持 picker 多选值")
@PostMapping("/api/notice/send")
@Transactional
public ResponseEntity<Map<String, Object>> send(@RequestBody Map<String, Object> body) {
String userId = requireCurrentUserId();
String title = string(body == null ? null : body.get("TITLE"));
String content = string(body == null ? null : body.get("CONTENT"));
List<String> receivers = parseReceiverIds(body == null ? null : body.get("TOUSERS"));
if (title.isBlank()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "标题为必输项");
}
if (receivers.isEmpty()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "接收人为必输项");
}
if (content.isBlank()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "内容不能为空");
}
Properties ctx = UserSession.get();
String orgId = blankToNull(ctx == null ? null : ctx.getProperty(LoggedInUser.CURRENTORG));
String departId = blankToNull(ctx == null ? null : ctx.getProperty(LoggedInUser.CURRENTDEPART));
long now = System.currentTimeMillis();
String messageId = UUID.randomUUID().toString();
int isReply = body != null && isTruthy(body.get("ISREPLY")) ? 1 : 0;
jdbcTemplate.update(
"INSERT INTO eip3.sys_message(sys_message_id,title,content,isdel,isreply,type,"
+ "createtime,createby,updatetime,updateby,orgid,departid,isactive,version,params)"
+ " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
messageId, title, content, 0, isReply, 2,
now, userId, now, userId, orgId, departId, 1, 0, null);
for (String receiverId : receivers) {
jdbcTemplate.update(
"INSERT INTO eip3.sys_messageuser(sys_messageuser_id,sys_message_id,sys_user_id,"
+ "isread,isdel,createtime,createby,updatetime,updateby,orgid,departid,isactive,version)"
+ " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",
UUID.randomUUID().toString(), messageId, receiverId,
0, 0, now, userId, now, userId, orgId, departId, 1, 0);
}
Map<String, Object> data = new LinkedHashMap<>();
data.put("id", messageId);
data.put("receiverCount", receivers.size());
Map<String, Object> resp = new LinkedHashMap<>();
resp.put("status", 0);
resp.put("msg", "发送成功");
resp.put("data", data);
return ResponseEntity.ok(resp);
}
// ---------------------------------------------------------------- 未读数计数
/**
@@ -390,4 +460,51 @@ public class NoticeController {
String s = String.valueOf(value).trim();
return "1".equals(s) || "true".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s);
}
private static String blankToNull(String value) {
String s = string(value);
return s.isBlank() ? null : s;
}
private static List<String> parseReceiverIds(Object raw) {
Set<String> out = new LinkedHashSet<>();
collectReceiverIds(raw, out);
return new ArrayList<>(out);
}
private static void collectReceiverIds(Object raw, Set<String> out) {
if (raw == null) {
return;
}
if (raw instanceof Iterable<?> values) {
for (Object value : values) {
collectReceiverIds(value, out);
}
return;
}
if (raw instanceof Map<?, ?> map) {
Object id = firstNonNull(map, "_id", "value", "id", "SYS_USER_ID", "VIEW_SYS_USER_ID");
collectReceiverIds(id, out);
return;
}
String text = string(raw);
if (text.isBlank()) {
return;
}
for (String part : text.split(",")) {
String id = part.trim();
if (!id.isBlank()) {
out.add(id);
}
}
}
private static Object firstNonNull(Map<?, ?> map, String... keys) {
for (String key : keys) {
if (map.containsKey(key) && map.get(key) != null) {
return map.get(key);
}
}
return null;
}
}