Bladeren bron

导入数据到新表

yq 4 jaren geleden
bovenliggende
commit
eb37f82b4d

+ 8 - 8
fiveep-controller/src/main/java/com/bizmatics/controller/web/MybatisGeneratorUtils.java

@@ -18,7 +18,7 @@ import java.util.List;
 public class MybatisGeneratorUtils {
     public static void main(String[] args) {
         //修改成自己的模块名称
-        String[] models = {"fiveep-controller", "test-service", "test-model", "test-persistence"};
+        String[] models = {"fiveep-controller", "fiveep-service", "fiveep-model", "fiveep-persistence"};
         for (String model : models) {
             shell(model);
         }
@@ -43,10 +43,10 @@ public class MybatisGeneratorUtils {
         //2、数据源配置
         //修改数据源
         DataSourceConfig dsc = new DataSourceConfig();
-        dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
+        dsc.setUrl("jdbc:mysql://124.71.145.219:3306/smart_electricity?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
         dsc.setDriverName("com.mysql.jdbc.Driver");
         dsc.setUsername("root");
-        dsc.setPassword(null);
+        dsc.setPassword("Wjzn2021Db");
         mpg.setDataSource(dsc);
 
         // 3、包配置
@@ -70,13 +70,13 @@ public class MybatisGeneratorUtils {
         // strategy.setTablePrefix("t_"); // 表名前缀
         strategy.setEntityLombokModel(true); //使用lombok
         //修改自己想要生成的表
-        strategy.setInclude("test");  // 逆向工程使用的表   如果要生成多个,这里可以传入String[]
+        strategy.setInclude("had_data_log");  // 逆向工程使用的表   如果要生成多个,这里可以传入String[]
         mpg.setStrategy(strategy);
 
         // 关闭默认 xml 生成,调整生成 至 根目录
         //修改对应的模块名称
         TemplateConfig tc = new TemplateConfig();
-        if ("test-persistence".equals(model)) {
+        if ("fiveep-persistence".equals(model)) {
             tc.setController(null);
             tc.setEntity(null);
             tc.setService(null);
@@ -104,18 +104,18 @@ public class MybatisGeneratorUtils {
             cfg.setFileOutConfigList(focList);
             mpg.setCfg(cfg);
             tc.setXml(null);
-        } else if ("test-model".equals(model)) {
+        } else if ("fiveep-model".equals(model)) {
             tc.setController(null);
             tc.setService(null);
             tc.setServiceImpl(null);
             tc.setMapper(null);
             tc.setXml(null);
-        } else if ("test-service".equals(model)) {
+        } else if ("fiveep-service".equals(model)) {
             tc.setController(null);
             tc.setMapper(null);
             tc.setXml(null);
             tc.setEntity(null);
-        } else if ("test-controller".equals(model)) {
+        } else if ("fiveep-controller".equals(model)) {
             tc.setMapper(null);
             tc.setXml(null);
             tc.setService(null);

+ 4 - 0
fiveep-model/src/main/java/com/bizmatics/model/HtAnalogData.java

@@ -412,4 +412,8 @@ public class HtAnalogData implements Serializable {
     private Date dataTime;
 
 
+    @TableField(exist = false)
+    private Integer siteId;
+
+
 }

+ 9 - 3
fiveep-persistence/src/main/resources/mapper/mysql/HtAnalogDataMapper.xml

@@ -74,16 +74,22 @@
         </where>
     </select>
     <select id="page" resultType="com.bizmatics.model.HtAnalogData">
-        select *
-        from ht_analog_data
+        select
+        had.id,deviceName,Busot,COS,COSa,COSb,COSc,Demand,DevResetTimes,DeviceTemp,Epn,Epn1,Epn2,Epn3,Epn4,Epp,
+        Epp1,Epp2,Epp3,Epp4,Eqn,Eqp,F,I0,I2,IHa,IHb,IHc,Ia,Ib,Ic,Ir,LastDayMD,LastDayMDt,P,Pa,Pb,Pc,Q,Qa,Qb,Qc,SignalIntensity,
+        T1,T2,T3,T4,THDUa,THDUb,THDUc,Ua,Uab,Ub,Ubc,UblU0,UblU2,Uc,Uca,Udt,Ul,Upt,Ust,freezingTime,dataTime,d.site_id as siteId
+        from ht_analog_data as had
+        left join device as d
+        on had.deviceName = d.device_code
         <where>
             <if test="id != null and id != 0">
-                and id > #{id}
+                and had.id > #{id}
             </if>
             <if test="endTime != null and startTime != null">
                 and dataTime BETWEEN #{startTime} and #{endTime}
             </if>
         </where>
+        order by dataTime asc
     </select>
 
 </mapper>

+ 56 - 5
fiveep-service/src/main/java/com/bizmatics/service/job/RatAnalogTask.java

@@ -2,21 +2,30 @@ package com.bizmatics.service.job;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.bizmatics.common.core.util.BeanMapperUtils;
+import com.bizmatics.common.core.util.DateUtils;
+import com.bizmatics.common.core.util.JsonMapper;
+import com.bizmatics.common.spring.util.JsonUtils;
 import com.bizmatics.model.AlarmPower;
+import com.bizmatics.model.HadDataLog;
 import com.bizmatics.model.HtAnalogData;
 import com.bizmatics.model.RtAnalogData;
 import com.bizmatics.model.es.RtAnalog;
+import com.bizmatics.persistence.mapper.HadDataLogMapper;
 import com.bizmatics.persistence.mapper.HtAnalogDataMapper;
 import com.bizmatics.persistence.mapper.RtAnalogDataMapper;
+import com.bizmatics.service.HadDataLogService;
 import com.bizmatics.service.es.RtAnalogService;
 import com.bizmatics.service.util.SessionLocal;
+import com.bizmatics.service.vo.HadDataLogVO;
+import com.google.common.collect.Lists;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections.CollectionUtils;
+import org.apache.poi.ss.usermodel.DateUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @author yq
@@ -29,24 +38,66 @@ public class RatAnalogTask {
 
     @Autowired
     private HtAnalogDataMapper htAnalogDataMapper;
-    @Autowired
-    private RtAnalogService rtAnalogService;
 
+    @Autowired
+    private HadDataLogMapper hadDataLogMapper;
 
     public void addAll(){
         List<HtAnalogData> list = null;
         Integer id = 0;
         Integer index = 1;
+        Date startTime = null;
+        Date endTime = null;
+        List<HadDataLogVO> hadVoList = new ArrayList<>();
         do {
             Page<HtAnalogData> page = new Page<>(index, 2000);
             page = htAnalogDataMapper.page(page, null,null,id);
             list = page.getRecords();
             if (!CollectionUtils.isEmpty(list)){
-                rtAnalogService.saveAll(BeanMapperUtils.mapList(list, HtAnalogData.class, RtAnalog.class));
+                if (id == 0){
+                    startTime = DateUtils.getDayStartTime(list.get(0).getDataTime());
+                    endTime = DateUtils.getDayEndTime(list.get(0).getDataTime());
+                }
                 id = list.get(list.size()-1).getId();
                 index++;
+                for (HtAnalogData htAnalogData:list) {
+                    if (htAnalogData.getDataTime().after(startTime) && htAnalogData.getDataTime().before(endTime)){
+                        insertList(hadVoList,htAnalogData,startTime);
+                    }else {
+                        List<HadDataLogVO> collect = hadVoList.stream().sorted(Comparator.comparing(HadDataLogVO::getSiteId)).collect(Collectors.toList());
+                        List<List<HadDataLogVO>> rsList = Lists.partition(collect, 2000);
+                        for (List<HadDataLogVO> logVOS:rsList) {
+                            HadDataLog hadDataLog = new HadDataLog();
+                            hadDataLog.setDataTime(startTime);
+                            hadDataLog.setMinId(logVOS.get(0).getSiteId());
+                            hadDataLog.setMaxId(logVOS.get(logVOS.size()-1).getSiteId());
+                            hadDataLog.setHadData(JsonUtils.toJson(logVOS));
+                            hadDataLogMapper.insert(hadDataLog);
+                        }
+                        startTime = DateUtils.getDayStartTime(htAnalogData.getDataTime());
+                        endTime = DateUtils.getDayEndTime(htAnalogData.getDataTime());
+                        hadVoList = new ArrayList<>();
+                        insertList(hadVoList,htAnalogData,startTime);
+                    }
+                }
             }
             log.info("处理到最大id"+id);
         }while (!CollectionUtils.isEmpty(list));
     }
+
+    public void insertList(List<HadDataLogVO> hadVoList,HtAnalogData htAnalogData,Date startTime){
+        HadDataLogVO hadDataLogVO = HadDataLogVO.builder()
+                .dataTime(startTime)
+                .deviceCode(htAnalogData.getDeviceName())
+                .epp(htAnalogData.getEpp())
+                .ia(htAnalogData.getIa())
+                .ib(htAnalogData.getIb())
+                .ic(htAnalogData.getIc())
+                .ua(htAnalogData.getUa())
+                .ub(htAnalogData.getUb())
+                .uc(htAnalogData.getUc())
+                .siteId(htAnalogData.getSiteId())
+                .build();
+        hadVoList.add(hadDataLogVO);
+    }
 }

+ 34 - 0
fiveep-service/src/main/java/com/bizmatics/service/vo/HadDataLogVO.java

@@ -0,0 +1,34 @@
+package com.bizmatics.service.vo;
+
+import lombok.Builder;
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * @author yq
+ * @date 2021/7/18 12:24
+ */
+@Builder
+@Data
+public class HadDataLogVO {
+    private Integer siteId;
+
+    private String deviceCode;
+
+    private Date dataTime;
+
+    private Double epp;
+
+    private Double ia;
+
+    private Double ib;
+
+    private Double ic;
+
+    private Double ua;
+
+    private Double ub;
+
+    private Double uc;
+}