Commit 1bbd0a5a authored by 赵啸非's avatar 赵啸非

修改同步业务逻辑

parent 2634e353
Pipeline #2860 canceled with stages
package com.mortals.xhx.daemon.applicationservice;
import com.mortals.framework.springcloud.config.web.BaseWebMvcConfigurer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.skin.service.SkinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
/**
* 应用级服务,在应用启动后、停止过程中调用
* 应用已经完成启动完成,才调用该服务
* 应用场景:
* 1、应用任务,应用启动后定时或间隔执行的任务
* 2、Socket服务端
* @author GM
* @date 2020年7月15日
*/
@Component
//@ConditionalOnProperty(name="com.mortal",prefix = "",havingValue = "xxx")
@Slf4j
public class DemoStartedService implements IApplicationStartedService {
private static Log logger = LogFactory.getLog(DemoStartedService.class);
@Autowired
private UserService userService;
@Autowired
private DeviceService deviceService;
@Autowired
private SkinService skinService;
@Override
public void start() {
logger.info("开始服务..[配置已加载完成,并且所有框架都已经初始化]");
log.info("开始服务..[配置已加载完成,并且所有框架都已经初始化]");
//初始执行同步用户与皮肤任务
log.info("开始同步设备列表!");
deviceService.syncDevice();
log.info("结束同步设备列表!");
log.info("开始同步皮肤列表!");
skinService.syncSkin();
log.info("结束同步皮肤列表!");
log.info("开始同步用户!");
userService.refreshUser();
log.info("结束同步用户!");
}
@Override
public void stop() {
logger.info("停止服务..");
log.info("停止服务..");
}
@Override
......
......@@ -5,6 +5,7 @@ import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.device.DevicePdu;
......@@ -34,110 +35,26 @@ import java.util.stream.Collectors;
@Service("SyncSiteDeviceTask")
public class SyncSiteDeviceTaskImpl implements ITaskExcuteService {
@Autowired
private ISiteFeign siteFeign;
@Autowired
private IDeviceFeign deviceFeign;
@Autowired
private DeviceService deviceService;
@Autowired
private SkinService skinService;
@Autowired
private UserService userService;
@Override
public void excuteTask(ITask task) throws AppException {
log.info("开始同步设备列表!");
syncDevice();
deviceService.syncDevice();
log.info("开始同步皮肤列表!");
skinService.syncSkin();
//syncSkin();
log.info("结束同步皮肤列表!");
}
private void syncDevice() {
SitePdu sitePdu = new SitePdu();
sitePdu.setId(1L);
Rest<List<SitePdu>> siteRest = siteFeign.getFlatSitesBySiteId(sitePdu);
if (siteRest.getCode() == YesNoEnum.YES.getValue()) {
log.info("站点总数量:{}", siteRest.getData().size());
siteRest.getData().forEach(site -> {
log.info("站点名称:{}", site.getSiteName());
DevicePdu devicePdu = new DevicePdu();
// devicePdu.setProductCode("tdj");
devicePdu.setProductId(9L);
devicePdu.setSiteId(site.getId());
devicePdu.setSize(-1);
Rest<RespData<List<DevicePdu>>> deviceRest = deviceFeign.list(devicePdu);
log.info("deviceRest:{}", JSON.toJSONString(deviceRest));
if (YesNoEnum.YES.getValue() == deviceRest.getCode()) {
List<DevicePdu> devicePduList = deviceRest.getData().getData();
log.info("填单机总数量:{}", devicePduList.size());
if (!ObjectUtils.isEmpty(devicePduList)) {
List<DeviceEntity> newDeviceList = devicePduList.stream().map(newDevice -> {
DeviceEntity deviceEntity = new DeviceEntity();
deviceEntity.initAttrValue();
BeanUtils.copyProperties(newDevice, deviceEntity, BeanUtil.getNullPropertyNames(newDevice));
return deviceEntity;
}).collect(Collectors.toList());
List<DeviceEntity> oldDeviceList = deviceService.find(new DeviceQuery().siteId(site.getId()));
Map<String, DeviceEntity> oldDeviceMap = oldDeviceList.stream().collect(Collectors.toMap(x -> x.getDeviceCode(), y -> y, (o, n) -> n));
Map<String, DeviceEntity> newDeviceMap = newDeviceList.stream().collect(Collectors.toMap(x -> x.getDeviceCode(), y -> y, (o, n) -> n));
List<DeviceEntity> updateDeviceLsit = newDeviceList.stream().map(item -> {
if (oldDeviceMap.containsKey(item.getDeviceCode())) {
item.setId(oldDeviceMap.get(item.getDeviceCode()).getId());
item.setDeviceId(item.getId());
item.setUpdateTime(new Date());
return item;
}
return null;
}).filter(f -> f != null).collect(Collectors.toList());
List<DeviceEntity> saveDeviceList = newDeviceList.stream().map(item -> {
if (!oldDeviceMap.containsKey(item.getDeviceCode())) {
item.setDeviceId(item.getId());
item.setCreateUserId(1L);
item.setCreateUserName("系统管理员");
item.setCreateTime(new Date());
return item;
}
return null;
}).filter(f -> f != null).collect(Collectors.toList());
//做差集
List<Long> delDeviceList = oldDeviceList.stream().map(item -> {
if (!newDeviceMap.containsKey(item.getDeviceCode())) {
return item.getId();
}
return null;
}).filter(f -> f != null).collect(Collectors.toList());
if (!ObjectUtils.isEmpty(updateDeviceLsit)) {
log.info("设备更新,size:{}", updateDeviceLsit.size());
deviceService.update(updateDeviceLsit);
}
if (!ObjectUtils.isEmpty(saveDeviceList)) {
log.info("设备新增,size:{}", saveDeviceList.size());
deviceService.save(saveDeviceList);
}
if (!ObjectUtils.isEmpty(delDeviceList)) {
log.info("设备删除,size:{}", delDeviceList.size());
//deviceService.remove(delDeviceList,null);
}
}
} else {
log.info("设备列表查询异常,{}", JSON.toJSONString(deviceRest));
}
});
} else {
log.info("站点列表查询异常,{}", JSON.toJSONString(siteRest));
}
log.info("开始同步用户!");
userService.refreshUser();
log.info("结束同步用户!");
}
......
......@@ -78,13 +78,8 @@ public class SyncSiteMatterTaskImpl implements ITaskExcuteService {
}).collect(Collectors.toList());
if (!ObjectUtils.isEmpty(sheetMatterEntities)) {
// sheetMatterService.getDao().delete(new SheetMatterQuery().siteId(site.getId()));
log.info("新增数量:{}",sheetMatterEntities.size());
sheetMatterService.save(sheetMatterEntities);
/* List<List<SheetMatterEntity>> partition = ListUtil.partition(sheetMatterEntities, 500);
for (List<SheetMatterEntity> matterEntities : partition) {
}*/
}
}else{
log.info("请求错误,code:{}",siteMatterRest.getCode());
......
......@@ -29,4 +29,8 @@ public interface DeviceService extends ICRUDCacheService<DeviceEntity,Long> {
* @param context
*/
void deviceEnabled(Long id, Integer status, Context context);
void syncDevice();
}
\ No newline at end of file
......@@ -11,19 +11,28 @@ import com.mortals.xhx.common.code.DeviceStatusEnum;
import com.mortals.xhx.common.code.EnabledEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.LoginForm;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.device.DevicePdu;
import com.mortals.xhx.common.pdu.device.DeviceReq;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.feign.device.IDeviceFeign;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.device.dao.DeviceDao;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* DeviceService
......@@ -37,10 +46,9 @@ import java.util.Date;
public class DeviceServiceImpl extends AbstractCRUDCacheServiceImpl<DeviceDao, DeviceEntity, Long> implements DeviceService {
@Autowired
private IDeviceFeign deviceFeign;
@Value("${token.loginName:admin}")
private String loginName;
@Value("${token.password:admin}")
private String password;
@Autowired
private ISiteFeign siteFeign;
@Override
protected String getExtKey(DeviceEntity data) {
......@@ -79,4 +87,91 @@ public class DeviceServiceImpl extends AbstractCRUDCacheServiceImpl<DeviceDao, D
log.info("启用结果:{}", JSON.toJSONString(rest));
}
@Override
public void syncDevice() {
SitePdu sitePdu = new SitePdu();
sitePdu.setId(1L);
Rest<List<SitePdu>> siteRest = siteFeign.getFlatSitesBySiteId(sitePdu);
if (siteRest.getCode() == YesNoEnum.YES.getValue()) {
log.info("站点总数量:{}", siteRest.getData().size());
siteRest.getData().forEach(site -> {
log.info("站点名称:{}", site.getSiteName());
DevicePdu devicePdu = new DevicePdu();
// devicePdu.setProductCode("tdj");
devicePdu.setProductId(9L);
devicePdu.setSiteId(site.getId());
devicePdu.setSize(-1);
Rest<RespData<List<DevicePdu>>> deviceRest = deviceFeign.list(devicePdu);
log.info("deviceRest:{}", JSON.toJSONString(deviceRest));
if (YesNoEnum.YES.getValue() == deviceRest.getCode()) {
List<DevicePdu> devicePduList = deviceRest.getData().getData();
log.info("填单机总数量:{}", devicePduList.size());
if (!ObjectUtils.isEmpty(devicePduList)) {
List<DeviceEntity> newDeviceList = devicePduList.stream().map(newDevice -> {
DeviceEntity deviceEntity = new DeviceEntity();
deviceEntity.initAttrValue();
BeanUtils.copyProperties(newDevice, deviceEntity, BeanUtil.getNullPropertyNames(newDevice));
return deviceEntity;
}).collect(Collectors.toList());
List<DeviceEntity> oldDeviceList = this.find(new DeviceQuery().siteId(site.getId()));
Map<String, DeviceEntity> oldDeviceMap = oldDeviceList.stream().collect(Collectors.toMap(x -> x.getDeviceCode(), y -> y, (o, n) -> n));
Map<String, DeviceEntity> newDeviceMap = newDeviceList.stream().collect(Collectors.toMap(x -> x.getDeviceCode(), y -> y, (o, n) -> n));
List<DeviceEntity> updateDeviceLsit = newDeviceList.stream().map(item -> {
if (oldDeviceMap.containsKey(item.getDeviceCode())) {
item.setId(oldDeviceMap.get(item.getDeviceCode()).getId());
item.setDeviceId(item.getId());
item.setUpdateTime(new Date());
return item;
}
return null;
}).filter(f -> f != null).collect(Collectors.toList());
List<DeviceEntity> saveDeviceList = newDeviceList.stream().map(item -> {
if (!oldDeviceMap.containsKey(item.getDeviceCode())) {
item.setDeviceId(item.getId());
item.setCreateUserId(1L);
item.setCreateUserName("系统管理员");
item.setCreateTime(new Date());
return item;
}
return null;
}).filter(f -> f != null).collect(Collectors.toList());
//做差集
List<Long> delDeviceList = oldDeviceList.stream().map(item -> {
if (!newDeviceMap.containsKey(item.getDeviceCode())) {
return item.getId();
}
return null;
}).filter(f -> f != null).collect(Collectors.toList());
if (!ObjectUtils.isEmpty(updateDeviceLsit)) {
log.info("设备更新,size:{}", updateDeviceLsit.size());
this.update(updateDeviceLsit);
}
if (!ObjectUtils.isEmpty(saveDeviceList)) {
log.info("设备新增,size:{}", saveDeviceList.size());
this.save(saveDeviceList);
}
if (!ObjectUtils.isEmpty(delDeviceList)) {
log.info("设备删除,size:{}", delDeviceList.size());
//deviceService.remove(delDeviceList,null);
}
}
} else {
log.info("设备列表查询异常,{}", JSON.toJSONString(deviceRest));
}
});
} else {
log.info("站点列表查询异常,{}", JSON.toJSONString(siteRest));
}
}
}
\ No newline at end of file
......@@ -22,8 +22,8 @@
<property name="showSql" value="false" />
</plugin>
<!-- <plugin interceptor="com.mortals.framework.thirty.dm.DmTransInterceptor">
<plugin interceptor="com.mortals.framework.thirty.dm.DmTransInterceptor">
</plugin>-->
</plugin>
</plugins>
</configuration>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 定义变量后,可以使“${}”来使用变量 source来源为spring 上下文信息 -->
<springProperty scope="context" name="springApplicationName" source="spring.application.name"/>
<springProperty scope="context" name="serverPort" source="server.port"/>
<springProperty scope="context" name="logFilePath" source="application.log.path" defaultValue="/mortals/app/logs" />
<springProperty scope="context" name="logLevel" source="application.log.level" defaultValue="INFO" />
<property name="logFilePath" value="${logFilePath:-/mortals/app/logs}" />
<property name="logLevel" value="${logLevel:-INFO}" />
<!-- appender用来格式化日志输出节点,有俩个属性name和class,class用来指定哪种输出策略,常用就是控制台输出策略和文件输出策略 -->
<!-- 控制台输出策略-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%X{traceId}] [%thread] [%.50c\(%L\)] - %msg%n</pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%thread] [%.50c\(%L\)] - %msg%n</pattern>
</encoder>
</appender>
<!-- 文件输出策略-->
<appender name="fileInfo" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%X{traceId}] [%thread] [%.50c\(%L\)] - %msg%n</pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%thread] [%.50c\(%L\)] - %msg%n</pattern>
</encoder>
<file>${logFilePath}/${springApplicationName:-default}/${springApplicationName:-default}-info.log</file>
<file>${logFilePath}/${springApplicationName:-default}/${serverPort:-default}-info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 文件保存策略-->
<fileNamePattern>${logFilePath}/${springApplicationName:-default}/${springApplicationName:-default}-info.log.%d{yyyyMMdd}</fileNamePattern>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
<!--日志文件保留天数-->
<MaxHistory>15</MaxHistory>
<fileNamePattern>${logFilePath}/${springApplicationName:-default}/${serverPort:-default}-info.log.%d{yyyyMMdd}</fileNamePattern>
</rollingPolicy>
</appender>
<!-- 异常文件输出策略-->
<appender name="fileError" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%X{traceId}] [%thread] [%.50c\(%L\)] - %msg%n</pattern>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%thread] [%.50c\(%L\)] - %msg%n</pattern>
</encoder>
<file>${logFilePath}/${springApplicationName:-default}/${springApplicationName:-default}-error.log</file>
<file>${logFilePath}/${springApplicationName:-default}/${serverPort:-default}-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logFilePath}/${springApplicationName:-default}/${springApplicationName:-default}-error.log.%d{yyyyMMdd}</fileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>15</MaxHistory>
<fileNamePattern>${logFilePath}/${springApplicationName:-default}/${serverPort:-default}-error.log.%d{yyyyMMdd}</fileNamePattern>
</rollingPolicy>
</appender>
<root level="${logLevel}">
<appender-ref ref="console"/>
<appender-ref ref="fileInfo"/>
<appender-ref ref="fileError"/>
</root>
<!--TRACE < DEBUG < INFO < WARN < ERROR < FATAL -->
<!-- 定义 Sentry Appender -->
<appender name="Sentry" class="io.sentry.logback.SentryAppender">
<options>
<dsn>https://47b91d2dc93b4d4a8c49847024f3426a@o1181109.ingest.sentry.io/6294610</dsn>
</options>
</appender>
<!--用来设置某一个包或者具体的某一个类的日志打印级别、以及指定<appender>。<logger>仅有一个name属性,一个可选的level和一个可选的additivity属性。-->
<!-- name 用来指定受此loger约束的某一个包或者具体的某一个类-->
<!-- level 用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,还有一个特俗值INHERITED或者同义词NULL,代表强制执行上级的级别。如果未设置此属性,那么当前logger将会继承上级的级别-->
<!-- additivity 是否向上级logger传递打印信息。默认是true。false:表示只用当前logger的appender-ref。true:表示当前logger的appender-ref和rootLogger的appender-ref都有效。-->
<!-- <logger name="com.mortals.xhx.module" level="DEBUG" additivity="false"/>-->
<logger name="com.mortals" level="${logLevel}" additivity="false">
<root level="${logLevel}">
<appender-ref ref="console"/>
<appender-ref ref="fileInfo"/>
<appender-ref ref="fileError"/>
</logger>
<logger name="com.mortals.xhx.module" level="${logLevel}" additivity="false">
<appender-ref ref="Sentry" />
</root>
<logger name="com.mortals.xhx.module" level="INFO" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="fileInfo"/>
<appender-ref ref="fileError"/>
</logger>
<!-- <logger name="com.mortals.xhx.module">
<level value="debug"/>
</logger>-->
</configuration>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment