package com.doumee.service.business.impl.collection; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; import com.doumee.core.haikang.isapi.IsapiClient; import com.doumee.core.haikang.isapi.IsapiConstants; import com.doumee.core.haikang.isapi.model.DeviceInfoDTO; import com.doumee.core.haikang.isapi.model.DockDeviceDTO; import com.doumee.core.haikang.isapi.model.DockStationBasicInfoDTO; import com.doumee.core.haikang.isapi.model.StorageInfoDTO; import com.doumee.core.utils.Constants; import com.doumee.core.utils.IsapiHttpUtil; import com.doumee.dao.business.CollectionDockDeviceMapper; import com.doumee.dao.business.CollectionStationMapper; import com.doumee.dao.business.model.CollectionDockDevice; import com.doumee.dao.business.model.CollectionStation; import com.doumee.service.business.CollectionStationService; import com.doumee.service.business.third.model.LoginUserInfo; import com.doumee.service.business.third.model.PageData; import com.doumee.service.business.third.model.PageWrap; import lombok.extern.slf4j.Slf4j; 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.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @Service @Slf4j public class CollectionStationServiceImpl implements CollectionStationService { @Autowired private CollectionStationMapper collectionStationMapper; @Autowired private CollectionDockDeviceMapper collectionDockDeviceMapper; private final IsapiClient isapiClient = new IsapiClient(); @Override @Transactional(rollbackFor = Exception.class) public Integer create(CollectionStation station) { validateStation(station); Date now = new Date(); station.setCreateDate(now); station.setEditDate(now); station.setIsdeleted(Constants.ZERO); if (station.getStatus() == null) { station.setStatus(Constants.ONE); } if (station.getPort() == null) { station.setPort(defaultPort(station)); } if (station.getUseHttps() == null) { station.setUseHttps(Constants.ZERO); } if (StringUtils.isBlank(station.getModel())) { station.setModel("UD39625B"); } if (station.getLoginUserInfo() != null) { station.setCreator(station.getLoginUserInfo().getUsername()); station.setEdirot(station.getLoginUserInfo().getUsername()); } collectionStationMapper.insert(station); return station.getId(); } @Override @Transactional(rollbackFor = Exception.class) public void updateById(CollectionStation station) { if (station.getId() == null) { throw new BusinessException(ResponseStatus.BAD_REQUEST); } station.setEditDate(new Date()); if (station.getLoginUserInfo() != null) { station.setEdirot(station.getLoginUserInfo().getUsername()); } collectionStationMapper.updateById(station); } @Override @Transactional(rollbackFor = Exception.class) public void deleteById(Integer id, LoginUserInfo user) { CollectionStation station = new CollectionStation(); station.setId(id); station.setIsdeleted(Constants.ONE); station.setEditDate(new Date()); if (user != null) { station.setEdirot(user.getUsername()); } collectionStationMapper.updateById(station); } @Override @Transactional(rollbackFor = Exception.class) public void deleteByIdInBatch(List ids, LoginUserInfo user) { if (ids == null) { return; } for (Integer id : ids) { deleteById(id, user); } } @Override public PageData findPage(PageWrap pageWrap) { CollectionStation model = pageWrap.getModel() != null ? pageWrap.getModel() : new CollectionStation(); QueryWrapper wrapper = buildQueryWrapper(model); IPage page = collectionStationMapper.selectPage( new Page<>(pageWrap.getPage(), pageWrap.getCapacity()), wrapper); return PageData.from(page); } @Override public List findList(CollectionStation query) { return collectionStationMapper.selectList(buildQueryWrapper(query != null ? query : new CollectionStation())); } @Override public CollectionStation findById(Integer id) { CollectionStation station = collectionStationMapper.selectById(id); if (station == null || Constants.equalsInteger(station.getIsdeleted(), Constants.ONE)) { throw new BusinessException(ResponseStatus.DATA_EMPTY); } return station; } @Override public String syncAllStations() { if (Constants.DEALING_HK_COLLECTION_STATION) { throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "采集站同步任务正在执行,请稍后"); } Constants.DEALING_HK_COLLECTION_STATION = true; try { List list = collectionStationMapper.selectList(new QueryWrapper().lambda() .eq(CollectionStation::getIsdeleted, Constants.ZERO) .eq(CollectionStation::getStatus, Constants.ONE)); int success = 0; int fail = 0; for (CollectionStation station : list) { try { syncStationStatusInternal(station); success++; } catch (Exception e) { fail++; log.error("采集站同步失败 id={}: {}", station.getId(), e.getMessage()); } } return "同步完成:成功【" + success + "】台,失败【" + fail + "】台"; } finally { Constants.DEALING_HK_COLLECTION_STATION = false; } } @Override public String syncStationStatus(Integer stationId) { CollectionStation station = findById(stationId); syncStationStatusInternal(station); return "同步成功"; } @Override public String probeIsapi(Integer stationId) { CollectionStation station = findById(stationId); boolean https = station.getUseHttps() != null && station.getUseHttps() == 1; String deviceInfo = IsapiHttpUtil.doGet(station.getIp(), station.getPort(), https, station.getUsername(), station.getPassword(), IsapiConstants.DEVICE_INFO); String storage = IsapiHttpUtil.doGet(station.getIp(), station.getPort(), https, station.getUsername(), station.getPassword(), IsapiConstants.STORAGE); String tracks = IsapiHttpUtil.doGet(station.getIp(), station.getPort(), https, station.getUsername(), station.getPassword(), IsapiConstants.RECORD_TRACKS); String dockBasic = IsapiHttpUtil.doGet(station.getIp(), station.getPort(), https, station.getUsername(), station.getPassword(), IsapiConstants.DOCK_BASIC_INFO); String dockDevices = IsapiHttpUtil.doGet(station.getIp(), station.getPort(), https, station.getUsername(), station.getPassword(), IsapiConstants.DOCK_DEVICE_MANAGEMENT); return "deviceInfo:\n" + StringUtils.defaultString(deviceInfo) + "\n\nstorage:\n" + StringUtils.defaultString(storage) + "\n\nrecord/tracks:\n" + StringUtils.defaultString(tracks) + "\n\ndockStation/basicInfo:\n" + StringUtils.defaultString(dockBasic) + "\n\ndockStation/deviceManagement:\n" + StringUtils.defaultString(dockDevices); } @Override public List findDockDevices(Integer stationId) { findById(stationId); return collectionDockDeviceMapper.selectList(new QueryWrapper().lambda() .eq(CollectionDockDevice::getStationId, stationId) .eq(CollectionDockDevice::getIsdeleted, Constants.ZERO) .orderByAsc(CollectionDockDevice::getId)); } @Override public PageData findDockDevicePage(PageWrap pageWrap) { CollectionDockDevice model = pageWrap.getModel() != null ? pageWrap.getModel() : new CollectionDockDevice(); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(CollectionDockDevice::getIsdeleted, Constants.ZERO) .eq(model.getStationId() != null, CollectionDockDevice::getStationId, model.getStationId()) .like(StringUtils.isNotBlank(model.getDeviceName()), CollectionDockDevice::getDeviceName, model.getDeviceName()) .like(StringUtils.isNotBlank(model.getShortSerialNumber()), CollectionDockDevice::getShortSerialNumber, model.getShortSerialNumber()) .orderByDesc(CollectionDockDevice::getId); IPage page = collectionDockDeviceMapper.selectPage( new Page<>(pageWrap.getPage(), pageWrap.getCapacity()), wrapper); fillStationName(page.getRecords()); return PageData.from(page); } private void fillStationName(List records) { if (records == null || records.isEmpty()) { return; } List stationIds = records.stream() .map(CollectionDockDevice::getStationId) .filter(id -> id != null) .distinct() .collect(Collectors.toList()); if (stationIds.isEmpty()) { return; } List stations = collectionStationMapper.selectBatchIds(stationIds); Map nameMap = stations.stream() .collect(Collectors.toMap(CollectionStation::getId, CollectionStation::getName, (a, b) -> a)); records.forEach(row -> row.setStationName(nameMap.get(row.getStationId()))); } private void syncStationStatusInternal(CollectionStation station) { Date now = new Date(); CollectionStation update = new CollectionStation(); update.setId(station.getId()); update.setLastSyncTime(now); update.setEditDate(now); boolean online = isapiClient.isOnline(station); update.setOnline(online ? Constants.ONE : Constants.ZERO); if (online) { DeviceInfoDTO deviceInfo = isapiClient.getDeviceInfo(station); if (deviceInfo != null) { if (StringUtils.isNotBlank(deviceInfo.getSerialNumber())) { update.setSerialNo(deviceInfo.getSerialNumber()); } if (StringUtils.isNotBlank(deviceInfo.getFirmwareVersion())) { update.setSoftwareVersion(deviceInfo.getFirmwareVersion()); } if (StringUtils.isBlank(station.getName()) && StringUtils.isNotBlank(deviceInfo.getDeviceName())) { update.setName(deviceInfo.getDeviceName()); } if (StringUtils.isNotBlank(deviceInfo.getModel())) { update.setModel(deviceInfo.getModel()); } } DockStationBasicInfoDTO basicInfo = isapiClient.getDockBasicInfo(station); if (basicInfo != null && StringUtils.isNotBlank(basicInfo.getDockStationId()) && StringUtils.isBlank(station.getSerialNo())) { update.setSerialNo(basicInfo.getDockStationId()); } StorageInfoDTO storage = isapiClient.getStorageInfo(station); if (storage != null) { update.setTotalSpace(storage.getTotalSpaceGb()); update.setFreeSpace(storage.getFreeSpaceGb()); } syncDockDevices(station.getId(), now); } collectionStationMapper.updateById(update); } private void syncDockDevices(Integer stationId, Date now) { CollectionStation station = collectionStationMapper.selectById(stationId); if (station == null) { return; } List devices = isapiClient.getDockDevices(station); for (DockDeviceDTO src : devices) { if (StringUtils.isBlank(src.getDeviceId())) { continue; } CollectionDockDevice existed = collectionDockDeviceMapper.selectOne(new QueryWrapper().lambda() .eq(CollectionDockDevice::getStationId, stationId) .eq(CollectionDockDevice::getDeviceId, src.getDeviceId()) .eq(CollectionDockDevice::getIsdeleted, Constants.ZERO)); if (existed == null) { CollectionDockDevice row = new CollectionDockDevice(); row.setStationId(stationId); row.setDeviceId(src.getDeviceId()); row.setDeviceName(src.getDeviceName()); row.setShortSerialNumber(src.getShortSerialNumber()); row.setAccessDeviceId(src.getAccessDeviceId()); row.setNetworkedDevice(Boolean.TRUE.equals(src.getNetworkedDevice()) ? Constants.ONE : Constants.ZERO); row.setLastSyncTime(now); row.setCreateDate(now); row.setIsdeleted(Constants.ZERO); collectionDockDeviceMapper.insert(row); } else { CollectionDockDevice row = new CollectionDockDevice(); row.setId(existed.getId()); row.setDeviceName(src.getDeviceName()); row.setShortSerialNumber(src.getShortSerialNumber()); row.setAccessDeviceId(src.getAccessDeviceId()); row.setNetworkedDevice(Boolean.TRUE.equals(src.getNetworkedDevice()) ? Constants.ONE : Constants.ZERO); row.setLastSyncTime(now); collectionDockDeviceMapper.updateById(row); } } } private QueryWrapper buildQueryWrapper(CollectionStation model) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(CollectionStation::getIsdeleted, Constants.ZERO) .like(StringUtils.isNotBlank(model.getName()), CollectionStation::getName, model.getName()) .like(StringUtils.isNotBlank(model.getIp()), CollectionStation::getIp, model.getIp()) .eq(model.getOnline() != null, CollectionStation::getOnline, model.getOnline()) .eq(model.getStatus() != null, CollectionStation::getStatus, model.getStatus()) .orderByDesc(CollectionStation::getId); return wrapper; } private void validateStation(CollectionStation station) { if (StringUtils.isBlank(station.getIp()) || StringUtils.isBlank(station.getUsername()) || StringUtils.isBlank(station.getPassword())) { throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "IP、用户名、密码不能为空"); } } private int defaultPort(CollectionStation station) { if (station.getUseHttps() != null && station.getUseHttps() == 1) { return 443; } return 80; } }