DELL
2024-07-27 831953352ea4df0ebaa2f47fb6b82b082883d6b5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.dec.fbs9100;
 
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.base.AppConfig;
import com.base.Com;
import com.battdata_rt.BattData_RT;
import com.battdata_rt.BattData_RT_Array;
import com.battdata_rt.BattStatData;
 
/**
 *
 * @author DELL
 *
 */
public class BattRealDataPro_Thread extends Thread{
    public MysqlConnPool con_pool;
    public AppConfig cfg;
    public BattData_RT_Array data;
    Logger logger = null;
    
    public static int MinRecordTimeLong = 60;            //ÿ´Î¼Ç¼µÄ×î¶Ìʱ¼ä¼ä¸ô
    public static int MaxRecordTimeLong = 90;            //×î´ó¼Ç¼ÀúÊ·Êý¾Ýʱ¼ä
    
    public BattRealDataPro_Thread(MysqlConnPool con_pool, AppConfig cfg, BattData_RT_Array data) {
        logger = LogManager.getLogger(this.getClass());
        
        this.cfg = cfg;
        this.con_pool = con_pool;
        this.data = data;
    }
    
    
    @Override
    public void run() {
        logger.info("BattRealDataPro_Thread Started ...");
        
        Date lastTime = new Date();
        Date lastdelTime = new Date(0);
        //Ïß³ÌÆô¶¯Ç°ÐÝÃß60Ãë                            //ÓÃÓÚÕç±ð»¹Î´Í¨Ñ¶ÉϵÄÉ豸
//        for(int i =0; i<60; i++) {
//            try {
//                sleep(1000);
//            } catch (InterruptedException e) {
//                logger.error(e.toString(), e);
//            }
//        }
        logger.info("BattRealDataPro_Thread Start  Record......");
        Date nowTime = null;
        while(true) {
            /**************** mxpopstar add @ 20200817 ***************/
            try {
                sleep(500);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                logger.error(e1.toString(), e1);
            }
            /**********************************************************/
            nowTime = new Date();
            long timelong =(nowTime.getTime()-lastTime.getTime())/1000;
            long deltimelong = (nowTime.getTime()-lastdelTime.getTime())/1000;
            try {
                /**************** lijun add @ 20200906 ***************/
                //ɾ³ýÐÞ¸Äʱ¼ä³¬Ê±µÄÊý¾Ý±í
                if(deltimelong > 60*60*24) {
                    /**
                     * 2021-08-25 @lijun 24Сʱ¶¨ÆÚɾ³ýÖ¸¶¨ÌìÊý֮ǰµÄÀúʷʵʱ±í,¸Ã²éѯռÓÃSQL×ÊÔ´½Ï´ó£¬¼õÇáSQL²éѯ¸ºµ£
                     */
                    //»ñȡָ¶¨ÌìÊý֮ǰµÄʱ¼ä
                    Date critical = getDateBefore(nowTime, MaxRecordTimeLong);
                    logger.error("¿ªÊ¼É¾³ý"+Com.get_DTF(critical, Com.DTF_YMDhms)+"֮ǰ´´½¨µÄµç³Ø×éÀúʷʵʱÊý¾Ý¿â±í");
                    BTS61850_Task_Thread_SQL.deleteHistoryData(con_pool, critical);
                    //logger.error("ɾ³ý½áÊø¡£¡£¡£¡£¡£¡£");
                    
                    lastdelTime = nowTime;
                }
                /**********************************************************/
                if(timelong < MinRecordTimeLong) {
                    continue;
                }
                
                boolean isRecorded = false;
                for(int i=0; i<data.getItemCount(); i++) {
                    BattData_RT battData = data.getItem(i);
                    if(battData.getBattState() == BattStatData.BATTSTATE_NULL) {
                        continue;                //Ïû³ýδÁ¬½Ó¹ýµÄµç³Ø×é
                    }
                    //2021-08-09 lijun ÐÞ¸´³ÌÐòÆô¶¯Ê±Ò²»á×Ô¶¯¼Ç¼һ±ÊÊý¾ÝΪ0Êý¾Ýbug
                    if((Math.abs(nowTime.getTime()-battData.mTestData.newDataRecordTime.getTime())/1000 > 60)) {
                        continue;                        
                    }            
                    
                    isRecorded = true;
                    //¼Ç¼µ±Ç°µç³Ø×éµÄÀúʷʵʱ¼Ç¼
                    RecordBattReadDataNew(con_pool, battData);                            //¼Ç¼µç³Ø×éÀúʷʵʱ¼Ç¼
                    sleep(50);
                }    
                if(isRecorded) {
                    lastTime = nowTime;
                }
                /**************** mxpopstar edit @ 20200817 ***************/
                /*sleep(500);*/        //this statement is big bug, because line 59 use "continue" statement; 
                /**************** mxpopstar edit @ 20200817 ***************/
            } catch (Exception e) {
                logger.error(e.toString(), e);
            }
        }
    }
    
