jinpengyong
2023-08-24 9655ea9ce8cdd671a766f87a0885b0fdff7b89c9
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.*;
import com.moral.api.mapper.*;
import com.moral.api.service.SpecialDeviceHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-08-11
 */
@Service
public class SpecialDeviceHistoryServiceImpl extends ServiceImpl<SpecialDeviceHistoryMapper, SpecialDeviceHistory> implements SpecialDeviceHistoryService {
 
    @Autowired(required = false)
    private SpecialDeviceHistoryMapper specialDeviceHistoryMapper;
 
    @Autowired(required = false)
    private ManageAccountMapper manageAccountMapper;
 
    @Autowired(required = false)
    private VersionMapper versionMapper;
 
    @Autowired(required = false)
    private SysDictDataMapper sysDictDataMapper;
 
    @Autowired(required = false)
    private OrganizationMapper organizationMapper;
 
    @Autowired(required = false)
    private GovMonitorPointMapper govMonitorPointMapper;
 
 
    @Override
    public Map<String, Object> getDataByCondition(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        if (!map.containsKey("current")||!map.containsKey("size")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        int current = Integer.parseInt(map.get("current").toString());
        int size = Integer.parseInt(map.get("size").toString());
        Page<SpecialDeviceHistory> page = new Page<>(current,size);
        QueryWrapper<SpecialDeviceHistory> wrapper_Condition = new QueryWrapper<>();
        wrapper_Condition.eq("is_delete",Constants.NOT_DELETE);
        if (!ObjectUtils.isEmpty(map.get("organization_id"))){
            wrapper_Condition.eq("organization_id",map.get("organization_id").toString());
        }
        if (!ObjectUtils.isEmpty(map.get("keyword"))){
            wrapper_Condition.and(wc -> wc.like("name",map.get("keyword").toString()).or().like("mac",map.get("keyword").toString()));
            //wrapper_Condition.like("name",map.get("keyword").toString()).or().like("mac",map.get("keyword").toString());
        }
        wrapper_Condition.orderByDesc("create_time");
        Page resultPage = specialDeviceHistoryMapper.selectPage(page,wrapper_Condition);
        int totalNumber = specialDeviceHistoryMapper.selectCount(wrapper_Condition);
        List<SpecialDeviceHistory> specialDeviceHistories = resultPage.getRecords();
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String,Object>> specialDeviceList = new ArrayList<>();
        for (SpecialDeviceHistory specialDeviceHistory:specialDeviceHistories) {
            Map specialDeviceHistoryMap = JSON.parseObject(JSON.toJSONString(specialDeviceHistory),Map.class);
            String createTime = SDF.format(specialDeviceHistory.getCreateTime());
            String updateTime = SDF.format(specialDeviceHistory.getUpdateTime());
            specialDeviceHistoryMap.put("createTime",createTime);
            specialDeviceHistoryMap.put("updateTime",updateTime);
            Map<String,Object> govMonitorPointMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDeviceHistory.getGuid()) && specialDeviceHistory.getGuid()!=null && !"".equals(specialDeviceHistory.getGuid())){
                String  guid = specialDeviceHistory.getGuid().toString();
                QueryWrapper<GovMonitorPoint> wapper_govMonitorPoint = new QueryWrapper<>();
                wapper_govMonitorPoint.eq("is_delete",Constants.NOT_DELETE);
                wapper_govMonitorPoint.eq("guid",guid);
                GovMonitorPoint govMonitorPoint = govMonitorPointMapper.selectOne(wapper_govMonitorPoint);
                if (!ObjectUtils.isEmpty(govMonitorPoint)){
                    govMonitorPointMap.put("id",govMonitorPoint.getId());
                    govMonitorPointMap.put("guid",govMonitorPoint.getGuid());
                    govMonitorPointMap.put("name",govMonitorPoint.getName());
                }
            }
            specialDeviceHistoryMap.put("govMonitorPoint",govMonitorPointMap);
            List<Map<String,Object>> operateList = new ArrayList<>();
            if (!ObjectUtils.isEmpty(specialDeviceHistory.getOperateIds()) && specialDeviceHistory.getOperateIds()!=null){
                String operateIds = specialDeviceHistory.getOperateIds();
                String[] operateIdArr = operateIds.split(",");
                if (operateIdArr.length>0){
                    List<Integer> operateIdList = new ArrayList<>();
                    for (int i = 0; i < operateIdArr.length; i++){
                        operateIdList.add(Integer.parseInt(operateIdArr[i]));
                    }
                    QueryWrapper<ManageAccount> wapper_manageAccount = new QueryWrapper<>();
                    wapper_manageAccount.eq("is_delete",Constants.NOT_DELETE);
                    wapper_manageAccount.in("id",operateIdList);
                    wapper_manageAccount.select("id", "user_name");
                    List<ManageAccount> manageAccounts = manageAccountMapper.selectList(wapper_manageAccount);
                    for (ManageAccount manageAccount:manageAccounts) {
                        Map<String,Object> operateMap = new HashMap<>();
                        operateMap.put("id",manageAccount.getId());
                        operateMap.put("name",manageAccount.getUserName());
                        operateList.add(operateMap);
                    }
                }
            }
            specialDeviceHistoryMap.put("operates",operateList);
            Map<String,Object> deviceVersionMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDeviceHistory.getDeviceVersionId()) && specialDeviceHistory.getDeviceVersionId()!=null && !"".equals(specialDeviceHistory.getDeviceVersionId())){
                int versionId = Integer.parseInt(specialDeviceHistory.getDeviceVersionId().toString());
                QueryWrapper<Version> wapper_version = new QueryWrapper<>();
                wapper_version.eq("is_delete",Constants.NOT_DELETE);
                wapper_version.eq("id",versionId);
                Version version = versionMapper.selectOne(wapper_version);
                if (!ObjectUtils.isEmpty(version)){
                    deviceVersionMap.put("id",version.getId());
                    deviceVersionMap.put("name",version.getName());
                }
            }
            specialDeviceHistoryMap.put("version",deviceVersionMap);
            Map<String,Object> specialTypeMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDeviceHistory.getSpecialType()) && specialDeviceHistory.getSpecialType()!=null && !"".equals(specialDeviceHistory.getSpecialType())){
                int specialTypeId = Integer.parseInt(specialDeviceHistory.getSpecialType().toString());
                QueryWrapper<SysDictData> wapper_sysDictData = new QueryWrapper<>();
                wapper_sysDictData.eq("is_delete",Constants.NOT_DELETE);
                wapper_sysDictData.eq("dict_type_id",27);
                wapper_sysDictData.eq("dataKey",specialTypeId);
                SysDictData sysDictData = sysDictDataMapper.selectOne(wapper_sysDictData);
                if (!ObjectUtils.isEmpty(sysDictData)){
                    specialTypeMap.put("id",sysDictData.getId());
                    specialTypeMap.put("dataKey",sysDictData.getDataKey());
                    specialTypeMap.put("name",sysDictData.getDataValue());
                }
            }
            specialDeviceHistoryMap.put("specialType",specialTypeMap);
            Map<String,Object> organizationMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDeviceHistory.getOrganizationId()) && specialDeviceHistory.getOrganizationId()!=null && !"".equals(specialDeviceHistory.getOrganizationId())){
                int organizationId = Integer.parseInt(specialDeviceHistory.getOrganizationId().toString());
                QueryWrapper<Organization> wapper_organization = new QueryWrapper<>();
                wapper_organization.eq("is_delete",Constants.NOT_DELETE);
                wapper_organization.eq("id",organizationId);
                Organization organization = organizationMapper.selectOne(wapper_organization);
                if (!ObjectUtils.isEmpty(organization)){
                    organizationMap.put("id",organization.getId());
                    organizationMap.put("name",organization.getName());
                }
            }
            specialDeviceHistoryMap.put("organization",organizationMap);
            specialDeviceList.add(specialDeviceHistoryMap);
        }
        resultMap.put("specialDevices",specialDeviceList);
        resultMap.put("totalNumber",totalNumber);
        resultMap.put("current",current);
        int totalPageNumber = totalNumber/size;
        if(totalNumber%size != 0){
            totalPageNumber += 1;
        }
        resultMap.put("totalPageNumber",totalPageNumber);
        return resultMap;
    }
 
    @Override
    public void delete(Integer specialDeviceHistoryId) {
        SpecialDeviceHistory specialDeviceHistory = specialDeviceHistoryMapper.selectById(specialDeviceHistoryId);
        UpdateWrapper<SpecialDeviceHistory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",specialDeviceHistoryId).set("is_delete",Constants.DELETE);
        specialDeviceHistoryMapper.update(null,updateWrapper);
        String mac = specialDeviceHistory.getMac();
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("删除了历史特殊设备:").append(specialDeviceHistory.getName()).append(";").append("mac:").append(mac);
        LogUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE);
    }
}