| | |
| | | package com.vincent.rsf.server.manager.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.vincent.rsf.framework.exception.CoolException; |
| | | import com.vincent.rsf.server.manager.controller.params.DeviceSiteParame; |
| | | import com.vincent.rsf.server.manager.controller.params.DeviceSiteRowParam; |
| | | import com.vincent.rsf.server.manager.entity.BasStation; |
| | | import com.vincent.rsf.server.manager.mapper.DeviceSiteMapper; |
| | | import com.vincent.rsf.server.manager.entity.DeviceSite; |
| | | import com.vincent.rsf.server.manager.service.BasStationService; |
| | | import com.vincent.rsf.server.manager.service.DeviceSiteService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | 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.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | @Service("deviceSiteService") |
| | | public class DeviceSiteServiceImpl extends ServiceImpl<DeviceSiteMapper, DeviceSite> implements DeviceSiteService { |
| | | |
| | | /** 与表 man_device_site.target 列长度一致,超长截断避免 Data too long */ |
| | | private static final int TARGET_MAX_LENGTH = 255; |
| | | |
| | | @Autowired |
| | | private BasStationService basStationService; |
| | | |
| | | |
| | | /** |
| | | * 初始化站点 |
| | | * @param param |
| | | * @return |
| | | * 初始化站点:多行(设备站点、作业站点、目标站点为一组),巷道英文逗号分隔,每组×巷道×作业类型生成多条记录 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean initSites(DeviceSiteParame param) { |
| | | if (param.getFlagInit() == 1) { |
| | | if (param.getFlagInit() != null && param.getFlagInit() == 1) { |
| | | List<DeviceSite> list = this.list(new LambdaQueryWrapper<DeviceSite>().select(DeviceSite::getId).last("limit 1")); |
| | | if (!list.isEmpty()) { |
| | | if (!this.remove(new LambdaQueryWrapper<>())) { |
| | |
| | | } |
| | | } |
| | | } |
| | | if (Objects.isNull(param.getDeviceSites()) || StringUtils.isBlank(param.getDeviceSites())) { |
| | | throw new CoolException("初始化失败: 设备作业站点不能为空!!"); |
| | | } |
| | | if (Objects.isNull(param.getSite()) || StringUtils.isBlank(param.getSite())) { |
| | | throw new CoolException("初始化失败: 作业站点不能为空!!"); |
| | | } |
| | | if (Objects.isNull(param.getTypeIds()) || param.getTypeIds().isEmpty()) { |
| | | throw new CoolException("初始化失败: 作业类型不能为空!!"); |
| | | } |
| | | if (Objects.isNull(param.getTarget()) || param.getTarget().isEmpty()) { |
| | | throw new CoolException("初始化失败: 目标站点不能为空!!"); |
| | | if (StringUtils.isBlank(param.getChannel())) { |
| | | throw new CoolException("初始化失败: 巷道不能为空!!"); |
| | | } |
| | | List<String> sites = Arrays.asList(StringUtils.split(param.getSite(), ",")); |
| | | List<String> dvSites = Arrays.asList(StringUtils.split(param.getDeviceSites(), ",")); |
| | | List<String> targets = Arrays.asList(StringUtils.split(param.getTarget(), ",")); |
| | | List<DeviceSite> deviceSites = new ArrayList<>(); |
| | | for (String site : sites) { |
| | | for (String deviceSite : dvSites) { |
| | | for (Long id : param.getTypeIds()) { |
| | | for (String target : targets) { |
| | | DeviceSite site1 = new DeviceSite(); |
| | | site1.setType(id + "") |
| | | .setSite(site) |
| | | .setDevice(param.getDeviceType()) |
| | | .setDeviceSite(deviceSite) |
| | | .setTarget(target) |
| | | .setDeviceCode(param.getDeviceCode()); |
| | | deviceSites.add(site1); |
| | | } |
| | | List<Integer> channels = parseChannels(param.getChannel()); |
| | | if (channels.isEmpty()) { |
| | | throw new CoolException("初始化失败: 巷道格式错误,请用英文逗号分隔,如 1,2,3!!"); |
| | | } |
| | | |
| | | List<DeviceSiteRowParam> rows = param.getRows(); |
| | | if (Objects.isNull(rows) || rows.isEmpty()) { |
| | | throw new CoolException("初始化失败: 请至少添加一行(设备站点、作业站点、目标站点)!!"); |
| | | } |
| | | |
| | | List<DeviceSite> deviceSites = new ArrayList<>(); |
| | | for (DeviceSiteRowParam row : rows) { |
| | | if (StringUtils.isBlank(row.getDeviceSite()) || StringUtils.isBlank(row.getSite()) || StringUtils.isBlank(row.getTarget())) { |
| | | throw new CoolException("初始化失败: 每行的设备站点、作业站点、目标站点均不能为空!!"); |
| | | } |
| | | BasStation siteStation = basStationService.getById(Long.parseLong(row.getSite().trim())); |
| | | if (siteStation == null) { |
| | | throw new CoolException("初始化失败: 作业站点未找到!!"); |
| | | } |
| | | BasStation deviceStation = basStationService.getById(Long.parseLong(row.getDeviceSite().trim())); |
| | | if (deviceStation == null) { |
| | | throw new CoolException("初始化失败: 设备站点未找到!!"); |
| | | } |
| | | for (Long typeId : param.getTypeIds()) { |
| | | for (Integer ch : channels) { |
| | | DeviceSite ds = new DeviceSite(); |
| | | String siteName = siteStation.getStationName(); |
| | | String deviceSiteName = deviceStation.getStationName(); |
| | | String targetVal = truncate(row.getTarget().trim(), TARGET_MAX_LENGTH); |
| | | String commonName = StringUtils.isNotBlank(param.getName()) ? param.getName().trim() : null; |
| | | String commonLabel = StringUtils.isNotBlank(param.getLabel()) ? param.getLabel().trim() : null; |
| | | ds.setType(String.valueOf(typeId)) |
| | | .setSite(siteName) |
| | | .setDevice(param.getDeviceType()) |
| | | .setDeviceSite(deviceSiteName) |
| | | .setTarget(targetVal) |
| | | .setDeviceCode(param.getDeviceCode()) |
| | | .setAreaIdStart(param.getAreaIdStart()) |
| | | .setAreaIdEnd(param.getAreaIdEnd()) |
| | | .setChannel(ch) |
| | | .setName(commonName != null ? commonName : (deviceSiteName + "-" + siteName + "-" + targetVal + "-" + ch)) |
| | | .setLabel(commonLabel); |
| | | deviceSites.add(ds); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | private static String truncate(String s, int maxLen) { |
| | | if (s == null) return null; |
| | | return s.length() <= maxLen ? s : s.substring(0, maxLen); |
| | | } |
| | | |
| | | /** 巷道英文逗号分割,解析为整数列表 */ |
| | | private List<Integer> parseChannels(String channelStr) { |
| | | if (StringUtils.isBlank(channelStr)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | String[] parts = channelStr.split(","); |
| | | List<Integer> list = new ArrayList<>(); |
| | | for (String p : parts) { |
| | | String t = (p == null) ? "" : p.trim(); |
| | | if (t.isEmpty()) continue; |
| | | try { |
| | | list.add(Integer.parseInt(t)); |
| | | } catch (NumberFormatException e) { |
| | | return Collections.emptyList(); |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | } |