lizijie
2021-08-12 4784ba9b5fe0d2c9ada694834ac6b0d0b7474b89
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.SpecialDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-08-11
 */
@Service
public class SpecialDeviceServiceImpl extends ServiceImpl<SpecialDeviceMapper, SpecialDevice> implements SpecialDeviceService {
 
    @Autowired(required = false)
    private SpecialDeviceMapper specialDeviceMapper;
 
    @Autowired(required = false)
    private ManageAccountMapper manageAccountMapper;
 
    @Autowired(required = false)
    private VersionMapper versionMapper;
 
    @Autowired(required = false)
    private SysDictDataMapper sysDictDataMapper;
 
    @Autowired(required = false)
    private SpecialDeviceHistoryMapper specialDeviceHistoryMapper;
 
    @Autowired(required = false)
    private OrganizationMapper organizationMapper;
 
    @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<SpecialDevice> page = new Page<>(current,size);
        QueryWrapper<SpecialDevice> wrapper_Condition = new QueryWrapper<>();
        wrapper_Condition.eq("is_delete",Constants.NOT_DELETE);
        if (!ObjectUtils.isEmpty(map.get("organization_id"))){
            wrapper_Condition.like("organization_id",map.get("organization_id").toString());
        }
        if (!ObjectUtils.isEmpty(map.get("keyword"))){
            wrapper_Condition.like("name",map.get("keyword").toString()).or().like("mac",map.get("keyword").toString());
        }
        wrapper_Condition.orderByDesc("create_time");
        Page resultPage = specialDeviceMapper.selectPage(page,wrapper_Condition);
        List<SpecialDevice> specialDevices = resultPage.getRecords();
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Map<String,Object>> specialDeviceList = new ArrayList<>();
        for (SpecialDevice specialDevice:specialDevices) {
            Map specialDeviceMap = JSON.parseObject(JSON.toJSONString(specialDevice),Map.class);
            String createTime = SDF.format(specialDevice.getCreateTime());
            String updateTime = SDF.format(specialDevice.getUpdateTime());
            specialDeviceMap.put("createTime",createTime);
            specialDeviceMap.put("updateTime",updateTime);
            List<Map<String,Object>> operateList = new ArrayList<>();
            if (!ObjectUtils.isEmpty(specialDevice.getOperateIds()) && specialDevice.getOperateIds()!=null){
                String operateIds = specialDevice.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);
                    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);
                    }
                }
            }
            specialDeviceMap.put("operates",operateList);
            Map<String,Object> deviceVersionMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDevice.getDeviceVersionId()) && specialDevice.getDeviceVersionId()!=null && !"".equals(specialDevice.getDeviceVersionId())){
                int versionId = Integer.parseInt(specialDevice.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());
                }
            }
            specialDeviceMap.put("deviceVersion",deviceVersionMap);
            Map<String,Object> specialTypeMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDevice.getSpecialType()) && specialDevice.getSpecialType()!=null && !"".equals(specialDevice.getSpecialType())){
                int specialTypeId = Integer.parseInt(specialDevice.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("name",sysDictData.getDataValue());
                }
            }
            specialDeviceMap.put("specialType",specialTypeMap);
            Map<String,Object> organizationMap = new HashMap<>();
            if (!ObjectUtils.isEmpty(specialDevice.getOrganizationId()) && specialDevice.getOrganizationId()!=null && !"".equals(specialDevice.getOrganizationId())){
                int organizationId = Integer.parseInt(specialDevice.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());
                }
            }
            specialDeviceMap.put("organazation",organizationMap);
            List<Map<String,Object>> organizationList = new ArrayList<>();
            QueryWrapper<SpecialDeviceHistory> wapper_specialDeviceHistory = new QueryWrapper<>();
            wapper_specialDeviceHistory.eq("is_delete",Constants.NOT_DELETE);
            wapper_specialDeviceHistory.eq("mac",specialDevice.getMac());
            List<SpecialDeviceHistory> specialDeviceHistories = specialDeviceHistoryMapper.selectList(wapper_specialDeviceHistory);
            if (!ObjectUtils.isEmpty(specialDeviceHistories)){
                List organizationIdList = new ArrayList();
                for (SpecialDeviceHistory specialDeviceHistory:specialDeviceHistories) {
                    organizationIdList.add(specialDeviceHistory.getOrganizationId());
                }
                QueryWrapper<Organization> wapper_organizationHistory = new QueryWrapper<>();
                wapper_organizationHistory.eq("is_delete",Constants.NOT_DELETE);
                wapper_organizationHistory.in("id",organizationIdList);
                List<Organization> organizations = organizationMapper.selectList(wapper_organizationHistory);
                for (Organization organization:organizations) {
                    Map<String,Object> organizationHistoryMap = new HashMap<>();
                    organizationHistoryMap.put("id",organization.getId());
                    organizationHistoryMap.put("name",organization.getName());
                    organizationList.add(organizationHistoryMap);
                }
            }
            specialDeviceMap.put("organizations",organizationList);
            specialDeviceList.add(specialDeviceMap);
        }
        resultMap.put("specialDevices",specialDeviceList);
        int totalNumber = specialDevices.size();
        resultMap.put("totalNumber",totalNumber);
        resultMap.put("current",current);
        int totalPageNumber = totalNumber/size;
        if(totalNumber%size != 0){
            totalPageNumber += 1;
        }
        resultMap.put("totalPageNumber",totalPageNumber);
        return resultMap;
    }
}