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.AreaUserMapper;
|
import com.whyc.mapper.AuthiruzeInfMapper;
|
import com.whyc.mapper.LockInfMapper;
|
import com.whyc.mapper.UserInfMapper;
|
import com.whyc.pojo.db_area.AreaUser;
|
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.Locale;
|
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;
|
|
@Autowired(required = false)
|
private UserInfMapper uinfMapper;
|
|
|
//查询所有授权信息
|
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<>();
|
UserInf userInf=ActionUtil.getUser();
|
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.setCtlUname(userInf.getUname());
|
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<>();
|
UserInf userInf=ActionUtil.getUser();
|
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);
|
authIdcard_add.setCtlUname(userInf.getUname());
|
idcards.add(authIdcard_add);
|
}
|
|
}
|
}
|
mapper.insertBatchSomeColumn(list);
|
if(auth.getKeyId()!=0){
|
idcardService.addAuthIdCardS(idcards);
|
}
|
return new Response().set(1,true);
|
}
|
//根据mac检测蓝牙锁是否有权限
|
public Response getAuthByUidAndMac(String mac,String uname) {
|
UserInf uinf=uinfMapper.getUinfByUname(uname);
|
//List areaList=areaInfService.getAllAreaUser(uinf.getUid(),uinf.getUrole());
|
//获取锁是否在人管理的区域下面
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.eq("lock_mac",mac.toUpperCase(Locale.ROOT));
|
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,auth!=null?"有权限":"没有权限");
|
}
|
}
|
//普通用户登录查看自己授权锁记录-----app
|
public Response getAuthByUid(String uname) {
|
//UserInf uinf=uinfMapper.getUinfByUname(uname);
|
//查询普通用户对应的区域
|
QueryWrapper wrapper=new QueryWrapper();
|
wrapper.select("distinct lock_id");
|
wrapper.eq("uname",uname);
|
wrapper.eq("key_id",0);//蓝牙钥匙
|
List<AuthiruzeInf> list=mapper.selectList(wrapper);
|
List<Integer> lockIds=list.stream().map(AuthiruzeInf::getLockId) // 提取id
|
.collect(Collectors.toList());
|
//根据锁的lockIds查询锁的记录
|
List<LockInf> linfs=lockInfMapper.selectlinfByLockIds(lockIds);
|
return new Response().setII(1,linfs!=null,linfs,"普通用户登录查看自己授权记录-----app");
|
}
|
}
|