whyclxw
2025-01-20 364adf83c47366a5b01dfe005fd0eed11f9346e6
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.corba.se.impl.protocol.giopmsgheaders.TargetAddress;
import com.whyc.dto.Response;
import com.whyc.mapper.AuthiruzeInfMapper;
import com.whyc.mapper.LockInfMapper;
import com.whyc.pojo.db_area.AreaUser;
import com.whyc.pojo.db_area.AuthiruzeInf;
import com.whyc.pojo.db_area.LockInf;
import com.whyc.pojo.db_user.UserInf;
import com.whyc.util.ActionUtil;
import com.whyc.util.PageInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class LockInfService {
    @Autowired(required = false)
    private LockInfMapper mapper;
 
    @Autowired(required = false)
    private AreaInfService areaInfService;
 
    @Autowired(required = false)
    private AuthiruzeInfMapper authMapper;
 
    @Autowired
    private ProcessSurveyService processSurveyService;
    //添加锁
    public Response addLock(Integer num, LockInf lockInf) {
        if(num==null){
            num=1;
        }
        List list=new ArrayList();
        //获取当前最大的锁
        int lockId=getMaxLockId()+1;
        for(int i=0;i<num;i++){
            LockInf linf=new LockInf();
            linf.setLockId(lockId+i);
            linf.setAreaId(lockInf.getAreaId());
            if(num>1){
                linf.setLockName(lockInf.getLockName()+(i+1));
            }else{
                linf.setLockName(lockInf.getLockName());
            }
            linf.setLockType(lockInf.getLockType());
            linf.setLockState(-1);
            if(lockInf.getLockAddress()!=null){
                linf.setLockAddress(lockInf.getLockAddress());
            }else{
                linf.setLockAddress("");
            }
            linf.setLockPath("");
            linf.setLatitude(lockInf.getLatitude());
            linf.setLongitude(lockInf.getLongitude());
            linf.setLockMac("FFFF");
            linf.setScreenBoxType(lockInf.getScreenBoxType());
            linf.setScreenBoxProduct(lockInf.getScreenBoxProduct());
            linf.setScreenFlag(lockInf.getScreenFlag());
            linf.setLockOnline(0);
            linf.setLastUpdateTime(new Date());
            list.add(linf);
        }
        mapper.insertBatchSomeColumn(list);
        processSurveyService.setUpThreadRestart();
        return new Response().set(1,true);
    }
    //获取当前最大的锁
    private int getMaxLockId() {
        int keyId=mapper.getMaxLockId();
        return keyId;
    }
 
    //查询所有锁信息
    public Response getAllLockInf(String lockName, Integer lockType, Integer lockState,Integer areaId, int pageNum, int pageSize) {
        List<Integer> areaList=new ArrayList();
        areaList.add(areaId);
        areaInfService.getAllAreaId(areaId,areaList);
        PageHelper.startPage(pageNum,pageSize);
        List<LockInf> list=mapper.getAllLockInf(lockName,lockType,lockState,areaList);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null,pageInfo,"查询所有锁信息");
    }
    //删除锁
    public Response delLock(Integer lockId) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("lock_id",lockId);
        mapper.delete(wrapper);
        return new Response().set(1,true);
    }
   //修改锁
    public Response updateLock(LockInf lockInf) {
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("lock_id",lockInf.getLockId());
        if(lockInf.getAreaId()!=null){
            wrapper.set("area_id",lockInf.getAreaId());
        }
        if(lockInf.getLockName()!=null){
            wrapper.set("lock_name",lockInf.getLockName());
        }
        if(lockInf.getLockType()!=null){
            wrapper.set("lock_type",lockInf.getLockType());
        }
        if(lockInf.getScreenBoxType()!=null){
            wrapper.set("screen_box_type",lockInf.getScreenBoxType());
        }
        if(lockInf.getScreenBoxProduct()!=null){
            wrapper.set("screen_box_product",lockInf.getScreenBoxProduct());
        }
        if(lockInf.getScreenFlag()!=null){
            wrapper.set("screen_flag",lockInf.getScreenFlag());
        }
        if(lockInf.getLongitude()!=null){
            wrapper.set("longitude",lockInf.getLongitude());
        }
        if(lockInf.getLatitude()!=null){
            wrapper.set("latitude",lockInf.getLatitude());
        }
        mapper.update((LockInf) ActionUtil.objeNull,wrapper);
        return new Response().set(1,true);
    }
    //授权时查询所有蓝牙锁信息(不分页)
    public Response getLockInfAuth() {
       /* QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("lock_type",1);*/
        List<LockInf> list=mapper.selectList(null);
        return new Response().setII(1,list!=null,list,"授权时查询所有蓝牙锁信息(不分页)");
    }
    //获取区域下所有锁的状态
    public Response getLockRt(int areaId) {
        //获取获取id下所有的区域id
        List areaList=new ArrayList();
        areaList.add(areaId);
        areaInfService.getAllAreaId(areaId,areaList);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.in("area_id",areaList);
        List<LockInf> list=mapper.selectList(wrapper);
        for (LockInf linf:list) {
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("lock_id",linf.getLockId());
            List<AuthiruzeInf>  authList=authMapper.selectList(wrapper1);
            linf.setAuthList(authList);
        }
        return new Response().setII(1,list!=null,list,"获取区域下所有锁的状态");
    }
    //查询所有锁名信息(用于下拉)
    public Response getLinf() {
        List<LockInf> list=mapper.selectList(null);
        List<String> lnameList = list.stream()
                .map(LockInf::getLockName) // 提取名字
                .collect(Collectors.toList()); // 转换为列表*/
        return new Response().setII(1,list!=null,lnameList,"查询所有锁名信息(用于下拉)");
    }
    //查询区域管理员的所有区域下锁具(用于下拉)
    public Response getAreaUserLock() {
        UserInf uinf= ActionUtil.getUser();
        List areaList=areaInfService.getAllAreaUser(uinf.getUid(),uinf.getUrole());
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.in("area_id",areaList);
        wrapper.orderByAsc("num");
        List<LockInf> list=mapper.selectList(wrapper);
        return new Response().setII(1,list!=null,list,"查询所有锁名信息(用于下拉)");
    }
    //查询屏柜的全部类型(下拉)
    public Response getScreenType() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.select("distinct screen_box_type");
        List<LockInf> list=mapper.selectList(wrapper);
        List<String> typeList = list.stream()
                .map(LockInf::getScreenBoxType) // 提取名字
                .collect(Collectors.toList()); // 转换为列表*/
        return new Response().setII(1,list!=null,typeList,"查询屏柜的全部类型(下拉)");
    }
   //查询屏柜全部品牌(下拉)
    public Response getScreenProduct() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.select("distinct screen_box_product");
        List<LockInf> list=mapper.selectList(wrapper);
        List<String> typeList = list.stream()
                .map(LockInf::getScreenBoxProduct) // 提取名字
                .collect(Collectors.toList()); // 转换为列表*/
        return new Response().setII(1,list!=null,typeList,"查询屏柜全部品牌(下拉)");
    }
    //实时获取获取区域下所有锁的状态
    public Response getRealLock(Integer areaId, Integer pageNum, Integer pageSize) {
        Map<String,Object> map=new HashMap<>();
        map.put("sumLinf",0);
        map.put("onlineNum",0);
        map.put("offLineNum",0);
        map.put("openNum",0);
        map.put("closeNum",0);
        map.put("unLoadNum",0);
        List<Integer> areaList=new ArrayList();
        areaList.add(areaId);
        areaInfService.getAllAreaId(areaId,areaList);
        List<LockInf> linfs=mapper.getRealLock(areaList);
        if(linfs!=null&&linfs.size()>0){
            map.put("sumLinf",linfs.size());
            Map<Integer, List<LockInf>> onlinemap = linfs.stream().collect(Collectors.groupingBy(LockInf::getLockOnline));
            for (Integer state : onlinemap.keySet()) {
                if(state==0){
                    map.put("offLineNum", onlinemap.get(0).size());//离线
                }
                if(state==1){
                    map.put("onlineNum", onlinemap.get(1).size());//在线
                }
            }
            Map<Integer, List<LockInf>> openmap = linfs.stream().collect(Collectors.groupingBy(LockInf::getLockState));
            for (Integer open : openmap.keySet()) {
                if(open==0){
                    map.put("closeNum", openmap.get(0).size());//闭锁
                }
                if(open==1){
                    map.put("openNum", openmap.get(1).size());//开锁
                }
                if(open==-1){
                    map.put("unLoadNum", openmap.get(-1).size());//未安装
                }
            }
        }
        PageInfo pageInfo= PageInfoUtils.list2PageInfo(linfs,pageNum,pageSize);
        map.put("pageInfo",pageInfo);
        return new Response().setII(1,linfs!=null,map,"实时获取获取区域下所有锁的状态");
    }
}