package com.slibra.business.service.impl; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import com.slibra.business.domain.TNeighborhoodBuilding; import com.slibra.business.domain.TPumpingStationNeighbourhoodNumber; import com.slibra.business.mapper.TNeighborhoodBuildingMapper; import com.slibra.business.mapper.TPumpingStationNeighbourhoodNumberMapper; import com.slibra.business.req.NeighbourhoodNumberAndAddFlagReq; import com.slibra.business.res.*; import com.slibra.common.exception.ServiceException; import com.slibra.common.utils.DateUtils; import org.apache.poi.ss.formula.functions.T; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.slibra.business.mapper.TPumpingStationMapper; import com.slibra.business.domain.TPumpingStation; import com.slibra.business.service.ITPumpingStationService; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; /** * 泵站Service业务层处理 * * @author slibra * @date 2024-11-05 */ @Service public class TPumpingStationServiceImpl implements ITPumpingStationService { @Autowired private TPumpingStationMapper tPumpingStationMapper; @Autowired private TNeighborhoodBuildingMapper tNeighborhoodBuildingMapper; @Autowired private TPumpingStationNeighbourhoodNumberMapper tPumpingStationNeighbourhoodNumberMapper; /** * 查询泵站 * * @param id 泵站主键 * @return 泵站 */ @Override public TPumpingStation selectTPumpingStationById(Long id) { return tPumpingStationMapper.selectTPumpingStationById(id); } /** * 查询泵站列表 * * @param tPumpingStation 泵站 * @return 泵站 */ @Override public List selectTPumpingStationList(TPumpingStation tPumpingStation) { List pumpingStations = tPumpingStationMapper.selectTPumpingStationList(tPumpingStation); //2024年11月12日15:51:43 返回泵站关联的小区信息 if(!CollectionUtils.isEmpty(pumpingStations)){ for (TPumpingStation pumpingStation : pumpingStations) { this.addNeighbourhoodAndBuildings(pumpingStation); } } return pumpingStations; } /** * 查询泵站关联的小区信息 * @param pumpingStation */ private void addNeighbourhoodAndBuildings(TPumpingStation pumpingStation) { List list = this.tPumpingStationMapper.getNeighbourhoodAndBuildingByPumpingId(pumpingStation.getId()); if(!CollectionUtils.isEmpty(list)){ Map> collect = list.stream().collect(Collectors.groupingBy(NeighbourhoodAndBuilding::getNeighborhoodName)); List result = new ArrayList<>(collect.size()); collect.forEach((key, value) -> { NeighbourhoodAndNumber neighbourhoodAndNumber = new NeighbourhoodAndNumber(); neighbourhoodAndNumber.setNeighbourhoodName(key); neighbourhoodAndNumber.setBuildingsNames(value.stream().map(NeighbourhoodAndBuilding::getNeighborhoodBuildingName).collect(Collectors.joining("、"))); result.add(neighbourhoodAndNumber); }); StringBuilder sb = new StringBuilder(); for (int i = 0; i < result.size(); i++) { NeighbourhoodAndNumber neighbourhoodAndNumber = result.get(i); sb.append(neighbourhoodAndNumber.getNeighbourhoodName()).append("(").append(neighbourhoodAndNumber.getBuildingsNames()).append(")"); if (i != result.size() - 1) { sb.append(","); } } pumpingStation.setNeighbourhoodAndBuildings(sb.toString()); } } /** * 新增泵站 * * @param tPumpingStation 泵站 * @return 结果 */ @Override public int insertTPumpingStation(TPumpingStation tPumpingStation) { tPumpingStation.setCreateTime(DateUtils.getNowDate()); return tPumpingStationMapper.insertTPumpingStation(tPumpingStation); } /** * 修改泵站 * * @param tPumpingStation 泵站 * @return 结果 */ @Override public int updateTPumpingStation(TPumpingStation tPumpingStation) { tPumpingStation.setUpdateTime(DateUtils.getNowDate()); return tPumpingStationMapper.updateTPumpingStation(tPumpingStation); } /** * 批量删除泵站 * * @param ids 需要删除的泵站主键 * @return 结果 */ @Override public int deleteTPumpingStationByIds(Long[] ids) { return tPumpingStationMapper.deleteTPumpingStationByIds(ids); } /** * 删除泵站信息 * * @param id 泵站主键 * @return 结果 */ @Override public int deleteTPumpingStationById(Long id) { return tPumpingStationMapper.deleteTPumpingStationById(id); } @Override public List getBuildingsAndFlagByID2(NeighbourhoodNumberAndAddFlagReq req) { Long neighbourhoodId = req.getNeighbourhoodId(); Long pumpingStationId = req.getPumpingStationId(); List result = new ArrayList<>(); //先获取该小区下的所有的楼号 List tNeighborhoodBuildings = this.tNeighborhoodBuildingMapper.selectTNeighborhoodBuildingList(TNeighborhoodBuilding.builder().neighborhoodId(neighbourhoodId).build()); if(CollectionUtils.isEmpty(tNeighborhoodBuildings)) return result; //有楼号 for (TNeighborhoodBuilding tNeighborhoodBuilding : tNeighborhoodBuildings) { NeighbourhoodNumberAndAddFlag neighbourhoodNumberAndAddFlag = new NeighbourhoodNumberAndAddFlag(); //楼号 neighbourhoodNumberAndAddFlag.setBuildingId(tNeighborhoodBuilding.getId()); neighbourhoodNumberAndAddFlag.setBuildingsNames(tNeighborhoodBuilding.getName()); //小区 neighbourhoodNumberAndAddFlag.setNeighbourhoodId(neighbourhoodId); //泵站ID neighbourhoodNumberAndAddFlag.setPumpingStationId(pumpingStationId); //通过楼号和泵站查询对应的楼号是否已经添加过了 List tPumpingStationNeighbourhoodNumbers = this.tPumpingStationNeighbourhoodNumberMapper.selectTPumpingStationNeighbourhoodNumberList(TPumpingStationNeighbourhoodNumber.builder().pumpingStationId(pumpingStationId).neighborhoodId(tNeighborhoodBuilding.getNeighborhoodId()).neighborhoodBuildingId(tNeighborhoodBuilding.getId()).build()); if(!CollectionUtils.isEmpty(tPumpingStationNeighbourhoodNumbers)){ TPumpingStationNeighbourhoodNumber tPumpingStationNeighbourhoodNumber = tPumpingStationNeighbourhoodNumbers.get(0); neighbourhoodNumberAndAddFlag.setId(tPumpingStationNeighbourhoodNumber.getId()); neighbourhoodNumberAndAddFlag.setAdd(true); } result.add(neighbourhoodNumberAndAddFlag); } return result; } @Override @Transactional(rollbackFor = Exception.class) public String addPumpingStationAndNeighbourhoodBuildings(List req) { //需要把之前所有的关联数据全部删除掉 if(!CollectionUtils.isEmpty(req)){ for (NeighbourhoodNumberAndAddFlag neighbourhoodNumberAndAddFlag : req) { List tPumpingStationNeighbourhoodNumbersExists = this.tPumpingStationNeighbourhoodNumberMapper.selectTPumpingStationNeighbourhoodNumberList(TPumpingStationNeighbourhoodNumber.builder().pumpingStationId(neighbourhoodNumberAndAddFlag.getPumpingStationId()).build()); if(!CollectionUtils.isEmpty(tPumpingStationNeighbourhoodNumbersExists)){ for (TPumpingStationNeighbourhoodNumber tPumpingStationNeighbourhoodNumber : tPumpingStationNeighbourhoodNumbersExists) { this.tPumpingStationNeighbourhoodNumberMapper.deleteTPumpingStationNeighbourhoodNumberById(tPumpingStationNeighbourhoodNumber.getId()); } } } //保存新的数据 for (NeighbourhoodNumberAndAddFlag addReq : req) { Long pumpingStationId = addReq.getPumpingStationId(); Long neighbourhoodId = addReq.getNeighbourhoodId(); Long buildingId = addReq.getBuildingId(); //判断是否已经添加过了,添加过了就抛出异常 不用判断是否已添加了,因为是每次都是覆盖添加 /*List tPumpingStationNeighbourhoodNumbers = this.tPumpingStationNeighbourhoodNumberMapper.selectTPumpingStationNeighbourhoodNumberList(TPumpingStationNeighbourhoodNumber.builder().pumpingStationId(pumpingStationId).neighborhoodId(neighbourhoodId).neighborhoodBuildingId(buildingId).build()); if(!CollectionUtils.isEmpty(tPumpingStationNeighbourhoodNumbers)){ throw new ServiceException("有存在重复添加的数据,请检查!"); }*/ //赋值 TPumpingStationNeighbourhoodNumber build = TPumpingStationNeighbourhoodNumber.builder().build(); build.setPumpingStationId(pumpingStationId); build.setNeighborhoodId(neighbourhoodId); build.setNeighborhoodBuildingId(buildingId); //保存 tPumpingStationNeighbourhoodNumberMapper.insertTPumpingStationNeighbourhoodNumber(build); } } return "操作成功"; } }