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.BattDto; import com.whyc.dto.InfoDto; import com.whyc.dto.PowerDto; import com.whyc.dto.Response; import com.whyc.mapper.BattInfMapper; import com.whyc.mapper.PowerInfMapper; import com.whyc.mapper.StationInfMapper; import com.whyc.pojo.db_station.BattInf; import com.whyc.pojo.db_station.PowerInf; import com.whyc.pojo.db_station.StationInf; 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.Date; import java.util.List; @Service public class BattInfService { @Autowired(required = false) private BattInfMapper mapper; @Autowired(required = false) private PowerInfMapper pinfMapper; @Autowired(required = false) private StationInfMapper sinfMapper; /*新建电池组(新的设备新的电池组) * @param binf * 1.在电源下新建设备,记录设备编号,devNum,设备名称为设备类型+devNum */ @Transactional public void addDev(BattInf adddinf) { //检测电源下是否存在设备 int devNum=mapper.getMaxDevNum(adddinf.getPowerId()); if(devNum==0){ devNum=1; }else{ devNum+=1; } adddinf.setDevNum(devNum); adddinf.setDevName(adddinf.getDevType()+devNum); //获取对应的设备id,电池组 int devId = mapper.getMaxdevId(); int battGroupId=mapper.getMaxBattGroupId(); if (devId == 0) {//数据库中没有站点 devId = 10001; } else { devId += 1; } if (battGroupId == 0) {//数据库中没有站点 battGroupId = 10001; } else { battGroupId += 1; } adddinf.setDevId(devId); adddinf.setBattgroupId(battGroupId); adddinf.setBattgroupName("电池组1"); adddinf.setBattgroupNum(1); adddinf.setCreateTime(new Date()); mapper.insert(adddinf); } //设备下添加电池组 @Transactional public void addBatt(BattInf addbinf) { //获取设备的通用信息 QueryWrapper wrapper=new QueryWrapper(); wrapper.eq("dev_id",addbinf.getDevId()); wrapper.select("dev_name","dev_type","dev_num","dev_ip","power_id","station_id"); wrapper.last("limit 1"); BattInf binf=mapper.selectOne(wrapper); addbinf.setPowerId(binf.getPowerId()); addbinf.setStationId(binf.getStationId()); addbinf.setDevName(binf.getDevName()); addbinf.setDevType(binf.getDevType()); addbinf.setDevNum(binf.getDevNum()); addbinf.setDevIp(binf.getDevIp()); //检测设备下是否存在电池组 int battgroupNum=mapper.getMaxBattgroupNum(addbinf.getDevId()); if(battgroupNum==0){ battgroupNum=1; }else{ battgroupNum+=1; } int battGroupId=mapper.getMaxBattGroupId(); if (battGroupId == 0) {//数据库中没有站点 battGroupId = 10001; } else { battGroupId += 1; } addbinf.setBattgroupNum(battgroupNum); addbinf.setBattgroupId(battGroupId); addbinf.setBattgroupName("电池组"+battgroupNum); addbinf.setBattgroupNum(battgroupNum); addbinf.setCreateTime(new Date()); mapper.insert(addbinf); } //删除电池组 @Transactional public Response delBatt(Integer stationId, Integer powerId,Integer battgroupId) { UpdateWrapper wrapper=new UpdateWrapper(); wrapper.eq("battgroup_id",battgroupId); mapper.delete(wrapper); //检测如果powerId下电池组删除完了,要将电源也删除掉 QueryWrapper wrapper1=new QueryWrapper(); wrapper1.eq("power_id",powerId); List binfList=mapper.selectList(wrapper1); if(binfList==null){ UpdateWrapper wrapper2=new UpdateWrapper(); wrapper2.eq("power_id",powerId); pinfMapper.delete(wrapper); //再检测如果机房下电源也全删除了,要将机房也删除 QueryWrapper wrapper3=new QueryWrapper(); wrapper3.eq("station_id",stationId); List pinfList=pinfMapper.selectList(wrapper3); if(pinfList==null){ UpdateWrapper wrapper4=new UpdateWrapper(); wrapper4.eq("station_id",stationId); sinfMapper.delete(wrapper4); } } return new Response().set(1,true); } //修改电池组 public Response updateBatt(BattInf binf) { UpdateWrapper wrapper1=new UpdateWrapper(); wrapper1.eq("dev_id",binf.getDevId()); if(binf.getDevIp()!=null){ wrapper1.set("dev_ip",binf.getDevIp()); } if(binf.getDevName()!=null){ wrapper1.set("dev_name",binf.getDevName()); } UpdateWrapper wrapper=new UpdateWrapper(); wrapper.eq("battgroup_id",binf.getBattgroupId()); if(binf.getBattgroupName()!=null){ //检测该机房下要修改的电源名是否粗在 QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.eq("battgroup_name",binf.getBattgroupName()); queryWrapper.eq("dev_id",binf.getDevId()); queryWrapper.last("limit 1"); BattInf juegeb=mapper.selectOne(queryWrapper); if(juegeb!=null){ return new Response().set(1,false,"该设备下已存在"+binf.getBattgroupName()+"的电池组"); } wrapper.set("battgroup_name",binf.getBattgroupName()); } if(binf.getMonvolstd()!=null){ wrapper.set("monvolstd",binf.getMonvolstd()); } if(binf.getMoncapstd()!=null){ wrapper.set("moncapstd",binf.getMoncapstd()); } if(binf.getMonresstd()!=null){ wrapper.set("monresstd",binf.getMonresstd()); } if(binf.getProduct()!=null){ wrapper.set("product",binf.getProduct()); } if(binf.getMoncount()!=null){ wrapper.set("moncount",binf.getMoncount()); } if(binf.getBattModel()!=null){ wrapper.set("batt_model",binf.getBattModel()); } mapper.update((BattInf) ActionUtil.objeNull,wrapper); return new Response().set(1,true); } //查询机房,电源,电池组信息 public Response getInfo(BattDto dto) { User user= ActionUtil.getUser(); dto.setUid(user.getId()); PageHelper.startPage(dto.getPageNum(),dto.getPageSize()); List list=mapper.getInfo(dto); PageInfo pageInfo=new PageInfo(list); return new Response().setII(1,list!=null,pageInfo,"查询电池"); } //删除电源下的电池组 public void delBattInPower(Integer pid) { UpdateWrapper wrapper=new UpdateWrapper(); wrapper.eq("power_id",pid); mapper.delete(wrapper); } //获取电池品牌(下拉) public Response getProductByUid(Integer uid) { List list=mapper.getProductByUid(uid); return new Response().setII(1,list.size()>0,list,"获取电池品牌(下拉)"); } //获取标称单体电压(下拉) public Response getMonVolByUid(Integer uid) { List list=mapper.getMonVolByUid(uid); return new Response().setII(1,list.size()>0,list,"获取标称单体电压(下拉)"); } //获取标称容量(下拉) public Response getMonCapByUid(Integer uid) { List list=mapper.getMonCapByUid(uid); return new Response().setII(1,list.size()>0,list,"获取标称容量(下拉)"); } //获取标称内阻(下拉) public Response getMonResByUid(Integer uid) { List list=mapper.getMonResByUid(uid); return new Response().setII(1,list.size()>0,list,"获取标称内阻(下拉)"); } //获取设备型号(下拉) public Response getDevTypeByUid(Integer uid) { List list=mapper.getDevTypeByUid(uid); return new Response().setII(1,list.size()>0,list,"获取设备型号(下拉)"); } }