    /**
     *     ¼Ç¼µç³Ø×éÀúʷʵʱ¼Ç¼
     * @param pool
     * @param battData
     */
    public void RecordBattReadDataNew(MysqlConnPool pool,BattData_RT battData) {
        Date time = new Date();
        //´´½¨Ö¸¶¨µç³Ø×éµÄµç³Ø×éµ±ÔÂÀúʷʵʱÊý¾Ý¼Ç¼±í
        BTS61850_Task_Thread_SQL.CreateTb_BattRealDataTable(con_pool, battData.BattGroupId,time);
        //String sql_str_delete = " DELETE FROM "+ Sql_Mysql.Tb_Batt_RealData+battData.BattGroupId +" WHERE recrod_time < DATE_SUB(NOW(),INTERVAL 1 YEAR) ";                    //ɾ³ý³¬¹ý1ÄêµÄ¼Ç¼            
        String sql_str = " INSERT INTO " + Sql_Mysql.Tb_Batt_RealData+battData.BattGroupId+"_"+Com.getDateTimeFormat(time, Com.DTF_YM)+"(BattGroupId,recrod_time,group_vol,online_vol,group_curr,group_tmp,batt_state,batt_test_type,batt_test_starttime,batt_test_tlong,batt_test_cap,mon_num,mon_vol,mon_tmp,mon_res,mon_ser,mon_conn_res,mon_JH_curr)  VALUES" ;
        Date dt_test_Start = new Date();
        Date dt_record = new Date();
        dt_test_Start.setTime(battData.getTestStartTime());
        dt_record.setTime(battData.getTestRecordTime());
        for(int i=0;i<battData.MonCount;i++) {
            if(i > 0){
                sql_str += ",";
            }
            sql_str+="("
                    + battData.BattGroupId+","
                    + "'" + Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms ) + "',"
                    + battData.getGroupVol() +","
                    + battData.getOnlineVol() +","
                    + battData.getGroupCurr() +","
                    + battData.getGroupTmp() +","
                    + battData.getBattState() +","
                    + battData.getBattTestType() +","
                    + "'" + Com.getDateTimeFormat(dt_test_Start, Com.DTF_YMDhms) + "',"
                    + battData.getTestTimeLong() +","
                    + battData.getTestCap() +","
                    + (i+1) +","
                    + battData.al_MonVol.get(i).monVol +","
                    + battData.al_MonVol.get(i).monTmp +","
                    + battData.al_MonVol.get(i).monRes +","
                    + battData.al_MonVol.get(i).monSer +","
                    + battData.al_MonVol.get(i).connRes +","
                    + battData.al_MonVol.get(i).mon_JH_curr
                    +")";
            
        }
        Sql_Mysql sql = new Sql_Mysql(con_pool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            logger.error(e.toString(), e);
        } finally {
            sql.close_con();
        }    
    }
    
    /**
     *     ¼Ç¼µç³Ø×éÀúʷʵʱ¼Ç¼
     * @param pool
     * @param battData
     */
    public void RecordBattReadData(MysqlConnPool pool,BattData_RT battData) {
        //´´½¨Ö¸¶¨µç³Ø×éµÄµç³Ø×éÀúʷʵʱÊý¾Ý¼Ç¼±í
        FBS9100_Task_Thread_SQL.CreateTb_BattRealDataTable(con_pool, battData.BattGroupId);
        String sql_str_delete = " DELETE FROM "+ Sql_Mysql.Tb_Batt_RealData+battData.BattGroupId +" WHERE recrod_time < DATE_SUB(NOW(),INTERVAL 1 YEAR) ";                    //ɾ³ý³¬¹ý1ÄêµÄ¼Ç¼    
        
        
        String sql_str = " INSERT INTO " + Sql_Mysql.Tb_Batt_RealData+battData.BattGroupId+"(BattGroupId,recrod_time,group_vol,online_vol,group_curr,group_tmp,batt_state,batt_test_type,batt_test_starttime,batt_test_tlong,batt_test_cap,mon_num,mon_vol,mon_tmp,mon_res,mon_ser,mon_conn_res,mon_JH_curr)  VALUES" ;
        Date dt_test_Start = new Date();
        Date dt_record = new Date();
        dt_test_Start.setTime(battData.getTestStartTime());
        dt_record.setTime(battData.getTestRecordTime());
        for(int i=0;i<battData.MonCount;i++) {
            if(i > 0){
                sql_str += ",";
            }
            sql_str+="("
                    + battData.BattGroupId+","
                    + "'" + Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms ) + "',"
                    + battData.getGroupVol() +","
                    + battData.getOnlineVol() +","
                    + battData.getGroupCurr() +","
                    + battData.getGroupTmp() +","
                    + battData.getBattState() +","
                    + battData.getBattTestType() +","
                    + "'" + Com.getDateTimeFormat(dt_test_Start, Com.DTF_YMDhms) + "',"
                    + battData.getTestTimeLong() +","
                    + battData.getTestCap() +","
                    + (i+1) +","
                    + battData.al_MonVol.get(i).monVol +","
                    + battData.al_MonVol.get(i).monTmp +","
                    + battData.al_MonVol.get(i).monRes +","
                    + battData.al_MonVol.get(i).monSer +","
                    + battData.al_MonVol.get(i).connRes +","
                    + battData.al_MonVol.get(i).mon_JH_curr
                    +")";
            
        }
        Sql_Mysql sql = new Sql_Mysql(con_pool);
        try {
            sql.sqlMysqlExecute(sql_str);
            sql.sqlMysqlExecute(sql_str_delete);                //ɾ³ýµ±Ç°µç³Ø×éÒ»ÄêǰµÄÀúÊ·Êý¾Ý
        } catch (SQLException e) {
            logger.error(e.toString(), e);
        } finally {
            sql.close_con();
        }    
    }
    
    /**
     *     »ñȡָ¶¨Ê±¼ä֮ǰָ¶¨ÌìÊýµÄʱ¼ä
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }
    
    public static void main(String[] args) {
        System.out.println(Com.getDateTimeFormat(getDateBefore(new Date(), 366), Com.DTF_YMDhms));
    }
}