危化品全生命周期管理后端
“djh”
2025-05-07 da2c594ade5d69621dd11a13bb758477e2dc079e
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
package com.gkhy.hazmat.system.service.impl;
 
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.api.CommonPage;
import com.gkhy.hazmat.common.config.IdTableNameHandler;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.utils.PageUtils;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.system.domain.HzWarning;
import com.gkhy.hazmat.system.domain.vo.HzEntryRecordVO;
import com.gkhy.hazmat.system.mapper.HzWarningMapper;
import com.gkhy.hazmat.system.service.HzWarningService;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 预警表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-08-05 14:41:40
 */
@Service
public class HzWarningServiceImpl extends ServiceImpl<HzWarningMapper, HzWarning> implements HzWarningService {
 
    @Override
    public CommonPage selectWarningList(HzWarning warning) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode()) && !currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            warning.setCompanyId(currentUser.getCompanyId());
        }
        PageUtils.startPage();
        List<HzWarning> studentList=baseMapper.selectWarningList(warning);
 
        return CommonPage.restPage(studentList);
    }
 
    @Override
    public HzWarning selectWarningById(Long warningId) {
        HzWarning warning= baseMapper.selectById(warningId);
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            return warning;
        }else if(!warning.getCompanyId().equals(currentUser.getCompanyId())){
            throw new ApiException("无权限查看其它企业数据");
        }
        return warning;
    }
 
    @Override
    public int markWarning(HzWarning warning) {
        if(warning.getId()==null||warning.getCompanyId()==null||warning.getState()==null){
            throw new ApiException("标记预警参数不正确");
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            if (!warning.getCompanyId().equals(currentUser.getCompanyId())) {
                throw new ApiException("无权限操作其它企业数据");
            }
        }
        int row=baseMapper.updateById(new HzWarning().setId(warning.getId()).setState(warning.getState()).setHandleTime(LocalDateTime.now()).setUpdateBy(currentUser.getUsername()));
        if(row<1){
            throw new ApiException("标记预警信息失败");
        }
        return row;
    }
 
    @Override
    public int deleteWarningById(Long warningId) {
        HzWarning warning=baseMapper.selectById(warningId);
        if(warning==null){
            throw new ApiException("预警信息不存在");
        }
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
            if (!warning.getCompanyId().equals(currentUser.getCompanyId())) {
                throw new ApiException("无权限操作其它企业数据");
            }
        }
        int row=baseMapper.deleteById(warningId);
        return row;
    }
 
    @Override
    public Integer selectWarningCount(Long companyId) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
            throw new ApiException("管理员不能操作");
        }
        if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())){
            return baseMapper.selectWarningCount(companyId);
        }
        return baseMapper.selectWarningCount(currentUser.getCompanyId());
    }
 
    @Override
    public List<HzEntryRecordVO> dailywarningCount() {
        // 获取当前时间并清零时分秒
        DateTime now = DateUtil.date().setField(Calendar.HOUR_OF_DAY, 0)
                .setField(Calendar.MINUTE, 0)
                .setField(Calendar.SECOND, 0)
                .setField(Calendar.MILLISECOND, 0);
 
        // 设置时间范围:30天前00:00:00 到 昨天23:59:59
        DateTime startTime = DateUtil.offsetDay(now, -30);
        DateTime endTime = DateUtil.endOfDay(DateUtil.offsetDay(now, -1));
 
        // 格式化成数据库参数
        String startDate = DateUtil.format(startTime, DatePattern.NORM_DATETIME_FORMAT);
        String endDate = DateUtil.format(endTime, DatePattern.NORM_DATETIME_FORMAT);
 
        // 生成30天的日期列表(格式:dd)
        List<HzEntryRecordVO> dailyEntryList = new ArrayList<>();
        DateTime currentDay = startTime;
        while (!currentDay.isAfter(endTime)) {
            String dayStr = DateUtil.format(currentDay, "MM-dd"); // 两位天数
            HzEntryRecordVO vo = new HzEntryRecordVO();
            vo.setDay(dayStr);
            vo.setCount(0);
            dailyEntryList.add(vo);
            currentDay = DateUtil.offsetDay(currentDay, 1);
        }
 
        // 查询数据库按天统计(需SQL返回dd格式)
        List<HzEntryRecordVO> dbResults = baseMapper.dailyWarningStatic(startDate, endDate);
        IdTableNameHandler.removeCurrentId();
 
        // 合并结果到初始化列表
        if (!dbResults.isEmpty()) {
            Map<String, HzEntryRecordVO> resultMap = dbResults.stream()
                    .collect(Collectors.toMap(HzEntryRecordVO::getDay, item -> item));
            for (HzEntryRecordVO dailyVO : dailyEntryList) {
                HzEntryRecordVO matched = resultMap.get(dailyVO.getDay());
                if (matched != null) {
                    dailyVO.setCount(matched.getCount());
                }
            }
        }
 
        return dailyEntryList;
    }
}