whyclxw
2025-01-06 4be169675d8ed349ac1a4f5ab50d93d0b1dedde9
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
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.whyc.dto.Response;
import com.whyc.mapper.AuthiruzeInfMapper;
import com.whyc.mapper.LockInfMapper;
import com.whyc.pojo.db_area.AuthIdcard;
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 org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.swing.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class AuthiruzeInfService {
    @Autowired(required = false)
    private AuthiruzeInfMapper mapper;
 
    @Autowired(required = false)
    private LockInfMapper lockInfMapper;
 
    @Autowired(required = false)
    private AreaInfService areaInfService;
 
    @Autowired(required = false)
    private AuthIdcardService idcardService;
 
    //查询所有授权信息
    public Response getAllAuthInf( String uname, int areaId,int pageNum, int pageSize) {
        QueryWrapper wrapper=new QueryWrapper();
        if(uname!=null){
            wrapper.like("uname",uname);
        }
        List areaList=new ArrayList();
        areaList.add(areaId);
        areaInfService.getAllAreaId(areaId,areaList);
        if(areaList!=null){
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.in("area_id",areaList);
            List<LockInf> lockInfList=lockInfMapper.selectList(wrapper1);
            List<Integer> lockIdList = lockInfList.stream()
                    .map(LockInf::getLockId) // 提取id值
                    .collect(Collectors.toList()); // 转换为列表*/
            wrapper.in("lock_id",lockIdList);
        }
        PageHelper.startPage(pageNum,pageSize);
        List<LockInf> list=mapper.selectList(wrapper);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null,pageInfo,"查询所有钥匙信息");
    }
    //添加授权
    @Transactional
    public Response addAuth(AuthiruzeInf auth) {
        List<LockInf> lockIdList=auth.getLockInfs();
        List<AuthiruzeInf> list=new ArrayList<>();
        List<AuthIdcard> idcards=new ArrayList<>();
        if(lockIdList!=null){
            for (LockInf linf:lockIdList) {
                AuthiruzeInf authiruzeInf=new AuthiruzeInf();
                BeanUtils.copyProperties(auth,authiruzeInf);
                authiruzeInf.setLockId(linf.getLockId());
                authiruzeInf.setLockName(linf.getLockName());
                authiruzeInf.setStartTime(new Date());
                authiruzeInf.setStopTime(new Date());
                authiruzeInf.setState(0);
                list.add(authiruzeInf);
                if(auth.getKeyId()!=0){
                    AuthIdcard authIdcard_add=new AuthIdcard();
                    authIdcard_add.setKeyId(auth.getKeyId());
                    authIdcard_add.setLockId(linf.getLockId());
                    authIdcard_add.setCreateTime(new Date());
                    authIdcard_add.setState(2);
                    idcards.add(authIdcard_add);
                }
            }
        }
        mapper.insertBatchSomeColumn(list);
        if(auth.getKeyId()!=0){
            idcardService.addAuthIdCardS(idcards);
        }
        return new Response().set(1,true);
    }
    //删除授权
    public Response delAuth(List<Integer> ids) {
        List<AuthIdcard> idcards=new ArrayList<>();
        for (Integer id:ids) {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("id",id);
            queryWrapper.last("limit 1");
            AuthiruzeInf authinf=mapper.selectOne(queryWrapper);
            if(authinf.getKeyId()!=0){
                AuthIdcard authIdcard_del=new AuthIdcard();
                authIdcard_del.setKeyId(authinf.getKeyId());
                authIdcard_del.setLockId(authinf.getLockId());
                authIdcard_del.setCreateTime(new Date());
                authIdcard_del.setState(1);
                idcards.add(authIdcard_del);
            }
            UpdateWrapper wrapper=new UpdateWrapper();
            wrapper.eq("id",id);
            mapper.delete(wrapper);
        }
        if(idcards!=null&&idcards.size()>0){
            idcardService.addAuthIdCardS(idcards);
        }
        return new Response().set(1,true);
    }
    //修改授权
    @Transactional
    public Response updateAuth(AuthiruzeInf auth,int id) {
        List<AuthIdcard> idcards=new ArrayList<>();
        if(auth.getKeyId()!=0){
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("id",id);
            queryWrapper.last("limit 1");
            AuthiruzeInf authinf=mapper.selectOne(queryWrapper);
            AuthIdcard authIdcard_del=new AuthIdcard();
            authIdcard_del.setKeyId(auth.getKeyId());
            authIdcard_del.setLockId(authinf.getLockId());
            authIdcard_del.setCreateTime(new Date());
            authIdcard_del.setState(1);
            idcards.add(authIdcard_del);
        }
        //先删除旧的
        UpdateWrapper wrapper=new UpdateWrapper();
        wrapper.eq("id",id);
        mapper.delete(wrapper);
        //再添加新的
        List<LockInf> lockIdList=auth.getLockInfs();
        List<AuthiruzeInf> list=new ArrayList<>();
        if(lockIdList!=null){
            for (LockInf linf:lockIdList) {
                AuthiruzeInf authiruzeInf=new AuthiruzeInf();
                BeanUtils.copyProperties(auth,authiruzeInf);
                authiruzeInf.setLockId(linf.getLockId());
                authiruzeInf.setLockName(linf.getLockName());
                authiruzeInf.setStartTime(new Date());
                authiruzeInf.setStopTime(new Date());
                authiruzeInf.setState(0);
                list.add(authiruzeInf);
 
                if(auth.getKeyId()!=0){
                    AuthIdcard authIdcard_add=new AuthIdcard();
                    authIdcard_add.setKeyId(auth.getKeyId());
                    authIdcard_add.setLockId(linf.getLockId());
                    authIdcard_add.setCreateTime(new Date());
                    authIdcard_add.setState(2);
                    idcards.add(authIdcard_add);
                }
 
            }
        }
        mapper.insertBatchSomeColumn(list);
        if(auth.getKeyId()!=0){
            idcardService.addAuthIdCardS(idcards);
        }
        return new Response().set(1,true);
    }
    //根据mac检测蓝牙锁是否有权限
    public Response getAuthByUidAndMac(Integer mac) {
        UserInf uinf= ActionUtil.getUser();
        List areaList=areaInfService.getAllAreaUser(uinf.getUid(),uinf.getUrole());
        //获取锁是否在人管理的区域下面
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("lock_mac",mac);
        wrapper.last("limit 1");
        wrapper.in("area_id",areaList);
        LockInf linf=lockInfMapper.selectOne(wrapper);
        if(linf==null){
            return new Response().set(1,false,"没有权限");
        }else{
            //检测是否有权限
            QueryWrapper wrapper1=new QueryWrapper();
            wrapper1.eq("uname",uinf.getUname());
            wrapper1.eq("lock_id",linf.getLockId());
            wrapper1.last("limit 1");
            AuthiruzeInf auth=mapper.selectOne(wrapper1);
            return new Response().set(1,auth!=null,"有权限");
        }
    }
    //普通用户登录查看自己授权记录-----app
    public Response getAuthByUid() {
        UserInf uinf=ActionUtil.getUser();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("uname",uinf.getUname());
        wrapper.eq("key_id",0);//蓝牙钥匙
        List<AuthiruzeInf> list=mapper.selectList(wrapper);
        if(list!=null){
            for (AuthiruzeInf auth:list) {
                QueryWrapper wrapper1=new QueryWrapper();
                wrapper1.eq("lock_id",auth.getLockId());
                wrapper1.last("limit 1");
                LockInf linf=lockInfMapper.selectOne(wrapper1);
                auth.setLinf(linf);
            }
        }
        return new Response().setII(1,list!=null,list,"普通用户登录查看自己授权记录-----app");
    }
}