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
package com.power.alarm;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.base.Com;
import com.power.mysql.MysqlConnPool;
 
/**
 *     ¶ÔÀúÊ·¸æ¾¯¼Ç¼½øÐÐ·Ö±í´¦Àí£¬´´½¨µÄ·Ö±íʱ¼ä³¬¹ý3ÄêµÄ¸æ¾¯¼Ç¼ɾ³ý
 * @author LiJun
 *
 */
public class PwrDevHistoryAlm_SubtableThread implements Runnable{
    public static int MaxRecordAlmTimeLong = 366*3;            //×î´ó¼Ç¼ÀúÊ·¸æ¾¯Ê±¼ä
    
    private List<PwrDeviceAlarm_Data> pwrAlmDatas;             //ÐèÒª·Ö±íµÄµçÔ´ÀúÊ·¸æ¾¯¼Ç¼
    private MysqlConnPool pool;
    private Logger logger = null;
    
    public PwrDevHistoryAlm_SubtableThread(MysqlConnPool pool) {
        this.pwrAlmDatas = new ArrayList<>();             //ÐèÒª·Ö±íµÄµçÔ´ÀúÊ·¸æ¾¯¼Ç¼
        this.pool = pool;
        this.logger = LogManager.getLogger(this);
    }
 
    @Override
    public void run() {
        logger.info(" PwrDevHistoryAlm_SubtableThread Start at " + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) );
        Date time = new Date(0);
        Date lastCheck = time;                //ÉÏÒ»´Î¼ì²âÀúÊ·¸æ¾¯Ê±¼ä
        Date lastDelete = time;                //ÉÏÒ»´Îɾ³ýÀúÊ·¸æ¾¯Ê±¼ä
        
        while(true) {
            try {
                pwrAlmDatas.clear();
                
                time = new Date();
                long checkTimeLong = (time.getTime()-lastCheck.getTime())/(24*60*60*1000);
                //System.out.println("checkTimeLong:"+checkTimeLong);
                if(checkTimeLong >= 1) {
                    //ÿÌì¼ì²âÒ»´ÎÀúÊ·¸æ¾¯Êý¾Ý
                    PwrDevHistoryAlm_SubtableThread_SQL.queryBattHistoryAlarm(pool, pwrAlmDatas);
                    //System.err.println(pwrAlmDatas);
                    if(pwrAlmDatas.size() > 0) {
                        //¿ªÊ¼½øÐÐµç³ØÀúÊ·¸æ¾¯¼Ç¼·Ö±í
                        for(int i=0;i<pwrAlmDatas.size();i++) {
                            /*
                            if(battAlmDatas.get(i).battgroup_id != 1000022) {
                                continue;
                            }
                            */
                            
                            if(i%10==0) {                                
                                Thread.sleep(100);
                            }
                            //½«³¬¹ý1¸öÔµĵçÔ´¸æ¾¯¼Ç¼¼ÇÈëÀúÊ·¸æ¾¯·Ö±í
                            PwrDevHistoryAlm_SubtableThread_SQL.changePwrAlarmToHistoryTable(pool, pwrAlmDatas.get(i));
                        }                        
                    }                
                    lastCheck = time;
                }
                
                
                long deleteTimeLong = (time.getTime()-lastDelete.getTime())/(24*60*60*1000);
                //System.out.println("deleteTimeLong:"+deleteTimeLong);
                if(deleteTimeLong >= 1) {
                    //10Ìì¼ì²âÒ»´Îɾ³ýÀúÊ·¸æ¾¯Êý¾Ý                    
                    Date deldate = getDateBefore(time, MaxRecordAlmTimeLong);
                    //¼ì²âÀúÊ·¸æ¾¯¼Ç¼,ɾ³ý3ÄêǰµÄ¸æ¾¯¼Ç¼·Ö±í
                    PwrDevHistoryAlm_SubtableThread_SQL.delBattAlarmData(pool, deldate);                
                    
                    lastDelete = time;
                }    
                
            } catch (Exception e) {
                logger.error(e.toString(), e);
            } finally {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    logger.error(e.toString(), e);
                }
            }            
        }    
    }
    
    /**
     *     »ñȡָ¶¨Ê±¼ä֮ǰָ¶¨ÌìÊýµÄʱ¼ä
     * @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();
    }
}