Bläddra i källkod

通用推送数据功能优化@2020/11/19 by pengdi

pengdi@zoniot.com 4 år sedan
förälder
incheckning
22f2b2fd2a

+ 6 - 1
smart-city-platform/src/main/java/com/bz/smart_city/dao/DeviceMapper.java

@@ -132,12 +132,17 @@ public interface DeviceMapper {
             @Param("customers") String customers,
             @Param("communitys") String communitys,
             @Param("buildings") String buildings,
+            @Param("channels") String channels ,
+            @Param("deviceTypes") String deviceTypes,
+            @Param("status") Integer status ,
             @Param("beginDate") Date beginDate,
             @Param("endDate") Date endDate);
     List<DeviceDto> getDeviceListByRange(
             @Param("customers") String customers,
             @Param("communitys") String communitys,
-            @Param("buildings") String buildings
+            @Param("buildings") String buildings,
+            @Param("channels") String channels ,
+            @Param("deviceTypes") String deviceTypes
     );
     List<PlanCommunityDTO> findFaultByCommunity(
             @Param("customerId") Integer customerId,

+ 4 - 0
smart-city-platform/src/main/java/com/bz/smart_city/entity/DeviceDataPushConfig.java

@@ -28,6 +28,10 @@ public class DeviceDataPushConfig {
 	String pushCommunitys;
     @ApiModelProperty(value="推送建筑")
 	String pushBuildings;
+    @ApiModelProperty(value="推送场景")
+    String pushChannels ;
+    @ApiModelProperty(value="推送设备类型")
+    String pushDeviceTypes ;
     @ApiModelProperty(value="推送URL,仅推送方式为http")
 	String pushUrl;
     @ApiModelProperty(value="推送服务器,仅推送方式为tcp")

+ 4 - 0
smart-city-platform/src/main/java/com/bz/smart_city/entity/DeviceInfoPushConfig.java

@@ -27,6 +27,10 @@ public class DeviceInfoPushConfig {
 	String pushCommunitys;
     @ApiModelProperty(value="推送建筑")
 	String pushBuildings;
+    @ApiModelProperty(value="推送场景")
+    String pushChannels ;
+    @ApiModelProperty(value="推送设备类型")
+    String pushDeviceTypes ;
     @ApiModelProperty(value="推送URL,仅推送方式为http")
 	String pushUrl;
     @ApiModelProperty(value="推送周期,仅批量推送用,填Cron表达式")

+ 16 - 3
smart-city-platform/src/main/java/com/bz/smart_city/service/impl/DeviceDataPushServiceImpl.java

@@ -92,8 +92,14 @@ public class DeviceDataPushServiceImpl implements DeviceDataPushService , Initia
             List<Map<String, String>> dataItemList = queryDeviceDataByRange(deviceDto, endDay, beginDay);
             for(Map<String, String> dataItemMap :  dataItemList){
                 total++ ;
-                Map<String, Object> dataMap = buildDeviceDataMap(deviceDto, dataItemMap);
-                dataMapList.add(dataMap);
+                // 此处要进行异常处理,单条数据失败时不会影响整条数据
+                try {
+                    Map<String, Object> dataMap = buildDeviceDataMap(deviceDto, dataItemMap);
+                    dataMapList.add(dataMap);
+                }catch (Exception e){
+                    e.printStackTrace();
+                    log.error("设备{}数据{}处理失败!",deviceDto.getId(),dataItemMap);
+                }
                 if(dataMapList.size()  == maxLimit){
                     // 触发批量发送
                     sendBatchData( dataMapList , beginDate ,endDate , config);
@@ -149,7 +155,14 @@ public class DeviceDataPushServiceImpl implements DeviceDataPushService , Initia
 		String pushCustomers = config.getPushCustomers();
 		String pushCommunities = config.getPushCommunitys();
 		String pushBuildings = config.getPushBuildings();
-		List<DeviceDto> deviceList = deviceMapper.getDeviceListByRange(pushCustomers, pushCommunities, pushBuildings);
+		String pushChannels = config.getPushChannels() ;
+		String pushDeviceTypes = config.getPushDeviceTypes();
+		List<DeviceDto> deviceList = deviceMapper.getDeviceListByRange(pushCustomers,
+				pushCommunities,
+				pushBuildings,
+				pushChannels,
+				pushDeviceTypes
+				);
 		return deviceList ;
 	}
 

+ 16 - 4
smart-city-platform/src/main/java/com/bz/smart_city/service/impl/DeviceInfoPushServiceImpl.java

@@ -59,25 +59,37 @@ public class DeviceInfoPushServiceImpl implements DeviceInfoPushService , Initia
 		// 2,计算推送数据时间;如果为首次推送,则判断是否需要将历史数据推送给对方;
 		Date beginDate = null ;
 		Date endDate = new Date();
+		Integer status = null ;
 		if(lastLog != null) {
 			beginDate = lastLog.getPushTime() ; 
 		}
 		else {
+			// 首次推送,只推送有效的设备
+			status = 1 ;
 			beginDate = DateTimeUtil.parseDate("1900-01-01", "yyyy-MM-dd");
 		}
 		// 3,根据推送数据时间进行数据查询
 		String pushCustomers = config.getPushCustomers();
 		String pushCommunitys = config.getPushCommunitys();
 		String pushBuildings = config.getPushBuildings();
+		String pushChannels = config.getPushChannels();
+		String pushDeviceTypes = config.getPushDeviceTypes();
 		// 4,查询推送数据
-		log.info("begin query push device list , pushCustomers ={}, pushCommunitys={}, pushBuildings={}, beginDate={}, endDate={}",
-				pushCustomers, pushCommunitys, pushBuildings, beginDate, endDate);
-		List<Map<String, Object>> dataList = deviceMapper.getPushDeviceDataList(pushCustomers, pushCommunitys, pushBuildings, beginDate, endDate);
+		log.info("begin query push device list , push config = {}, beginDate={}, endDate={}",
+				JSON.toJSONString(config), beginDate, endDate);
+		List<Map<String, Object>> dataList = deviceMapper.getPushDeviceDataList(pushCustomers,
+				pushCommunitys,
+				pushBuildings,
+				pushChannels,
+				pushDeviceTypes,
+				status,
+				beginDate,
+				endDate);
 		log.info("end query push device list , size = {}",dataList.size());
 		if(dataList.size() != 0) {
 			sendBatchData(dataList,beginDate,endDate,config);
 		}
-		log.info("end generalPushMethod !");
+		log.info("end generalPushMethod , Config Id = {}!",config.getId());
 	}
 
 	protected void sendBatchData(List<Map<String,Object>> dataMapList ,Date beginDate ,Date endDate , DeviceInfoPushConfig config) {

+ 7 - 1
smart-city-platform/src/main/resources/mapper/DeviceDataPushConfigMapper.xml

@@ -13,7 +13,11 @@
 		<result column="push_communitys" jdbcType="VARCHAR"
 			property="pushCommunitys" />
 		<result column="push_buildings" jdbcType="VARCHAR"
-			property="pushBuildings" />	
+			property="pushBuildings" />
+		<result column="push_channels" jdbcType="VARCHAR"
+				property="pushChannels" />
+		<result column="push_device_types" jdbcType="VARCHAR"
+				property="pushDeviceTypes" />
 		<result column="push_url" jdbcType="VARCHAR"
 			property="pushUrl" />		
 		<result column="push_server" jdbcType="VARCHAR"
@@ -48,6 +52,8 @@
 		push_customers,
 		push_communitys,
 		push_buildings,
+		push_channels,
+		push_device_types,
 		push_url,
 		push_server,
 		push_port,

+ 7 - 1
smart-city-platform/src/main/resources/mapper/DeviceInfoPushConfigMapper.xml

@@ -14,7 +14,11 @@
 		<result column="push_communitys" jdbcType="VARCHAR"
 			property="pushCommunitys" />
 		<result column="push_buildings" jdbcType="VARCHAR"
-			property="pushBuildings" />	
+			property="pushBuildings" />
+		<result column="push_channels" jdbcType="VARCHAR"
+				property="pushChannels"/>
+		<result column="push_device_types" jdbcType="VARCHAR"
+				property="pushDeviceTypes"/>
 		<result column="push_url" jdbcType="VARCHAR"
 			property="pushUrl" />		
 		<result column="period" jdbcType="VARCHAR"
@@ -38,6 +42,8 @@
 		push_customers,
 		push_communitys,
 		push_buildings,
+		push_channels,
+		push_device_types,
 		push_url,
 		period,
 		status,

+ 44 - 43
smart-city-platform/src/main/resources/mapper/DeviceMapper.xml

@@ -961,43 +961,44 @@
 	</select>
     <select id="getDeviceListByRange" resultType="com.bz.smart_city.dto.DeviceDto">
         SELECT
-            d.id ,
-            d.device_no  ,
+            d.id,
+            d.device_no,
             dt.equipment_type,
             d.device_type
         FROM
-        sc_device d
+            sc_device d
         LEFT JOIN sc_device_type dt ON ( d.device_type = dt.id )
         LEFT JOIN sc_building b ON ( d.building_id = b.id )
         LEFT JOIN sc_community c ON ( b.community = c.id )
         WHERE
-        d.`status` = 1
+            d.`status` = 1
         AND d.sys_id != - 99
-        AND d.sys_id != 55
-        AND d.id IN ( SELECT dd.device_id FROM sc_device_dimension dd WHERE
-        <trim prefixOverrides="or" >
-            <if test="customers != null and customers != '' ">
-                FIND_IN_SET( dd.customer, #{customers} )
-            </if>
-            <if test="communitys != null and communitys != '' ">
-                or FIND_IN_SET( dd.community, #{communitys} )
-            </if>
-            <if test="buildings != null and buildings != '' ">
-                or FIND_IN_SET( dd.building, #{buildings} )
-            </if>
-        </trim>
-        )
+        <if test="customers != null and customers != '' ">
+            AND FIND_IN_SET( d.customer_id, #{customers} )
+        </if>
+        <if test="communitys != null and communitys != '' ">
+            AND FIND_IN_SET( c.id, #{communitys} )
+        </if>
+        <if test="buildings != null and buildings != '' ">
+            AND FIND_IN_SET( d.building_id, #{buildings} )
+        </if>
+        <if test="channels != null and channels != '' ">
+            AND FIND_IN_SET( d.sys_id, #{channels} )
+        </if>
+        <if test="deviceTypes != null and deviceTypes != '' ">
+            AND FIND_IN_SET( d.device_type, #{deviceTypes} )
+        </if>
     </select>
     <select id="getPushDeviceDataList" resultType="java.util.HashMap">
         SELECT
         d.id AS deviceId,
         d.device_no AS deviceNo,
-        d.status AS status,
+        cast(d.status as SIGNED  ) AS status,
         dt.equipment_type AS deviceType,
-        DATE_FORMAT(d.date_update,'%Y-%m-%d') AS dateCreate,
+        DATE_FORMAT( d.date_create, '%Y-%m-%d' ) AS dateCreate,
         ifnull(m.factory,f.`name`) as factory,
         m.user_name AS userName,
-        '0' AS beginWSV,
+        ifnull(ll.new_meter_start,'0') AS beginWSV,
         ifnull(m.area_name,c.`name`) AS areaName,
         d.loc_desc AS location,
         d.water_meter_no AS meterNumber,
@@ -1009,33 +1010,33 @@
         m.caliber
         FROM
         sc_device d
+        Left JOIN sc_install_list ll on (d.id = ll.device_id and ll.status  =1 )
         LEFT JOIN sc_meter_info m ON ( CONCAT('91000',d.water_meter_no) = m.meter_no )
         LEFT JOIN sc_device_type dt ON ( d.device_type = dt.id )
         LEFT JOIN sc_building b on (d.building_id =b.id)
         LEFT JOIN sc_community c on (b.community = c.id)
         left join sc_device_manufacturer f on (dt.manufacturer_id = f.id)
-        where d.`status` = 1
-        and d.sys_id != -99
-        and d.sys_id != 55
-        and d.id in (
-        SELECT
-        dd.device_id
-        FROM sc_device_dimension dd
-        WHERE
-        <trim prefixOverrides="or" >
-            <if test="customers != null and customers != '' ">
-                FIND_IN_SET( dd.customer, #{customers} )
-            </if>
-            <if test="communitys != null and communitys != '' ">
-                or FIND_IN_SET( dd.community, #{communitys} )
-            </if>
-            <if test="buildings != null and buildings != '' ">
-                or FIND_IN_SET( dd.building, #{buildings} )
-            </if>
-        </trim>
-        )
-        and d.date_update >= #{beginDate ,jdbcType=TIMESTAMP }
-        and d.date_update <![CDATA[ < ]]> #{endDate ,jdbcType=TIMESTAMP }
+        where d.sys_id != -99
+        <if test="customers != null and customers != '' ">
+            AND FIND_IN_SET( d.customer_id, #{customers} )
+        </if>
+        <if test="communitys != null and communitys != '' ">
+            AND FIND_IN_SET( c.id, #{communitys} )
+        </if>
+        <if test="buildings != null and buildings != '' ">
+            AND FIND_IN_SET( d.building_id, #{buildings} )
+        </if>
+        <if test="channels != null and channels != '' ">
+            AND FIND_IN_SET( d.sys_id, #{channels} )
+        </if>
+        <if test="deviceTypes != null and deviceTypes != '' ">
+            AND FIND_IN_SET( d.device_type, #{deviceTypes} )
+        </if>
+        <if test="status != null">
+            AND d.status = #{status}
+        </if>
+        and d.date_update > #{beginDate ,jdbcType=TIMESTAMP }
+        and d.date_update <![CDATA[ <= ]]> #{endDate ,jdbcType=TIMESTAMP }
     </select>
     <select id="findFaultByCommunity" resultType="com.bz.smart_city.dto.assistant.PlanCommunityDTO">
         SELECT

+ 15 - 0
smart-city-platform/src/test/java/com/bz/smart_city/DeviceDataPushServiceTest.java

@@ -1,7 +1,10 @@
 package com.bz.smart_city;
 
 import com.bz.smart_city.dao.DeviceDataPushConfigMapper;
+import com.bz.smart_city.dao.DeviceInfoPushConfigMapper;
 import com.bz.smart_city.entity.DeviceDataPushConfig;
+import com.bz.smart_city.entity.DeviceInfoPushConfig;
+import com.bz.smart_city.service.DeviceInfoPushService;
 import com.bz.smart_city.service.impl.ZunYiNewRegionSyncServiceImpl;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -39,11 +42,23 @@ public class DeviceDataPushServiceTest {
 	@Autowired
 	DeviceDataPushConfigMapper deviceDataPushConfigMapper ;
 
+	@Autowired
+	DeviceInfoPushConfigMapper deviceInfoPushConfigMapper ;
+
 
+	@Autowired
+	DeviceInfoPushService deviceInfoPushService ;
 
 	@Test
 	public void contextLoads() {
 	}
+
+	@Test
+	public void devicePushInfoTest(){
+		DeviceInfoPushConfig config = deviceInfoPushConfigMapper.findConfigById(3);
+		deviceInfoPushService.generalPushMethod(config);
+	}
+
 	@Test
 	public void deviceInfoPushJobTest() {
 		QuartzEntity entity = new QuartzEntity();