package com.whyc.service; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.whyc.dto.Response; import com.whyc.mapper.*; import com.whyc.pojo.db_station.PowerInf; import com.whyc.pojo.db_user.Baojigroup; import com.whyc.pojo.db_user.BaojigroupPower; import com.whyc.pojo.db_user.BaojigroupUsr; import com.whyc.pojo.db_user.User; import com.whyc.util.ActionUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.stream.Collectors; @Service public class BaojigroupService { @Autowired(required = false) private BaojigroupMapper mapper; @Autowired(required = false) private BaojigroupPowerMapper bjPowermapper; @Autowired(required = false) private BaojigroupUsrMapper bjUsrmapper; @Autowired(required = false) private UserMapper UserMapper; @Autowired(required = false) private PowerInfMapper powerInfMapper; //将内置用户加入到包机组下 public void insertNeiZhi(Integer baojiId){ List list=new ArrayList<>(); QueryWrapper wrapper=new QueryWrapper(); wrapper.lt("id",99); List uList=UserMapper.selectList(wrapper); for (User uinf:uList) { BaojigroupUsr baojiusr=new BaojigroupUsr(); baojiusr.setUid(uinf.getId()); baojiusr.setBaojiGroupId(baojiId); list.add(baojiusr); } bjUsrmapper.insertBatchSomeColumn(list); } //添加包机组 @Transactional public Response addBaoji(String baojiName) { //验证包机组名是否重复 //将内置用户加入到包机组下 QueryWrapper wrapper=new QueryWrapper(); wrapper.eq("baoji_group_name",baojiName); wrapper.last("limit 1"); Baojigroup baoji=mapper.selectOne(wrapper); if(baoji!=null){ return new Response().set(0,false,"包机组已存在"); }else{ baoji=new Baojigroup(); baoji.setBaojiGroupName(baojiName); int bl=mapper.insert(baoji); /*if(bl>0){ //将内置用户加入到包机组下 QueryWrapper wrapper1=new QueryWrapper(); wrapper1.eq("baoji_group_name",baojiName); wrapper1.last("limit 1"); baoji=mapper.selectOne(wrapper1); insertNeiZhi(baoji.getBaojiGroupId()); }*/ return new Response().set(1,bl>0,"添加包机组成功"); } } //编辑包机组 public Response updateBaoji(Integer id, String baojiName) { UpdateWrapper wrapper=new UpdateWrapper(); wrapper.eq("baoji_group_id",id); wrapper.set("baoji_group_name",baojiName); int bl=mapper.update((Baojigroup) ActionUtil.objeNull,wrapper); return new Response().set(1,bl>0,"编辑包机组成功"); } //删除包机组 @Transactional public Response delBaoji(Integer id) { //先删除包机组对应人下的记录 UpdateWrapper wrapper1=new UpdateWrapper(); wrapper1.eq("baoji_group_id",id); bjUsrmapper.delete(wrapper1); //再删除包机组对应下的机房和电源 UpdateWrapper wrapper2=new UpdateWrapper(); wrapper2.eq("baoji_group_id",id); bjPowermapper.delete(wrapper2); //最后删除包机组 UpdateWrapper wrapper=new UpdateWrapper(); wrapper.eq("baoji_group_id",id); int bl=mapper.delete(wrapper); return new Response().set(1,bl>0,"删除包机组成功"); } //查询包机组及包机组对应的用户和对应机房和锁 public Response getAllBaojiInf() { QueryWrapper wrapper=new QueryWrapper(); wrapper.orderByAsc("baoji_group_id"); List baojiList=mapper.selectList(wrapper); for (Baojigroup bj:baojiList) { //查询包机组对应的用户信息 List usrList=bjUsrmapper.getALlUsrByBjId(bj.getBaojiGroupId()); bj.setUsrList(usrList); //查询包机组对应的机房和锁的信息 List powerList=bjPowermapper.getALlLockByBjId(bj.getBaojiGroupId()); bj.setPowerList(powerList); } return new Response().setII(1,baojiList!=null,baojiList,"查询包机组及包机组对应的用户和对应机房和锁"); } //包机组添加机房和锁|移除机房和锁 @Transactional public void updateStationAndLockList(List BaojigroupPowerList, int operationFlag) { if(operationFlag==1){ bjPowermapper.insertBatchSomeColumn(BaojigroupPowerList); }else{ bjPowermapper.deleteStationAndLockList(BaojigroupPowerList); } } //包机组添加用户|移除用户 @Transactional public void updateUserList(List baoJiGroupUserList, int operationFlag) { if(operationFlag==1){ bjUsrmapper.insertBatchSomeColumn(baoJiGroupUserList); }else{ bjUsrmapper.deleteUserList(baoJiGroupUserList); } } // 包机组列表 public Response> getBaoJiGroup() { List list=mapper.selectList((Wrapper) ActionUtil.objeNull); return new Response().setII(1,list!=null,list,"包机组列表"); } //包机组已添加用户和未添加用户列表 public Map> getUserList(int id) { Map> resultMap = new LinkedHashMap<>(); //查询包机组已添加的用户信息 List relatedUserList = bjUsrmapper.getUserList(id); //查询包机组未添加的用户信息 QueryWrapper wrapper=new QueryWrapper(); wrapper.select("id", "name"); wrapper.gt("id",100); List allUserList = UserMapper.selectList(wrapper); List notRelatedUserList = allUserList.stream().filter(User -> !relatedUserList.contains(User)).collect(Collectors.toList()); resultMap.put("usersAdded",relatedUserList); resultMap.put("usersNotAdded",notRelatedUserList); return resultMap; } //已添加机房锁和未添加机房锁 public Map> getStationList(int id) { Map> resultMap = new LinkedHashMap<>(); //查询已添加的机房站点列表 List powerList = bjPowermapper.getPowerList(id); List allPowerList = powerInfMapper.getPowerList(); List notRelatedLockList = allPowerList.stream().filter(PowerInf -> !powerList.contains(PowerInf)).collect(Collectors.toList()); resultMap.put("stationsAdded",powerList); resultMap.put("stationsNotAdded",notRelatedLockList); return resultMap; } //当前用户所在包机组下所有的用户(下拉) public Response getBaojiUserByUid(Integer uid) { //查询当前人所在包机组 QueryWrapper wrapper=new QueryWrapper(); wrapper.select("baoji_group_id"); if(uid>100){ wrapper.eq("uid",uid); } List usrList=bjUsrmapper.selectList(wrapper); if(usrList.size()==0){ return new Response().setII(1,null,null,"当前用户未加入包机组"); } List baojiIdList=usrList.stream().map(BaojigroupUsr::getBaojiGroupId).collect(Collectors.toList()); List list=UserMapper.getBaojiUserByUid(baojiIdList); return new Response().setII(1,list!=null,list,"当前用户所在包机组下所有的用户(下拉)"); } public boolean getGroupFlag(int userId){ Integer flag = bjUsrmapper.getGroupFlag(userId); return flag != null && flag == 1; } //先校验当前用户是否存在包机组 public int checkUserBaojiGroup(Integer id) { QueryWrapper wrapper=new QueryWrapper(); wrapper.eq("uid",id); List list=bjUsrmapper.selectList(wrapper); return list.size()>0?1:0 ; } //设置包机组标记 public Response updateTeamFlag(Integer baoJiGroupId, Integer flag) { if(flag==1) { //查询要设置的包机组的站点 QueryWrapper query = Wrappers.query(); query.select("baoji_group_id", "station_id").eq("baoji_group_id", baoJiGroupId); List stationIdList = bjPowermapper.selectList(query); stationIdList = stationIdList.stream().distinct().collect(Collectors.toList()); //查询其他的组,包含的所有站点 List stationIdListWithDischargePlanFlag = bjPowermapper.getStationIdListWithDischargePlanFlag(); stationIdListWithDischargePlanFlag = stationIdListWithDischargePlanFlag.stream().distinct().collect(Collectors.toList()); List repeatedStationIdList = new LinkedList<>(); for (int i = 0; i < stationIdList.size(); i++) { BaojigroupPower baojiGroupBattGroup = stationIdList.get(i); for (int j = 0; j < stationIdListWithDischargePlanFlag.size(); j++) { BaojigroupPower baojiGroupBattGroupWithFlag = stationIdListWithDischargePlanFlag.get(j); if (baojiGroupBattGroupWithFlag.getStationId().equals(baojiGroupBattGroup.getStationId())) { repeatedStationIdList.add(baojiGroupBattGroupWithFlag); break; } } } //查询重复站点的信息 if (repeatedStationIdList.size() > 0) { return new Response().setII(1, false, repeatedStationIdList, "设置失败"); } } UpdateWrapper update = Wrappers.update(); update.set("team_flag",flag).eq("baoji_group_id",baoJiGroupId); mapper.update((Baojigroup) ActionUtil.objeNull,update); return new Response().set(1,true,"设置完成"); } //删除包机组下的电源 public void delPowerInBaoji(Integer pid) { UpdateWrapper update = Wrappers.update(); update.eq("power_id",pid); bjPowermapper.delete(update); } //删除包机组下的机房 public void delStationInBaoji(Integer sid) { UpdateWrapper update = Wrappers.update(); update.eq("station_id",sid); bjPowermapper.delete(update); } //查询机房所在的班组 public String getGroupName(Integer powerId) { String groupName = powerInfMapper.getGroupName(powerId); return groupName; } //查询所有的包机组名集合(班组) public List getGroupList() { QueryWrapper wrapper=new QueryWrapper(); wrapper.eq("team_flag",1); List list=mapper.selectList(wrapper); return list; } }