feiyu02
2024-12-16 b3e273990fda27b68dfcf2c7f9d78311a5279885
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
package com.flightfeather.uav.lightshare.service.impl
 
import com.flightfeather.uav.common.exception.BizException
import com.flightfeather.uav.domain.entity.DeviceInfo
import com.flightfeather.uav.domain.repository.DeviceRep
import com.flightfeather.uav.lightshare.service.DeviceService
import com.flightfeather.uav.socket.eunm.UWDeviceType
import org.springframework.stereotype.Service
import java.util.*
 
@Service
class DeviceServiceImpl(private val deviceRep: DeviceRep) : DeviceService {
 
    override fun getDeviceInfo(type: String?): List<DeviceInfo> {
        return deviceRep.find(UWDeviceType.fromValue(type))
    }
 
    override fun createDevice(deviceInfo: DeviceInfo): Int {
        if (deviceRep.find(DeviceInfo().apply { deviceCode = deviceInfo.deviceCode }).isNotEmpty()) {
            throw BizException("设备编号已存在")
        }
        val deviceType = UWDeviceType.fromValue(deviceInfo.deviceType) ?: throw BizException("设备类型不存在")
        val dList = deviceRep.find(deviceType, false)
        val index = if (dList.isEmpty()) 1 else ++dList[0].displayIndex
        deviceInfo.apply {
            deviceName = "${deviceType.des}${index}号"
            displayIndex = index
            createTime = Date()
        }
        return deviceRep.insert(deviceInfo)
    }
 
    override fun deleteDevice(deviceCode: String): Int {
        return deviceRep.delete(deviceCode)
    }
}