feiyu02
2021-11-17 306ca22e966c0dc25841dcb7bd3db9db7c013bd9
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
package com.flightfeather.uav.lightshare.service.impl
 
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.domain.entity.toAirData
import com.flightfeather.uav.domain.mapper.CompanyDeviceMapper
import com.flightfeather.uav.domain.mapper.ElectricMinuteValueMapper
import com.flightfeather.uav.lightshare.bean.BaseResponse
import com.flightfeather.uav.lightshare.bean.DataHead
import com.flightfeather.uav.lightshare.bean.DataVo
import com.flightfeather.uav.lightshare.bean.ElectricVo
import com.flightfeather.uav.lightshare.eunm.ElectricityType
import com.flightfeather.uav.lightshare.service.ElectricityService
import com.flightfeather.uav.socket.bean.AirData
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.text.SimpleDateFormat
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
 
@Service
class ElectricityServiceImpl(
    private val electricMinuteValueMapper: ElectricMinuteValueMapper, private val companyDeviceMapper: CompanyDeviceMapper
) : ElectricityService {
 
    private var dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    private var dateFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
 
    override fun getMinuteData(
        deviceCode: String, startTime: String?,
        endTime: String?, page: Int?, perPage: Int?
    ): BaseResponse<List<ElectricMinuteValue>> {
        val perP = perPage ?: 60
        val p = page ?: 1
        val sTime = startTime?.let { dateFormatter.parse(it) }
        val eTime = endTime?.let { dateFormatter.parse(it) }
        val pageInfo = PageHelper.startPage<ElectricMinuteValue>(p, perP)
        val result = mutableListOf<ElectricMinuteValue>()
        electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply {
            createCriteria().andEqualTo("mvStatCode", deviceCode)
                .apply {
                    sTime?.let { andGreaterThanOrEqualTo("mvDataTime", it) }
                    eTime?.let { andLessThanOrEqualTo("mvDataTime", it) }
                }
            orderBy("mvDataTime").apply {
                // 当请求接口不传递起始时间,默认获取最新的数据
                if (startTime == null && endTime == null) {
                    desc()
                }
            }
        }).forEach {
            it?.let { result.add(it) }
        }
        if (startTime == null && endTime == null) {
            result.reverse()
        }
        return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result)
    }
 
    override fun getMinuteData2(deviceCode: String, startTime: String?, endTime: String?, page: Int?, perPage: Int?): BaseResponse<List<DataVo>> {
        val result = mutableListOf<DataVo>()
        getMinuteData(deviceCode, startTime, endTime, page, perPage).run {
            data?.forEach {
                result.add(DataVo(
                    DateUtil.instance.dateToString(it.mvDataTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS),
                    it.mvStatCode,
                    it.toAirData()
                ))
            }
            return BaseResponse(success, head = head, data = result)
        }
    }
 
    override fun getByCompany(cId: String): BaseResponse<List<ElectricMinuteValue>> {
        val result = mutableListOf<ElectricMinuteValue>()
        companyDeviceMapper.selectByExample(Example(CompanyDevice::class.java).apply {
            createCriteria().andEqualTo("cdCompanyId", cId)
        }).forEach {
            val p = PageHelper.startPage<ElectricMinuteValue>(1, 1)
            electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply {
                createCriteria().andEqualTo("mvStatCode", it?.cdDeviceCode)
                orderBy("mvDataTime").desc()
            })?.let {
                if (it.isNotEmpty()) {
                    it[0]?.let {e-> result.add(e) }
                }
            }
        }
 
        return BaseResponse(true, data = result)
    }
 
    override fun getElectricityInfo(cId: String, startTime: String?, endTime: String?, page: Int?, perPage: Int?): BaseResponse<List<ElectricVo>> {
        val perP = perPage ?: 60
        val p = page ?: 1
        val sTime = startTime?.let { dateFormatter.parse(it) }
        val eTime = endTime?.let { dateFormatter.parse(it) }
        val result = mutableListOf<ElectricVo>()
        val deviceCodeList = companyDeviceMapper.selectByExample(Example(CompanyDevice::class.java).apply {
            createCriteria().andEqualTo("cdCompanyId", cId)
        })
        //产线风机
        var d1: CompanyDevice? = null
        //废气净化装置
        var d2: CompanyDevice? = null
 
        deviceCodeList.forEach {
            if (it?.cdType == ElectricityType.ProductionLine.value) {
                d1 = it
            }else if (it?.cdType == ElectricityType.Purify.value) {
                d2 = it
            }
        }
 
        var pageInfo = PageHelper.startPage<ElectricMinuteValue>(p, perP)
        val dataList1 = electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply {
            createCriteria().andEqualTo("mvStatCode", d1?.cdDeviceCode)
                .apply {
                    sTime?.let { andGreaterThanOrEqualTo("mvDataTime", it) }
                    eTime?.let { andLessThanOrEqualTo("mvDataTime", it) }
                }
            orderBy("mvDataTime").apply {
                // 当请求接口不传递起始时间,默认获取最新的数据
                if (startTime == null && endTime == null) {
                    desc()
                }
            }
        })
        if (startTime == null && endTime == null) {
            dataList1.reverse()
        }
        pageInfo = PageHelper.startPage<ElectricMinuteValue>(p, perP)
        val dataList2 = electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply {
            createCriteria().andEqualTo("mvStatCode", d2?.cdDeviceCode)
                .apply {
                    sTime?.let { andGreaterThanOrEqualTo("mvDataTime", it) }
                    eTime?.let { andLessThanOrEqualTo("mvDataTime", it) }
                }
            orderBy("mvDataTime").apply {
                // 当请求接口不传递起始时间,默认获取最新的数据
                if (startTime == null && endTime == null) {
                    desc()
                }
            }
        })
        if (startTime == null && endTime == null) {
            dataList2.reverse()
        }
 
        // FIXME: 2021/11/5 返回结果需要根据两台设备的数据始末时间,选取最长的两个始末时间
        val sT = if (dataList1.isEmpty() && dataList2.isNotEmpty()) {
            dataList2[0]?.mvDataTime
        }else if (dataList1.isNotEmpty() && dataList2.isEmpty()) {
            dataList1[0]?.mvDataTime
        } else if (dataList1.isNotEmpty() && dataList2.isNotEmpty()) {
            if (dataList1[0]?.mvDataTime?.after(dataList2[0]?.mvDataTime) == true) {
                dataList2[0]?.mvDataTime
            } else {
                dataList1[0]?.mvDataTime
            }
        } else {
            null
        }
        val eT = if (dataList1.isEmpty() && dataList2.isNotEmpty()) {
            dataList2.last()?.mvDataTime
        }else if (dataList1.isNotEmpty() && dataList2.isEmpty()) {
            dataList1.last()?.mvDataTime
        } else if (dataList1.isNotEmpty() && dataList2.isNotEmpty()) {
            if (dataList1.last()?.mvDataTime?.after(dataList2.last()?.mvDataTime) == true) {
                dataList1.last()?.mvDataTime
            } else {
                dataList2.last()?.mvDataTime
            }
        } else {
            null
        }
 
        var lsT = LocalDateTime.ofInstant(sT?.toInstant(), ZoneId.systemDefault()).withSecond(0)
        val leT = LocalDateTime.ofInstant(eT?.toInstant(), ZoneId.systemDefault()).withSecond(0)
 
        if (sT != null && eT != null) {
            while (!lsT.isAfter(leT)) {
                val vo = ElectricVo(lsT.format(dateFormatter2))
                if (dataList1.isNotEmpty()) {
                    val d = dataList1[0]
                    val t = LocalDateTime.ofInstant(d?.mvDataTime?.toInstant(), ZoneId.systemDefault()).withSecond(0)
                    if (lsT.isEqual(t)) {
                        vo.apply {
                            d1eA = d?.mvElectricityA ?: .0
                            d1eB = d?.mvElectricityB ?: .0
                            d1eC = d?.mvElectricityC ?: .0
                            val s = getStatus(d, d1)
                            d1Status = s.first
                            d1StatusName = s.second
                        }
                        dataList1.removeAt(0)
                    }
                }
                if (dataList2.isNotEmpty()) {
                    val d = dataList2[0]
                    val t = LocalDateTime.ofInstant(d?.mvDataTime?.toInstant(), ZoneId.systemDefault()).withSecond(0)
                    if (lsT.isEqual(t)) {
                        vo.apply {
                            d2eA = d?.mvElectricityA ?: .0
                            d2eB = d?.mvElectricityB ?: .0
                            d2eC = d?.mvElectricityC ?: .0
                            val s = getStatus(d, d2)
                            d2Status = s.first
                            d2StatusName = s.second
                        }
                        dataList2.removeAt(0)
                    }
                }
                result.add(vo)
 
                lsT = lsT.plusMinutes(1)
            }
        }
 
        return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result)
    }
 
    // FIXME: 2021/11/5 此处暂时写死,后续修改
    private fun getStatus(e: ElectricMinuteValue?, d: CompanyDevice?): Pair<String, String> {
        var values = mutableListOf<Int>().apply {
            d?.cdLimits?.split(";")?.forEach {
                it.toIntOrNull()?.let { i -> add(i) }
            }
        }
        var status = d?.cdStatus?.split(";") ?: emptyList()
        var statusNames = d?.cdStatusName?.split(";") ?: emptyList()
        if (values.isEmpty()) values = mutableListOf(1, 100)
        if (status.isEmpty()) status = listOf("0", "2", "3")
        if (statusNames.isEmpty()) statusNames = listOf("待机", "运行", "超负荷")
        if (e == null) {
            return Pair(status.first(), statusNames.first())
        }
 
        val electricityList = mutableListOf<Double>()
        electricityList.add(e.mvElectricityA)
        electricityList.add(e.mvElectricityB)
        electricityList.add(e.mvElectricityC)
 
        val avg = electricityList.average()
        for (i in values.indices) {
            if (avg < values[i]) {
                return Pair(status[i], statusNames[i])
            }
        }
        return Pair(status.last(), statusNames.last())
    }
}