|
|
@@ -1,279 +1,272 @@
|
|
|
package cn.vbdsm.analysis.dao;
|
|
|
|
|
|
-import java.text.ParseException;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.Comparator;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import cn.vbdsm.analysis.model.OnlineDataEnergy;
|
|
|
+import cn.vbdsm.common.constants.DataSourceConstant;
|
|
|
+import cn.vbdsm.common.utils.DataSourceUtil;
|
|
|
+import cn.vbdsm.core.dao.datasource.DynamicDataSource;
|
|
|
+import cn.vbdsm.core.dao.datasource.DynamicDataSourceEntry;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import org.springframework.stereotype.Repository;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.core.common.jdbc.BaseDaoSupport;
|
|
|
import javax.core.common.jdbc.QueryRule;
|
|
|
import javax.core.common.utils.DateTime;
|
|
|
import javax.sql.DataSource;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
|
|
|
-import org.springframework.stereotype.Repository;
|
|
|
+@Repository
|
|
|
+public class OnlineDataEnergyDao extends BaseDaoSupport<OnlineDataEnergy, String> {
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
-import cn.vbdsm.analysis.model.OnlineDataEnergy;
|
|
|
-import cn.vbdsm.common.constants.DataSourceConstant;
|
|
|
-import cn.vbdsm.common.utils.DataSourceUtil;
|
|
|
-import cn.vbdsm.core.dao.datasource.DynamicDataSource;
|
|
|
-import cn.vbdsm.core.dao.datasource.DynamicDataSourceEntry;
|
|
|
+ private final static String PK_COLUMN = "terminalId";
|
|
|
+ private final SimpleDateFormat dateSdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
+ private final SimpleDateFormat hourSdf = new SimpleDateFormat("yyyyMMddHH");
|
|
|
+ private DynamicDataSourceEntry dynamicDataSourceEntry;
|
|
|
|
|
|
-@Repository
|
|
|
-public class OnlineDataEnergyDao extends BaseDaoSupport<OnlineDataEnergy,String>{
|
|
|
-
|
|
|
- private DynamicDataSourceEntry dynamicDataSourceEntry;
|
|
|
- private final static String PK_COLUMN = "terminalId";
|
|
|
-
|
|
|
- private SimpleDateFormat dateSdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
- private SimpleDateFormat hourSdf = new SimpleDateFormat("yyyyMMddHH");
|
|
|
-
|
|
|
// private Logger LOG = Logger.getLogger(this.getClass());
|
|
|
-
|
|
|
- @Override
|
|
|
- protected String getPKColumn() {
|
|
|
- return PK_COLUMN;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 配置动态数据源
|
|
|
- *
|
|
|
- * @param dataSource
|
|
|
- */
|
|
|
- @Resource(name = "dynamicDataSource")
|
|
|
- public void setDataSource(DataSource dataSource) {
|
|
|
- dynamicDataSourceEntry = ((DynamicDataSource) dataSource).getDataSourceEntry();
|
|
|
- this.setDataSourceReadOnly(dataSource);
|
|
|
- this.setDataSourceWrite(dataSource);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public List<OnlineDataEnergy> selectByTimeAndTerminalIds(Long time,List<String> terminalIds){
|
|
|
- if(null == terminalIds || terminalIds.size() == 0){ return new ArrayList<OnlineDataEnergy>(); }
|
|
|
- Date today = new Date();
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- try {
|
|
|
- c.setTime(dateSdf.parse(time.toString().substring(0,8)));
|
|
|
- } catch (ParseException e1) {
|
|
|
- e1.printStackTrace();
|
|
|
- }
|
|
|
- c.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
- Date tomrrow = c.getTime();
|
|
|
- Long todayTime = Long.valueOf(dateSdf.format(today));
|
|
|
-
|
|
|
- QueryRule queryRule = QueryRule.getInstance();
|
|
|
- try{
|
|
|
- Long startTime = Long.valueOf(dateSdf.format(dateSdf.parse("" + time)) + "000000");
|
|
|
- Long endTime = Long.valueOf(dateSdf.format(tomrrow) + "000000");
|
|
|
-
|
|
|
- queryRule.andBetween("getTime", startTime,endTime);
|
|
|
- queryRule.andIn("terminalId", terminalIds);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取两个时间之间所有月份列表<br>传入格式为yyyyDDmm
|
|
|
+ *
|
|
|
+ * @param minDate
|
|
|
+ * @param maxDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> dateSplitAllMonth(String startDate, String endDate) {
|
|
|
+ ArrayList<String> result = new ArrayList<String>();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");// 格式化为年月
|
|
|
+ try {
|
|
|
+ Calendar min = Calendar.getInstance();
|
|
|
+ Calendar max = Calendar.getInstance();
|
|
|
+
|
|
|
+ min.setTime(sdf.parse(startDate));
|
|
|
+ min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
|
|
|
+
|
|
|
+ max.setTime(sdf.parse(endDate));
|
|
|
+ max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
|
|
|
+
|
|
|
+ SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM");// 格式化为年月
|
|
|
+ Calendar curr = min;
|
|
|
+ while (curr.before(max)) {
|
|
|
+ result.add(sdf1.format(curr.getTime()));
|
|
|
+ curr.add(Calendar.MONTH, 1);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected String getPKColumn() {
|
|
|
+ return PK_COLUMN;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 配置动态数据源
|
|
|
+ *
|
|
|
+ * @param dataSource
|
|
|
+ */
|
|
|
+ @Resource(name = "dynamicDataSource")
|
|
|
+ public void setDataSource(DataSource dataSource) {
|
|
|
+ dynamicDataSourceEntry = ((DynamicDataSource) dataSource).getDataSourceEntry();
|
|
|
+ this.setDataSourceReadOnly(dataSource);
|
|
|
+ this.setDataSourceWrite(dataSource);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<OnlineDataEnergy> selectByTimeAndTerminalIds(Long time, List<String> terminalIds) {
|
|
|
+ if (null == terminalIds || terminalIds.size() == 0) {
|
|
|
+ return new ArrayList<OnlineDataEnergy>();
|
|
|
+ }
|
|
|
+ Date today = new Date();
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ try {
|
|
|
+ c.setTime(dateSdf.parse(time.toString().substring(0, 8)));
|
|
|
+ } catch (ParseException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ c.add(Calendar.DAY_OF_YEAR, 1);
|
|
|
+ Date tomrrow = c.getTime();
|
|
|
+ Long todayTime = Long.valueOf(dateSdf.format(today));
|
|
|
+
|
|
|
+ QueryRule queryRule = QueryRule.getInstance();
|
|
|
+ try {
|
|
|
+ Long startTime = Long.valueOf(dateSdf.format(dateSdf.parse("" + time)) + "000000");
|
|
|
+ Long endTime = Long.valueOf(dateSdf.format(tomrrow) + "000000");
|
|
|
+
|
|
|
+ queryRule.andBetween("getTime", startTime, endTime);
|
|
|
+ queryRule.andIn("terminalId", terminalIds);
|
|
|
// queryRule.addAscOrder("getTime");
|
|
|
- }catch(Exception e){
|
|
|
- return new ArrayList<OnlineDataEnergy>();
|
|
|
- }
|
|
|
-
|
|
|
- if(todayTime > time){
|
|
|
- //根据年,月 切换到 年数据源,月分表
|
|
|
- int year = DataSourceUtil.parse(time).get("year");
|
|
|
- int month = DataSourceUtil.parse(time).get("month");
|
|
|
- dynamicDataSourceEntry.set(year);
|
|
|
- this.setTableName(DataSourceUtil.getTableName(super.op.tableName,month));
|
|
|
- List<OnlineDataEnergy> r = super.find(queryRule);
|
|
|
- this.setTableName(super.op.tableName);
|
|
|
- return r;
|
|
|
- }else{
|
|
|
- dynamicDataSourceEntry.set(DataSourceConstant.DB_CURR);
|
|
|
- this.setTableName(super.op.tableName);
|
|
|
- return super.find(queryRule);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public List<OnlineDataEnergy> selectByGetDateAndTerminalIds(Long getDate,List<String> terminalIds) {
|
|
|
- QueryRule queryRule = QueryRule.getInstance();
|
|
|
- try{
|
|
|
- Long startTime = Long.valueOf(dateSdf.format(dateSdf.parse("" + getDate)) + "000000");
|
|
|
- Long endTime = Long.valueOf(dateSdf.format(dateSdf.parse("" + getDate)) + "235959");
|
|
|
- queryRule.andBetween("getTime", startTime,endTime);
|
|
|
- queryRule.andIn("terminalId", terminalIds);
|
|
|
- queryRule.addAscOrder("getTime");
|
|
|
- }catch(Exception e){
|
|
|
- return new ArrayList<OnlineDataEnergy>();
|
|
|
- }
|
|
|
-
|
|
|
- //根据年,月 切换到 年数据源,月分表
|
|
|
- int year = DataSourceUtil.parse(getDate).get("year");
|
|
|
- int month = DataSourceUtil.parse(getDate).get("month");
|
|
|
- dynamicDataSourceEntry.set(year);
|
|
|
- this.setTableName(DataSourceUtil.getTableName(super.op.tableName,month));
|
|
|
- List<OnlineDataEnergy> r = super.find(queryRule);
|
|
|
- this.setTableName(super.op.tableName);
|
|
|
- return r;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public List<OnlineDataEnergy> selectByHour(Date hour,String terminalId){
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- c.setTime(hour);
|
|
|
-
|
|
|
- Long getDate = Long.valueOf(dateSdf.format(c.getTime()));
|
|
|
-
|
|
|
- c.add(Calendar.HOUR_OF_DAY, -1);
|
|
|
-
|
|
|
- Date start = c.getTime();
|
|
|
- c.add(Calendar.HOUR_OF_DAY, 1);
|
|
|
-
|
|
|
- Date end = c.getTime();
|
|
|
-
|
|
|
- Long startTime = Long.valueOf(hourSdf.format(start) + "0000");
|
|
|
- Long endTime = Long.valueOf(hourSdf.format(end) + "0000");
|
|
|
-
|
|
|
- int year = DataSourceUtil.parse(getDate).get("year");
|
|
|
- int month = DataSourceUtil.parse(getDate).get("month");
|
|
|
- String tableName = DataSourceUtil.getTableName(super.op.tableName,month);
|
|
|
-
|
|
|
- StringBuffer sql = new StringBuffer("");
|
|
|
- sql.append("(select * from " + tableName + " where getTime BETWEEN " + startTime + " and " + endTime + " and terminalId = ? limit 0,1)");
|
|
|
- sql.append("union all");
|
|
|
- sql.append("(select * from " + tableName + " where getTime BETWEEN " + startTime + " and " + endTime + " and terminalId = ? limit 0,1)");
|
|
|
-
|
|
|
- dynamicDataSourceEntry.set(year);
|
|
|
- List<Map<String, Object>> r = super.findBySql(sql.toString(),terminalId,terminalId);
|
|
|
- List<OnlineDataEnergy> result = JSONArray.parseArray(JSON.toJSONString(r), OnlineDataEnergy.class);
|
|
|
- Collections.sort(result,new Comparator<OnlineDataEnergy>() {
|
|
|
-
|
|
|
- @Override
|
|
|
- public int compare(OnlineDataEnergy o1, OnlineDataEnergy o2) {
|
|
|
- if(o1.getGetTime() > o2.getGetTime()){
|
|
|
- return 1;
|
|
|
- }else if(o1.getGetTime() < o2.getGetTime()){
|
|
|
- return -1;
|
|
|
- }else{
|
|
|
- return 1;
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据时间段查询所有电量实时数据
|
|
|
- * @param terminalId
|
|
|
- * @param start
|
|
|
- * @param end
|
|
|
- * @return
|
|
|
- */
|
|
|
- public List<OnlineDataEnergy> selectByTerminalIdAndGetTime(String terminalId,Long start,Long end) {
|
|
|
- super.setTableName(null);//数据源置空,后面将重新加载数据源
|
|
|
- List<OnlineDataEnergy> dataList = new ArrayList<OnlineDataEnergy>();
|
|
|
- if(DataSourceUtil.isSameDay(start, end)){ //同一天
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ArrayList<OnlineDataEnergy>();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (todayTime > time) {
|
|
|
+ //根据年,月 切换到 年数据源,月分表
|
|
|
+ int year = DataSourceUtil.parse(time).get("year");
|
|
|
+ int month = DataSourceUtil.parse(time).get("month");
|
|
|
+ dynamicDataSourceEntry.set(year);
|
|
|
+ this.setTableName(DataSourceUtil.getTableName(super.op.tableName, month));
|
|
|
+ List<OnlineDataEnergy> r = super.find(queryRule);
|
|
|
+ this.setTableName(super.op.tableName);
|
|
|
+ return r;
|
|
|
+ } else {
|
|
|
+ dynamicDataSourceEntry.set(DataSourceConstant.DB_CURR);
|
|
|
+ this.setTableName(super.op.tableName);
|
|
|
+ return super.find(queryRule);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<OnlineDataEnergy> selectByGetDateAndTerminalIds(Long getDate, List<String> terminalIds) {
|
|
|
+ QueryRule queryRule = QueryRule.getInstance();
|
|
|
+ try {
|
|
|
+ Long startTime = Long.valueOf(dateSdf.format(dateSdf.parse("" + getDate)) + "000000");
|
|
|
+ Long endTime = Long.valueOf(dateSdf.format(dateSdf.parse("" + getDate)) + "235959");
|
|
|
+ queryRule.andBetween("getTime", startTime, endTime);
|
|
|
+ queryRule.andIn("terminalId", terminalIds);
|
|
|
+ queryRule.addAscOrder("getTime");
|
|
|
+ } catch (Exception e) {
|
|
|
+ return new ArrayList<OnlineDataEnergy>();
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据年,月 切换到 年数据源,月分表
|
|
|
+ int year = DataSourceUtil.parse(getDate).get("year");
|
|
|
+ int month = DataSourceUtil.parse(getDate).get("month");
|
|
|
+ dynamicDataSourceEntry.set(year);
|
|
|
+ this.setTableName(DataSourceUtil.getTableName(super.op.tableName, month));
|
|
|
+ List<OnlineDataEnergy> r = super.find(queryRule);
|
|
|
+ this.setTableName(super.op.tableName);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<OnlineDataEnergy> selectByHour(Date hour, String terminalId) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(hour);
|
|
|
+
|
|
|
+ Long getDate = Long.valueOf(dateSdf.format(c.getTime()));
|
|
|
+
|
|
|
+ c.add(Calendar.HOUR_OF_DAY, -1);
|
|
|
+ Date start = c.getTime();
|
|
|
+ c.add(Calendar.HOUR_OF_DAY, 1);
|
|
|
+ Date mid = c.getTime();
|
|
|
+ c.add(Calendar.HOUR_OF_DAY, 1);
|
|
|
+ Date end = c.getTime();
|
|
|
+
|
|
|
+ Long startTime = Long.valueOf(hourSdf.format(start) + "0000");
|
|
|
+ Long midTime = Long.valueOf(hourSdf.format(mid) + "0000");
|
|
|
+ Long endTime = Long.valueOf(hourSdf.format(end) + "0000");
|
|
|
+
|
|
|
+ int year = DataSourceUtil.parse(getDate).get("year");
|
|
|
+ int month = DataSourceUtil.parse(getDate).get("month");
|
|
|
+ String tableName = DataSourceUtil.getTableName(super.op.tableName, month);
|
|
|
+
|
|
|
+ String sql = "(select * from " + tableName + " where getTime BETWEEN " + startTime + " and " + midTime + " and terminalId = ? limit 0,1)" +
|
|
|
+ "union all" +
|
|
|
+ "(select * from " + tableName + " where getTime BETWEEN " + midTime + " and " + endTime + " and terminalId = ? limit 0,1)";
|
|
|
+
|
|
|
+ dynamicDataSourceEntry.set(year);
|
|
|
+ List<Map<String, Object>> r = super.findBySql(sql, terminalId, terminalId);
|
|
|
+ List<OnlineDataEnergy> result = JSONArray.parseArray(JSON.toJSONString(r), OnlineDataEnergy.class);
|
|
|
+ Collections.sort(result, new Comparator<OnlineDataEnergy>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int compare(OnlineDataEnergy o1, OnlineDataEnergy o2) {
|
|
|
+ if (o1.getGetTime() > o2.getGetTime()) {
|
|
|
+ return 1;
|
|
|
+ } else if (o1.getGetTime() < o2.getGetTime()) {
|
|
|
+ return -1;
|
|
|
+ } else {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据时间段查询所有电量实时数据
|
|
|
+ *
|
|
|
+ * @param terminalId
|
|
|
+ * @param start
|
|
|
+ * @param end
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<OnlineDataEnergy> selectByTerminalIdAndGetTime(String terminalId, Long start, Long end) {
|
|
|
+ super.setTableName(null);//数据源置空,后面将重新加载数据源
|
|
|
+ List<OnlineDataEnergy> dataList = new ArrayList<OnlineDataEnergy>();
|
|
|
+ if (DataSourceUtil.isSameDay(start, end)) { //同一天
|
|
|
// if(DataSourceUtil.isToday(start)) {//今天
|
|
|
// return getOnlineDataEnergyByTerminalId(terminalId, start, end);
|
|
|
// }
|
|
|
- //不是今天,切换数据源和表查询数据
|
|
|
- dynamicDataSourceEntry.set(DataSourceUtil.parse(start).get(DataSourceUtil.YEAR_KEY));
|
|
|
- String tableName = DataSourceUtil.getTableName(super.getTableName(), DataSourceUtil.parse(start).get(DataSourceUtil.MONTH_KEY));
|
|
|
- super.setTableName(tableName);
|
|
|
-
|
|
|
- return getOnlineDataEnergyByTerminalId(terminalId, start, end);
|
|
|
- }
|
|
|
- //不是同一天
|
|
|
- String startDate = new DateTime(String.valueOf(start), DateTime.TYPE_YEAR_TO_SECOND,DateTime.TYPE_YEAR_TO_DAY).toString();
|
|
|
- String endDate = new DateTime(String.valueOf(end), DateTime.TYPE_YEAR_TO_SECOND,DateTime.TYPE_YEAR_TO_DAY).toString();
|
|
|
- List<String> monthList = dateSplitAllMonth(startDate, endDate);
|
|
|
- for (String date : monthList) {
|
|
|
- super.setTableName(null);//数据源置空,后面将重新加载数据源
|
|
|
- dynamicDataSourceEntry.set(DataSourceUtil.parse(date).get(DataSourceUtil.YEAR_KEY));
|
|
|
- String tableName = DataSourceUtil.getTableName(super.getTableName(), DataSourceUtil.parse(date).get(DataSourceUtil.MONTH_KEY));
|
|
|
- super.setTableName(tableName);
|
|
|
- List<OnlineDataEnergy> list = getOnlineDataEnergyByTerminalId(terminalId, start, end);
|
|
|
- if(!list.isEmpty()){
|
|
|
- dataList.addAll(list);
|
|
|
- }
|
|
|
- }
|
|
|
- return dataList;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private List<OnlineDataEnergy> getOnlineDataEnergyByTerminalId(String terminalId,Long start,Long end) {
|
|
|
- QueryRule queryRule = QueryRule.getInstance();
|
|
|
- queryRule.andEqual("terminalId", terminalId);
|
|
|
- queryRule.andGreaterEqual("getTime", start);
|
|
|
- queryRule.andLessEqual("getTime", end);
|
|
|
- queryRule.addAscOrder("getTime");
|
|
|
- return super.find(queryRule);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取两个时间之间所有月份列表<br>传入格式为yyyyDDmm
|
|
|
- * @param minDate
|
|
|
- * @param maxDate
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static List<String> dateSplitAllMonth(String startDate, String endDate) {
|
|
|
- ArrayList<String> result = new ArrayList<String>();
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");// 格式化为年月
|
|
|
- try {
|
|
|
- Calendar min = Calendar.getInstance();
|
|
|
- Calendar max = Calendar.getInstance();
|
|
|
-
|
|
|
- min.setTime(sdf.parse(startDate));
|
|
|
- min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
|
|
|
-
|
|
|
- max.setTime(sdf.parse(endDate));
|
|
|
- max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
|
|
|
-
|
|
|
- SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMM");// 格式化为年月
|
|
|
- Calendar curr = min;
|
|
|
- while (curr.before(max)) {
|
|
|
- result.add(sdf1.format(curr.getTime()));
|
|
|
- curr.add(Calendar.MONTH, 1);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- public int replaceForBatch(List<OnlineDataEnergy> data) throws Exception {
|
|
|
- //保存到历史库
|
|
|
- Long time = data.get(0).getGetTime();
|
|
|
- if(time == 0){ return 0;}
|
|
|
- //根据年,月 切换到 年数据源,月分表
|
|
|
- int year = 0;
|
|
|
- int month = 0;
|
|
|
- try{
|
|
|
- year = DataSourceUtil.parse(time).get("year");
|
|
|
- month = DataSourceUtil.parse(time).get("month");
|
|
|
- }catch(Exception e){
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if(0 == year || month == 0){
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- // LOG.error("replaceAll==========time=" + time + ",year=" + year + ",month=" + month);
|
|
|
- // LOG.error("replaceAll,历史库" + year + month);
|
|
|
- dynamicDataSourceEntry.set(year);
|
|
|
- this.setTableName(DataSourceUtil.getTableName(super.op.tableName,month));
|
|
|
- this.replaceAll(data);
|
|
|
- // LOG.warn("成功写入" + data.size() + "条数据");
|
|
|
-
|
|
|
- //保存到实时库
|
|
|
- this.setTableName(super.op.tableName);
|
|
|
- dynamicDataSourceEntry.set(DataSourceConstant.DB_CURR);
|
|
|
+ //不是今天,切换数据源和表查询数据
|
|
|
+ dynamicDataSourceEntry.set(DataSourceUtil.parse(start).get(DataSourceUtil.YEAR_KEY));
|
|
|
+ String tableName = DataSourceUtil.getTableName(super.getTableName(), DataSourceUtil.parse(start).get(DataSourceUtil.MONTH_KEY));
|
|
|
+ super.setTableName(tableName);
|
|
|
+
|
|
|
+ return getOnlineDataEnergyByTerminalId(terminalId, start, end);
|
|
|
+ }
|
|
|
+ //不是同一天
|
|
|
+ String startDate = new DateTime(String.valueOf(start), DateTime.TYPE_YEAR_TO_SECOND, DateTime.TYPE_YEAR_TO_DAY).toString();
|
|
|
+ String endDate = new DateTime(String.valueOf(end), DateTime.TYPE_YEAR_TO_SECOND, DateTime.TYPE_YEAR_TO_DAY).toString();
|
|
|
+ List<String> monthList = dateSplitAllMonth(startDate, endDate);
|
|
|
+ for (String date : monthList) {
|
|
|
+ super.setTableName(null);//数据源置空,后面将重新加载数据源
|
|
|
+ dynamicDataSourceEntry.set(DataSourceUtil.parse(date).get(DataSourceUtil.YEAR_KEY));
|
|
|
+ String tableName = DataSourceUtil.getTableName(super.getTableName(), DataSourceUtil.parse(date).get(DataSourceUtil.MONTH_KEY));
|
|
|
+ super.setTableName(tableName);
|
|
|
+ List<OnlineDataEnergy> list = getOnlineDataEnergyByTerminalId(terminalId, start, end);
|
|
|
+ if (!list.isEmpty()) {
|
|
|
+ dataList.addAll(list);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dataList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<OnlineDataEnergy> getOnlineDataEnergyByTerminalId(String terminalId, Long start, Long end) {
|
|
|
+ QueryRule queryRule = QueryRule.getInstance();
|
|
|
+ queryRule.andEqual("terminalId", terminalId);
|
|
|
+ queryRule.andGreaterEqual("getTime", start);
|
|
|
+ queryRule.andLessEqual("getTime", end);
|
|
|
+ queryRule.addAscOrder("getTime");
|
|
|
+ return super.find(queryRule);
|
|
|
+ }
|
|
|
+
|
|
|
+ public int replaceForBatch(List<OnlineDataEnergy> data) throws Exception {
|
|
|
+ //保存到历史库
|
|
|
+ Long time = data.get(0).getGetTime();
|
|
|
+ if (time == 0) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ //根据年,月 切换到 年数据源,月分表
|
|
|
+ int year = 0;
|
|
|
+ int month = 0;
|
|
|
+ try {
|
|
|
+ year = DataSourceUtil.parse(time).get("year");
|
|
|
+ month = DataSourceUtil.parse(time).get("month");
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (0 == year || month == 0) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // LOG.error("replaceAll==========time=" + time + ",year=" + year + ",month=" + month);
|
|
|
+ // LOG.error("replaceAll,历史库" + year + month);
|
|
|
+ dynamicDataSourceEntry.set(year);
|
|
|
+ this.setTableName(DataSourceUtil.getTableName(super.op.tableName, month));
|
|
|
+ this.replaceAll(data);
|
|
|
+ // LOG.warn("成功写入" + data.size() + "条数据");
|
|
|
+
|
|
|
+ //保存到实时库
|
|
|
+ this.setTableName(super.op.tableName);
|
|
|
+ dynamicDataSourceEntry.set(DataSourceConstant.DB_CURR);
|
|
|
// LOG.error("replaceAll,实时库" + this.getTableName());
|
|
|
- return this.replaceAll(data);
|
|
|
- }
|
|
|
+ return this.replaceAll(data);
|
|
|
+ }
|
|
|
}
|