DeviceService 简单封装

This commit is contained in:
shikong 2023-09-07 14:12:28 +08:00
parent bd9e15f232
commit a7e453fbda

View File

@ -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<WvpProxyDevice> getDeviceById(Long id){
return deviceMapper.selectOne(s->
s.where(WvpProxyDeviceDynamicSqlSupport.id, isEqualTo(id)));
}
public Optional<WvpProxyDevice> getDeviceByDeviceCode(String deviceCode){
return deviceMapper.selectOne(s->
s.where(WvpProxyDeviceDynamicSqlSupport.deviceCode,isEqualTo(deviceCode)));
}
public Optional<WvpProxyDevice> 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<WvpProxyDevice> 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<WvpProxyDevice> getDevicesWithPage(int page, int size, ISelect select){
PageInfo<WvpProxyDevice> pageInfo;
try (Page<WvpProxyDevice> startPage = PageHelper.startPage(page, size)) {
pageInfo = startPage.doSelectPageInfo(select);
}
return pageInfo;
}
}