ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YanYu
 * @since 2023-04-19
 */
@Service
public class PortalDataServiceImpl extends SuperServiceImpl<PortalDataMapper, PortalDataEntity> 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<PortalDataEntity> 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<PortalDataEntity> 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);
    }
 
    /**
     * 创建或更新
     * <p>
     * 门户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<PortalDataEntity> 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<String> 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;
    }
 
    /**
     * 设置门户默认主页
     * <p>
     * 用户ID -> (平台、系统ID) -> 门户ID
     * 基础:用户ID绑定门户ID(多对多)、条件:平台、系统ID
     * Map格式:Map <platform:systemId, portalId>
     *
     * @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<String, Object> 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<String> authPortalIds, String systemId, String userId, String platform) {
        SystemEntity systemEntity = systemApi.getInfoById(systemId);
        UserEntity userEntity = userApi.getInfoById(userId);
        //应用设置的门户首页资源(存储的是菜单id)
        List<String> 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<String, Object> 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<PortalListVO> selectorMenu() {
        List<PortalListVO> 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<ModuleModel> 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<String> 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;
    }
}