whyclxw
2025-01-02 5a107820f3353c0c40e892cade88faf42322467e
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
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.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 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;
 
    //查询所有授权信息
    public Response getAllAuthInf(Integer state, String uname, int areaId,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper wrapper=new QueryWrapper();
        if(state!=null){
            wrapper.eq("state",state);
        }
        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);
        }
        List<LockInf> list=mapper.selectList(wrapper);
        PageInfo pageInfo=new PageInfo(list);
        return new Response().setII(1,list!=null,pageInfo,"查询所有钥匙信息");
    }
    //添加授权
    public Response addAuth(AuthiruzeInf auth) {
        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);
            }
        }
        mapper.insertBatchSomeColumn(list);
        return new Response().set(1,true);
    }
    //删除授权
    public Response delAuth(List<Integer> ids) {
        for (Integer id:ids) {
            UpdateWrapper wrapper=new UpdateWrapper();
            wrapper.eq("id",id);
            mapper.delete(wrapper);
        }
        return new Response().set(1,true);
    }
    //修改授权
    public Response updateAuth(AuthiruzeInf auth,int id) {
        //先删除旧的
        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);
            }
        }
        mapper.insertBatchSomeColumn(list);
        return new Response().set(1,true);
    }
    //根据mac检测蓝牙锁是否有权限
    public Response getAuthByUidAndMac(Integer mac) {
        UserInf uinf= ActionUtil.getUser();
        //获取锁的id
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("lock_mac",mac);
        wrapper.last("limit 1");
        LockInf linf=lockInfMapper.selectOne(wrapper);
        //检测是否有权限
        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,"根据mac检测蓝牙锁是否有权限");
    }
}