diff --git a/api/src/main/java/cn/skcks/docking/gb28181/api/device/DeviceChannelController.java b/api/src/main/java/cn/skcks/docking/gb28181/api/device/DeviceChannelController.java new file mode 100644 index 0000000..44209be --- /dev/null +++ b/api/src/main/java/cn/skcks/docking/gb28181/api/device/DeviceChannelController.java @@ -0,0 +1,39 @@ +package cn.skcks.docking.gb28181.api.device; + +import cn.skcks.docking.gb28181.annotation.web.JsonMapping; +import cn.skcks.docking.gb28181.annotation.web.methods.GetJson; +import cn.skcks.docking.gb28181.api.device.dto.DeviceChannelPageDTO; +import cn.skcks.docking.gb28181.common.json.JsonResponse; +import cn.skcks.docking.gb28181.common.page.PageWrapper; +import cn.skcks.docking.gb28181.config.SwaggerConfig; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDeviceChannel; +import cn.skcks.docking.gb28181.service.device.DeviceChannelService; +import com.github.pagehelper.PageInfo; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springdoc.core.annotations.ParameterObject; +import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.context.annotation.Bean; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RestController; + +@Tag(name = "设备通道") +@JsonMapping("/api/device/channel") +@RestController +@RequiredArgsConstructor +public class DeviceChannelController { + private final DeviceChannelService deviceChannelService; + + @Bean + public GroupedOpenApi deviceChannelApi() { + return SwaggerConfig.api("DeviceChannel", "/api/device/channel"); + } + + @Operation(summary = "分页查询 设备通道信息") + @GetJson("/page") + public JsonResponse> getWithPage(@ParameterObject @Validated DeviceChannelPageDTO dto){ + PageInfo withPage = deviceChannelService.getWithPage(dto.getPage(), dto.getSize()); + return JsonResponse.success(PageWrapper.of(withPage)); + } +} diff --git a/api/src/main/java/cn/skcks/docking/gb28181/api/device/dto/DeviceChannelPageDTO.java b/api/src/main/java/cn/skcks/docking/gb28181/api/device/dto/DeviceChannelPageDTO.java new file mode 100644 index 0000000..eb3471a --- /dev/null +++ b/api/src/main/java/cn/skcks/docking/gb28181/api/device/dto/DeviceChannelPageDTO.java @@ -0,0 +1,17 @@ +package cn.skcks.docking.gb28181.api.device.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +@Data +public class DeviceChannelPageDTO { + @Schema(description = "页数", example = "1") + @Min(value = 1, message = "page 必须为正整数") + Integer page; + + @Schema(description = "每页条数", example = "10") + @Min(value = 1, message = "size 必须为正整数") + Integer size; +} diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/catalog/CatalogService.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/catalog/CatalogService.java index b1617f3..253d1c7 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/catalog/CatalogService.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/catalog/CatalogService.java @@ -5,7 +5,10 @@ import cn.skcks.docking.gb28181.core.sip.message.request.SipRequestBuilder; import cn.skcks.docking.gb28181.core.sip.message.subscribe.GenericSubscribe; import cn.skcks.docking.gb28181.core.sip.message.subscribe.SipSubscribe; import cn.skcks.docking.gb28181.core.sip.service.SipService; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.mapper.DockingDeviceChannelMapper; import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDevice; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDeviceChannel; +import cn.skcks.docking.gb28181.service.device.DeviceChannelService; import cn.skcks.docking.gb28181.service.docking.device.cache.DockingDeviceCacheService; import cn.skcks.docking.gb28181.sip.manscdp.catalog.query.CatalogQueryDTO; import cn.skcks.docking.gb28181.sip.manscdp.catalog.response.CatalogItemDTO; @@ -37,6 +40,7 @@ public class CatalogService { private final DockingDeviceCacheService deviceCacheService; private final SipConfig sipConfig; private final SipSubscribe subscribe; + private final DeviceChannelService deviceChannelService; @SneakyThrows public CompletableFuture> catalog(String gbDeviceId){ @@ -100,6 +104,16 @@ public class CatalogService { @Override public void onComplete() { log.info("{} 返回结果 {}", key, result.complete(data)); + + data.stream().map(item->{ + DockingDeviceChannel model = new DockingDeviceChannel(); + model.setGbDeviceId(device.getDeviceId()); + model.setGbDeviceChannelId(item.getDeviceId()); + model.setName(item.getName()); + model.setAddress(item.getAddress()); + return model; + }).forEach(deviceChannelService::add); + subscribe.getSipRequestSubscribe().delPublisher(key); } }); diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/device/DeviceChannelService.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/device/DeviceChannelService.java new file mode 100644 index 0000000..776e7bc --- /dev/null +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/device/DeviceChannelService.java @@ -0,0 +1,112 @@ +package cn.skcks.docking.gb28181.service.device; + +import cn.skcks.docking.gb28181.common.json.JsonException; +import cn.skcks.docking.gb28181.common.json.JsonUtils; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.mapper.DockingDeviceChannelDynamicSqlSupport; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.mapper.DockingDeviceChannelMapper; +import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDeviceChannel; +import com.github.pagehelper.ISelect; +import com.github.pagehelper.Page; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.mybatis.dynamic.sql.AndOrCriteriaGroup; +import org.mybatis.dynamic.sql.SqlBuilder; +import org.mybatis.dynamic.sql.where.WhereDSL; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo; + +@Slf4j +@Service +@RequiredArgsConstructor +public class DeviceChannelService { + private final DockingDeviceChannelMapper deviceChannelMapper; + + public Optional getDeviceChannelById(Long id){ + return deviceChannelMapper.selectOne(s->s.where(DockingDeviceChannelDynamicSqlSupport.id, isEqualTo(id))); + } + + public List getDeviceByGbDeviceId(String gbDeviceId){ + return deviceChannelMapper.select(s-> + s.where(DockingDeviceChannelDynamicSqlSupport.gbDeviceId,isEqualTo(gbDeviceId))); + } + + public Optional getDeviceChannel(String gbDeviceId,String gbDeviceChannelId){ + return deviceChannelMapper.selectOne(s-> + s.where() + .and(DockingDeviceChannelDynamicSqlSupport.gbDeviceId, isEqualTo(gbDeviceId)) + .and(DockingDeviceChannelDynamicSqlSupport.gbDeviceChannelId, isEqualTo(gbDeviceChannelId)) + .limit(1)); + } + + /** + * 分页查询 + * @param page 页数 + * @param size 数量 + * @param select 查询语句 + * @return 分页设备 + */ + public PageInfo getWithPage(int page, int size, ISelect select){ + PageInfo pageInfo; + try (Page startPage = PageHelper.startPage(page, size)) { + pageInfo = startPage.doSelectPageInfo(select); + } + return pageInfo; + } + + public PageInfo getWithPage(int page, int size){ + ISelect select = () -> deviceChannelMapper.select(u -> u.orderBy(DockingDeviceChannelDynamicSqlSupport.id.descending())); + return getWithPage(page, size, select); + } + + @SneakyThrows + @Transactional + public boolean add(DockingDeviceChannel model){ + if(model == null){ + return false; + } + + String gbDeviceId = model.getGbDeviceId(); + String gbDeviceChannelId = model.getGbDeviceChannelId(); + if(StringUtils.isAnyBlank(gbDeviceChannelId,gbDeviceId)){ + throw new JsonException("国标id 或 通道id 不能为空"); + } + + Optional deviceChannel = getDeviceChannel(gbDeviceId, gbDeviceChannelId); + log.info("{}", JsonUtils.toJson(deviceChannel.orElse(null))); + return deviceChannelMapper.insert(model) > 0; + } + + @SneakyThrows + @Transactional + public boolean del(DockingDeviceChannel model){ + if(model == null){ + return false; + } + + Long id = model.getId(); + String gbDeviceId = model.getGbDeviceId(); + String gbDeviceChannelId = model.getGbDeviceChannelId(); + if(id != null){ + return deviceChannelMapper.deleteByPrimaryKey(id) > 0; + } + + if(StringUtils.isBlank(gbDeviceId)){ + throw new JsonException("国标id 不能为空"); + } + + if(StringUtils.isBlank(gbDeviceChannelId)){ + return deviceChannelMapper.delete(d->d.where(DockingDeviceChannelDynamicSqlSupport.gbDeviceId, isEqualTo(gbDeviceId))) > 0; + } + + return deviceChannelMapper.delete(d->d.where(DockingDeviceChannelDynamicSqlSupport.gbDeviceId, isEqualTo(gbDeviceId))) > 0; + } +}