ny
23 小时以前 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package jnpf.base.controller;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.ModuleEntity;
import jnpf.base.entity.SystemEntity;
import jnpf.base.model.base.SystemBaeModel;
import jnpf.base.model.home.ChartModel;
import jnpf.base.model.home.HomeModel;
import jnpf.base.model.home.MenuModel;
import jnpf.base.model.home.TeamWorkModel;
import jnpf.base.model.module.MenuSelectByUseNumVo;
import jnpf.base.model.module.ModuleModel;
import jnpf.base.service.*;
import jnpf.constant.JnpfConst;
import jnpf.flowable.TaskApi;
import jnpf.flowable.TemplateApi;
import jnpf.flowable.model.task.TaskTo;
import jnpf.flowable.model.template.TemplatePagination;
import jnpf.message.NoticeApi;
import jnpf.message.model.MessageInfoVO;
import jnpf.message.model.NoticeVO;
import jnpf.permission.AuthorizeApi;
import jnpf.permission.RoleApi;
import jnpf.permission.model.authorize.AuthorizeVO;
import jnpf.util.JsonUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import jnpf.util.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
/**
 * 控制台首页数据
 *
 * @author JNPF开发平台组
 * @version v6.0.0
 * @copyright 引迈信息技术有限公司
 * @date 2025/4/1 11:51:02
 */
@Tag(name = "控制台首页数据", description = "HomeDataController")
@RestController
@RequestMapping("/HomeData")
public class HomeDataController {
 
    private static final String NO1_WELCOME = "welcome";
    private static final String NO2_BANNER = "banner";
    private static final String NO3_WORK_FLOW = "workFlow";
    private static final String NO4_TEAMWORK = "teamwork";
    private static final String NO5_NOTICE = "notice";
    private static final String NO6_MESSAGE = "message";
    private static final String NO7_LATELY_USE = "latelyUse";
    private static final String NO8_COMMON_USE = "commonUse";
    private static final String NO9_FAVORITES = "favorites";
    private static final String N10_SYSTEM = "system";
    private static final String NO11_PIE_CHART = "pieChart";
    private static final String NO12_COLUMN_CHART = "columnChart";
    private static final String NO13_SERVER = "server";
    private static final String NO14_HELP = "help";
 
