package cn.flightfeather.supervision.lightshare.service.Impl
|
|
import cn.flightfeather.supervision.domain.entity.*
|
import cn.flightfeather.supervision.domain.enumeration.SceneType
|
import cn.flightfeather.supervision.domain.mapper.*
|
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
|
import cn.flightfeather.supervision.lightshare.service.DeviceService
|
import cn.flightfeather.supervision.lightshare.vo.*
|
import com.github.pagehelper.PageHelper
|
import org.springframework.stereotype.Service
|
import tk.mybatis.mapper.entity.Example
|
import java.text.DecimalFormat
|
import java.util.*
|
import kotlin.math.round
|
|
@Service
|
class DeviceServiceImpl(
|
val fumePurifyDeviceMapper: FumePurifyDeviceMapper,
|
val monitorDeviceMapper: MonitorDeviceMapper,
|
val fumeMinuteValueMapper: FumeMinuteValueMapper,
|
val baseInfoMapper: BaseInfoMapper,
|
val deviceInfoMapper: DeviceInfoMapper,
|
val vocHourValueMapper: VOCHourValueMapper,
|
val userinfoMapper: UserinfoMapper
|
) : DeviceService {
|
|
override fun getPurifyDeviceInfo(userId: String): List<FumePurifyDevice> {
|
return fumePurifyDeviceMapper.selectByExample(Example(FumePurifyDevice::class.java).apply {
|
createCriteria().andEqualTo("fpUserId", userId)
|
})
|
}
|
|
override fun getMonitorDeviceInfo(userId: String): List<MonitorDevice> {
|
return monitorDeviceMapper.selectByExample(Example(MonitorDevice::class.java).apply {
|
createCriteria().andEqualTo("mdUserId", userId)
|
})
|
}
|
|
override fun getLatestMinuteValue(userId: String): List<DataVo> {
|
val result = mutableListOf<DataVo>()
|
val baseInfo = baseInfoMapper.selectByPrimaryKey(userId)
|
if (baseInfo != null) {
|
val mn = baseInfo.biExtension2
|
if (!mn.isNullOrBlank()) {
|
PageHelper.startPage<FumeMinuteValue>(1, 15)
|
fumeMinuteValueMapper.selectByExample(Example(FumeMinuteValue::class.java).apply {
|
createCriteria().andEqualTo("mvStatCode", mn)
|
orderBy("mvDataTime").desc()
|
}).map {
|
DataVo(DateUtil().DateToString(it.mvDataTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM), if (it.mvFumeConcentration<0) it.mvFumeConcentration2 else it.mvFumeConcentration)
|
}.run {
|
asReversed()
|
}.also {
|
result.addAll(it)
|
}
|
}
|
}
|
|
return result
|
}
|
|
override fun getLatestHourValue(userId: String): List<DataVo> {
|
val result = mutableListOf<DataVo>()
|
|
val baseInfo = baseInfoMapper.selectByPrimaryKey(userId)
|
if (baseInfo != null) {
|
val mn = baseInfo.biExtension2
|
if (!mn.isNullOrBlank()) {
|
val sceneType = deviceInfoMapper.selectByExample(Example(DeviceInfo::class.java).apply {
|
createCriteria().andEqualTo("diCode", mn)
|
})?.takeIf { it.isNotEmpty() }?.get(0)?.let {
|
when (it.diSceneTypeId) {
|
SceneType.Restaurant.value -> {
|
PageHelper.startPage<FumeMinuteValue>(1, 1)
|
val latest = fumeMinuteValueMapper.selectByExample(Example(FumeMinuteValue::class.java).apply {
|
createCriteria().andEqualTo("mvStatCode", mn)
|
orderBy("mvDataTime").desc()
|
}).takeIf { it.isNotEmpty() }?.get(0)?.let {
|
val now = Calendar.getInstance().apply { time = it.mvDataTime ?: Date() }
|
now.set(Calendar.MINUTE, 0)
|
now.set(Calendar.SECOND, 0)
|
now.set(Calendar.MILLISECOND, 0)
|
val endTime = now.time
|
now.add(Calendar.HOUR_OF_DAY, -1)
|
val startTime = now.time
|
|
val hourValue = fumeMinuteValueMapper.selectByExample(Example(FumeMinuteValue::class.java).apply {
|
createCriteria().andBetween("mvDataTime", startTime, endTime)
|
.andEqualTo("mvStatCode", mn)
|
}).map {
|
if (it.mvFumeConcentration < 0) it.mvFumeConcentration2 else it.mvFumeConcentration
|
}.average().let {a->
|
if (a.isNaN()) {
|
0.0
|
} else {
|
a
|
}
|
}
|
|
result.add(DataVo(
|
DateUtil().DateToString(startTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM),
|
DecimalFormat("#.00").format(hourValue).toDouble()
|
))
|
}
|
}
|
SceneType.VehicleRepair.value -> {
|
PageHelper.startPage<VOCHourValue>(1, 15)
|
vocHourValueMapper.selectByExample(Example(VOCHourValue::class.java).apply {
|
createCriteria().andEqualTo("vocStatCode", mn)
|
orderBy("vocDataTime").desc()
|
}).map {v ->
|
DataVo(
|
DateUtil().DateToString(v?.vocDataTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM),
|
DecimalFormat("#.00").format(v?.vocValue).toDouble()
|
)
|
}.run {
|
asReversed()
|
}.also { l ->
|
result.addAll(l)
|
}
|
}
|
else -> Unit
|
}
|
}
|
}
|
}
|
|
return result
|
}
|
|
override fun getMonAvgData(userId: String, timeList: List<DateVo>): List<MonAvgDataVo> {
|
val result = mutableListOf<MonAvgDataVo>()
|
val baseInfo = baseInfoMapper.selectByPrimaryKey(userId)
|
if (baseInfo != null) {
|
val mn = baseInfo.biExtension2
|
if (!mn.isNullOrBlank()) {
|
deviceInfoMapper.selectByExample(Example(DeviceInfo::class.java).apply {
|
createCriteria().andEqualTo("diCode", mn)
|
})?.takeIf { it.isNotEmpty() }?.get(0)?.let {
|
val cal = Calendar.getInstance(Locale.CHINA)
|
cal.set(Calendar.DAY_OF_MONTH, 1)
|
cal.set(Calendar.HOUR_OF_DAY, 0)
|
cal.set(Calendar.MINUTE, 0)
|
cal.set(Calendar.SECOND, 0)
|
cal.set(Calendar.MILLISECOND, 0)
|
|
when (it.diSceneTypeId) {
|
SceneType.Restaurant.value -> {
|
timeList.forEach {d ->
|
d.year?.let { year ->cal.set(Calendar.YEAR, year) }
|
d.month?.let { mon-> cal.set(Calendar.MONTH, mon-1) }
|
val startTime = cal.time
|
cal.add(Calendar.MONTH, 1)
|
val endTime = cal.time
|
|
var total = 0.0
|
val dataList = fumeMinuteValueMapper.selectByExample(Example(FumeMinuteValue::class.java).apply {
|
createCriteria().andEqualTo("mvStatCode", mn)
|
.andGreaterThanOrEqualTo("mvDataTime", startTime)
|
.andLessThan("mvDataTime", endTime)
|
}).also {l ->
|
l.forEach {v->
|
v?.let {
|
total += if (v.mvFumeConcentration < 0) v.mvFumeConcentration2 else v.mvFumeConcentration
|
}
|
}
|
}
|
val avg = if (dataList.isNotEmpty()) {
|
round(total / dataList.size * 100) / 100
|
} else {
|
0.0
|
}
|
|
// TODO: 2020/12/8 超标数和较区均值目前无获取途径,后续再添加
|
val exceedNum = 0
|
val compared = "/"
|
result.add(MonAvgDataVo(d.year, d.month, avg, exceedNum, compared))
|
}
|
}
|
SceneType.VehicleRepair.value -> {
|
timeList.forEach {d ->
|
d.year?.let { year ->cal.set(Calendar.YEAR, year) }
|
d.month?.let { mon-> cal.set(Calendar.MONTH, mon-1) }
|
val startTime = cal.time
|
cal.add(Calendar.MONTH, 1)
|
val endTime = cal.time
|
|
var total = 0.0
|
val dataList = vocHourValueMapper.selectByExample(Example(VOCHourValue::class.java).apply {
|
createCriteria().andEqualTo("vocStatCode", mn)
|
.andGreaterThanOrEqualTo("vocDataTime", startTime)
|
.andLessThan("vocDataTime", endTime)
|
}).also {l ->
|
l.forEach {v->
|
v?.let {
|
total += v.vocValue
|
}
|
}
|
}
|
val avg = if (dataList.isNotEmpty()) {
|
round(total / dataList.size * 100) / 100
|
} else {
|
0.0
|
}
|
|
// TODO: 2020/12/8 超标数和较区均值目前无获取途径,后续再添加
|
val exceedNum = 0
|
val compared = "/"
|
result.add(MonAvgDataVo(d.year, d.month, avg, exceedNum, compared))
|
}
|
}
|
else -> Unit
|
}
|
}
|
}
|
}
|
return result.asReversed()
|
}
|
|
override fun getHistoryValue(userId: String, startTime: String, endTime: String, period: Byte): List<DataVo> {
|
val result = mutableListOf<DataVo>()
|
val baseInfo = baseInfoMapper.selectByPrimaryKey(userId)
|
val sT = DateUtil().StringToDate(startTime)
|
val eT = DateUtil().StringToDate(endTime)
|
if (baseInfo != null) {
|
val mn = baseInfo.biExtension2
|
if (!mn.isNullOrBlank()) {
|
fumeMinuteValueMapper.selectByExample(Example(FumeMinuteValue::class.java).apply {
|
createCriteria().andEqualTo("mvStatCode", mn)
|
.andBetween("mvDataTime", sT, eT)
|
orderBy("mvDataTime")
|
}).map {
|
DataVo(DateUtil().DateToString(it.mvDataTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM), if (it.mvFumeConcentration < 0) it.mvFumeConcentration2 else it.mvFumeConcentration)
|
}.also {
|
result.addAll(it)
|
}
|
}
|
}
|
|
return result
|
}
|
|
override fun getRealTimeData(page: Int, perPage: Int): BaseResponse<List<DataVo>> {
|
val deviceList = mutableListOf<String>()
|
baseInfoMapper.selectByExample(Example(BaseInfo::class.java).apply {
|
createCriteria().andIsNotNull("biExtension2")
|
.andNotEqualTo("biExtension2", "")
|
orderBy("biExtension2")
|
}).forEach {
|
deviceList.add(it.biExtension2)
|
}
|
val resultList = mutableListOf<DataVo>()
|
val p = PageHelper.startPage<FumeMinuteValue>(page, perPage)
|
fumeMinuteValueMapper.getRealTimeData().forEach {
|
resultList.add(DataVo(
|
DateUtil().DateToString(it.mvDataTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM),
|
if (it.mvFumeConcentration < 0) it.mvFumeConcentration2 else it.mvFumeConcentration,
|
it.mvStatCode
|
))
|
}
|
|
return BaseResponse(true, head = DataHead(p.pageNum, p.pages), data = resultList)
|
}
|
}
|