|
@@ -1,8 +1,6 @@
|
|
|
package com.platform.opc.servie;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.platform.common.constant.UpmsRedisKeyConstants;
|
|
|
-import com.platform.common.util.BeanUtils;
|
|
|
import com.platform.common.util.DateUtils;
|
|
|
import com.platform.common.util.RedisUtils;
|
|
|
import com.platform.common.util.StringUtils;
|
|
@@ -10,27 +8,22 @@ import com.platform.dao.entity.remote.RemoteOpc;
|
|
|
import com.platform.dao.entity.remote.RemoteOpcLog;
|
|
|
import com.platform.dao.mapper.remote.RemoteOpcLogMapper;
|
|
|
import com.platform.dao.mapper.remote.RemoteOpcMapper;
|
|
|
-import com.platform.dao.mapper.upms.SysDictMapper;
|
|
|
import com.platform.opc.entity.OpcResult;
|
|
|
import com.platform.opc.util.OpcDAClient;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.context.annotation.DependsOn;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
+import tk.mybatis.mapper.weekend.Weekend;
|
|
|
+import tk.mybatis.mapper.weekend.WeekendCriteria;
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
@Service("opcTaskService")
|
|
|
@AllArgsConstructor
|
|
@@ -71,13 +64,13 @@ public class OpcTaskService {
|
|
|
|
|
|
/**
|
|
|
* 1: 保存获取的数据
|
|
|
- * b: 开启新线程,队列写入数据库,每一分钟启动一次
|
|
|
+ * b: 开启新线程,队列写入数据库,每2分钟启动一次
|
|
|
* c: 每个点位,每条数据保存一天的
|
|
|
* 1)循环查询点位在当天是否存在记录,如果存在,则追加
|
|
|
- * 2)批量写入数据库
|
|
|
+ * 2)批量写入数据库,每天的数据追加到一条里面
|
|
|
*/
|
|
|
@Async
|
|
|
- @Scheduled(fixedDelay = 60000) //间隔60秒
|
|
|
+ @Scheduled(fixedDelay = 300000) //间隔300秒,5分钟保存一次数据到数据库,确保每天不超过700万数据
|
|
|
public void saveValue() {
|
|
|
log.info("开始读取redis");
|
|
|
String jsonStr = RedisUtils.getString("opcList");
|
|
@@ -87,6 +80,16 @@ public class OpcTaskService {
|
|
|
log.info("id: " + result.getId() + ", value: " + result.getValue() + ", time: " + result.getTime());
|
|
|
}*/
|
|
|
// 保存入库
|
|
|
+ // 查询当天是否已经存在了,存在则追加,否则则更新
|
|
|
+ Weekend<RemoteOpcLog> weekend = new Weekend<>(RemoteOpcLog.class);
|
|
|
+ WeekendCriteria<RemoteOpcLog, Object> weekendCriteria = weekend.weekendCriteria();
|
|
|
+ OpcResult log = resultList.get(0);
|
|
|
+ LocalDateTime time = DateUtils.strToLocalDateTime(log.getTime(), DateUtils.PATTERN_YMD_HMS);
|
|
|
+ weekendCriteria.andEqualTo(RemoteOpcLog::getPositionNum, log.getId());
|
|
|
+ weekendCriteria.andEqualTo(RemoteOpcLog::getYear, time.getYear());
|
|
|
+ weekendCriteria.andEqualTo(RemoteOpcLog::getMonth, time.getMonthValue());
|
|
|
+ weekendCriteria.andEqualTo(RemoteOpcLog::getDay, time.getDayOfMonth());
|
|
|
+ int count = remoteOpcLogMapper.selectCountByExample(weekend);
|
|
|
List<RemoteOpcLog> remoteOpcLogList = new ArrayList<>();
|
|
|
for (OpcResult result : resultList) {
|
|
|
RemoteOpcLog remoteOpcLog = new RemoteOpcLog();
|
|
@@ -97,12 +100,19 @@ public class OpcTaskService {
|
|
|
remoteOpcLog.setYear(localDateTime.getYear());
|
|
|
remoteOpcLog.setMonth(localDateTime.getMonthValue());
|
|
|
remoteOpcLog.setDay(localDateTime.getDayOfMonth());
|
|
|
- remoteOpcLog.setHour(localDateTime.getHour());
|
|
|
- remoteOpcLog.setMinute(localDateTime.getMinute());
|
|
|
+ // remoteOpcLog.setHour(localDateTime.getHour());
|
|
|
+ // remoteOpcLog.setMinute(localDateTime.getMinute());
|
|
|
+ remoteOpcLog.setRemark(result.getTime() + "," + result.getValue() + ";");
|
|
|
remoteOpcLogList.add(remoteOpcLog);
|
|
|
//log.info("id: " + result.getId() + ", value: " + result.getValue() + ", time: " + result.getTime());
|
|
|
}
|
|
|
- remoteOpcLogMapper.insertListforComplex(remoteOpcLogList);
|
|
|
+
|
|
|
+ // TODO:判断remoteOpcLogList是否大于5000,大于5000,就分多次存入
|
|
|
+ if (count > 0) {
|
|
|
+ remoteOpcLogMapper.updateBatch(remoteOpcLogList);
|
|
|
+ } else {
|
|
|
+ remoteOpcLogMapper.insertListforComplex(remoteOpcLogList);
|
|
|
+ }
|
|
|
}
|
|
|
log.info("结束读取redis");
|
|
|
}
|