feiyu02
2025-08-14 b10c22af595bd995e56946bff63b8f2f984b13e8
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
package com.flightfeather.uav.lightshare.bean
 
import com.flightfeather.uav.common.utils.DateUtil
import com.flightfeather.uav.domain.entity.CompanyDevice
import com.flightfeather.uav.domain.entity.ElectricMinuteValue
import com.flightfeather.uav.lightshare.eunm.ElectricityStatus
import com.flightfeather.uav.lightshare.eunm.ElectricityType
import java.util.*
import kotlin.math.round
 
/**
 * 用电量日分析结果
 * Date 2021/12/1 15:23
 * Created by feiyu
 */
class ElectricDailyInfo {
    var day = ""
    var dayIndex = 0
 
    /***********产线设备ProductionLine, pl************/
    var plDCode: String? = null  // 设备编号
    private var plSTime: Date? = null    // 开启时间
    private var plRTime: Date? = null    // 正式运行时间
    private var plETime: Date? = null    // 关闭时间
    var plRunTime: Int = 0    // 运行时长(分钟)(状态为运行、高负荷的时长)
    private var plRunPeriod = mutableListOf<Int>()// 运行时段(小时)
    var plPower = .0 // 用电量(千瓦时)
    /***********净化设备Purify, pf********************/
    var pfDCode: String? = null
    private var pfSTime: Date? = null
    private var pfRTime: Date? = null
    private var pfETime: Date? = null
    var pfRunTime: Int = 0
    private var pfRunPeriod = mutableListOf<Int>()
    var pfPower = .0
 
    var plRTimeStr: String? = null
    var plETimeStr: String? = null
    var pfRTimeStr: String? = null
    var pfETimeStr: String? = null
    var rTimeDiff:Int? = 0 //正式运行时间差
    var sResult:Boolean = false //设备开启是否合规
    var eTimeDiff: Int? = 0 //关闭时间差
    var eResult: Boolean = false //设备关闭是否合规
    var runningTimeDiff = 0 //运行时长差
    var rResult:Boolean = false //运行过程是否合规
    var dailyResult: String = ""// 当日分析结果描述
 
    // 当前设备类型,用于插入数据时决定赋值字段
    private var deviceType: ElectricityType = ElectricityType.ProductionLine
 
    fun changeType(d: CompanyDevice?) {
        deviceType = when (d?.cdType) {
            ElectricityType.ProductionLine.value -> {
                plDCode = d.cdDeviceCode
                ElectricityType.ProductionLine
            }
            ElectricityType.Purify.value -> {
                pfDCode = d.cdDeviceCode
                ElectricityType.Purify
            }
            else -> ElectricityType.UnKnow
        }
    }
 
    fun setStartTime(date: Date) {
        when (deviceType) {
            ElectricityType.ProductionLine -> plSTime = date
            ElectricityType.Purify -> pfSTime = date
            else -> Unit
        }
    }
 
    private var lastPlStatus = -2 // 上个时间点的产线设备状态
    private var plTag = false // true:当日最后一分钟的数据依旧是运行状态,未关闭产线设备
    private var lastPfStatus = -2 // 上个时间点的净化设备状态
    private var pfTag = false
    fun setEndTime(s: Triple<String, String, Double>, e:ElectricMinuteValue?) {
        val hourMinute = DateUtil.instance.dateToString(e?.mvDataTime, DateUtil.DateStyle.HH_MM)
        when (deviceType) {
            ElectricityType.ProductionLine -> {
                if (lastPlStatus == -1) {
                    return
                } else if (lastPlStatus <= ElectricityStatus.A.value) {
                    lastPlStatus = s.first.toInt()
                } else if (lastPlStatus >= ElectricityStatus.B.value) {
                    if (s.first.toInt() <= ElectricityStatus.A.value) {
                        plETime = e?.mvDataTime
                        lastPlStatus = -1
                    } else {
                        if (hourMinute == "23:59") plTag = true
                        lastPlStatus = s.first.toInt()
                    }
                }
            }
            ElectricityType.Purify -> {
                if (lastPfStatus == -1) {
                    return
                } else if (lastPfStatus <= ElectricityStatus.A.value) {
                    lastPfStatus = s.first.toInt()
                } else if (lastPfStatus >= ElectricityStatus.B.value) {
                    if (s.first.toInt() <= ElectricityStatus.A.value) {
                        pfETime = e?.mvDataTime
                        lastPfStatus = -1
                    } else {
                        if (hourMinute == "23:59") pfTag = true
                        lastPfStatus = s.first.toInt()
                    }
                }
            }
            else -> Unit
        }
    }
 
