whyclxw
2024-12-20 88a34da304025257cda19d72e11a29c66e7a7822
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.Response;
import com.whyc.mapper.*;
import com.whyc.pojo.db_area.AreaInf;
import com.whyc.pojo.db_area.KeyInf;
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 org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class AreaInfService {
 
    @Autowired(required = false)
    private AreaInfMapper mapper;
 
    @Autowired(required = false)
    private LockInfMapper linfMapper;
 
    @Autowired(required = false)
    private KeyInfMapper kinfMapper;
 
    @Autowired(required = false)
    private UserInfMapper uInfMapper;
 
    @Autowired(required = false)
    private LockCtlLogMapper ctlLogMapper;
 
 
    //查询所有区域信息
    public Response getAllAreaInf() {
        UserInf uinf=new UserInf();
        uinf.setUname("test_admin");
        uinf.setUid(105);
        uinf.setAreaId(3);
        //UserInf uinf= ActionUtil.getUser();
        if(uinf.getUid()>100&&uinf.getUid()<=1000){
            //查询出当前用户所在区域
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("id",uinf.getAreaId());
            wrapper.last("limit 1");
            AreaInf ainf=mapper.selectOne(wrapper);
            List areaList=new ArrayList();
            if(ainf!=null){
                areaList.add(ainf.getId());
                getAllAreaId(ainf.getId(),areaList);
            }
            List<AreaInf> list1=mapper.selectAreaByUid(areaList,1);
            return new Response().setII(1,list1!=null,list1,"查询区域管理员的所有信息");
        }else if(uinf.getUid()>1000){
            return new Response().set(1,false,"暂无管理区域");
        }else {
            List<AreaInf> list2=mapper.selectAreaByUid(null,-1);
            return new Response().setII(1,list2!=null,list2,"查询所有区域信息");
        }
    }
    //添加区域
    public Response addArea(Integer parentId, String areaName,String areaUname,String areaDescript) {
        AreaInf ainf=new AreaInf();
        ainf.setParentId(0);
        ainf.setAreaLevel(1);
        ainf.setAreaPath(areaName);
        ainf.setAreaName(areaName);
        if(areaUname!=null){
            ainf.setAreaUname(areaUname);
        }
        if(areaDescript!=null){
            ainf.setAreaDescript(areaDescript);
        }
        if(parentId!=null){
            //获取上一级信息
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("id",parentId);
            wrapper.last("limit 1");
            AreaInf painf=mapper.selectOne(wrapper);
            ainf.setParentId(parentId);
            ainf.setAreaLevel(painf.getAreaLevel()+1);
            ainf.setAreaPath(painf.getAreaName()+"_"+areaName);
        }
        int flag= mapper.insert(ainf);
        return new Response().set(1,flag>0);
    }
    //删除区域
    public Response delArea(Integer id) {
        delAll(id);
        return new Response().set(1,true);
    }
    @Transactional
    public void delAll(Integer id) {
        //获取上一级信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",id);
        List<AreaInf> list=mapper.selectList(wrapper);
        if(list!=null){
            for (AreaInf painf:list) {
                delAll(painf.getId());
            }
        }
        UpdateWrapper wrapper1=new UpdateWrapper();
        wrapper1.eq("id",id);
        mapper.delete(wrapper1);
    }
    //修改区域
    public Response updateArea(Integer id, String areaName, String areaUname, String areaDescript) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        wrapper.last("limit 1");
        AreaInf ainf=mapper.selectOne(wrapper);
        ainf.setAreaUname(areaUname);
        ainf.setAreaDescript(areaDescript);
        ainf.setAreaName(areaName);
        String oldPath=ainf.getAreaPath();
        String newPath=oldPath.substring(0,oldPath.lastIndexOf("_")+1)+areaName;
        UpdateWrapper wrapper1=new UpdateWrapper();
        wrapper1.eq("id",id);
        mapper.update(ainf,wrapper1);
        updateAll(id,newPath);
        return new Response().set(1,true);
    }
    public void updateAll(Integer id, String newPath) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",id);
        List<AreaInf> list=mapper.selectList(wrapper);
        if(list!=null){
            for (AreaInf painf:list) {
                String path=newPath+"_"+painf.getAreaName();
                updateAll(painf.getId(),path);
            }
        }
        UpdateWrapper wrapper2=new UpdateWrapper();
        wrapper2.set("area_path",newPath);
        wrapper2.eq("id",id);
        mapper.update(null,wrapper2);
    }
    //查询所有区域下所有锁信息
    public Response getLinfById(Integer id, int pageNum, int pageSize) {
        //获取获取id下所有的区域id
        List areaList=new ArrayList();
        areaList.add(id);
        getAllAreaId(id,areaList);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.in("area_id",areaList);
        List<LockInf> list=linfMapper.selectList(wrapper);
        PageInfo pageInfo=PageInfoUtils.list2PageInfo(list,pageNum,pageSize);
        return new Response().setII(1,list!=null,pageInfo,"查询所有区域下所有锁信息");
    }
    //获取获取id下所有的区域id
    public void getAllAreaId(Integer id,List areaList) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",id);
        List<AreaInf> list=mapper.selectList(wrapper);
        if(list!=null){
            for (AreaInf painf:list) {
                areaList.add(painf.getId());
                getAllAreaId(painf.getId(),areaList);
            }
        }
    }
 
    //查询所有区域下所有用户信息
    public Response getUinfById(Integer id, int pageNum, int pageSize) {
        //获取获取id下所有的区域id
        List uinfList=new ArrayList();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        wrapper.last("limit 1");
        AreaInf ainf=mapper.selectOne(wrapper);
        if(ainf!=null){
            uinfList.add(ainf.getAreaUname());
        }
        getAllUinfId(id,uinfList);
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.select("uid","uname","CREATE_TIME");
        wrapper1.in("uname",uinfList);
        List<UserInf> list=uInfMapper.selectList(wrapper1);
        PageInfo pageInfo=PageInfoUtils.list2PageInfo(list,pageNum,pageSize);
        return new Response().setII(1,list!=null,pageInfo,"查询所有区域下所有用户信息");
    }
 
    //获取获取id下所有的区域用户名
    private void getAllUinfId(Integer id,List uinfList) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_id",id);
        List<AreaInf> list=mapper.selectList(wrapper);
        if(list!=null){
            for (AreaInf painf:list) {
                uinfList.add(painf.getAreaUname());
                getAllUinfId(painf.getId(),uinfList);
            }
        }
    }
   //查询所有区域下所有钥匙信息
    public Response getKinfById(Integer id, int pageNum, int pageSize) {
        //获取获取id下所有的区域id
        List areaList=new ArrayList();
        areaList.add(id);
        getAllAreaId(id,areaList);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.in("area_id",areaList);
        List<KeyInf> list=kinfMapper.selectList(wrapper);
        PageInfo pageInfo=PageInfoUtils.list2PageInfo(list,pageNum,pageSize);
        return new Response().setII(1,list!=null,pageInfo,"查询所有区域下所有钥匙信息");
    }
    //查询所在区域下所有开锁日志
    public Response getLogById(Integer id, int pageNum, int pageSize) {
        //获取获取id下所有的区域id
        List areaList=new ArrayList();
        areaList.add(id);
        getAllAreaId(id,areaList);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.in("area_id",areaList);
        List<LockInf> lockList=linfMapper.selectList(wrapper);
        List<Integer> lockIdList=lockList.stream().map(LockInf::getLockId).collect(Collectors.toList());
        QueryWrapper logwrapper=new QueryWrapper();
        wrapper.in("lock_id",lockIdList);
        List<KeyInf> list=ctlLogMapper.selectList(logwrapper);
        PageInfo pageInfo=PageInfoUtils.list2PageInfo(list,pageNum,pageSize);
        return new Response().setII(1,list!=null,pageInfo,"查询所在区域下所有开锁日志");
    }
}