whyclxw
2025-04-21 3a6d4de77253bb86aed3383f9b9c54be5c25752c
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package com.whyc.service;
 
import com.baomidou.mybatisplus.annotation.TableField;
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.whyc.dto.LockIdDto;
import com.whyc.dto.LockInfDto;
import com.whyc.dto.RealDto;
import com.whyc.dto.Response;
import com.whyc.mapper.*;
import com.whyc.pojo.plus_inf.LockAddress;
import com.whyc.pojo.plus_inf.LockInf;
import com.whyc.pojo.plus_inf.StationInf;
import com.whyc.pojo.plus_lock_ram.LockIdcard;
import com.whyc.pojo.plus_user.Baojigroup;
import com.whyc.pojo.plus_user.BaojigroupLock;
import com.whyc.pojo.plus_user.BaojigroupUsr;
import com.whyc.pojo.plus_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 org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
 
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class LockInfService {
    @Autowired(required = false)
    private LockInfMapper mapper;
 
    @Autowired(required = false)
    private StationInfMapper sinfMapper;
 
    @Autowired(required = false)
    private BaojigroupLockMapper bjLockMapper;
 
    @Autowired(required = false)
    private BaojigroupUsrMapper bjUserMapper;
 
    @Autowired(required = false)
    private BaojigroupMapper bjMapper;
 
    @Autowired(required = false)
    private LockIdcardMapper idcardMapper;
 
    @Autowired(required = false)
    private LockAddressMapper addressMapper;
 
 
    @Autowired
    private ProcessSurveyService processSurveyService;
    //添加锁
    @Transactional
    public Response addLock( LockInf lockInf) {
        //判断添加锁的时候机房是不是新机房
        String stationName=lockInf.getStationName1()+"_"+lockInf.getStationName2()+"_"+lockInf.getStationName3()+"_"+lockInf.getStationName4();
        //判断机房是否存在
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("station_name",stationName);
        wrapper.last("limit 1");
        StationInf sinf=sinfMapper.selectOne(wrapper);
        int stationId=0;
        int lockId=0;
        if(sinf!=null){
            stationId=sinf.getStationId();
            //根据锁名判断机房下是否存在同名锁
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("station_id",stationId);
            wrapper1.eq("lock_name",lockInf.getLockName());
            wrapper1.last("limit 1");
            LockInf judgeLock=mapper.selectOne(wrapper1);
            if (judgeLock!=null){
                return new Response().set(1,false,"当前机房下已经存在同名锁");
            }
            //获取当前最大的锁
            lockId=getMaxLockId(stationId);
            if(lockId==0){
                lockId=(1000+sinf.getStationNum())*10000+1;
            }else {
                lockId+=1;
            }
        }else {
            //获取当前最大的机房id
            Integer maxStationNum=sinfMapper.getMaxStationNum();
            if(maxStationNum==0){//数据库中没有站点
                stationId=40000001;
                lockId=10010001;
            }else{
                //获取对应的机房id
                stationId=sinfMapper.getStaitonIdByNum(maxStationNum);
                stationId+=1;
                lockId=(1000+maxStationNum+1)*10000+1;
            }
            StationInf newSinf=new StationInf();
            newSinf.setStationId(stationId);
            newSinf.setStationName(stationName);
            newSinf.setStationNum(maxStationNum+1);
            newSinf.setStationName1(lockInf.getStationName1());
            newSinf.setStationName2(lockInf.getStationName2());
            newSinf.setStationName3(lockInf.getStationName3());
            newSinf.setStationName4(lockInf.getStationName4());
            sinfMapper.insert(newSinf);
        }
        LockInf linf=new LockInf();
        linf.setStationId(stationId);
        linf.setLockId(lockId);
        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("FFFFFFFF");
        linf.setScreenBoxType(lockInf.getScreenBoxType());
        linf.setScreenBoxProduct(lockInf.getScreenBoxProduct());
        linf.setScreenFlag(lockInf.getScreenFlag());
        linf.setLockOnline(0);
        linf.setLastUpdateTime(new Date());
        linf.setModel(0);
        linf.setLockOpenCount(0);
        int flag=mapper.insert(linf);
        if(flag==1){//添加成功(包机组操作)
            insertInbaoji(stationId,lockId);
        }
        return new Response().set(1,true);
    }
    //包机组操作
    public void insertInbaoji(Integer stationId,Integer lockId){
        //重启线程并将锁加到当前人的包机组
        UserInf uinf= ActionUtil.getUser();
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("uid",uinf.getUid());
        List<BaojigroupUsr> groupIdList=bjUserMapper.selectList(wrapper1);
        List<BaojigroupLock> baojigroupList=new ArrayList();
        if(groupIdList!=null&&groupIdList.size()>0){
            BaojigroupLock bjLock=new BaojigroupLock();
            bjLock.setStationId(stationId);
            bjLock.setLockId(lockId);
            bjLock.setBaojiId(groupIdList.get(0).getBaojiId());
            baojigroupList.add(bjLock);
        }else{//人对应没有包机组先创建自己的默认包机组
            QueryWrapper wrapper3=new QueryWrapper();
            wrapper3.eq("baoji_name","默认包机组");
            wrapper3.last("limit 1");
            Baojigroup group=bjMapper.selectOne(wrapper3);
            if(group==null){
                Baojigroup bj=new Baojigroup();
                bj.setBaojiName("默认包机组");
                bjMapper.insert(bj);
                QueryWrapper wrapper4=new QueryWrapper();
                wrapper4.eq("baoji_name","默认包机组");
                wrapper4.last("limit 1");
                group=bjMapper.selectOne(wrapper4);
            }
            BaojigroupUsr bjUsr=new BaojigroupUsr();
            bjUsr.setUid(uinf.getUid());
            bjUsr.setBaojiId(group.getId());
            bjUserMapper.insert(bjUsr);
 
            BaojigroupLock bjLock=new BaojigroupLock();
            bjLock.setStationId(stationId);
            bjLock.setLockId(lockId);
            bjLock.setBaojiId(group.getId());
            baojigroupList.add(bjLock);
        }
        bjLockMapper.insertBatchSomeColumn(baojigroupList);
        processSurveyService.setUpThreadRestart();
    }
    //获取当前及放下最大的锁id
    public int getMaxLockId(Integer stationId) {
        int lockId=mapper.getMaxLockId(stationId);
        return lockId;
    }
 
    //查询所有锁信息
    public Response getAllLockInf(LockInfDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<LockInf> list=mapper.getAllLockInf(dto);
        for (LockInf linf:list) {
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("lock_id",linf.getLockId());
            wrapper.last("limit 1");
            LockIdcard idcard=idcardMapper.selectOne(wrapper);
            linf.setLockIdcard(idcard);
        }
        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.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 getScreenType(Integer uid) {
        List<String> list=mapper.getScreenType(uid);
        return new Response().setII(1,list!=null,list,"查询屏柜的全部类型(下拉)");
    }
   //查询屏柜全部品牌(下拉)
    public Response getScreenProduct(Integer uid) {
        List<String> list=mapper.getScreenProduct(uid);
        return new Response().setII(1,list!=null,list,"查询屏柜全部品牌(下拉)");
    }
    //实时获取获取区域下所有锁的状态
    public Response getRealLock(RealDto dto) {
        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);
        //根据dto中stationid和包机组id查询出管理的lockid
        List<Integer> lockIdList=bjLockMapper.getLocIdByStationidAndBjId(dto.getStationId(),dto.getBaojiId());
        List<LockInf> linfs=mapper.getRealLock(lockIdList);
        //锁的位置信息
        List<LockAddress> addressList=addressMapper.getAddressList(lockIdList);
        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, dto.getPageNum(), dto.getPageSize());
        map.put("pageInfo",pageInfo);
        return new Response().setIII(1,linfs!=null,map,addressList,"实时获取获取区域下所有锁的状态");
    }
    //查询锁的信息和id卡
    public Response getLockId(LockIdDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<LockInf> list=mapper.getLockId(dto);
        for (LockInf linf:list) {
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("lock_id",linf.getLockId());
            wrapper.last("limit 1");
            LockIdcard idcard=idcardMapper.selectOne(wrapper);
            linf.setLockIdcard(idcard);
        }
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null,pageInfo,"查询锁的信息和id卡");
    }
    //历史界面查询锁(下拉)
    public Response getLockInHis(Integer stationId,  Integer baojiId) {
        //根据dto中stationid和包机组id查询出管理的lockid
        List<Integer> lockIdList=bjLockMapper.getLocIdByStationidAndBjId(stationId,baojiId);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.in("lock_id",lockIdList);
        wrapper.orderByAsc("lock_id");
        List<LockInf> list=mapper.selectList(wrapper);
        return new Response().setII(1,list!=null,list,"历史界面查询锁(下拉)");
    }
}