@@ -0,0 +1,154 @@
|
||||
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.UsedApp;
|
||||
import com.yunhai.userhome.model.SetType;
|
||||
import com.yunhai.userhome.service.AppRemoteService;
|
||||
import com.yunhai.userhome.service.UsedAppService;
|
||||
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.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 常用应用接口(CAS 登录态,路径 {@code /{platform}/usedapp/}),对齐旧 UserHome契约
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(value = Config.API_PATH + "/usedapp/")
|
||||
public class UsedAppController {
|
||||
|
||||
@Autowired
|
||||
private UsedAppService usedAppService;
|
||||
|
||||
@Autowired
|
||||
private AppRemoteService appRemoteService;
|
||||
|
||||
/**
|
||||
* 取当前角色生效的常用应用(BoaApp 详情,按 appIds 顺序)。
|
||||
*
|
||||
* @param roleType 角色类型
|
||||
* @param schId 学校ID(0 时按用户主学校/主归属组推断)
|
||||
* @param isSch 是否学校级(超管设置学校常用时 true,isSch 时 uid=schId)
|
||||
*/
|
||||
@GetMapping("getUsedApp")
|
||||
public List<BoaApp> getUsedApp(BoaUser authUserWithDept, Integer roleType, Integer schId,
|
||||
@RequestParam(name = "isSch", defaultValue = "false") Boolean isSch) {
|
||||
if (schId == null || schId == 0) {
|
||||
schId = resolveSchId(authUserWithDept);
|
||||
}
|
||||
Integer uid = authUserWithDept.getId();
|
||||
if (authUserWithDept.getType() != null && authUserWithDept.getType() == 1) {
|
||||
isSch = false; // 超管设置学校常用时走用户维度
|
||||
}
|
||||
if (isSch) {
|
||||
uid = schId;
|
||||
}
|
||||
return usedAppService.getUsedAppList(schId, uid, roleType, authUserWithDept);
|
||||
}
|
||||
|
||||
/**
|
||||
* 覆盖式保存常用应用(≤8 由前端控制)。
|
||||
* 参数:appIds(逗号分隔)、type(0系统/1学校/2用户)、roleType、schId;超管强制 type=0。
|
||||
*/
|
||||
@PostMapping("saveUsedApp")
|
||||
public void saveUsedApp(BoaUser authUserWithDept, @RequestBody Map<String, Object> params) {
|
||||
String appIds = params.get("appIds") == null ? "" : String.valueOf(params.get("appIds"));
|
||||
Integer type = params.get("type") == null ? SetType.USER.id()
|
||||
: Integer.valueOf(String.valueOf(params.get("type")));
|
||||
Integer roleType = params.get("roleType") == null ? -1
|
||||
: Integer.valueOf(String.valueOf(params.get("roleType")));
|
||||
Integer schId = params.get("schId") == null ? 0
|
||||
: Integer.valueOf(String.valueOf(params.get("schId")));
|
||||
if (schId == 0) {
|
||||
schId = resolveSchId(authUserWithDept);
|
||||
}
|
||||
|
||||
UsedApp usedApp = new UsedApp();
|
||||
usedApp.setAppIds(appIds);
|
||||
usedApp.setType(SetType.byId(type));
|
||||
usedApp.setRoleType(roleType);
|
||||
usedApp.setCDate(new Date());
|
||||
usedApp.setCId(authUserWithDept.getId());
|
||||
usedApp.setSchId(schId);
|
||||
usedApp.setSetId(usedApp.getType() == SetType.SCHOOL ? schId : authUserWithDept.getId());
|
||||
if (authUserWithDept.getType() != null && authUserWithDept.getType() == 1) {
|
||||
// 超管保存 → 系统级
|
||||
usedApp.setType(SetType.BASE);
|
||||
usedApp.setSetId(authUserWithDept.getId());
|
||||
}
|
||||
usedAppService.save(usedApp);
|
||||
}
|
||||
|
||||
/** 用户级常用(type=USER, roleType=-1)精简映射(id/name/icon/url)。 */
|
||||
@GetMapping("getUsedAppById")
|
||||
public List<Map<String, Object>> getUsedAppById(BoaUser authUserWithDept) {
|
||||
return usedAppService.getUsedAppMapList(authUserWithDept, authUserWithDept.getId());
|
||||
}
|
||||
|
||||
/** 用户级保存常用(type=USER, roleType=-1)。 */
|
||||
@PostMapping("saveUsedAppById")
|
||||
public void saveUsedAppById(BoaUser authUserWithDept, @RequestBody Map<String, Object> params) {
|
||||
String appIds = params.get("appIds") == null ? "" : String.valueOf(params.get("appIds"));
|
||||
UsedApp usedApp = new UsedApp();
|
||||
usedApp.setAppIds(appIds);
|
||||
usedApp.setType(SetType.USER);
|
||||
usedApp.setRoleType(-1);
|
||||
usedApp.setCDate(new Date());
|
||||
usedApp.setCId(authUserWithDept.getId());
|
||||
usedApp.setSetId(authUserWithDept.getId());
|
||||
usedAppService.saveUserId(usedApp);
|
||||
}
|
||||
|
||||
/** 按 appIds(逗号分隔)取应用精简映射(id/name/icon/url),在全集中查找。 */
|
||||
@GetMapping("getAppByIds")
|
||||
public List<Map<String, Object>> getAppByIds(BoaUser authUserWithDept, String appIds) {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
if (appIds == null || appIds.trim().isEmpty()) {
|
||||
return list;
|
||||
}
|
||||
List<BoaApp> appList = appRemoteService.getUserApps(authUserWithDept).stream()
|
||||
.filter(t -> t.getApps() != null)
|
||||
.flatMap(t -> t.getApps().stream())
|
||||
.collect(Collectors.toList());
|
||||
for (String appId : appIds.split(",")) {
|
||||
try {
|
||||
Integer id = Integer.parseInt(appId.trim());
|
||||
appList.stream().filter(a -> id.equals(a.getId())).findFirst().ifPresent(app -> {
|
||||
Map<String, Object> m = new HashMap<>();
|
||||
m.put("id", app.getId());
|
||||
m.put("name", app.getName());
|
||||
m.put("icon", app.getIcon());
|
||||
m.put("url", app.getWebAddr());
|
||||
list.add(m);
|
||||
});
|
||||
} catch (NumberFormatException ignore) {
|
||||
// 脏数据容错
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/** schId=0 时推断:主学校 → 主归属组(bureau)→ 0。 */
|
||||
private Integer resolveSchId(BoaUser user) {
|
||||
if (user.getSchool() != null) {
|
||||
return user.getSchool().getId();
|
||||
}
|
||||
if (user.getBureau() != null) {
|
||||
return user.getBureau().getId();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user