模糊查询优化和缓存优化

master
liuguijing 2 years ago
parent 37cbdcbde8
commit 862ce46b28

@ -20,8 +20,4 @@ public class LineAndDyNameDto {
private Integer status;
private Integer dyId;
private Integer dyValue;
}

@ -3,6 +3,7 @@ package com.shxy.xymanager_common.dto;
import lombok.Data;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;
@Data
@ -43,9 +44,7 @@ public class TerminalsWithHeart implements Serializable {
private Date updateTime;
private Long lastheartbeat;
private Integer onlinestatus;
private BigInteger lastheartbeat;
private static final long serialVersionUID = 1L;

@ -3,6 +3,7 @@ package com.shxy.xymanager_common.entity;
import lombok.Data;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;
@Data
@ -41,7 +42,7 @@ public class Terminals implements Serializable {
private Date updateTime;
private Long lastheartbeat;
private BigInteger lastheartbeat;
private Integer onlinestatus;

@ -3,7 +3,6 @@ package com.shxy.xymanager_dao.dao;
import com.shxy.xymanager_common.dto.ChannelAndTermDto;
import com.shxy.xymanager_common.dto.TerminalInfoDto;
import com.shxy.xymanager_common.dto.TerminalsAndStatusDto;
import com.shxy.xymanager_common.entity.TerminalStatus;
import com.shxy.xymanager_common.entity.Terminals;
import com.shxy.xymanager_common.vo.RelateTerminalListRuleIdVo;
import com.shxy.xymanager_common.vo.TerminalIdUpdateVo;
@ -49,6 +48,4 @@ public interface TerminalsDao {
int updateCmd(TerminalIdUpdateVo vo);
List<TerminalStatus> getTerminalStatus(@Param("list")List<Terminals> list);
}

@ -18,8 +18,6 @@
<result column="dy_level_id" jdbcType="INTEGER" property="dyLevelId"/>
<result column="dy_level_name" jdbcType="INTEGER" property="dyLevelName"/>
<result column="status" jdbcType="INTEGER" property="status"/>
<result column="dy_id" jdbcType="INTEGER" property="dyId"/>
<result column="dy_value" jdbcType="INTEGER" property="dyValue"/>
</resultMap>
<sql id="Base_Column_List">
@ -32,9 +30,7 @@
x.bs_manufacturer as bs_manufacturer,
x.dy_level_id as dy_level_id,
x.status as status,
y.name as dy_level_name,
y.dy_value as dy_value,
y.id as dy_id
y.name as dy_level_name
from `lines` x,
dy_level y
where x.dy_level_id = y.id

@ -56,7 +56,6 @@
where a.term_id = b.id and b.cmdid = #{cmdid}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>

@ -43,7 +43,7 @@
<result column="boot_time" jdbcType="INTEGER" property="bootTime"/>
<result column="battery_capacity" jdbcType="FLOAT" property="batteryCapacity"/>
<result column="battery_voltage" jdbcType="FLOAT" property="batteryVoltage"/>
<result column="last_heartbeat" jdbcType="INTEGER" property="lastHeartbeat"/>
<result column="last_heartbeat" jdbcType="BIGINT" property="lastHeartbeat"/>
</resultMap>
<resultMap id="ChannelAndTerm" type="com.shxy.xymanager_common.dto.ChannelAndTermDto">
@ -506,15 +506,4 @@
set cmdid = #{newCmdId,jdbcType=VARCHAR}
where cmdid = #{cmdId,jdbcType=VARCHAR}
</update>
<select id="getTerminalStatus" resultType="com.shxy.xymanager_common.entity.TerminalStatus">
select
term_id as termId,
last_heartbeat as lastHeartbeat
from terminal_status
where term_id in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
#{item.id}
</foreach>
</select>
</mapper>

@ -16,7 +16,7 @@ public class XyCache {
@PostConstruct
public void init() {
//系统启动中。。。加载codeMap
cacheService.updateDyMap();
// cacheService.updateDyMap();
cacheService.updateLineMap();

@ -1,6 +1,5 @@
package com.shxy.xymanager_service.impl;
import com.google.common.collect.Maps;
import com.shxy.xymanager_common.dto.LineAndDyNameDto;
import com.shxy.xymanager_common.dto.TermAndChannelDto;
import com.shxy.xymanager_common.dto.TowerDto;
@ -8,16 +7,18 @@ import com.shxy.xymanager_common.entity.*;
import com.shxy.xymanager_common.enums.CommonStatus;
import com.shxy.xymanager_dao.dao.*;
import com.shxy.xymanager_service.service.CacheService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@ -49,9 +50,6 @@ public class CacheServiceImpl implements CacheService {
@Autowired
TerminalImgAlarmParamsDao terminalImgAlarmParamsDao;
@Autowired
TerminalStatusDao terminalStatusDao;
// @Override
// @Cacheable(value = "globalParams", key = "#param")
// public GlobalParams getGlobalParams(String param) {
@ -292,28 +290,6 @@ public class CacheServiceImpl implements CacheService {
return lineMap;
}
@Override
@Cacheable(value = "dyMap")
public Map<Integer,List<LineAndDyNameDto>> getDyMap() {
Map<Integer, List<LineAndDyNameDto>> lineMap;
List<LineAndDyNameDto> lineAndDyNameDtos = linesDao.selectAll(CommonStatus.EFFECTIVE.value());
lineMap = lineAndDyNameDtos.stream().collect(Collectors.groupingBy(LineAndDyNameDto::getDyId));
return lineMap;
}
@Override
@CachePut(value = "dyMap")
public Map<Integer, List<LineAndDyNameDto>> updateDyMap() {
Map<Integer, List<LineAndDyNameDto>> lineMap;
List<LineAndDyNameDto> lineAndDyNameDtos = linesDao.selectAll(CommonStatus.EFFECTIVE.value());
lineMap = lineAndDyNameDtos.stream().collect(Collectors.groupingBy(LineAndDyNameDto::getDyId));
return lineMap;
}
@Override
@CachePut(value = "globalMap")

@ -5,23 +5,18 @@ import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.shxy.xymanager_common.bean.ServiceBody;
import com.shxy.xymanager_common.dto.*;
import com.shxy.xymanager_common.dto.DyLineAndTerminalWithHeartDto;
import com.shxy.xymanager_common.entity.DyLevel;
import com.shxy.xymanager_common.entity.TerminalStatus;
import com.shxy.xymanager_common.entity.Terminals;
import com.shxy.xymanager_common.dto.TerminalsWithHeart;
import com.shxy.xymanager_common.enums.CommonStatus;
import com.shxy.xymanager_common.exception.Asserts;
import com.shxy.xymanager_common.model.DyLineTreeListModel;
import com.shxy.xymanager_common.model.DyListModel;
import com.shxy.xymanager_common.util.MyDateUtils;
import com.shxy.xymanager_common.util.TerminalUtils;
import com.shxy.xymanager_common.vo.TestVo;
import com.shxy.xymanager_dao.dao.DyLevelDao;
import com.shxy.xymanager_dao.dao.TerminalStatusDao;
import com.shxy.xymanager_dao.dao.TerminalsDao;
import com.shxy.xymanager_service.service.CacheService;
import com.shxy.xymanager_service.service.DyLevelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@ -31,8 +26,6 @@ import org.springframework.stereotype.Service;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
@ -44,15 +37,6 @@ public class DyLevelServiceImpl implements DyLevelService {
@Autowired
DyLevelDao dyLevelDao;
@Value("${heart.time}")
public Integer hearttime;
@Autowired
CacheService cacheService;
@Autowired
private TerminalsDao terminalsDao;
/**
* 线
*
@ -61,99 +45,31 @@ public class DyLevelServiceImpl implements DyLevelService {
@Override
public ServiceBody<DyLineTreeListModel> getdyTreeList() {
DyLineTreeListModel model = new DyLineTreeListModel();
/*List<DyLineAndTerminalWithHeartDto> list = dyLevelDao.selectTreeList(CommonStatus.EFFECTIVE.value());*/
List<DyLineAndTerminalWithHeartDto> list = Lists.newArrayList();
getDyList(list);
List<DyLineAndTerminalWithHeartDto> list = dyLevelDao.selectTreeList(CommonStatus.EFFECTIVE.value());
boolean empty = CollectionUtil.isEmpty(list);
if (empty) {
model.setList(new ArrayList<>());
} else {
List<DyLineTreeListModel.DyListBean> beans = BeanUtil.copyToList(list, DyLineTreeListModel.DyListBean.class, CopyOptions.create().ignoreCase());
/*for (int i = 0; i < beans.size(); i++) {
for (int i = 0; i < beans.size(); i++) {
for (int j = 0; j < beans.get(i).getList().size(); j++) {
for (int k = 0; k < beans.get(i).getList().get(j).getList().size(); k++) {
// beans.get(i).getList().get(j).getList().get(k).setName(list.get(i).getList().get(j).getList().get(k).getDisplayName());
TerminalsWithHeart terminalsWithHeart = list.get(i).getList().get(j).getList().get(k);
if (!BeanUtil.isEmpty(terminalsWithHeart)) {
Long lastheartbeat = terminalsWithHeart.getLastheartbeat();
if (!BeanUtil.isEmpty(lastheartbeat)) {
long time = MyDateUtils.TimeSecond2MillSecond(lastheartbeat);
DateTime date = MyDateUtils.date(time);
long between = MyDateUtils.between(MyDateUtils.getNowDate(), date, DateUnit.MINUTE);
if (between > hearttime) {
beans.get(i).getList().get(j).getList().get(k).setOnlinestatus(CommonStatus.DELETE.value());
} else {
beans.get(i).getList().get(j).getList().get(k).setOnlinestatus(CommonStatus.EFFECTIVE.value());
}
}
BigInteger lastheartbeat = terminalsWithHeart.getLastheartbeat();
boolean b = TerminalUtils.judgeTerminalStatus(lastheartbeat);
if (b) {
beans.get(i).getList().get(j).getList().get(k).setOnlinestatus(CommonStatus.DELETE.value());
} else {
beans.get(i).getList().get(j).getList().get(k).setOnlinestatus(CommonStatus.EFFECTIVE.value());
}
}
}
}*/
}
model.setList(beans);
}
return Asserts.success(model);
}
private void getDyList(List<DyLineAndTerminalWithHeartDto> list) {
List<LineAndTerminalWithHeartDto> lineList = Lists.newArrayList();
Map<Integer, Terminals> terminalsMap = cacheService.getTerminalMap();
List<Terminals> termList = terminalsMap.values().stream().collect(Collectors.toList());
Map<Integer, TerminalStatus> statusMap = Maps.newHashMap();
List<TerminalStatus> terminalStatusDtos = terminalsDao.getTerminalStatus(termList);
for (TerminalStatus status : terminalStatusDtos) {
statusMap.put(status.getTermId(), status);
}
Map<Integer, TowerDto> towerMap = cacheService.getTowerMap();
List<TowerDto> towerList = towerMap.values().stream().collect(Collectors.toList());
Map<Integer, TowerDto> towerByLineMap = Maps.newHashMap();
for (TowerDto item : towerList) {
towerByLineMap.put(item.getLineId(), item);
}
termList.stream().forEach(var->{
if(null!=statusMap.get(var.getId())&&null!=statusMap.get(var.getId()).getLastHeartbeat()) {
long time = MyDateUtils.TimeSecond2MillSecond(statusMap.get(var.getId()).getLastHeartbeat().longValue());
DateTime date = MyDateUtils.date(time);
long between = MyDateUtils.between(MyDateUtils.getNowDate(), date, DateUnit.MINUTE);
if (between > hearttime) {
var.setOnlinestatus(CommonStatus.DELETE.value());
} else {
var.setOnlinestatus(CommonStatus.EFFECTIVE.value());
}
var.setLastheartbeat(statusMap.get(var.getId()).getLastHeartbeat().longValue());
}
if(null!=towerByLineMap.get(var.getLineid())) {
var.setTowerid(towerByLineMap.get(var.getLineid()).getId());
}
});
Map<Integer, List<Terminals>> termByLineIdMap = termList.stream().collect(Collectors.groupingBy(Terminals::getLineid));
Map<Integer, List<LineAndDyNameDto>> map = cacheService.getDyMap();
map.forEach((key, value) -> {
DyLineAndTerminalWithHeartDto dyDto = new DyLineAndTerminalWithHeartDto();
dyDto.setId(key);
dyDto.setName(value.get(0).getName());
dyDto.setDyValue(value.get(0).getDyValue());
for(LineAndDyNameDto line:value) {
List<TerminalsWithHeart> terminalList = Lists.newArrayList();
if(null!=termByLineIdMap.get(line.getId())) {
terminalList = BeanUtil.copyToList(termByLineIdMap.get(line.getId()), TerminalsWithHeart.class, CopyOptions.create().ignoreCase());
}
LineAndTerminalWithHeartDto lineDto = new LineAndTerminalWithHeartDto();
lineDto.setBsManufacturer(line.getBsManufacturer());
lineDto.setDyLevel(line.getDyLevelId());
lineDto.setName(line.getName());
lineDto.setId(line.getId());
lineDto.setList(terminalList);
lineList.add(lineDto);
}
dyDto.setList(lineList);
list.add(dyDto);
});
}
/**
*

@ -15,6 +15,7 @@ import com.shxy.xymanager_common.enums.CommonStatus;
import com.shxy.xymanager_common.util.CmaUtil;
import com.shxy.xymanager_common.util.MyDateUtils;
import com.shxy.xymanager_common.util.ProcessExecUtils;
import com.shxy.xymanager_common.util.TerminalUtils;
import com.shxy.xymanager_common.vo.OpenCmdidVo;
import com.shxy.xymanager_common.vo.OpenDeviceCaptureVo;
import com.shxy.xymanager_common.vo.UserLoginVo;
@ -55,9 +56,6 @@ public class OpenServiceImpl implements OpenService {
@Value("${session.open_expire_time}")
public Integer time;
@Value("${heart.time}")
public Integer hearttime;
/**
*
*
@ -213,11 +211,9 @@ public class OpenServiceImpl implements OpenService {
jsonObject.set("errmsg", "device not register");
return jsonObject;
}
Long lastheartbeat = terminalsWithHeart.getLastheartbeat();
long time = MyDateUtils.TimeSecond2MillSecond(lastheartbeat);
DateTime date = MyDateUtils.date(time);
long between = MyDateUtils.between(MyDateUtils.getNowDate(), date, DateUnit.MINUTE);
if (between > hearttime) {
BigInteger lastheartbeat = terminalsWithHeart.getLastheartbeat();
boolean b = TerminalUtils.judgeTerminalStatus(lastheartbeat);
if (b) {
jsonObject.set("isonline", false);
jsonObject.set("errcode", 0);
jsonObject.set("errmsg", "ok");

@ -76,8 +76,6 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
@Value("${video.address}")
private String videoaddress;
@Value("${heart.time}")
public Integer hearttime;
/**
* idid

@ -10,7 +10,6 @@ import com.github.pagehelper.PageInfo;
import com.shxy.xymanager_common.bean.ServiceBody;
import com.shxy.xymanager_common.constant.Constants;
import com.shxy.xymanager_common.dto.*;
import com.shxy.xymanager_common.entity.Lines;
import com.shxy.xymanager_common.entity.TerminalChannelMapper;
import com.shxy.xymanager_common.entity.TerminalStatus;
import com.shxy.xymanager_common.entity.Terminals;
@ -75,9 +74,6 @@ public class TerminalServiceImpl implements TerminalService {
@Value("${photo.address}")
private String photoaddress;
@Value("${heart.time}")
public Integer hearttime;
/**
*
@ -100,12 +96,12 @@ public class TerminalServiceImpl implements TerminalService {
tower = null;
} else {
if (lineid == null || lineid.intValue() == -1) {
dyLineAndTowertDtos = dyLevelDao.selectDyAndLineAndTowerList(dyid, null, null,search);
dyLineAndTowertDtos = dyLevelDao.selectDyAndLineAndTowerList(dyid, null, null, search);
} else {
if (tower == null || tower.intValue() == -1) {
dyLineAndTowertDtos = dyLevelDao.selectDyAndLineAndTowerList(dyid, lineid, null,search);
dyLineAndTowertDtos = dyLevelDao.selectDyAndLineAndTowerList(dyid, lineid, null, search);
} else {
dyLineAndTowertDtos = dyLevelDao.selectDyAndLineAndTowerList(dyid, lineid, tower,search);
dyLineAndTowertDtos = dyLevelDao.selectDyAndLineAndTowerList(dyid, lineid, tower, search);
}
}
}
@ -120,9 +116,9 @@ public class TerminalServiceImpl implements TerminalService {
for (DyLineAndTowertDto item : dyLineAndTowertDtos) {
toweridlist.add(item.getTowerId());
}
list2 = terminalsDao.selectTermAndStatusList(null, toweridlist,search);
list2 = terminalsDao.selectTermAndStatusList(null, toweridlist, search);
} else {
list2 = terminalsDao.selectTermAndStatusList(null, null,search);
list2 = terminalsDao.selectTermAndStatusList(null, null, search);
}
boolean empty = CollectionUtil.isEmpty(list2);
if (empty) {
@ -390,19 +386,13 @@ public class TerminalServiceImpl implements TerminalService {
list.stream().forEach(var -> {
var.getList().forEach(item -> {
item.getList().forEach(terminalsWithHeart -> {
Long lastheartbeat = terminalsWithHeart.getLastheartbeat();
if (!BeanUtil.isEmpty(lastheartbeat)) {
long time = MyDateUtils.TimeSecond2MillSecond(lastheartbeat);
DateTime date = MyDateUtils.date(time);
long between = MyDateUtils.between(MyDateUtils.getNowDate(), date, DateUnit.MINUTE);
if (between > hearttime) {
terminalsWithHeart.setOnlinestatus(CommonStatus.DELETE.value());
} else {
terminalsWithHeart.setOnlinestatus(CommonStatus.EFFECTIVE.value());
}
BigInteger lastheartbeat = terminalsWithHeart.getLastheartbeat();
boolean b = TerminalUtils.judgeTerminalStatus(lastheartbeat);
if (b) {
terminalsWithHeart.setOnlinestatus(CommonStatus.DELETE.value());
} else {
terminalsWithHeart.setOnlinestatus(CommonStatus.EFFECTIVE.value());
}
});
});
});

@ -4,6 +4,9 @@ import com.shxy.xymanager_common.dto.LineAndDyNameDto;
import com.shxy.xymanager_common.dto.TermAndChannelDto;
import com.shxy.xymanager_common.dto.TowerDto;
import com.shxy.xymanager_common.entity.*;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import java.util.List;
import java.util.Map;
@ -109,11 +112,4 @@ public interface CacheService {
Map<Integer, Terminals> updateTerminalMap();
Map<Integer, LineAndDyNameDto> updateLineMap();
Map<Integer,List<LineAndDyNameDto>> updateDyMap();
Map<Integer,List<LineAndDyNameDto>> getDyMap();
}

Loading…
Cancel
Save