liurunyu
2024-12-27 27bd4b5c03bbc8b665ffecb87d6c2cae3262e503
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
package com.dy.pipIrrStatistics.statistics;
 
import com.dy.common.multiDataSource.DataSourceContext;
import com.dy.common.schedulerTask.TaskJob;
import com.dy.common.schedulerTask.Test;
import com.dy.common.springUtil.SpringContextUtil;
import com.dy.common.util.DateTime;
import com.dy.common.util.IDLongGenerator;
import com.dy.pipIrrGlobal.util.Org;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
import java.util.Date;
import java.util.List;
 
/**
 * @Author: liurunyu
 * @Date: 2024/7/22 16:41
 * @Description
 */
public class StatisticsJob extends TaskJob {
 
    private static Logger log = LogManager.getLogger(Test.class.getName()) ;
 
    private String orgTag ;
 
    private StLoss stLoss ;
    private StIntake stIntake ;
    private StClient stClient ;
    private StChargeByIc stChargeByIc ;
    private StChargeByClient stChargeByClient ;
    private StConsumeByIc stConsumeByIc ;
    private StConsumeByClient stConsumeByClient ;
 
    private Integer statisticsYear;
    private Integer statisticsMonth;
    private Integer statisticsDay;
    private Long statistics4DayStartId ;
    private Long statistics4DayEndId ;
    private Date statistics4DayStartDt ;
    private Date statistics4DayEndDt ;
    private Long statistics4MonthYearStartId ;
    private Long statistics4MonthYearEndId ;
    //private Date statistics4MonthYearStartDt ;
    //private Date statistics4MonthYearEndDt ;
 
 
    @Override
    public void execute(JobExecutionContext ctx) throws JobExecutionException {
        JobDataMap jobDataMap = ctx.getJobDetail().getJobDataMap() ;
        if(jobDataMap != null){
            orgTag = (String)jobDataMap.get(StatisticsListener.orgKey) ;
        }
        if(orgTag == null){
            return ;
        }
        DataSourceContext.set(orgTag);//设置数据源
        stLoss = SpringContextUtil.getBean(StLoss.class);
        stIntake = SpringContextUtil.getBean(StIntake.class);
        stClient = SpringContextUtil.getBean(StClient.class);
        stChargeByIc = SpringContextUtil.getBean(StChargeByIc.class);
        stChargeByClient = SpringContextUtil.getBean(StChargeByClient.class);
        stConsumeByIc = SpringContextUtil.getBean(StConsumeByIc.class);
        stConsumeByClient = SpringContextUtil.getBean(StConsumeByClient.class);
        if(stLoss != null && stIntake != null && stClient != null
                && stChargeByIc != null && stChargeByClient != null
                && stConsumeByIc != null && stConsumeByClient != null){
            String yesterday = DateTime.lastXDay_yyyy_MM_dd(1) ;//得到昨天,零晨时刻统计,统计昨天的数
            int[] yesterdayGrp = DateTime.yyyy_MM_dd_2_ymdGroup(yesterday) ;
            statisticsYear = yesterdayGrp[0] ;//统计年
            statisticsMonth = yesterdayGrp[1] ;//统计月
            statisticsDay = yesterdayGrp[2] ;//统计日
            statistics4DayStartId = IDLongGenerator.generateOneDayStartId(statisticsYear, statisticsMonth, 1) ;
            statistics4DayEndId = IDLongGenerator.generateOneDayEndId(statisticsYear, statisticsMonth, DateTime.endDayOfMonth(statisticsYear, statisticsMonth)) ;
            statistics4DayStartDt = DateTime.startOfDay(statisticsYear, statisticsMonth, statisticsDay) ;
            statistics4DayEndDt = DateTime.endOfDay(statisticsYear, statisticsMonth, statisticsDay) ;
            //统计日的量
            doStatisticsDay() ;
 
            int[] ymd = DateTime.yyyy_MM_dd_2_ymdGroup(DateTime.yyyy_MM_dd()) ;
            if(ymd[2] == 1){
                //统计上个月的
                String statisticsYyyy_mm = DateTime.lastMonth_ym() ; //统计年月
                statisticsYear = Integer.parseInt(statisticsYyyy_mm.substring(0, 4)) ;//统计年
                statisticsMonth = Integer.parseInt(statisticsYyyy_mm.substring(5, 7)) ;//统计月
                statistics4MonthYearStartId = IDLongGenerator.generateOneDayStartId(statisticsYear, statisticsMonth, 1) ;
                statistics4MonthYearEndId = IDLongGenerator.generateOneDayEndId(statisticsYear, statisticsMonth, DateTime.endDayOfMonth(statisticsYear, statisticsMonth)) ;
                //statistics4MonthYearStartDt = DateTime.startOfDay(statisticsYear, statisticsMonth, 1) ;
                //statistics4MonthYearEndDt = DateTime.endOfDay(statisticsYear, statisticsMonth, DateTime.endDayOfMonth(statisticsYear, statisticsMonth)) ;
            }else{
                //统计本月的
                statisticsYear = ymd[0] ;//统计年
                statisticsMonth =  ymd[1] ;//统计月
                statistics4MonthYearStartId = IDLongGenerator.generateOneDayStartId(ymd[0], ymd[1], 1) ;
                statistics4MonthYearEndId = IDLongGenerator.generateOneDayEndId(ymd[0], ymd[1], DateTime.endDayOfMonth(statisticsYear, statisticsMonth)) ;
                //statistics4MonthYearStartDt = DateTime.startOfDay(statisticsYear, statisticsMonth, 1) ;
                //statistics4MonthYearEndDt = DateTime.endOfDay(statisticsYear, statisticsMonth, DateTime.endDayOfMonth(statisticsYear, statisticsMonth)) ;
            }
            doStatisticsMonthAndYear() ;
        }else{
            log.error("未能从Spring容器中得到统计bean");
        }
    }
 
    /**
     * 统计
     */
    private void doStatisticsDay(){
        stChargeByIc.statistics4Day(statisticsYear, statisticsMonth, statisticsDay, statistics4DayStartId, statistics4DayEndId);
        stChargeByClient.statistics4Day(statisticsYear, statisticsMonth, statisticsDay, statistics4DayStartId, statistics4DayEndId);
        stConsumeByIc.statistics4Day(statisticsYear, statisticsMonth, statisticsDay, statistics4DayStartDt, statistics4DayEndDt);
        stConsumeByClient.statistics4Day(statisticsYear, statisticsMonth, statisticsDay, statistics4DayStartDt, statistics4DayEndDt);
    }
    /**
     * 统计
     */
    private void doStatisticsMonthAndYear(){
        stLoss.statistics(statisticsYear, statisticsMonth, statistics4MonthYearStartId, statistics4MonthYearEndId) ;
        stIntake.statistics(statisticsYear, statisticsMonth, statistics4MonthYearStartId, statistics4MonthYearEndId) ;
        stClient.statistics(statisticsYear, statisticsMonth, statistics4MonthYearStartId, statistics4MonthYearEndId) ;
 
        stChargeByIc.statistics4MonthAndYear(statisticsYear, statisticsMonth);
        stChargeByClient.statistics4MonthAndYear(statisticsYear, statisticsMonth);
        stConsumeByIc.statistics4MonthAndYear(statisticsYear, statisticsMonth);
        stConsumeByClient.statistics4MonthAndYear(statisticsYear, statisticsMonth);
    }
}