Merge branch 'dev-V0.4.5' into dev-V0.4.5-postgre
commit
71f3004df6
|
@ -10,10 +10,15 @@ import cc.iotkit.data.manager.IDeviceInfoData;
|
||||||
import cc.iotkit.data.manager.IProductData;
|
import cc.iotkit.data.manager.IProductData;
|
||||||
import cc.iotkit.data.model.*;
|
import cc.iotkit.data.model.*;
|
||||||
import cc.iotkit.data.util.PageBuilder;
|
import cc.iotkit.data.util.PageBuilder;
|
||||||
|
import cc.iotkit.data.util.PredicateBuilder;
|
||||||
import cc.iotkit.model.device.DeviceInfo;
|
import cc.iotkit.model.device.DeviceInfo;
|
||||||
import cc.iotkit.model.product.Category;
|
import cc.iotkit.model.product.Category;
|
||||||
import cc.iotkit.model.product.Product;
|
import cc.iotkit.model.product.Product;
|
||||||
import cc.iotkit.model.stats.DataItem;
|
import cc.iotkit.model.stats.DataItem;
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import com.querydsl.core.types.Predicate;
|
||||||
|
import com.querydsl.core.types.Projections;
|
||||||
|
import com.querydsl.jpa.impl.JPAQuery;
|
||||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
@ -29,7 +34,10 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static cc.iotkit.data.model.QTbDeviceGroup.tbDeviceGroup;
|
||||||
|
import static cc.iotkit.data.model.QTbDeviceGroupMapping.tbDeviceGroupMapping;
|
||||||
import static cc.iotkit.data.model.QTbDeviceInfo.tbDeviceInfo;
|
import static cc.iotkit.data.model.QTbDeviceInfo.tbDeviceInfo;
|
||||||
|
import static cc.iotkit.data.model.QTbDeviceSubUser.tbDeviceSubUser;
|
||||||
import static cc.iotkit.data.model.QTbProduct.tbProduct;
|
import static cc.iotkit.data.model.QTbProduct.tbProduct;
|
||||||
|
|
||||||
@Primary
|
@Primary
|
||||||
|
@ -48,7 +56,6 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
|
||||||
|
|
||||||
private final DeviceTagRepository deviceTagRepository;
|
private final DeviceTagRepository deviceTagRepository;
|
||||||
|
|
||||||
private final JdbcTemplate jdbcTemplate;
|
|
||||||
|
|
||||||
@Qualifier("productDataCache")
|
@Qualifier("productDataCache")
|
||||||
private final IProductData productData;
|
private final IProductData productData;
|
||||||
|
@ -186,9 +193,8 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> findSubDeviceIds(String parentId) {
|
public List<String> findSubDeviceIds(String parentId) {
|
||||||
return jdbcTemplate.queryForList(
|
return jpaQueryFactory.select(tbDeviceInfo.deviceId).from(tbDeviceInfo)
|
||||||
"select device_id from device_info " +
|
.where(tbDeviceInfo.parentId.eq(parentId)).fetch();
|
||||||
"where parent_id=?", String.class, parentId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -213,131 +219,45 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
|
||||||
String productKey, String groupId,
|
String productKey, String groupId,
|
||||||
String state, String keyword,
|
String state, String keyword,
|
||||||
int page, int size) {
|
int page, int size) {
|
||||||
String sql = "SELECT\n" +
|
JPAQuery<TbDeviceInfo> query = jpaQueryFactory.selectFrom(tbDeviceInfo);
|
||||||
"a.id,\n" +
|
|
||||||
"a.device_id,\n" +
|
|
||||||
"a.product_key,\n" +
|
|
||||||
"a.device_name,\n" +
|
|
||||||
"a.model,\n" +
|
|
||||||
"a.secret,\n" +
|
|
||||||
"a.parent_id,\n" +
|
|
||||||
"a.longitude,\n" +
|
|
||||||
"a.latitude,\n" +
|
|
||||||
"a.uid,\n" +
|
|
||||||
"a.state,\n" +
|
|
||||||
"a.online_time,\n" +
|
|
||||||
"a.offline_time,\n" +
|
|
||||||
"a.create_at\n" +
|
|
||||||
"FROM device_info a ";
|
|
||||||
|
|
||||||
if (StringUtils.isNotBlank(groupId)) {
|
if (StringUtils.isNotBlank(uid)) {
|
||||||
sql += " JOIN device_group_mapping b on a.device_id=b.device_id\n" +
|
query.where(tbDeviceInfo.uid.eq(uid));
|
||||||
" JOIN device_group c on b.group_id=c.id ";
|
|
||||||
}
|
|
||||||
if (StringUtils.isNotBlank(subUid)) {
|
|
||||||
sql += " JOIN device_sub_user d on d.device_id=a.device_id ";
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Object> args = new ArrayList<>();
|
|
||||||
sql += " where 1=1 ";
|
|
||||||
if (StringUtils.isNotBlank(groupId)) {
|
|
||||||
sql += "and c.id=? ";
|
|
||||||
args.add(groupId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtils.isNotBlank(subUid)) {
|
if (StringUtils.isNotBlank(subUid)) {
|
||||||
sql += "and d.uid=? ";
|
query.join(tbDeviceSubUser).on(tbDeviceSubUser.deviceId.eq(tbDeviceInfo.deviceId));
|
||||||
args.add(subUid);
|
query.where(tbDeviceSubUser.uid.eq(subUid));
|
||||||
} else if (StringUtils.isNotBlank(uid)) {
|
|
||||||
sql += "and a.uid=? ";
|
|
||||||
args.add(uid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtils.isNotBlank(productKey)) {
|
if (StringUtils.isNotBlank(productKey)) {
|
||||||
sql += "and a.product_key=? ";
|
query.where(tbDeviceInfo.productKey.eq(productKey));
|
||||||
args.add(productKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtils.isNotBlank(state)) {
|
if (StringUtils.isNotBlank(state)) {
|
||||||
sql += "and a.state=? ";
|
query.where(tbDeviceInfo.state.eq(state));
|
||||||
args.add(state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtils.isNotBlank(keyword)) {
|
if (StringUtils.isNotBlank(keyword)) {
|
||||||
keyword = "%" + keyword.trim() + "%";
|
query.where(tbDeviceInfo.deviceId.like("%" + keyword + "%")
|
||||||
sql += "and (a.device_id like ? or a.device_name like ?) ";
|
.or(tbDeviceInfo.deviceName.like("%" + keyword + "%")));
|
||||||
args.add(keyword);
|
|
||||||
args.add(keyword);//两个参数
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sql += String.format("order by create_at desc limit %d,%d", (page - 1) * size, size);
|
query.orderBy(tbDeviceInfo.createAt.desc());
|
||||||
|
query.offset((page - 1) * size).limit(size);
|
||||||
|
|
||||||
List<DeviceInfo> list = jdbcTemplate.query(sql, (rs, rowNum) -> DeviceInfo.builder()
|
List<TbDeviceInfo> tbDeviceInfos = query.fetch();
|
||||||
.id(rs.getString("id"))
|
long total = query.fetchCount();
|
||||||
.deviceId(rs.getString("device_id"))
|
List<DeviceInfo> deviceInfos = new ArrayList<>(tbDeviceInfos.size());
|
||||||
.deviceName(rs.getString("device_name"))
|
for (TbDeviceInfo tbDeviceInfo : tbDeviceInfos) {
|
||||||
.productKey(rs.getString("product_key"))
|
DeviceInfo deviceInfo = MapstructUtils.convert(tbDeviceInfo, DeviceInfo.class);
|
||||||
.model(rs.getString("model"))
|
fillDeviceInfo(tbDeviceInfo.getDeviceId(), tbDeviceInfo, deviceInfo);
|
||||||
.secret(rs.getString("secret"))
|
deviceInfos.add(deviceInfo);
|
||||||
.parentId(rs.getString("parent_id"))
|
|
||||||
.locate(new DeviceInfo.Locate(rs.getString("longitude"), rs.getString("latitude")))
|
|
||||||
.uid(rs.getString("uid"))
|
|
||||||
.state(new DeviceInfo.State(
|
|
||||||
"online".equals(rs.getString("state")),
|
|
||||||
rs.getLong("online_time"),
|
|
||||||
rs.getLong("offline_time")
|
|
||||||
))
|
|
||||||
.createAt(rs.getLong("create_at"))
|
|
||||||
.build(), args.toArray());
|
|
||||||
|
|
||||||
sql = sql.replaceAll("SELECT[\\s\\S]+FROM", "SELECT count(*) FROM ");
|
|
||||||
sql = sql.replaceAll("order by create_at desc limit.*", "");
|
|
||||||
Long total = jdbcTemplate.queryForObject(sql, Long.class, args.toArray());
|
|
||||||
|
|
||||||
//把当前页的deviceId串连起来作为in的参数
|
|
||||||
String deviceIds = list.stream().map(d -> "'" + d.getDeviceId() + "'").collect(Collectors.joining(","));
|
|
||||||
|
|
||||||
//取设备所属分组
|
|
||||||
List<DeviceIdGroup> groups = list.isEmpty() ? new ArrayList<>() :
|
|
||||||
jdbcTemplate.query("SELECT \n" +
|
|
||||||
"a.id,\n" +
|
|
||||||
"a.name, \n" +
|
|
||||||
"b.device_id as deviceId \n" +
|
|
||||||
"FROM\n" +
|
|
||||||
"device_group a \n" +
|
|
||||||
"JOIN device_group_mapping b on a.id=b.group_id\n" +
|
|
||||||
String.format("WHERE b.device_id in(%s)", deviceIds), new BeanPropertyRowMapper<>(DeviceIdGroup.class));
|
|
||||||
|
|
||||||
//取设备标签
|
|
||||||
// List<TbDeviceTag> tags = list.size() == 0 ? new ArrayList<>() :
|
|
||||||
// jdbcTemplate.query("\n" +
|
|
||||||
// "SELECT\n" +
|
|
||||||
// "a.id,\n" +
|
|
||||||
// "a.code,\n" +
|
|
||||||
// "a.name,\n" +
|
|
||||||
// "a.value\n" +
|
|
||||||
// "FROM device_tag a " +
|
|
||||||
// String.format("WHERE a.device_id IN(%s)", deviceIds), new BeanPropertyRowMapper<>(TbDeviceTag.class));
|
|
||||||
|
|
||||||
for (DeviceInfo device : list) {
|
|
||||||
//设置设备分组
|
|
||||||
Map<String, DeviceInfo.Group> groupMap = new HashMap<>();
|
|
||||||
groups.stream().filter(g -> device.getDeviceId().equals(g.getDeviceId()))
|
|
||||||
.forEach(g -> groupMap.put(g.getId(),
|
|
||||||
new DeviceInfo.Group(g.getId(), g.getName())));
|
|
||||||
device.setGroup(groupMap);
|
|
||||||
|
|
||||||
//设置设备标签
|
|
||||||
// Map<String, DeviceInfo.Tag> tagMap = new HashMap<>();
|
|
||||||
// tags.stream().filter(t -> device.getDeviceId().equals(t.getDeviceId()))
|
|
||||||
// .forEach(t -> tagMap.put(t.getCode(),
|
|
||||||
// new DeviceInfo.Tag(t.getCode(), t.getName(), t.getValue())));
|
|
||||||
// device.setTag(tagMap);
|
|
||||||
}
|
}
|
||||||
|
return new Paging<>(total, deviceInfos);
|
||||||
return new Paging<>(total, list);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateTag(String deviceId, DeviceInfo.Tag tag) {
|
public void updateTag(String deviceId, DeviceInfo.Tag tag) {
|
||||||
TbDeviceTag deviceTag = deviceTagRepository.findByDeviceIdAndCode(deviceId, tag.getId());
|
TbDeviceTag deviceTag = deviceTagRepository.findByDeviceIdAndCode(deviceId, tag.getId());
|
||||||
|
@ -361,20 +281,19 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
|
||||||
@Override
|
@Override
|
||||||
public List<DataItem> getDeviceStatsByCategory(String uid) {
|
public List<DataItem> getDeviceStatsByCategory(String uid) {
|
||||||
//先按产品统计设备数量
|
//先按产品统计设备数量
|
||||||
String sql = "SELECT COUNT(*) as value,product_key as name from " +
|
JPAQuery<DataItem> query = jpaQueryFactory.select(Projections.bean(DataItem.class,
|
||||||
"device_info %s GROUP BY product_key";
|
tbDeviceInfo.productKey,
|
||||||
List<Object> args = new ArrayList<>();
|
tbDeviceInfo.count()))
|
||||||
|
.from(tbDeviceInfo)
|
||||||
|
.groupBy(tbDeviceInfo.productKey);
|
||||||
|
|
||||||
if (StringUtils.isNotBlank(uid)) {
|
if (StringUtils.isNotBlank(uid)) {
|
||||||
sql = String.format(sql, "where uid=:uid");
|
query.where(tbDeviceInfo.uid.eq(uid));
|
||||||
args.add(uid);
|
|
||||||
} else {
|
|
||||||
sql = String.format(sql, "");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
List<DataItem> stats = new ArrayList<>();
|
List<DataItem> stats = new ArrayList<>();
|
||||||
|
|
||||||
List<DataItem> rst = jdbcTemplate.query(sql,
|
List<DataItem> rst = query.fetch();
|
||||||
new BeanPropertyRowMapper<>(DataItem.class),
|
|
||||||
args.toArray());
|
|
||||||
for (DataItem item : rst) {
|
for (DataItem item : rst) {
|
||||||
//找到产品对应的品类取出品类名
|
//找到产品对应的品类取出品类名
|
||||||
Product product = productData.findByProductKey(item.getName());
|
Product product = productData.findByProductKey(item.getName());
|
||||||
|
@ -427,8 +346,10 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void removeGroup(String deviceId, String groupId) {
|
public void removeGroup(String deviceId, String groupId) {
|
||||||
jdbcTemplate.update("delete from device_group_mapping " +
|
jpaQueryFactory.delete(tbDeviceGroupMapping)
|
||||||
"where device_id=? and group_id=?", deviceId, groupId);
|
.where(tbDeviceGroupMapping.deviceId.eq(deviceId)
|
||||||
|
.and(tbDeviceGroupMapping.groupId.eq(groupId)))
|
||||||
|
.execute();
|
||||||
//更新设备数量
|
//更新设备数量
|
||||||
updateGroupDeviceCount(groupId);
|
updateGroupDeviceCount(groupId);
|
||||||
}
|
}
|
||||||
|
@ -436,8 +357,9 @@ public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceI
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void removeGroup(String groupId) {
|
public void removeGroup(String groupId) {
|
||||||
jdbcTemplate.update("delete from device_group_mapping " +
|
jpaQueryFactory.delete(tbDeviceGroupMapping)
|
||||||
"where group_id=?", groupId);
|
.where(tbDeviceGroupMapping.groupId.eq(groupId))
|
||||||
|
.execute();
|
||||||
//更新设备数量
|
//更新设备数量
|
||||||
updateGroupDeviceCount(groupId);
|
updateGroupDeviceCount(groupId);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue