tuqin
6 天以前 6e43a9e4daa56a07fe605d6bec1370f36bd5fbfc
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
package cn.iocoder.yudao.module.mes.controller.admin.safety;
 
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.statistics.MesSafetyHazardExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.statistics.MesSafetyStatisticsReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.statistics.MesSafetyStatisticsRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkshopDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.MesSafetyHazardDO;
import cn.iocoder.yudao.module.mes.dal.mysql.md.workstation.MesMdWorkshopMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.safety.MesSafetyHazardMapper;
import cn.iocoder.yudao.module.mes.service.safety.MesSafetyStatisticsService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 
/**
 * 安全生产 - 统计分析 Controller
 *
 * @author 超级管理员
 */
@Tag(name = "管理后台 - 安全生产-统计分析")
@RestController
@RequestMapping("/mes/safety/statistics")
@Validated
public class MesSafetyStatisticsController {
 
    @Resource
    private MesSafetyStatisticsService statisticsService;
 
    @Resource
    private MesSafetyHazardMapper hazardMapper;
 
    @Resource
    private MesMdWorkshopMapper workshopMapper;
 
    @Resource
    private AdminUserApi adminUserApi;
 
    /** 隐患等级文本 */
    private static final String[] HAZARD_LEVEL_NAMES = {null, "一般隐患", "重大隐患"};
    /** 隐患来源文本 */
    private static final String[] HAZARD_SOURCE_NAMES = {null, "日常排查", "专项检查", "上级检查", "员工上报", "其他"};
 
    @GetMapping("/report")
    @Operation(summary = "获得安全生产统计报表")
    @PreAuthorize("@ss.hasPermission('mes:safety-statistics:query')")
    public CommonResult<MesSafetyStatisticsRespVO> getReport(@Valid MesSafetyStatisticsReqVO reqVO) {
        return success(statisticsService.getStatistics(reqVO));
    }
 
    @GetMapping("/export-hazard")
    @Operation(summary = "导出隐患明细报表")
    @PreAuthorize("@ss.hasPermission('mes:safety-statistics:export')")
    public void exportHazard(@Valid MesSafetyStatisticsReqVO reqVO,
                             HttpServletResponse response) throws IOException {
        List<MesSafetyHazardDO> list = hazardMapper.selectAllList().stream()
                .filter(h -> inRange(h.getCreateTime(), reqVO))
                .filter(h -> reqVO.getWorkshopId() == null
                        || Objects.equals(h.getWorkshopId(), reqVO.getWorkshopId()))
                .collect(Collectors.toList());
 
        // 字典与关联名称在导出前转为中文,业务人员可直接查看
        Map<Long, String> workshopNameMap = workshopMapper.selectList().stream()
                .collect(Collectors.toMap(MesMdWorkshopDO::getId, MesMdWorkshopDO::getName, (a, b) -> a));
        Set<Long> userIds = list.stream().map(MesSafetyHazardDO::getChargeUserId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<Long, AdminUserRespDTO> userMap = userIds.isEmpty()
                ? Collections.emptyMap() : adminUserApi.getUserMap(userIds);
 
        LocalDate today = LocalDate.now();
        List<MesSafetyHazardExcelVO> excelList = new ArrayList<>(list.size());
        for (MesSafetyHazardDO hazard : list) {
            MesSafetyHazardExcelVO vo = BeanUtils.toBean(hazard, MesSafetyHazardExcelVO.class);
            vo.setHazardLevelName(dictName(HAZARD_LEVEL_NAMES, hazard.getHazardLevel()));
            vo.setSourceName(dictName(HAZARD_SOURCE_NAMES, hazard.getSource()));
            vo.setWorkshopName(workshopNameMap.get(hazard.getWorkshopId()));
            vo.setStatusName(hazardStatusName(hazard.getStatus()));
            AdminUserRespDTO charge = hazard.getChargeUserId() == null
                    ? null : userMap.get(hazard.getChargeUserId());
            vo.setChargeUserName(charge == null ? null : charge.getNickname());
            boolean overdue = hazard.getDeadline() != null
                    && !MesSafetyHazardDO.STATUS_CLOSED.equals(hazard.getStatus())
                    && hazard.getDeadline().isBefore(today);
            vo.setOverdueText(overdue ? "是" : "否");
            excelList.add(vo);
        }
 
        ExcelUtils.write(response, "隐患明细.xlsx", "隐患明细", MesSafetyHazardExcelVO.class, excelList);
    }
 
    // ==================== 工具 ====================
 
    private String dictName(String[] names, Integer value) {
        if (value == null || value < 0 || value >= names.length) {
            return null;
        }
        return names[value];
    }
 
    private String hazardStatusName(Integer status) {
        if (status == null) {
            return null;
        }
        return switch (status) {
            case 0 -> "待整改";
            case 10 -> "整改中";
            case 20 -> "待验收";
            case 30 -> "已闭环";
            default -> "未知";
        };
    }
 
    private boolean inRange(LocalDateTime createTime, MesSafetyStatisticsReqVO reqVO) {
        if (reqVO.getStartDate() == null && reqVO.getEndDate() == null) {
            return true;
        }
        if (createTime == null) {
            return false;
        }
        if (reqVO.getStartDate() != null && createTime.isBefore(reqVO.getStartDate().atStartOfDay())) {
            return false;
        }
        return reqVO.getEndDate() == null || !createTime.isAfter(reqVO.getEndDate().atTime(23, 59, 59));
    }
 
}