package jnpf.portal.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.alibaba.fastjson.JSONObject; import jnpf.base.ActionResult; import jnpf.base.ModuleApi; import jnpf.base.SystemApi; import jnpf.base.entity.ModuleEntity; import jnpf.base.entity.SystemEntity; import jnpf.base.model.module.ModuleModel; import jnpf.base.model.online.VisualMenuModel; import jnpf.base.service.SuperServiceImpl; import jnpf.constant.JnpfConst; import jnpf.constant.MsgCode; import jnpf.exception.WorkFlowException; import jnpf.permission.AuthorizeApi; import jnpf.permission.UserApi; import jnpf.permission.entity.UserEntity; import jnpf.permission.model.authorize.AuthorizeVO; import jnpf.portal.entity.PortalDataEntity; import jnpf.portal.entity.PortalEntity; import jnpf.portal.mapper.PortalDataMapper; import jnpf.portal.mapper.PortalMapper; import jnpf.portal.model.*; import jnpf.portal.service.PortalDataService; import jnpf.util.JsonUtil; import jnpf.util.JsonUtilEx; import jnpf.util.StringUtil; import jnpf.util.UserProvider; import jnpf.util.context.RequestContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author YanYu * @since 2023-04-19 */ @Service public class PortalDataServiceImpl extends SuperServiceImpl implements PortalDataService { @Autowired private UserApi userApi; @Autowired private ModuleApi moduleApi; @Autowired private SystemApi systemApi; @Autowired private AuthorizeApi authorizeApi; @Autowired private PortalMapper portalMapper; @Override public String getModelDataForm(PortalModPrimary primary) throws Exception { return this.baseMapper.getModelDataForm(primary); } @Override public void releaseModule(ReleaseModel releaseModel, String portalId) throws Exception { PortalEntity info = portalMapper.getInfo(portalId); if (info != null) { VisualMenuModel visual = new VisualMenuModel(); visual.setApp(releaseModel.getApp()); visual.setPc(releaseModel.getPc()); visual.setPcModuleParentId(releaseModel.getPcModuleParentId()); visual.setAppModuleParentId(releaseModel.getAppModuleParentId()); String formData = getModelDataForm(new PortalModPrimary(portalId)); if (releaseModel.getPc() == 1) { createOrUpdate(new PortalReleasePrimary(portalId, JnpfConst.WEB), formData); // 查询所有相关的自定义数据 List list = list(new PortalCustomPrimary(JnpfConst.WEB, portalId).getQuery()); final String finalFormData = formData; if (list.size() > 0) { // 把所有数据进行重置formData list.forEach(entity -> entity.setFormData(finalFormData)); updateBatchById(list); } } if (releaseModel.getApp() == 1) { createOrUpdate(new PortalReleasePrimary(portalId, JnpfConst.APP), formData); // 查询所有相关的自定义数据 List list = list(new PortalCustomPrimary(JnpfConst.WEB, portalId).getQuery()); final String finalFormData = formData; if (list.size() > 0) { // 把所有数据进行重置formData list.forEach(entity -> entity.setFormData(finalFormData)); updateBatchById(list); } } visual.setType(8); visual.setFullName(info.getFullName()); visual.setEnCode(info.getEnCode()); visual.setId(info.getId()); ActionResult res = moduleApi.pubulish(visual); if (res.getCode() != 200) { throw new WorkFlowException(res.getMsg()); } info.setState(1); info.setEnabledMark(1); info.setPlatformRelease(releaseModel.getPlatformRelease()); portalMapper.update(portalId, info); } } @Override public void deleteAll(String portalId) { this.baseMapper.deleteAll(portalId); } /** * 创建或更新 *

* 门户ID ->(平台、系统ID、用户ID)-> 排版信息 * 基础:门户ID绑定排版信息(一对多)、 条件:平台、系统ID、用户ID */ @Override public void createOrUpdate(PortalCustomPrimary primary, String formData) throws Exception { this.baseMapper.createOrUpdate(primary, formData); } @Override public void createOrUpdate(PortalModPrimary primary, String formData) throws Exception { this.baseMapper.createOrUpdate(primary, formData); } @Override public void createOrUpdate(PortalReleasePrimary primary, String formData) throws Exception { this.baseMapper.createOrUpdate(primary, formData); } /** * 根据id返回门户信息 */ @Override public PortalInfoAuthVO getDataFormView(String menuId, String platform) throws Exception { // portalId当前版本这个是菜单id PortalEntity entity; PortalEntity pEnt = portalMapper.getInfo(menuId); if (pEnt != null) { entity = pEnt; } else { ModuleEntity info = moduleApi.getModuleByList(menuId); if (info == null) throw new Exception(MsgCode.FA001.get()); entity = portalMapper.getInfo(info.getModuleId()); if (entity == null) throw new Exception(MsgCode.VS415.get()); } String portalId = entity.getId(); // if (entity.getEnabledMark() == 0) throw new Exception("门户被禁止"); PortalInfoAuthVO infoVo = JsonUtil.getJsonToBean(JsonUtilEx.getObjectToStringDateFormat (entity, "yyyy-MM-dd HH:mm:ss"), PortalInfoAuthVO.class); // 查询自定义设计的门户信息 infoVo.setFormData(getDataForm(platform, portalId)); return infoVo; } private String getDataForm(String platform, String portalId) throws Exception { List dataList = list(new PortalCustomPrimary(platform, portalId).getQuery()); if (CollectionUtil.isEmpty(dataList)) { dataList = list(new PortalReleasePrimary(portalId, platform).getQuery()); } // 当没有自定义的排版信息时,使用已发布模板排版信息 if (CollectionUtil.isNotEmpty(dataList)) { PortalDataEntity entity = dataList.get(0); if (dataList.size() != 1) { List ids = dataList.stream().map(PortalDataEntity::getId).collect(Collectors.toList()); removeBatchByIds(ids.stream().filter(id -> !id.equals(entity.getId())).collect(Collectors.toList())); } return entity.getFormData(); } return null; } /** * 设置门户默认主页 *

* 用户ID -> (平台、系统ID) -> 门户ID * 基础:用户ID绑定门户ID(多对多)、条件:平台、系统ID * Map格式:Map * * @param portalId 门户ID * @param platform 平台 */ @Override public void setCurrentDefault(SystemEntity systemEntity, UserEntity userEntity, String platform, String portalId) { if (systemEntity == null) { String appCode = RequestContext.getAppCode(); systemEntity = systemApi.getInfoByEnCode(appCode); } if (userEntity == null) { userEntity = userApi.getInfoById(UserProvider.getUser().getUserId()); } Map map = new HashMap<>(); try { map = JSONObject.parseObject(userEntity.getPortalId()).getInnerMap(); } catch (Exception ignore) { } if (JnpfConst.APP.equalsIgnoreCase(platform)) { map.put(platform + ":" + systemEntity.getId(), portalId); } else { map.put(platform + ":" + systemEntity.getId(), portalId); } //移除null和空字符串 map.entrySet().removeIf(entry -> entry.getValue() == null || "".equals(entry.getValue().toString())); UserEntity update = new UserEntity(); update.setId(userEntity.getId()); update.setPortalId(JSONObject.toJSONString(map)); userApi.updateUserById(update); } @Override public String getCurrentDefault(List authPortalIds, String systemId, String userId, String platform) { SystemEntity systemEntity = systemApi.getInfoById(systemId); UserEntity userEntity = userApi.getInfoById(userId); //应用设置的门户首页资源(存储的是菜单id) List sysPortalMenuIdList = new ArrayList<>(); if (JnpfConst.WEB.equalsIgnoreCase(platform)) { if (StringUtil.isNotBlank(systemEntity.getPortalId())) { sysPortalMenuIdList.addAll(Arrays.asList(systemEntity.getPortalId().split(","))); } } else { if (StringUtil.isNotBlank(systemEntity.getAppPortalId())) { sysPortalMenuIdList.addAll(Arrays.asList(systemEntity.getAppPortalId().split(","))); } } String menuId = ""; try { Map map = JSONObject.parseObject(userEntity.getPortalId()).getInnerMap(); menuId = map.get(platform + ":" + systemId).toString(); } catch (Exception ignore) { } //获取有权限的门户菜单集合 authPortalIds = authPortalIds.stream().filter(t -> sysPortalMenuIdList.contains(t)).collect(Collectors.toList()); ModuleEntity info = moduleApi.getModuleByList(menuId); PortalEntity mainPortal = info != null ? portalMapper.getInfo(info.getModuleId()) : null; // 校验门户有效性 if (mainPortal != null && mainPortal.getEnabledMark().equals(1)) { if (CollectionUtil.isNotEmpty(authPortalIds) && authPortalIds.contains(menuId)) { return menuId; } } // 当前存储的门户不在权限范围内,重新设置一个有权限的门户首页为默认门户 if (CollectionUtil.isNotEmpty(authPortalIds)) { for (String item : authPortalIds) { ModuleEntity itemInfo = moduleApi.getModuleByList(item); PortalEntity itemPort = itemInfo != null ? portalMapper.getInfo(itemInfo.getModuleId()) : null; if (itemPort != null) { setCurrentDefault(systemEntity, userEntity, platform, item); return item; } } } return null; } @Override public List selectorMenu() { List list = new ArrayList<>(); String appCode = RequestContext.getAppCode(); SystemEntity systemEntity = systemApi.getInfoByEnCode(appCode); AuthorizeVO authorize = authorizeApi.getAuthorize(false, appCode, null); String category = RequestContext.isOrignPc() ? JnpfConst.WEB : JnpfConst.APP; //门户菜单 List portalMenu = authorize.getModuleList().stream().filter(t -> Objects.equals(t.getType(), 8) && category.equals(t.getCategory())).collect(Collectors.toList()); String portalId = RequestContext.isOrignPc() ? systemEntity.getPortalId() : systemEntity.getAppPortalId(); List portalIds = new ArrayList<>(); if (StringUtil.isNotEmpty(portalId)) { portalIds.addAll(Arrays.asList(portalId.split(","))); } for (String id : portalIds) { ModuleModel moduleModel = portalMenu.stream().filter(t -> Objects.equals(t.getId(), id)).findFirst().orElse(null); if (moduleModel != null) { PortalListVO vo = new PortalListVO(); vo.setId(moduleModel.getId()); vo.setFullName(moduleModel.getFullName()); list.add(vo); } } return list; } }