feat: mqtt重新开发
parent
abe4383222
commit
062b038478
@ -1,24 +1,20 @@
|
||||
package com.xydl;
|
||||
|
||||
import com.xydl.service.impl.MqttServiceImpl;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
@EnableSwagger2
|
||||
@EnableScheduling
|
||||
@MapperScan(basePackages = "com.xydl.mapper")
|
||||
@SpringBootApplication//标识该类为主程序启动类
|
||||
@SpringBootApplication
|
||||
public class MqttApplication {
|
||||
//主程序启动方法
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MqttApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.xydl.entity;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "i2relation")
|
||||
@ApiModel("i2relation表")
|
||||
public class I2relation {
|
||||
|
||||
@Id
|
||||
@Column(name = "sensorid")
|
||||
private String sensorid;
|
||||
|
||||
@Column(name = "eqmid")
|
||||
private Integer eqmid;
|
||||
|
||||
@Column(name = "cacid")
|
||||
private String cacid;
|
||||
|
||||
@Column(name = "equipmentid")
|
||||
private String equipmentid;
|
||||
|
||||
@Column(name = "monitortype")
|
||||
private String monitortype;
|
||||
|
||||
@Column(name = "phase")
|
||||
private String phase;
|
||||
|
||||
@Column(name = "sensorindex")
|
||||
private Integer sensorindex;
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.xydl.entity;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "sync_fields_info")
|
||||
@ApiModel("sync_fields_info表")
|
||||
public class SyncFieldsInfo {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private Integer id;
|
||||
|
||||
@Column(name = "client_id")
|
||||
private Integer clientId;
|
||||
|
||||
@Column(name = "table_name")
|
||||
private String tableName;
|
||||
|
||||
@Column(name = "field_name")
|
||||
private String fieldName;
|
||||
|
||||
@Column(name = "field_type")
|
||||
private Byte fieldType;
|
||||
|
||||
@Column(name = "dest_field_name")
|
||||
private String destFieldName;
|
||||
|
||||
@Column(name = "sensorindex")
|
||||
private Integer sensorindex;
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.xydl.entity;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "sync_records")
|
||||
@ApiModel("sync_records表")
|
||||
public class SyncRecords {
|
||||
|
||||
@EmbeddedId
|
||||
private SyncRecordsKey id;
|
||||
|
||||
@Column(name = "field_val1")
|
||||
private Long fieldVal1;
|
||||
|
||||
@Column(name = "field_val2")
|
||||
private Date fieldVal2;
|
||||
|
||||
@Column(name = "update_time")
|
||||
private Date updateTime;
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.xydl.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Id;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Embeddable
|
||||
public class SyncRecordsKey implements Serializable {
|
||||
|
||||
|
||||
@Column(name = "client_id")
|
||||
private Integer clientId;
|
||||
|
||||
@Column(name = "table_name")
|
||||
private String tableName;
|
||||
|
||||
|
||||
@Column(name = "devid_val")
|
||||
private String devidVal;
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.xydl.entity;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "sync_tables_info")
|
||||
@ApiModel("sync_tables_info表")
|
||||
public class SyncTablesInfo {
|
||||
|
||||
@EmbeddedId
|
||||
private SyncTablesInfoKey id;
|
||||
|
||||
@Column(name = "sql")
|
||||
private String sql;
|
||||
|
||||
@Column(name = "devid_field_name")
|
||||
private String devidFieldName;
|
||||
|
||||
@Column(name = "outer_devid_fname")
|
||||
private String outerDevidFname;
|
||||
|
||||
@Column(name = "field_name")
|
||||
private String fieldName;
|
||||
|
||||
@Column(name = "field_type")
|
||||
private Byte fieldType;
|
||||
|
||||
@Column(name = "update_time")
|
||||
private Date updateTime;
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.xydl.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Id;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Embeddable
|
||||
public class SyncTablesInfoKey implements Serializable {
|
||||
|
||||
|
||||
@Column(name = "client_id")
|
||||
private Integer clientId;
|
||||
|
||||
|
||||
@Column(name = "table_name")
|
||||
private String tableName;
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.xydl.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.xydl.entity.I2relation;
|
||||
import com.xydl.entity.SyncFieldsInfo;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class AssetItem {
|
||||
@JsonProperty("AssetCode")
|
||||
String assetCode;
|
||||
@JsonProperty("AttributeList")
|
||||
List<AttributeItem> attributeList;
|
||||
@JsonProperty("Timestamp")
|
||||
Long timestamp;
|
||||
|
||||
public static AssetItem fromData(Map<String, Object> map, String timeField,
|
||||
I2relation relation, List<SyncFieldsInfo> fieldList) {
|
||||
AssetItem root = new AssetItem();
|
||||
root.setAssetCode(relation.getSensorid());
|
||||
List<AttributeItem> list = new ArrayList<>();
|
||||
for (SyncFieldsInfo field : fieldList) {
|
||||
if (relation.getSensorindex().equals(field.getSensorindex())) {
|
||||
AttributeItem item = AttributeItem.fromData(map, field);
|
||||
list.add(item);
|
||||
}
|
||||
}
|
||||
root.setAttributeList(list);
|
||||
LocalDateTime time = (LocalDateTime) map.get(timeField);
|
||||
root.setTimestamp(time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
|
||||
return root;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.xydl.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.xydl.entity.I2relation;
|
||||
import com.xydl.entity.SyncFieldsInfo;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class AssetList {
|
||||
@JsonProperty("AssetList")
|
||||
List<AssetItem> assetList;
|
||||
|
||||
public static AssetList fromData(List<Map<String, Object>> dataList, String timeField,
|
||||
List<I2relation> relationList, List<SyncFieldsInfo> fieldList) {
|
||||
AssetList root = new AssetList();
|
||||
List<AssetItem> list = new ArrayList<>();
|
||||
if (relationList.size() > 1){
|
||||
int a = 1;
|
||||
}
|
||||
for (Map<String, Object> map : dataList) {
|
||||
for (I2relation relation : relationList) {
|
||||
AssetItem item = AssetItem.fromData(map, timeField, relation, fieldList);
|
||||
list.add(item);
|
||||
}
|
||||
}
|
||||
root.setAssetList(list);
|
||||
return root;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.xydl.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.xydl.entity.SyncFieldsInfo;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class AttributeItem {
|
||||
@JsonProperty("AttributeCode")
|
||||
String attributeCode;
|
||||
@JsonProperty("DataValue")
|
||||
Object dataValue;
|
||||
|
||||
public static AttributeItem fromData(Map<String, Object> map, SyncFieldsInfo field) {
|
||||
AttributeItem root = new AttributeItem();
|
||||
root.setAttributeCode(field.getDestFieldName());
|
||||
root.setDataValue(map.get(field.getFieldName()));
|
||||
return root;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.xydl.repository;
|
||||
|
||||
import com.xydl.entity.I2relation;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface I2relationRepository extends JpaRepository<I2relation, String>, JpaSpecificationExecutor<I2relation> {
|
||||
|
||||
List<I2relation> findAllByEqmid(Integer eqmid);
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.xydl.repository;
|
||||
|
||||
import com.xydl.entity.SyncFieldsInfo;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface SyncFieldsInfoRepository extends JpaRepository<SyncFieldsInfo, Integer>, JpaSpecificationExecutor<SyncFieldsInfo> {
|
||||
|
||||
List<SyncFieldsInfo> findAllByClientIdAndTableName(Integer clientId, String tableName);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.xydl.repository;
|
||||
|
||||
import com.xydl.entity.SyncRecords;
|
||||
import com.xydl.entity.SyncRecordsKey;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface SyncRecordsRepository extends JpaRepository<SyncRecords, SyncRecordsKey>, JpaSpecificationExecutor<SyncRecords> {
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.xydl.repository;
|
||||
|
||||
import com.xydl.entity.SyncTablesInfo;
|
||||
import com.xydl.entity.SyncTablesInfoKey;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface SyncTablesInfoRepository extends JpaRepository<SyncTablesInfo, SyncTablesInfoKey>, JpaSpecificationExecutor<SyncTablesInfo> {
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.xydl.service;
|
||||
|
||||
import com.xydl.entity.SyncTablesInfo;
|
||||
import com.xydl.util.DateUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class DbsqlService {
|
||||
@Resource
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
|
||||
public List<Integer> getDistinctEqmids(String tableName, String field) {
|
||||
String sql = "SELECT DISTINCT(" + field + ") FROM " + tableName;
|
||||
List<Integer> list = jdbcTemplate.queryForList(sql, Integer.class);
|
||||
return list;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getData(Integer eqmid, Date lasttime, SyncTablesInfo table) {
|
||||
String sqlSelect = "SELECT * FROM " + table.getId().getTableName();
|
||||
|
||||
String sqlWhere = " WHERE " + table.getDevidFieldName() + "=" + eqmid;
|
||||
if (lasttime != null) {
|
||||
sqlWhere = sqlWhere + " AND " + table.getFieldName() + ">'" + DateUtil.format(lasttime) + "'";
|
||||
}
|
||||
String sqlOrder = " ORDER BY " + table.getFieldName() + " ASC LIMIT 1000";
|
||||
|
||||
String sql = sqlSelect + sqlWhere + sqlOrder;
|
||||
// log.debug(sql);
|
||||
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
|
||||
return list;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.xydl.service;
|
||||
|
||||
import com.xydl.entity.I2relation;
|
||||
import com.xydl.repository.I2relationRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class I2relationService {
|
||||
@Resource
|
||||
I2relationRepository repository;
|
||||
|
||||
public List<I2relation> getList(Integer eqmid) {
|
||||
return repository.findAllByEqmid(eqmid);
|
||||
}
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
package com.xydl.service;
|
||||
|
||||
import com.xydl.entity.*;
|
||||
import com.xydl.model.AssetList;
|
||||
import com.xydl.util.DateUtil;
|
||||
import com.xydl.util.JSONUtil;
|
||||
import com.xydl.util.MqttUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class MqttService {
|
||||
|
||||
@Resource
|
||||
MqttUtil mqttUtil;
|
||||
@Resource
|
||||
I2relationService i2relationService;
|
||||
@Resource
|
||||
SyncTablesInfoService tablesInfoService;
|
||||
@Resource
|
||||
SyncFieldsInfoService fieldsInfoService;
|
||||
@Resource
|
||||
SyncRecordsService recordsService;
|
||||
@Resource
|
||||
DbsqlService dbsqlService;
|
||||
|
||||
// 上传任务
|
||||
public void uploadTask() {
|
||||
log.info("转发任务开始.");
|
||||
List<SyncTablesInfo> tableList = tablesInfoService.findAll();
|
||||
for (SyncTablesInfo table : tableList) {
|
||||
try {
|
||||
processOneTable(table);
|
||||
} catch (Exception e) {
|
||||
log.error("processOneTable exception: " + table.getId().getTableName(), e);
|
||||
}
|
||||
}
|
||||
log.info("转发任务结束.");
|
||||
}
|
||||
|
||||
// 处理单个表
|
||||
private void processOneTable(SyncTablesInfo table) throws Exception {
|
||||
List<SyncFieldsInfo> fieldList = fieldsInfoService.findAll(table.getId().getClientId(), table.getId().getTableName());
|
||||
if (!CollectionUtils.isEmpty(fieldList)) {
|
||||
List<Integer> eqmidList = dbsqlService.getDistinctEqmids(table.getId().getTableName(), table.getDevidFieldName());
|
||||
log.info("表{},共{}个设备", table.getId().getTableName(), eqmidList.size());
|
||||
if (!CollectionUtils.isEmpty(eqmidList)) {
|
||||
mqttUtil.connect();
|
||||
for (Integer eqmid : eqmidList) {
|
||||
this.processOneEqmid(eqmid, table, fieldList);
|
||||
}
|
||||
mqttUtil.disconnect();
|
||||
}
|
||||
} else {
|
||||
log.error("表{}未配置sync_fields_info", table.getId().getTableName());
|
||||
}
|
||||
}
|
||||
|
||||
// 处理单个设备
|
||||
private void processOneEqmid(Integer eqmid, SyncTablesInfo table, List<SyncFieldsInfo> fieldList) {
|
||||
|
||||
List<I2relation> relationList = i2relationService.getList(eqmid);
|
||||
|
||||
if (!CollectionUtils.isEmpty(relationList)) {
|
||||
try {
|
||||
int count = this.uploadOneBlock(eqmid, null,
|
||||
table, fieldList, relationList);
|
||||
if (count > 1000) {
|
||||
log.debug("表{}设备{}一共推送成功{}条数据.", table.getId().getTableName(), eqmid, count);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("表{}设备{}推送异常", table.getId().getTableName(), eqmid, e);
|
||||
}
|
||||
} else {
|
||||
log.warn("表{}设备{}未配置i2relation", table.getId().getTableName(), eqmid);
|
||||
}
|
||||
}
|
||||
|
||||
// 查询并上传单个设备的数据
|
||||
private int uploadOneBlock(Integer eqmid, SyncRecords record,
|
||||
SyncTablesInfo table, List<SyncFieldsInfo> fieldList, List<I2relation> relationList) throws Exception {
|
||||
|
||||
if (record == null) {
|
||||
Optional<SyncRecords> optional = recordsService.findByKey(table.getId().getClientId(), table.getId().getTableName(), String.valueOf(eqmid));
|
||||
if (optional.isPresent()) {
|
||||
record = optional.get();
|
||||
} else {
|
||||
SyncRecordsKey key = SyncRecordsKey.builder()
|
||||
.clientId(table.getId().getClientId())
|
||||
.tableName(table.getId().getTableName())
|
||||
.devidVal(String.valueOf(eqmid))
|
||||
.build();
|
||||
record = SyncRecords.builder()
|
||||
.id(key)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
List<Map<String, Object>> dataList = dbsqlService.getData(eqmid, record.getFieldVal2(), table);
|
||||
if (CollectionUtils.isEmpty(dataList)) {
|
||||
return 0;
|
||||
} else {
|
||||
count = dataList.size();
|
||||
AssetList assetList = AssetList.fromData(dataList, table.getFieldName(), relationList, fieldList);
|
||||
String json = JSONUtil.object2Json(assetList);
|
||||
mqttUtil.publish2MQTT(json);
|
||||
|
||||
Map<String, Object> lastMap = dataList.get(count - 1);
|
||||
LocalDateTime locdate = (LocalDateTime) lastMap.get(table.getFieldName());
|
||||
Date date = Date.from(locdate.atZone(ZoneId.systemDefault()).toInstant());
|
||||
record.setFieldVal2(date);
|
||||
recordsService.update(record);
|
||||
|
||||
log.debug("表{}设备{}推送成功{}条数据,最后时间{}", table.getId().getTableName(), eqmid, count, DateUtil.format(date));
|
||||
|
||||
if (count >= 1000) {
|
||||
count += uploadOneBlock(eqmid, record, table, fieldList, relationList);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.xydl.service;
|
||||
|
||||
import com.xydl.entity.SyncFieldsInfo;
|
||||
import com.xydl.repository.SyncFieldsInfoRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class SyncFieldsInfoService {
|
||||
@Resource
|
||||
SyncFieldsInfoRepository repository;
|
||||
|
||||
public List<SyncFieldsInfo> findAll(Integer clientId, String tableName) {
|
||||
return repository.findAllByClientIdAndTableName(clientId, tableName);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.xydl.service;
|
||||
|
||||
import com.xydl.entity.SyncRecords;
|
||||
import com.xydl.entity.SyncRecordsKey;
|
||||
import com.xydl.repository.SyncRecordsRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class SyncRecordsService {
|
||||
@Resource
|
||||
SyncRecordsRepository repository;
|
||||
|
||||
public Optional<SyncRecords> findByKey(Integer clientId, String tableName, String devid) {
|
||||
SyncRecordsKey key = SyncRecordsKey.builder()
|
||||
.clientId(clientId)
|
||||
.tableName(tableName)
|
||||
.devidVal(devid)
|
||||
.build();
|
||||
return repository.findById(key);
|
||||
}
|
||||
|
||||
public void update(SyncRecords record) {
|
||||
repository.save(record);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.xydl.service;
|
||||
|
||||
import com.xydl.entity.SyncTablesInfo;
|
||||
import com.xydl.entity.SyncTablesInfoKey;
|
||||
import com.xydl.repository.SyncTablesInfoRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class SyncTablesInfoService {
|
||||
@Resource
|
||||
SyncTablesInfoRepository repository;
|
||||
|
||||
public List<SyncTablesInfo> findAll() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public Optional<SyncTablesInfo> findByKey(Integer clientId, String tableName) {
|
||||
SyncTablesInfoKey key = SyncTablesInfoKey.builder()
|
||||
.clientId(clientId)
|
||||
.tableName(tableName)
|
||||
.build();
|
||||
return repository.findById(key);
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package com.xydl.util;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateUtil {
|
||||
public final static String defaultDatePattern = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
/**
|
||||
* 获得默认的 date pattern
|
||||
*/
|
||||
public static String getDatePattern() {
|
||||
return defaultDatePattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用预设Format格式化Date成字符串
|
||||
*/
|
||||
public static String format(Date date) {
|
||||
return format(date, getDatePattern());
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用参数Format格式化Date成字符串
|
||||
*/
|
||||
public static String format(Date date, String pattern) {
|
||||
String returnValue = "";
|
||||
|
||||
if (date != null) {
|
||||
SimpleDateFormat df = new SimpleDateFormat(pattern);
|
||||
returnValue = df.format(date);
|
||||
}
|
||||
|
||||
return (returnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用预设格式将字符串转为Date
|
||||
*/
|
||||
public static Date parse(String strDate) throws ParseException {
|
||||
return parse(strDate, getDatePattern());
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用参数Format将字符串转为Date
|
||||
*/
|
||||
public static Date parse(String strDate, String pattern) throws ParseException {
|
||||
SimpleDateFormat df = new SimpleDateFormat(pattern);
|
||||
return df.parse(strDate);
|
||||
}
|
||||
|
||||
public static Date getTodayZero() throws ParseException {
|
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String str = df.format(new Date());
|
||||
return df.parse(str);
|
||||
}
|
||||
|
||||
public static Date getMonthZero() throws ParseException {
|
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
|
||||
String str = df.format(new Date());
|
||||
return df.parse(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在日期上增加数个整日(n为负数则是减少数日)
|
||||
*/
|
||||
public static Date addDay(Date date, int n) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.add(Calendar.DAY_OF_MONTH, n);
|
||||
return cal.getTime();
|
||||
}
|
||||
|
||||
public static Date addMonth(Date date, int n) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
cal.add(Calendar.MONTH, n);
|
||||
return cal.getTime();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package com.xydl.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class JSONProcessor {
|
||||
|
||||
|
||||
public Map<String, Object> fromJSON2Map(String json) throws JsonParseException, JsonMappingException, IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> productMap = mapper.readValue(json, Map.class);
|
||||
return productMap;
|
||||
}
|
||||
|
||||
public Map<String, Object> fromJSON2Map(Object obj) throws JsonParseException, JsonMappingException, IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> productMap = mapper.convertValue(obj, Map.class);
|
||||
return productMap;
|
||||
}
|
||||
|
||||
public String buildJSONFromJSONObject(Object obj) {
|
||||
String jsonInString = null;
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
jsonInString = mapper.writeValueAsString(obj);
|
||||
} catch (Exception e) {
|
||||
// LOG.error("JSON transform failed. ", e);
|
||||
}
|
||||
return jsonInString;
|
||||
}
|
||||
|
||||
public String buildJSONFromJSONObject(Object obj, boolean prettyPrinter) {
|
||||
String jsonInString = null;
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
|
||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||
if (prettyPrinter) {
|
||||
jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
||||
} else {
|
||||
jsonInString = mapper.writeValueAsString(obj);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// LOG.error("JSON transform failed. ", e);
|
||||
}
|
||||
return jsonInString;
|
||||
}
|
||||
|
||||
public <T> T buildJSONObjectFromJSON(String json, Class<T> clazz) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper.readValue(json, clazz);
|
||||
} catch (UnrecognizedPropertyException e) {
|
||||
throw new Exception(e.getPropertyName(), e);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("JSON Object transform failed. ", e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T buildJSONObjectFromJSON(File file, Class<T> clazz) throws Exception {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
return mapper.readValue(file, clazz);
|
||||
} catch (UnrecognizedPropertyException e) {
|
||||
throw new Exception(e.getPropertyName(), e);
|
||||
} catch (Exception e) {
|
||||
throw new Exception("JSON Object transform failed. ", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @author roger - Sep 9, 2016
|
||||
* @version 2.0
|
||||
* file name: JSONProcessor.java
|
||||
* package name: com.roam2free.rest.util
|
||||
*/
|
||||
|
||||
package com.xydl.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class JSONUtil {
|
||||
|
||||
private static JSONProcessor jSONProcessor = new JSONProcessor();
|
||||
|
||||
public static String object2Json(Object obj) {
|
||||
return jSONProcessor.buildJSONFromJSONObject(obj);
|
||||
}
|
||||
|
||||
public static String object2Json(Object obj, boolean prettyPrinter) {
|
||||
return jSONProcessor.buildJSONFromJSONObject(obj, prettyPrinter);
|
||||
}
|
||||
|
||||
public static <T> T json2Object(String json, Class<T> clazz) throws Exception {
|
||||
return jSONProcessor.buildJSONObjectFromJSON(json, clazz);
|
||||
}
|
||||
|
||||
public static <T> T file2Object(File file, Class<T> clazz) throws Exception {
|
||||
return jSONProcessor.buildJSONObjectFromJSON(file, clazz);
|
||||
}
|
||||
|
||||
public static Map<String, Object> object2Map(Object obj) {
|
||||
try {
|
||||
return jSONProcessor.fromJSON2Map(obj);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue