liurunyu
2025-01-11 0cec0071fdace0410690c5aca60d2edad82cbe2b
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.dy.pipIrrTemp.changeSome;
 
import com.dy.common.util.DateTime;
import com.dy.pipIrrGlobal.daoTmp.ChangeMapper;
import com.dy.pipIrrGlobal.pojoRm.RmIntakeAmountDay;
import com.dy.pipIrrGlobal.pojoSt.StIntakeAmountMonth;
import com.dy.pipIrrGlobal.pojoSt.StIntakeAmountYear;
import com.dy.pipIrrGlobal.voSt.VoIntake;
import com.dy.pipIrrTemp.util.StClientAmountConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.List;
 
/**
 * @Author: liurunyu
 * @Date: 2024/12/14 9:25
 * @Description
 */
 
 
@Slf4j
@Service
public class ChSomeIntakeAmountSv {
 
    private ChangeMapper dao;
 
    @Autowired
    private void setDao(ChangeMapper dao){
        this.dao = dao;
    }
 
 
    ////////////////////////////////////////
    //
    // 有关日统计
    //
    ///////////////////////////////////////
 
    /**
     * 调整取水口日取水量统计
     * @throws Exception
     */
    public void chIntakeAmountDay() throws Exception{
        RmIntakeAmountDay lastAd = null ;
        List<VoIntake> list = dao.selectAllPrIntakes() ;
        if(list != null && list.size() > 0){
            for(VoIntake vo : list){
                lastAd = null ;
                List<RmIntakeAmountDay> adList = dao.selectOneIntakeAllAmountDay(vo.getIntakeId()) ;
                if(adList != null && adList.size() > 0){
                    //adList中数据以id升序排列
                    for(RmIntakeAmountDay ad : adList){
                        if(lastAd == null){
                            lastAd = ad ;
                            if(lastAd.amount != null && lastAd.amount > StClientAmountConstant.IntakeAmountDayFlag){
                                this.doUpdateOneIntakeAmountDay(lastAd.id, 0.0);
                            }
                        }else{
                            if(ad.amount != null && ad.amount > StClientAmountConstant.IntakeAmountDayFlag){
                                if(ad.totalAmountLast != null && lastAd.totalAmountLast != null){
                                    //重新计算日取水量
                                    ad.amount = ad.totalAmountLast - lastAd.totalAmountLast;
                                    if(ad.amount < 0){//计算日取水量后,可能会有这种情况发生(负值)
                                        log.info("出现调整后的日取水量出现负值" + ad.amount
                                                + "情况,数据id=" + ad.id
                                                + ",取水口id=" + ad.intakeId
                                                + ",日期=" + ad.dt
                                                + ",设置水量=0.0"
                                        );
                                        ad.amount = 0.0 ;
                                    }
                                    if(ad.amount > StClientAmountConstant.IntakeAmountDayFlag){
                                        //计算日取水量后,仍大于IntakeAmountDayFlag
                                        log.info("出现调整后的日取水量仍大于"
                                                + StClientAmountConstant.IntakeAmountDayFlag
                                                + "的情况,数据id=" + ad.id
                                                + ",取水口id=" + ad.intakeId
                                                + ",日期=" + ad.dt
                                                + ad.amount
                                        );
                                        if(ad.dt != null && lastAd.dt != null){
                                            if(DateTime.daysBetweenyyyy_MM_dd(ad.dt, lastAd.dt) == 1){
                                                //只相差一天
                                                log.info("出现相差一天但日取水量大于"
                                                        + StClientAmountConstant.IntakeAmountDayFlag
                                                        + "的情况,数据id=" + ad.id
                                                        + ",取水口id=" + ad.intakeId
                                                        + ",日期=" + ad.dt
                                                        + ",设置水量=0.0"
                                                        + ",设置前水量= " + ad.amount
                                                );
                                                ad.amount = 0.0 ;
                                            }else{
                                                //已经修改后的数值仍然大于IntakeAmountDayFlag,说明是大日未上报数据而积累下来,可以大于IntakeAmountDayFlag
                                                //ad.amount = ad.amount ;
                                                log.info("出现相差多天日取水量大于"
                                                        + StClientAmountConstant.IntakeAmountDayFlag
                                                        + "的情况,数据id=" + ad.id
                                                        + ",取水口id=" + ad.intakeId
                                                        + ",日期=" + ad.dt
                                                        + ",水量保持不变=" + ad.amount
                                                );
                                            }
                                        }
                                    }
                                }else{
                                    log.info("出现相差多天日取水量大于"
                                            + StClientAmountConstant.IntakeAmountDayFlag
                                            + "并且属性totalAmountLast为null的情况,数据id=" + ad.id
                                            + ",取水口id=" + ad.intakeId
                                            + ",日期=" + ad.dt
                                            + ",设置水量=0.0"
                                            + ",设置前水量= " + ad.amount
                                    );
                                    ad.amount = 0.0 ;
                                }
                                this.doUpdateOneIntakeAmountDay(ad.id,  ad.amount);
                            }
                        }
                        lastAd = ad ;
                    }
                }
            }
        }
    }
 
    @Transactional
    protected int doUpdateOneIntakeAmountDay(Long id, Double amount){
        return dao.updateOneIntakeAmountDay(id, amount);
    }
 
    /**
     * 删除所有取水口日取水量统计
     * @throws Exception
     */
    @Transactional
    public void deleteAllIntakeAmountDay(){
        dao.deleteAllIntakeAmountDay();
    }
 
    /**
     * 统计取水口日取水量
     * @throws Exception
     */
    public void statisticsIntakeAmountDay() throws Exception{
        int nowYear = Integer.parseInt(DateTime.yyyy()) ;
        int nowMonth = Integer.parseInt(DateTime.MM()) ;
        int nowDay = Integer.parseInt(DateTime.dd()) ;
        Date startDt ;
        Date endDt ;
        List<RmIntakeAmountDay> list ;
        boolean stop = false ;
        for(int[] ym : StClientAmountConstant.yearMonthGrp) {
            int[] dates = DateTime.dayGrpInMonth(ym[0], ym[1]);
            for(int day: dates){
                if(ym[0] == nowYear && ym[1] == nowMonth && day > nowDay){
                    stop = true ;
                }
                if(!stop){
                    startDt = DateTime.dateFrom_yyyy_MM_dd_HH_mm_ss(ym[0] + "-" + ym[1] + "-" + day + " 00:00:00") ;
                    endDt = DateTime.dateFrom_yyyy_MM_dd_HH_mm_ss(ym[0] + "-" + ym[1] + "-" + day + " 23:59:59") ;
                    list = dao.statisticAllIntakeAmountDayFromOpenCloseValveHistory(startDt, endDt) ;
                    this.doStatisticsIntakeAmountDay(ym[0], ym[1], day, list) ;
                }else{
                    break ;
                }
            }
        }
    }
    @Transactional
    protected void doStatisticsIntakeAmountDay(int year, int month, int day, List<RmIntakeAmountDay> list) throws Exception{
        if(list != null && list.size() > 0){
            Date date = DateTime.dateFrom_yyyy_MM_dd(year + "-" + month + "-" + day) ;
            for(RmIntakeAmountDay po : list){
                dao.updateOneIntakeAmountDay1(po.intakeId, date, po.amount, po.money, po.times);
            }
        }
    }
 
 
    ////////////////////////////////////////
    //
    // 有关月统计
    //
    ///////////////////////////////////////
 
    /**
     * 删除所有取水口月取水量统计
     * @throws Exception
     */
    @Transactional
    public void deleteAllIntakeAmountMonth(){
        dao.deleteAllIntakeAmountMonth();
    }
 
    /**
     * 统计取水口月取水量
     * @throws Exception
     */
    public void statisticsIntakeAmountMonth() throws Exception{
        int nowYear = Integer.parseInt(DateTime.yyyy()) ;
        int nowMonth = Integer.parseInt(DateTime.MM()) ;
        Date startDt ;
        Date endDt ;
        List<StIntakeAmountMonth> list ;
        for(int year = StClientAmountConstant.DealStartYear; year <= nowYear; year ++){
            list = null ;
            if(year < nowYear){
                for(int month = 1 ; month <= 12 ; month ++){
                    startDt = DateTime.dateFrom_yyyy_MM_dd(year + "-" + month + "-01") ;
                    endDt = DateTime.dateFrom_yyyy_MM_dd(year + "-" + month + "-" + DateTime.endDayOfMonth(year, month)) ;
                    list = dao.statisticAllIntakeAmountMonthFromAmountDay(startDt, endDt) ;
                    this.doStatisticsIntakeAmountMonth(year, month, list) ;
                }
            }else{
                for(int month = 1 ; month <= nowMonth ; month ++){
                    startDt = DateTime.dateFrom_yyyy_MM_dd(year + "-" + month + "-01") ;
                    endDt = DateTime.dateFrom_yyyy_MM_dd(year + "-" + month + "-" + DateTime.endDayOfMonth(year, month)) ;
                    list = dao.statisticAllIntakeAmountMonthFromAmountDay(startDt, endDt) ;
                    this.doStatisticsIntakeAmountMonth(year, month, list) ;
                }
            }
        }
    }
    @Transactional
    protected void doStatisticsIntakeAmountMonth(int year, int month, List<StIntakeAmountMonth> list) throws Exception{
        if(list != null && list.size() > 0){
            for(StIntakeAmountMonth po : list){
                po.year = year ;
                po.month = month ;
                dao.saveOneIntakeAmountMonth(po) ;
            }
        }
    }
 
 
    ////////////////////////////////////////
    //
    // 有关年统计
    //
    ///////////////////////////////////////
    /**
     * 删除所有取水口年取水量统计
     * @throws Exception
     */
    @Transactional
    public void deleteAllIntakeAmountYear(){
        dao.deleteAllIntakeAmountYear();
    }
 
    /**
     * 统计取水口月取水量
     * @throws Exception
     */
    public void statisticsIntakeAmountYear() throws Exception{
        int nowYear = Integer.parseInt(DateTime.yyyy()) ;
        Date startDt ;
        Date endDt ;
        List<StIntakeAmountYear> list ;
        for(int year = StClientAmountConstant.DealStartYear; year <= nowYear; year ++){
            list = null ;
            startDt = DateTime.dateFrom_yyyy_MM_dd(year + "-01-01") ;
            endDt = DateTime.dateFrom_yyyy_MM_dd(year + "-12-31") ;
            list = dao.statisticAllIntakeAmountYearFromAmountDay(startDt, endDt) ;
            this.doStatisticsIntakeAmountYear(year, list) ;
        }
    }
    @Transactional
    protected void doStatisticsIntakeAmountYear(int year, List<StIntakeAmountYear> list) throws Exception{
        if(list != null && list.size() > 0){
            for(StIntakeAmountYear po : list){
                po.year = year ;
                dao.saveOneIntakeAmountYear(po) ;
            }
        }
    }
 
}