Commit c09d6168 authored by 赵啸非's avatar 赵啸非

修改同步任务

parent 105a2aae
package com.mortals.xhx.daemon.applicationservice;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.feign.device.IDeviceFeign;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.skin.service.SkinService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@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
......
......@@ -56,13 +56,11 @@ public class SyncSiteDeviceTaskImpl implements ITaskExcuteService {
private DeviceService deviceService;
@Autowired
private SkinService skinService;
@Autowired
private ISkinBaseFeign skinBaseFeign;
@Override
public void excuteTask(ITask task) throws AppException {
log.info("开始同步设备列表!");
syncDevice();
deviceService.syncDevice();
log.info("结束同步设备列表!");
log.info("开始同步皮肤列表!");
......@@ -109,7 +107,7 @@ public class SyncSiteDeviceTaskImpl implements ITaskExcuteService {
DeviceEntity deviceEntity = new DeviceEntity();
deviceEntity.initAttrValue();
BeanUtils.copyProperties(newDevice, deviceEntity, BeanUtil.getNullPropertyNames(newDevice));
deviceEntity.setDeviceId(newDevice.getId());
deviceEntity.setDeviceId(newDevice.getId());
return deviceEntity;
}).collect(Collectors.toList());
......@@ -124,7 +122,7 @@ public class SyncSiteDeviceTaskImpl implements ITaskExcuteService {
List<DeviceEntity> updateDeviceLsit = newDeviceList.stream().map(item -> {
if (oldDeviceMap.containsKey(item.getDeviceCode())) {
item.setId(oldDeviceMap.get(item.getDeviceCode()).getId());
// item.setDeviceId(item.getId());
// item.setDeviceId(item.getId());
item.setProductCode(oldDeviceMap.get(item.getDeviceCode()).getProductCode());
item.setProductName(oldDeviceMap.get(item.getDeviceCode()).getProductName());
item.setUpdateTime(new Date());
......@@ -136,7 +134,7 @@ public class SyncSiteDeviceTaskImpl implements ITaskExcuteService {
List<DeviceEntity> saveDeviceList = newDeviceList.stream().map(item -> {
//log.info("deviceCode:{},oldDeviceMap:{}",item.getDeviceCode(),oldDeviceMap.containsKey(item.getDeviceCode()));
if (!oldDeviceMap.containsKey(item.getDeviceCode())) {
// item.setDeviceId(item.getId());
// item.setDeviceId(item.getId());
item.setCreateUserId(1L);
item.setProductName("样表机");
item.setCreateUserName("系统管理员");
......
......@@ -82,10 +82,6 @@ public class SyncSiteMatterTaskImpl implements ITaskExcuteService {
// 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:{} msg:{}",siteMatterRest.getCode(),siteMatterRest.getMsg());
......
......@@ -30,4 +30,9 @@ 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
......@@ -9,20 +9,31 @@ import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.common.code.DeviceMethodEnum;
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.product.ProductPdu;
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
......@@ -43,6 +54,10 @@ public class DeviceServiceImpl extends AbstractCRUDCacheServiceImpl<DeviceDao, D
private String password;
@Autowired
private ISiteFeign siteFeign;
@Override
protected String getExtKey(DeviceEntity data) {
return data.getDeviceCode();
......@@ -83,6 +98,113 @@ public class DeviceServiceImpl extends AbstractCRUDCacheServiceImpl<DeviceDao, D
log.info("启用结果:{}", JSON.toJSONString(rest));
}
@Override
public void syncDevice() {
Long productId = null;
ProductPdu productPdu = new ProductPdu();
productPdu.setProductCode("ybj");
Rest<RespData<List<ProductPdu>>> rest = deviceFeign.list(productPdu);
if (rest.getCode() == YesNoEnum.YES.getValue() && !ObjectUtils.isEmpty(rest.getData())) {
if (!ObjectUtils.isEmpty(rest.getData().getData())) {
ProductPdu pdu = rest.getData().getData().get(0);
productId = pdu.getId();
}
}
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());
for (SitePdu site : siteRest.getData()) {
log.info("站点名称:{}", site.getSiteName());
DevicePdu devicePdu = new DevicePdu();
if (!ObjectUtils.isEmpty(productId)) {
devicePdu.setProductId(productId);
} else {
devicePdu.setProductName("样表机");
}
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));
deviceEntity.setDeviceId(newDevice.getId());
return deviceEntity;
}).collect(Collectors.toList());
List<DeviceEntity> oldDeviceList = this.find(new DeviceQuery().siteId(site.getId()));
log.info("siteId:{} oldDeviceList size:{}", site.getId(), oldDeviceList.size());
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));
//log.info("oldDeviceMap:{}", JSON.toJSONString(oldDeviceMap));
List<DeviceEntity> updateDeviceLsit = newDeviceList.stream().map(item -> {
if (oldDeviceMap.containsKey(item.getDeviceCode())) {
item.setId(oldDeviceMap.get(item.getDeviceCode()).getId());
// item.setDeviceId(item.getId());
item.setProductCode(oldDeviceMap.get(item.getDeviceCode()).getProductCode());
item.setProductName(oldDeviceMap.get(item.getDeviceCode()).getProductName());
item.setUpdateTime(new Date());
return item;
}
return null;
}).filter(f -> f != null).collect(Collectors.toList());
List<DeviceEntity> saveDeviceList = newDeviceList.stream().map(item -> {
//log.info("deviceCode:{},oldDeviceMap:{}",item.getDeviceCode(),oldDeviceMap.containsKey(item.getDeviceCode()));
if (!oldDeviceMap.containsKey(item.getDeviceCode())) {
// item.setDeviceId(item.getId());
item.setCreateUserId(1L);
item.setProductName("样表机");
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());
this.remove(delDeviceList, null);
}
}
} else {
log.info("设备列表查询异常,{}", JSON.toJSONString(deviceRest));
}
}
} else {
log.info("站点列表查询异常,{}", JSON.toJSONString(siteRest));
}
}
/* private String getToken() {
LoginForm loginForm = new LoginForm();
......
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