    /**
     * 如果设备关闭时是直接从运行状态断电,则在此处将最后一个数据的时间记为关闭时间
     */
    fun setEndTime2(date: Date) {
        when (deviceType) {
            ElectricityType.ProductionLine -> {
                if (plETime == null && !plTag) {
                    plETime = date
                }
            }
            ElectricityType.Purify -> {
                if (pfETime == null && !pfTag) {
                    pfETime = date
                }
            }
            else -> Unit
        }
    }
 
    /**
     * 设置正式运行时间
     */
    fun setRunningTime(s: Triple<String, String, Double>, e:ElectricMinuteValue?) {
        when (deviceType) {
            ElectricityType.ProductionLine -> {
                if (plRTime == null) {
                    if (s.first.toInt() >= ElectricityStatus.C.value) {
                        plRTime = e?.mvDataTime
                    }
                }
            }
            ElectricityType.Purify -> {
                if (pfRTime == null) {
                    if (s.first.toInt() >= ElectricityStatus.C.value) {
                        pfRTime = e?.mvDataTime
                    }
                }
            }
            else -> Unit
        }
    }
 
    /**
     * 增加运行时长
     */
    fun addRunTime(s: Triple<String, String, Double>) {
        if (s.first.toInt() >= ElectricityStatus.C.value) {
            when (deviceType) {
                ElectricityType.ProductionLine -> plRunTime++
                ElectricityType.Purify -> pfRunTime++
                else -> Unit
            }
        }
    }
 
    /**
     * 添加运行时段(小时)
     */
    fun addRunPeriod(hour: Int) {
        when (deviceType) {
            ElectricityType.ProductionLine -> if (!plRunPeriod.contains(hour)) plRunPeriod += hour
            ElectricityType.Purify -> if (!pfRunPeriod.contains(hour)) pfRunPeriod += hour
            else -> Unit
        }
    }
 
    /**
     * 累计用电量
     */
    fun addPower(e: ElectricMinuteValue?) {
        e?.let {
            val avgElectric = (it.mvElectricityA + it.mvElectricityB + it.mvElectricityC) / 3
            val power = avgElectric * 1 / 60
            when (deviceType) {
                ElectricityType.ProductionLine -> plPower += power
                ElectricityType.Purify -> pfPower += power
                else -> Unit
            }
        }
    }
 
    /**
     * 统计得出当日分析结果
     */
    fun getResult() {
        // 用电量
        plPower = round(plPower * 100) / 100
 
        pfPower = round(pfPower * 100) / 100
        // 开关时间格式化
        plRTimeStr = DateUtil.instance.dateToString(plRTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)
        plETimeStr = DateUtil.instance.dateToString(plETime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)
        pfRTimeStr = DateUtil.instance.dateToString(pfRTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)
        pfETimeStr = DateUtil.instance.dateToString(pfETime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS)
 
        // 1. 设备正式运行时间差 =》开启是否合规
        if (pfRTime != null && plRTime != null) {
            rTimeDiff = ((pfRTime!!.time - plRTime!!.time) / 1000 / 60).toInt()
            sResult = rTimeDiff == null || rTimeDiff!! < 0
        }
        // 2,设备关闭时间差 =》关闭是否合规
        if (pfETime != null && plETime != null) {
            eTimeDiff = ((pfETime!!.time - plETime!!.time) / 1000 / 60).toInt()
            eResult = eTimeDiff == null || eTimeDiff!! > 0
        } else {
            // 当关闭时间存在null时,说明当日并没有关闭设备,关闭合规
            eTimeDiff = null
            eResult = true
        }
        // 3. 运行时长差 =》运行过程是否合规
        runningTimeDiff = pfRunTime - plRunTime
        rResult = runningTimeDiff > 0
 
        // TODO: 2021/12/1 5. 多日摘要统计,运行天数、合规天数等
    }
}