    @Autowired
    private TaskApi taskApi;
    @Autowired
    private TemplateApi templateApi;
    @Autowired
    private AuthorizeApi authorizeService;
    @Autowired
    private NoticeApi messageService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private RoleApi roleService;
    @Autowired
    private ModuleUseNumService moduleUseNumService;
    @Autowired
    private ModuleDataService moduleDataService;
    @Autowired
    private SysconfigService sysConfigApi;
    @Autowired
    private ModuleService moduleService;
 
 
    @Operation(summary = "首页数据")
    @GetMapping
    public ActionResult get() {
        UserInfo userInfo = UserProvider.getUser();
        List<HomeModel> list = new ArrayList<>();
        AuthorizeVO authorize = authorizeService.getAuthorizeByUser(false);
        List<SystemEntity> sysListAll = systemService.getList();
        List<ModuleEntity> allMenu = moduleService.getList();
        Map<String, SystemEntity> sysMap = sysListAll.stream().collect(Collectors.toMap(SystemEntity::getId, t -> t));
        List<ModuleModel> moduleList = authorize.getModuleList();
        List<String> authMenuList = moduleList.stream().filter(t -> JnpfConst.WEB.equals(t.getCategory()))
                .map(ModuleModel::getId).collect(Collectors.toList());
        Boolean iaAdmin = userInfo.getIsAdministrator();//超管
        Boolean isManageRole = userInfo.getIsManageRole();
        Boolean isDevRole = userInfo.getIsDevRole();
        boolean isFlowEnabled = Objects.equals(userInfo.getWorkflowEnabled(), 1);
        boolean isTeamEnabled = moduleList.stream().anyMatch(t -> JnpfConst.TEAMWORK_MODULE.contains(t.getEnCode()) && JnpfConst.WEB.equals(t.getCategory()));
        //1-欢迎语
        list.add(new HomeModel(NO1_WELCOME, 1, null));
        //2-banner
        if (isManageRole || isDevRole || iaAdmin) {
            list.add(new HomeModel(NO2_BANNER, 1, null));
        } else {
            list.add(new HomeModel(NO2_BANNER, 0, null));
        }
        //3-流程数据
        if (isFlowEnabled) {
            SystemEntity flowSys = systemService.getInfoByEnCode(JnpfConst.WORK_FLOW_CODE);
            List<ModuleModel> flowMenuList = moduleList.stream().filter(t -> t.getSystemId().equals(flowSys.getId())).collect(Collectors.toList());
            setFlow(flowMenuList, list);
 
        } else {
            list.add(new HomeModel(NO3_WORK_FLOW, 0, null));
        }
        //4-协作数据
        if (isTeamEnabled) {
            SystemEntity teamSys = systemService.getInfoByEnCode(JnpfConst.TEAMWORK_CODE);
            List<ModuleModel> teamworkList = moduleList.stream().filter(t -> t.getSystemId().equals(teamSys.getId())).collect(Collectors.toList());
            setTeamwork(teamworkList, list);
        } else {
            list.add(new HomeModel(NO4_TEAMWORK, 0, null));
        }
 
        //5-公告
        List<NoticeVO> noticeList = JsonUtil.getJsonToList(messageService.getNoticeList(new ArrayList<>()), NoticeVO.class);
        if (noticeList.size() > 0) {
            noticeList = Lists.partition(noticeList, 5).get(0);
        }
        list.add(new HomeModel(NO5_NOTICE, 1, noticeList));
        //6-消息
        List<MessageInfoVO> messageList = messageService.getUserMessageList();
        list.add(new HomeModel(NO6_MESSAGE, 1, messageList));
 
        //7-最近使用
        List<Object> menuNum = new ArrayList<>();
        List<MenuSelectByUseNumVo> menuLatelyList = moduleUseNumService.getMenuUseNum(1, authMenuList, allMenu);
        for (MenuSelectByUseNumVo item : menuLatelyList) {
            MenuModel menuModel = JsonUtil.getJsonToBean(item, MenuModel.class);
            setMenuModel(menuModel, sysMap, menuNum);
        }
        HomeModel latelyModel = HomeModel.builder().code(NO7_LATELY_USE).enable(1).appList(menuNum).flowEnabled(isFlowEnabled).build();
        if (isFlowEnabled) {
            List<Object> flowNum = new ArrayList<>(templateApi.getMenuUseNum(1, authorize.getFlowIdList()));
            latelyModel.setFlowList(flowNum);
        }
        list.add(latelyModel);
        //8-最近常用
        List<Object> menuUseNum = new ArrayList<>();
        List<MenuSelectByUseNumVo> menuComList = moduleUseNumService.getMenuUseNum(0, authMenuList, allMenu);
        for (MenuSelectByUseNumVo item : menuComList) {
            MenuModel menuModel = JsonUtil.getJsonToBean(item, MenuModel.class);
            setMenuModel(menuModel, sysMap, menuUseNum);
        }
        HomeModel commonUseModel = HomeModel.builder().code(NO8_COMMON_USE).enable(1).appList(menuUseNum).flowEnabled(isFlowEnabled).build();
        if (isFlowEnabled) {
            List<Object> flowUseNum = new ArrayList<>(templateApi.getMenuUseNum(0, authorize.getFlowIdList()));
            commonUseModel.setFlowList(flowUseNum);
        }
        list.add(commonUseModel);
        //9-我的收藏
        List<Object> favoritesMenuNum = new ArrayList<>();
        List<ModuleModel> favoritesList = moduleDataService.getFavoritesList(moduleList);
        for (ModuleModel item : favoritesList) {
            MenuModel menuModel = JsonUtil.getJsonToBean(item, MenuModel.class);
            setMenuModel(menuModel, sysMap, favoritesMenuNum);
        }
        HomeModel favoritesModel = HomeModel.builder().code(NO9_FAVORITES).enable(1).appList(favoritesMenuNum).flowEnabled(isFlowEnabled).build();
        if (isFlowEnabled || iaAdmin) {
            TemplatePagination pagination = new TemplatePagination();
            pagination.setPageSize(10000);
            pagination.setCategory("commonFlow");
            pagination.setIsLaunch(1);
            pagination.setAuthorize(authorize);
            List<Object> favoritesNum = new ArrayList<>(templateApi.getCommonList(pagination));
            favoritesModel.setFlowList(favoritesNum);
        }
        list.add(favoritesModel);
 
        //10-我的应用
        List<SystemBaeModel> systemList = authorize.getSystemList().stream().filter(t -> !Objects.equals(t.getIsMain(), 1)).collect(Collectors.toList());
        list.add(new HomeModel(N10_SYSTEM, 1, systemList));
 
        if (isManageRole || iaAdmin) {
            //11-饼图
            Map<String, Integer> roleCount = roleService.roleUserCount();
            List<ChartModel> roleCountList = roleCount.entrySet().stream().map(entry -> new ChartModel(entry.getKey(), entry.getValue())).collect(Collectors.toList());
            list.add(new HomeModel(NO11_PIE_CHART, 1, roleCountList));
            //12-柱形图
            setColumnChart(list);
            //13-服务器信息
            list.add(new HomeModel(NO13_SERVER, 1, null));
        } else {
            list.add(new HomeModel(NO11_PIE_CHART, 0, null));
            list.add(new HomeModel(NO12_COLUMN_CHART, 0, null));
            list.add(new HomeModel(NO13_SERVER, 0, null));
        }
 
        String help = sysConfigApi.getValueByKey(NO14_HELP);
        if (StringUtil.isNotBlank(help)) {
            JSONObject jsonObject = JSON.parseObject(help);
            list.add(new HomeModel(NO14_HELP, 1, jsonObject.get("java")));
        } else {
            list.add(new HomeModel(NO14_HELP, 0, null));
        }
        return ActionResult.success(list);
    }
 
