139 lines
2.4 KiB
JavaScript
139 lines
2.4 KiB
JavaScript
const db = require('../../db/database')
|
|
|
|
// const Op = db.Sequelize.Op
|
|
|
|
class DeviceModel {
|
|
async create (req) {
|
|
return db.devices.create({
|
|
name: req.name,
|
|
serial_number: req.serial_number,
|
|
mac_address: req.mac_address,
|
|
connect_priority: await db.devices.count()
|
|
})
|
|
}
|
|
|
|
async update (req, id) {
|
|
req.updated_at = db.Sequelize.literal('CURRENT_TIMESTAMP')
|
|
if (id != null) {
|
|
const ret = await db.devices.update(
|
|
req,
|
|
{
|
|
where: {
|
|
id: id
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
} else {
|
|
const ret = await db.devices.update(
|
|
req,
|
|
{
|
|
where: {}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
}
|
|
|
|
async updateByMac (req, mac) {
|
|
req.updated_at = db.Sequelize.literal('CURRENT_TIMESTAMP')
|
|
const ret = await db.devices.update(
|
|
req,
|
|
{
|
|
where: {
|
|
mac_address: mac
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async updateByAttr (req, attr, value) {
|
|
const whereObj = {}
|
|
const attrList = attr.split('-')
|
|
const valueList = value.split('-')
|
|
attrList.forEach((key, idx) => {
|
|
whereObj[key] = valueList[idx]
|
|
})
|
|
|
|
const ret = await db.devices.update(
|
|
req,
|
|
{
|
|
where: whereObj
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async clearDeleted () {
|
|
const ret = await db.devices.destroy(
|
|
{
|
|
where: {
|
|
deleted: true
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getByID (id) {
|
|
const ret = await db.devices.findAll(
|
|
{
|
|
where: {
|
|
id: id
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getByName (name) {
|
|
const ret = await db.devices.findAll(
|
|
{
|
|
where: {
|
|
name: name
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getByMac (mac) {
|
|
const ret = await db.devices.findAll(
|
|
{
|
|
where: {
|
|
mac_address: mac
|
|
}
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getByAttr (attr, value) {
|
|
const attrList = attr.split('-')
|
|
const valueList = value.split('-')
|
|
const whereObj = {}
|
|
attrList.forEach((key, idx) => {
|
|
whereObj[key] = valueList[idx]
|
|
})
|
|
|
|
const ret = await db.devices.findAll(
|
|
{
|
|
where: whereObj
|
|
}
|
|
)
|
|
return ret
|
|
}
|
|
|
|
async getAll () {
|
|
const ret = await db.devices.findAll({
|
|
order: [
|
|
['connect_priority', 'ASC']
|
|
]
|
|
})
|
|
return ret
|
|
}
|
|
}
|
|
|
|
export default DeviceModel
|