@@ -0,0 +1,111 @@
|
||||
package com.yunhai.userhome.controller.api;
|
||||
|
||||
import com.yunhai.platform.utils.model.BoaApp;
|
||||
import com.yunhai.platform.utils.model.BoaAppType;
|
||||
import com.yunhai.platform.utils.model.BoaUser;
|
||||
import com.yunhai.userhome.config.Config;
|
||||
import com.yunhai.userhome.entity.UserMessage;
|
||||
import com.yunhai.userhome.service.AppRemoteService;
|
||||
import com.yunhai.userhome.service.IUserMessageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 消息中心接口(CAS 登录态,路径 {@code /{platform}/}),对齐旧 UserHome MessageController 契约。
|
||||
* <p>响应字段与旧版 {@code getMessageByUserId} 完全一致:{id,msg,date,name,avatar,appPath,msgType}。</p>
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = Config.API_PATH)
|
||||
public class MessageController {
|
||||
|
||||
@Autowired
|
||||
private IUserMessageService userMessageService;
|
||||
|
||||
@Autowired
|
||||
private AppRemoteService appRemoteService;
|
||||
|
||||
/** schId=0 时推断:主学校 → 主归属组(bureau)→ 0(对齐旧版 school.id→bureau.id→0)。 */
|
||||
private Integer resolveSchId(BoaUser user) {
|
||||
if (user.getSchool() != null) {
|
||||
return user.getSchool().getId();
|
||||
}
|
||||
if (user.getBureau() != null) {
|
||||
return user.getBureau().getId();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private List<BoaApp> flattenApps(BoaUser user) {
|
||||
List<BoaAppType> types = appRemoteService.getUserApps(user);
|
||||
if (types == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<BoaApp> apps = new ArrayList<>();
|
||||
for (BoaAppType t : types) {
|
||||
if (t.getApps() != null) {
|
||||
apps.addAll(t.getApps());
|
||||
}
|
||||
}
|
||||
return apps;
|
||||
}
|
||||
|
||||
// 获取个人最近消息(对齐旧 getMessageByUserId,响应 List<Map>,字段逐字核对)
|
||||
@GetMapping("getMessageByUserId")
|
||||
public List<Map<String, Object>> getMessageByUserId(BoaUser authUserWithDept, Integer page, Integer count) {
|
||||
Integer userId = authUserWithDept.getId();
|
||||
Integer schId = resolveSchId(authUserWithDept);
|
||||
|
||||
List<UserMessage> msgList = userMessageService.findUserMessageByUserId(userId);
|
||||
Collections.reverse(msgList);
|
||||
|
||||
List<BoaApp> appList = flattenApps(authUserWithDept);
|
||||
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
for (UserMessage msg : msgList) {
|
||||
Optional<BoaApp> boaApp = appList.stream().filter(x -> x.getId() != null && x.getId().equals(msg.getAppId())).findFirst();
|
||||
if (!boaApp.isPresent()) {
|
||||
continue;
|
||||
}
|
||||
BoaApp model = boaApp.get();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", msg.getId());
|
||||
map.put("msg", msg.getContent());
|
||||
map.put("date", msg.getCDate());
|
||||
map.put("name", model.getName());
|
||||
map.put("avatar", model.getIcon());
|
||||
map.put("appPath", model.getWebAddr());
|
||||
map.put("msgType", model.getType());
|
||||
list.add(map);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
// 已读消息(对齐旧 readMessage,POST,参数 id,返回 void)
|
||||
@PostMapping("readMessage")
|
||||
public void readMessage(BoaUser authUserWithDept, Integer id) {
|
||||
userMessageService.updateUserRead(id, authUserWithDept.getId());
|
||||
}
|
||||
|
||||
// 全部已读消息(对齐旧 readMessageAll,POST 无参,返回 void)
|
||||
@PostMapping("readMessageAll")
|
||||
public void readMessageAll(BoaUser authUserWithDept) {
|
||||
userMessageService.updateReadAll(authUserWithDept.getId());
|
||||
}
|
||||
|
||||
// 是否有新消息(对齐旧 findUserMessageUnRead,GET,返回 Boolean)
|
||||
@GetMapping("findUserMessageUnRead")
|
||||
public Boolean findUserMessageUnRead(BoaUser authUserWithDept) {
|
||||
return userMessageService.findUserMessageUnRead(authUserWithDept.getId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user