fix: 物联网模块异步处理;增加根据租户id获取企业信息方法

master
wangrunpu 4 weeks ago
parent 731c4425d4
commit b078f17db8

@ -7,6 +7,7 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import java.util.ArrayList;
import java.util.HashMap;
@ -46,6 +47,7 @@ public class IotLocalMessageBus implements IotMessageBus {
topic, subscriber.getGroup(), subscriber.getClass().getName());
}
@Async
@EventListener
@SuppressWarnings({"unchecked", "rawtypes"})
public void onMessage(IotLocalMessage message) {

@ -39,7 +39,7 @@ public class IotTcpConnectionManager {
* @param deviceId ID
* @param connectionInfo
*/
public void registerConnection(NetSocket socket, Long deviceId, ConnectionInfo connectionInfo) {
public synchronized void registerConnection(NetSocket socket, Long deviceId, ConnectionInfo connectionInfo) {
// 如果设备已有其他连接,先清理旧连接
NetSocket oldSocket = deviceSocketMap.get(deviceId);
if (oldSocket != null && oldSocket != socket) {

@ -1,5 +1,7 @@
package cn.iocoder.yudao.module.system.controller.admin.dustcompanyinfo;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.module.system.controller.admin.dustdustinfo.vo.DustDustinfoRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dustdustinfo.DustDustinfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dusttype.DustTypeDO;
@ -29,6 +31,7 @@ import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import cn.iocoder.yudao.module.system.controller.admin.dustcompanyinfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.dustcompanyinfo.DustCompanyinfoDO;
@ -111,6 +114,18 @@ public class DustCompanyinfoController {
return success(respVO);
}
@GetMapping("/get-by-tenant")
@Operation(summary = "通过租户id获得企业基础信息")
@Parameter(name = "tenantId", description = "租户编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('system:dust-companyinfo:query')")
public CommonResult<DustCompanyinfoRespVO> getDustCompanyinfoByTenantId(@RequestParam("tenantId") Long tenantId) {
if (ObjectUtil.isNull(tenantId)) {
LoginUser loginUser = getLoginUser();
tenantId = loginUser.getTenantId();
}
return success(dustCompanyinfoService.getCompanyinfoByTenantId(tenantId));
}
@GetMapping("/page")
@Operation(summary = "获得企业基础信息分页")
@PreAuthorize("@ss.hasPermission('system:dust-companyinfo:query')")

@ -60,4 +60,6 @@ public interface DustCompanyinfoService {
PageResult<DustCompanyinfoDO> getDustCompanyinfoPage(DustCompanyinfoPageReqVO pageReqVO);
String getDataIdByTenantId(Long tenantId);
DustCompanyinfoRespVO getCompanyinfoByTenantId(Long tenantId);
}

@ -1,25 +1,30 @@
package cn.iocoder.yudao.module.system.service.dustcompanyinfo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.system.controller.admin.dustdustinfo.vo.DustDustinfoRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dustdustinfo.vo.DustDustinfoSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dustdustinfo.DustDustinfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dusttype.DustTypeDO;
import cn.iocoder.yudao.module.system.service.dustdustinfo.DustDustinfoService;
import cn.iocoder.yudao.module.system.service.dusttype.DustTypeService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import cn.iocoder.yudao.module.system.controller.admin.dustcompanyinfo.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.dustcompanyinfo.DustCompanyinfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.dal.mysql.dustcompanyinfo.DustCompanyinfoMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
/**
@ -36,6 +41,8 @@ public class DustCompanyinfoServiceImpl implements DustCompanyinfoService {
@Resource
private DustDustinfoService dustDustinfoService;
@Resource
private DustTypeService dustTypeService;
@Override
@Transactional(rollbackFor = Exception.class)
public String createDustCompanyinfo(DustCompanyinfoSaveReqVO createReqVO) {
@ -104,11 +111,44 @@ public class DustCompanyinfoServiceImpl implements DustCompanyinfoService {
@Override
public String getDataIdByTenantId(Long tenantId) {
DustCompanyinfoDO selectedOne = dustCompanyinfoMapper.selectOne("tenantId", tenantId);
if (selectedOne == null || selectedOne.getDeleted()) {
return null;
}
return selectedOne.getDataId();
AtomicReference<DustCompanyinfoDO> selectedOne = new AtomicReference<>();
return TenantUtils.executeIgnore(() -> {
selectedOne.set(dustCompanyinfoMapper.selectOne("tenant_id", tenantId));
if (selectedOne.get() == null || selectedOne.get().getDeleted()) {
return null;
}
return selectedOne.get().getDataId();
});
}
@Override
public DustCompanyinfoRespVO getCompanyinfoByTenantId(Long tenantId){
return TenantUtils.executeIgnore(() -> {
//查询企业基础信息
DustCompanyinfoDO dustCompanyinfo = dustCompanyinfoMapper.selectOne("tenant_id",tenantId);
if (ObjectUtil.isNull(dustCompanyinfo)) return null;
DustCompanyinfoRespVO respVO = BeanUtils.toBean(dustCompanyinfo, DustCompanyinfoRespVO.class);
//根据id查询企业涉尘信息
DustDustinfoDO dustDustinfo = dustDustinfoService.getDustDustinfo(dustCompanyinfo.getId());
List<String> list = Arrays.stream(dustDustinfo.getDustTechnology().split(",")).toList();
DustDustinfoRespVO dustinfoRespVO = BeanUtils.toBean(dustDustinfo, DustDustinfoRespVO.class);
dustinfoRespVO.setDustTechnology(list);
String fullDustName="";
if (dustDustinfo.getDustType() != null ){
DustTypeDO dustType = dustTypeService.getDustType(dustDustinfo.getDustType());
if (dustType != null && dustType.getName()!=null){
fullDustName = dustType.getName();
DustTypeDO dustType1 = dustTypeService.getDustType(dustType.getParentId());
if (dustType1!=null && dustType1.getName()!=null){
fullDustName = dustType1.getName()+"/"+fullDustName;
}
}
dustinfoRespVO.setDustTypeName(fullDustName);
}
respVO.setDustDustinfo(dustinfoRespVO);
return respVO;
});
}
}

@ -51,18 +51,18 @@ public class DustEquipinfoServiceImpl implements DustEquipinfoService {
// createReqVO.setEquipCode(String.valueOf(maxEquipCode + 1l));
// }
// 使用 Optional 链式操作进行改造(体验一下函数式编程)
Optional.ofNullable(getLoginUser())
.map(LoginUser::getTenantId)
.map(tenantId -> dustCompanyinfoService.getDataIdByTenantId(tenantId))
.ifPresent(dataId -> {
createReqVO.setDataId(dataId);
// 继续查询最大设备编码
Long maxCode = Optional.ofNullable(dustEquipinfoMapper.getMaxEquipCode(dataId))
.filter(code -> code > 0)
.map(code -> code + 1)
.orElse(Long.valueOf(dataId+"001")); // 默认从1开始
createReqVO.setEquipCode(String.valueOf(maxCode));
});
// Optional.ofNullable(getLoginUser())
// .map(LoginUser::getTenantId)
// .map(tenantId -> dustCompanyinfoService.getDataIdByTenantId(tenantId))
// .ifPresent(dataId -> {
// createReqVO.setDataId(dataId);
// // 继续查询最大设备编码
// Long maxCode = Optional.ofNullable(dustEquipinfoMapper.getMaxEquipCode(dataId))
// .filter(code -> code > 0)
// .map(code -> code + 1)
// .orElse(Long.valueOf(dataId+"001")); // 默认从1开始
// createReqVO.setEquipCode(String.valueOf(maxCode));
// });
DustEquipinfoDO dustEquipinfo = BeanUtils.toBean(createReqVO, DustEquipinfoDO.class);
dustEquipinfo.setDustTechnology(join);
dustEquipinfoMapper.insert(dustEquipinfo);

Loading…
Cancel
Save