swb
2024-10-21 0aea8bd18daaaf91e622fdcfdd81dbb2e1b4860c
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
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.moral.api.entity.Device;
import com.moral.api.entity.OrganizationLayout;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.OrganizationLayoutMapper;
import com.moral.api.service.OrganizationLayoutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-08-23
 */
@Service
public class OrganizationLayoutServiceImpl extends ServiceImpl<OrganizationLayoutMapper, OrganizationLayout> implements OrganizationLayoutService {
 
    @Autowired(required = false)
    private OrganizationLayoutMapper organizationLayoutMapper;
 
    @Autowired(required = false)
    private DeviceMapper deviceMapper;
 
    @Override
    public List<Map<String, Object>> getData(Map map) {
        int organization_id = Integer.parseInt(map.get("organization_id").toString());
        int version_id = Integer.parseInt(map.get("version_id").toString());
        QueryWrapper<OrganizationLayout> wrapper_organizationLayout = new QueryWrapper<>();
        wrapper_organizationLayout.eq("organization_id",organization_id);
        wrapper_organizationLayout.eq("version_id",version_id);
        wrapper_organizationLayout.eq("is_delete",Constants.NOT_DELETE);
        List<Map<String,Object>> organizationLayouts = new ArrayList<>();
        organizationLayouts = organizationLayoutMapper.selectMaps(wrapper_organizationLayout);
        return organizationLayouts;
    }
 
    @Transactional
    @Override
    public void setUp(Map map, List<OrganizationLayout> list) {
        int organization_id = Integer.parseInt(map.get("organization_id").toString());
        int version_id = Integer.parseInt(map.get("version_id").toString());
        QueryWrapper<OrganizationLayout> wrapper_organizationLayout = new QueryWrapper<>();
        wrapper_organizationLayout.eq("organization_id",organization_id);
        wrapper_organizationLayout.eq("version_id",version_id);
        wrapper_organizationLayout.eq("is_delete",Constants.NOT_DELETE);
        List<OrganizationLayout> organizationLayouts = organizationLayoutMapper.selectList(wrapper_organizationLayout);
        //int delCount = organizationLayoutMapper.delete(wrapper_organizationLayout);
        OrganizationLayout organizationLayout_update = new OrganizationLayout();
        organizationLayout_update.setIsDelete(Constants.DELETE);
        int delCount = organizationLayoutMapper.update(organizationLayout_update,wrapper_organizationLayout);
        if (delCount>0){
            //操作插入日志
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String content = "页面布局表删除了组织id:"+organization_id+"、型号id:"+version_id+";"+"id:";
            for (OrganizationLayout organizationLayout:organizationLayouts) {
                content = content +organizationLayout.getId()+" ";
            }
            content = content+";";
            LogUtils.saveOperationForManage(request,content,Constants.DELETE_OPERATE_TYPE);
        }
        if (!ObjectUtils.isEmpty(list) && list.size()>0){
            organizationLayoutMapper.insertList(list);
            QueryWrapper<OrganizationLayout> wrapper_organizationLayoutIns = new QueryWrapper<>();
            wrapper_organizationLayoutIns.eq("organization_id",organization_id);
            wrapper_organizationLayoutIns.eq("version_id",version_id);
            List<OrganizationLayout> organizationLayoutList = organizationLayoutMapper.selectList(wrapper_organizationLayout);
            //操作插入日志
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String contentInsert = "页面布局表插入了组织id:"+organization_id+"、型号id:"+version_id+";"+"id:";
            for (OrganizationLayout organizationLayout:organizationLayoutList) {
                contentInsert = contentInsert + organizationLayout.getId()+" ";
            }
            contentInsert = contentInsert+";";
            LogUtils.saveOperationForManage(request,contentInsert,Constants.INSERT_OPERATE_TYPE);
        }
    }
 
    @Transactional
    @Override
    public Map<String, Object> getLayoutByMac(String mac) {
        QueryWrapper<Device> wrapper_device = new QueryWrapper<>();
        wrapper_device.eq("is_delete",Constants.NOT_DELETE);
        wrapper_device.eq("mac",mac);
        Device device = deviceMapper.selectOne(wrapper_device);
        if (ObjectUtils.isEmpty(device)){
            return null;
        }
        int organizationId = device.getOrganizationId();
        int versionId = device.getDeviceVersionId();
        QueryWrapper<OrganizationLayout> wrapper_organizationLayout = new QueryWrapper<>();
        wrapper_organizationLayout.eq("organization_id",organizationId);
        wrapper_organizationLayout.eq("version_id",versionId);
        wrapper_organizationLayout.eq("is_delete",Constants.NOT_DELETE);
        List<OrganizationLayout> organizationLayouts = organizationLayoutMapper.selectList(wrapper_organizationLayout);
        if (ObjectUtils.isEmpty(organizationLayouts) || organizationLayouts.size()==0){
            return null;
        }
        Map<String, Object> resultMap = new HashMap<>();
        List<OrganizationLayout> chartSensorKeyList = new ArrayList<>();
        List<OrganizationLayout> coreMonitorItemsList = new ArrayList<>();
        List<OrganizationLayout> defaultMonitorItemsList = new ArrayList<>();
        List<OrganizationLayout> fixedMonitorItemsList = new ArrayList<>();
        for (OrganizationLayout organizationgLayout:organizationLayouts) {
            if (organizationgLayout.getPosition().equals("chartSensorKey")){
                chartSensorKeyList.add(organizationgLayout);
            }
            if (organizationgLayout.getPosition().equals("coreMonitorItems")){
                coreMonitorItemsList.add(organizationgLayout);
            }
            if (organizationgLayout.getPosition().equals("defaultMonitorItems")){
                defaultMonitorItemsList.add(organizationgLayout);
            }
            if (organizationgLayout.getPosition().equals("fixedMonitorItems")){
                fixedMonitorItemsList.add(organizationgLayout);
            }
        }
        chartSensorKeyList = sort(chartSensorKeyList);
        coreMonitorItemsList = sort(coreMonitorItemsList);
        defaultMonitorItemsList = sort(defaultMonitorItemsList);
        fixedMonitorItemsList = sort(fixedMonitorItemsList);
        resultMap.put("chartSensorKey",chartSensorKeyList);
        resultMap.put("coreMonitorItems",coreMonitorItemsList);
        resultMap.put("defaultMonitorItems",defaultMonitorItemsList);
        resultMap.put("fixedMonitorItems",fixedMonitorItemsList);
        return resultMap;
    }
 
    private List<OrganizationLayout> sort(List<OrganizationLayout> list){
        if (list.size() < 0){
            return list;
        }
        OrganizationLayout temp = null;
        int j;
        for (int i = 1; i<list.size(); i++){
            temp = list.get(i);
            for (j=i-1; j>=0; j--){
                if (list.get(j).getPositionIndex()>temp.getPositionIndex()){
                    list.set(j+1,list.get(j));
                }else {
                    break;
                }
            }
            System.out.println(list.get(0));
            list.set(j+1,temp);
        }
        return list;
    }
}