    @Operation(summary = "首页数据")
    @GetMapping("/app")
    public ActionResult getApp() {
        List<HomeModel> list = new ArrayList<>();
        AuthorizeVO authorize = authorizeService.getAuthorize(false, RequestContext.getAppCode(), 0);
        List<SystemEntity> sysListAll = systemService.getList();
        List<ModuleEntity> allMenu = moduleService.getList();
        Map<String, SystemEntity> sysMap = sysListAll.stream().collect(Collectors.toMap(SystemEntity::getId, t -> t));
        List<ModuleModel> moduleList = authorize.getModuleList();
        List<String> authMenuList = moduleList.stream().filter(t -> JnpfConst.APP.equals(t.getCategory()))
                .map(ModuleModel::getId).collect(Collectors.toList());
        boolean isFlowEnabled = moduleList.stream().anyMatch(t -> JnpfConst.MODULE_CODE.contains(t.getEnCode()) && JnpfConst.APP.equals(t.getCategory()));
 
        //7-最近使用
        List<Object> menuNum = new ArrayList<>();
        List<MenuSelectByUseNumVo> menuLatelyList = moduleUseNumService.getMenuUseNum(1, authMenuList, allMenu);
        for (MenuSelectByUseNumVo item : menuLatelyList) {
            MenuModel menuModel = JsonUtil.getJsonToBean(item, MenuModel.class);
            setMenuModel(menuModel, sysMap, menuNum);
        }
        HomeModel latelyModel = HomeModel.builder().code(NO7_LATELY_USE).enable(1).appList(menuNum).flowEnabled(isFlowEnabled).build();
        List<Object> flowNum = new ArrayList<>(templateApi.getMenuUseNum(1, authorize.getFlowIdList()));
        latelyModel.setFlowList(flowNum);
        list.add(latelyModel);
        //8-最近常用
        List<Object> menuUseNum = new ArrayList<>();
        List<MenuSelectByUseNumVo> menuComList = moduleUseNumService.getMenuUseNum(0, authMenuList, allMenu);
        for (MenuSelectByUseNumVo item : menuComList) {
            MenuModel menuModel = JsonUtil.getJsonToBean(item, MenuModel.class);
            setMenuModel(menuModel, sysMap, menuUseNum);
        }
        HomeModel commonUseModel = HomeModel.builder().code(NO8_COMMON_USE).enable(1).appList(menuUseNum).flowEnabled(isFlowEnabled).build();
        List<Object> flowUseNum = new ArrayList<>(templateApi.getMenuUseNum(0, authorize.getFlowIdList()));
        commonUseModel.setFlowList(flowUseNum);
        list.add(commonUseModel);
        //9-我的收藏
        List<Object> favoritesMenuNum = new ArrayList<>();
        List<ModuleModel> favoritesList = moduleDataService.getFavoritesList(moduleList);
        for (ModuleModel item : favoritesList) {
            MenuModel menuModel = JsonUtil.getJsonToBean(item, MenuModel.class);
            setMenuModel(menuModel, sysMap, favoritesMenuNum);
        }
        HomeModel favoritesModel = HomeModel.builder().code(NO9_FAVORITES).enable(1).appList(favoritesMenuNum).flowEnabled(isFlowEnabled).build();
        SystemEntity infoByEnCode = systemService.getInfoByEnCode(RequestContext.getAppCode());
        TemplatePagination pagination = new TemplatePagination();
        pagination.setPageSize(10000);
        pagination.setCategory("commonFlow");
        pagination.setIsLaunch(1);
        pagination.setSystemId(infoByEnCode.getId());
        List<Object> favoritesNum = new ArrayList<>(templateApi.getCommonList(pagination));
        favoritesModel.setFlowList(favoritesNum);
        list.add(favoritesModel);
        return ActionResult.success(list);
    }
 
 
    /**
     * 菜单信息补充
     *
     * @param menuModel
     * @param sysMap
     * @param list
     */
    private static void setMenuModel(MenuModel menuModel, Map<String, SystemEntity> sysMap, List<Object> list) {
        JSONObject propertyJSON = JSONObject.parseObject(Optional.of(menuModel.getPropertyJson()).orElse("{}"));
        menuModel.setIconBackground(propertyJSON.getString("iconBackgroundColor"));
        List<String> appComModule = new ArrayList<>();
        appComModule.addAll(JnpfConst.APP_CONFIG_MODULE);
        appComModule.addAll(JnpfConst.ONLINE_DEV_MODULE);
        SystemEntity systemEntity = sysMap.get(menuModel.getSystemId());
        if (systemEntity != null) {
            menuModel.setSystemName(systemEntity.getFullName());
            if (StringUtil.isEmpty(menuModel.getSystemCode())) {
                menuModel.setSystemCode(systemEntity.getEnCode());
            }
        }
        if (appComModule.contains(menuModel.getEnCode())) {
            menuModel.setIsBackend(1);
        } else {
            menuModel.setIsBackend(0);
        }
        list.add(menuModel);
    }
 
