diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/device/DeviceService.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/device/DeviceService.java new file mode 100644 index 0000000..7b78604 --- /dev/null +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/device/DeviceService.java @@ -0,0 +1,114 @@ +package cn.skcks.docking.gb28181.wvp.service.device; + +import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.mapper.WvpProxyDeviceDynamicSqlSupport; +import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.mapper.WvpProxyDeviceMapper; +import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDevice; +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.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo; + +@Slf4j +@RequiredArgsConstructor +@Service +public class DeviceService { + private final WvpProxyDeviceMapper deviceMapper; + + public Optional getDeviceById(Long id){ + return deviceMapper.selectOne(s-> + s.where(WvpProxyDeviceDynamicSqlSupport.id, isEqualTo(id))); + } + + public Optional getDeviceByDeviceCode(String deviceCode){ + return deviceMapper.selectOne(s-> + s.where(WvpProxyDeviceDynamicSqlSupport.deviceCode,isEqualTo(deviceCode))); + } + + public Optional getDeviceByGbDeviceId(String gbDeviceId){ + return deviceMapper.selectOne(s-> + s.where(WvpProxyDeviceDynamicSqlSupport.gbDeviceId,isEqualTo(gbDeviceId))); + } + + /** + * 添加设备 + * @param device 设备 + * @return 是否成功 + */ + public boolean addDevice(WvpProxyDevice device){ + if(device == null){ + return false; + } + + String deviceCode = device.getDeviceCode(); + if(getDeviceByDeviceCode(deviceCode).isPresent()){ + log.info("设备编码 {} 已存在" ,deviceCode); + return false; + } + + String gbDeviceId = device.getGbDeviceId(); + if(getDeviceByGbDeviceId(gbDeviceId).isPresent()){ + log.info("国标编码 {} 已存在" ,gbDeviceId); + return false; + } + + return deviceMapper.insert(device) > 0; + } + + /** + * 依据 id 或 deviceCode 或 gbDeviceId 删除设备信息 + * @param device 设备 + * @return 是否成功 + */ + public boolean updateDevice(WvpProxyDevice device){ + if(device == null){ + return false; + } + + Long id = device.getId(); + String deviceCode = device.getDeviceCode(); + String gbDeviceId = device.getGbDeviceId(); + if(id != null){ + return deviceMapper.updateByPrimaryKey(device) > 0; + } else if(StringUtils.isNotBlank(deviceCode)){ + return deviceMapper.delete(d->d.where(WvpProxyDeviceDynamicSqlSupport.deviceCode,isEqualTo(deviceCode))) > 0; + } else if(StringUtils.isNotBlank(gbDeviceId)){ + return deviceMapper.delete(d->d.where(WvpProxyDeviceDynamicSqlSupport.gbDeviceId,isEqualTo(deviceCode))) > 0; + } else { + return false; + } + } + + /** + * 分页查询设备 + * @param page 页数 + * @param size 数量 + * @return 分页设备 + */ + public PageInfo getDevicesWithPage(int page, int size){ + ISelect select = () -> deviceMapper.select(u -> u.orderBy(WvpProxyDeviceDynamicSqlSupport.id.descending())); + return getDevicesWithPage(page,size, select); + } + + /** + * 分页查询设备 + * @param page 页数 + * @param size 数量 + * @param select 查询语句 + * @return 分页设备 + */ + public PageInfo getDevicesWithPage(int page, int size, ISelect select){ + PageInfo pageInfo; + try (Page startPage = PageHelper.startPage(page, size)) { + pageInfo = startPage.doSelectPageInfo(select); + } + return pageInfo; + } +}