    /**
     * 柱形图数据
     *
     * @param list
     */
    private void setColumnChart(List<HomeModel> list) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<String> dateList = IntStream.rangeClosed(0, 30)
                .mapToObj(i -> LocalDate.now().minusDays(30 - i))
                .map(formatter::format)
                .collect(Collectors.toList());
        List<String> category = dateList.stream().map(t -> t.substring(5)).collect(Collectors.toList());
        List<SystemEntity> allSysList = systemService.getList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, List<SystemEntity>> createDate = allSysList.stream()
                .collect(Collectors.groupingBy(obj -> sdf.format(obj.getCreatorTime())));
        Map<String, List<SystemEntity>> updateDate = allSysList.stream()
                .collect(Collectors.groupingBy(obj -> Optional.ofNullable(obj.getLastModifyTime()).map(sdf::format).orElse("N/A")));
        List<Integer> createData = new ArrayList<>();
        List<Integer> updateData = new ArrayList<>();
        for (String t : dateList) {
            createData.add(CollectionUtil.isEmpty(createDate.get(t)) ? 0 : createDate.get(t).size());
            updateData.add(CollectionUtil.isEmpty(updateDate.get(t)) ? 0 : updateDate.get(t).size());
        }
        List<ChartModel> listChart = new ArrayList<>();
        ChartModel createModel = new ChartModel();
        createModel.setName("创建");
        createModel.setData(createData);
        createModel.setCategory(category);
        listChart.add(createModel);
        ChartModel updateModel = new ChartModel();
        updateModel.setName("修改");
        updateModel.setData(updateData);
        updateModel.setCategory(category);
        listChart.add(updateModel);
        list.add(new HomeModel(NO12_COLUMN_CHART, 1, listChart));
    }
 
    /**
     * 添加流程数据
     *
     * @param moduleList
     * @param list
     */
    private void setFlow(List<ModuleModel> moduleList, List<HomeModel> list) {
        List<TeamWorkModel> flowData = new ArrayList<>();
        List<ModuleModel> flowMenu = moduleList.stream().filter(t -> JnpfConst.MODULE_CODE.contains(t.getEnCode())
                && !JnpfConst.WORK_FLOW_CODE.equals(t.getEnCode())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(flowMenu)) {
            TaskTo flowModel = new TaskTo();
            flowModel.setModuleList(moduleList);
            TaskTo taskTo = taskApi.getFlowTodoCount(flowModel);
            for (ModuleModel menu : flowMenu) {
                TeamWorkModel model = JsonUtil.getJsonToBean(menu, TeamWorkModel.class);
                switch (menu.getEnCode()) {
                    case JnpfConst.WORK_FLOWLAUNCH:
                        model.setCount(taskTo.getFlowLaunch());
                        break;
                    case JnpfConst.WORK_FLOWSIGN:
                        model.setCount(taskTo.getFlowToSign());
                        break;
                    case JnpfConst.WORK_FLOWTODO:
                        model.setCount(taskTo.getFlowTodo());
                        break;
                    case JnpfConst.WORK_FLOWDOING:
                        model.setCount(taskTo.getFlowDoing());
                        break;
                    case JnpfConst.WORK_FLOWDONE:
                        model.setCount(taskTo.getFlowDone());
                        break;
                    case JnpfConst.WORK_FLOWCIRCULATE:
                        model.setCount(taskTo.getFlowCirculate());
                        break;
                    default:
                        break;
                }
                flowData.add(model);
            }
            list.add(new HomeModel(NO3_WORK_FLOW, 1, flowData));
        } else {
            list.add(new HomeModel(NO3_WORK_FLOW, 0, flowData));
        }
    }
 
    /**
     * 添加协作数据
     *
     * @param moduleList
     * @param list
     */
    private void setTeamwork(List<ModuleModel> moduleList, List<HomeModel> list) {
        List<TeamWorkModel> teamworkData = new ArrayList<>();
        List<ModuleModel> flowMenu = moduleList.stream().filter(t -> JnpfConst.TEAMWORK_MODULE.contains(t.getEnCode())
                && !JnpfConst.TEAMWORK_CODE.equals(t.getEnCode())).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(flowMenu)) {
            teamworkData.addAll(JsonUtil.getJsonToList(flowMenu, TeamWorkModel.class));
            list.add(new HomeModel(NO4_TEAMWORK, 1, teamworkData));
        } else {
            list.add(new HomeModel(NO4_TEAMWORK, 0, teamworkData));
        }
    }
}