更新设备管理

This commit is contained in:
yaooo 2023-11-28 14:50:04 +08:00
parent aa5958b3b8
commit a424d10d91
7 changed files with 198 additions and 35 deletions

View File

@ -1,5 +1,15 @@
import request from '@/utils/request'
// 用户列表
export function getUserList(params: any) {
return request.get({ url: '/user.user/lists', params }, { ignoreCancelToken: true })
}
// 产品列表列表
export function apiProductLists(params: any) {
return request.get({ url: '/land.product/lists', params })
}
// 监测设备列表
export function apiDeviceLists(params: any) {
return request.get({ url: '/device.device/lists', params })

View File

@ -9,6 +9,42 @@
@close="handleClose"
>
<el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
<el-form-item label="用户ID" prop="user_id">
<el-select
v-model="formData.user_id"
filterable
remote
reserve-keyword
placeholder="请输入用户信息"
:remote-method="queryUser"
:loading="loading"
>
<el-option
v-for="item in userOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
<el-form-item label="产品ID" prop="product_id">
<el-select
v-model="formData.product_id"
filterable
remote
reserve-keyword
placeholder="请输入产品信息"
:remote-method="queryProduct"
:loading="loading"
>
<el-option
v-for="item in productOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
<el-form-item label="设备编码" prop="code">
<el-input v-model="formData.code" clearable placeholder="请输入设备编码" />
</el-form-item>
@ -63,7 +99,7 @@
<script lang="ts" setup name="deviceEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiDeviceAdd, apiDeviceEdit, apiDeviceDetail } from '@/api/device'
import { apiDeviceAdd, apiDeviceEdit, apiDeviceDetail, getUserList, apiProductLists} from '@/api/device'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
@ -86,6 +122,8 @@ const popupTitle = computed(() => {
//
const formData = reactive({
id: '',
user_id: '',
product_id: '',
code: '',
name: '',
type: '',
@ -97,6 +135,16 @@ const formData = reactive({
//
const formRules = reactive<any>({
user_id: [{
required: true,
message: '请输入用户ID',
trigger: ['blur']
}],
product_id: [{
required: true,
message: '请输入产品id',
trigger: ['blur']
}],
code: [{
required: true,
message: '请输入设备编码',
@ -138,10 +186,57 @@ const setFormData = async (data: Record<any, any>) => {
formData[key] = data[key]
}
}
}
interface ListItem {
value: string
label: string
}
const productOptions = ref<ListItem[]>([])
const userOptions = ref<ListItem[]>([])
const loading = ref(false)
const queryUser = async (query: string) => {
if (query) {
loading.value = true
const userList = await getUserList({
keyword: query
})
loading.value = false
if (userList.count > 0) {
userOptions.value = userList.lists.map((user: any) => {
return { value: `${user.id}`, label: `ID: ${user.id} / 账户: ${user.account}` }
})
} else {
userOptions.value = []
}
loading.value = false
} else {
userOptions.value = []
}
}
const queryProduct = async (query: string) => {
if (query) {
loading.value = true
const productList = await apiProductLists({
name: query
})
loading.value = false
if (productList.count > 0) {
productOptions.value = productList.lists.map((product: any) => {
return { value: `${product.id}`, label: `ID: ${product.id} / 名称: ${product.name}` }
})
} else {
productOptions.value = []
}
loading.value = false
} else {
productOptions.value = []
}
}
const getDetail = async (row: Record<string, any>) => {
const data = await apiDeviceDetail({
id: row.id

View File

@ -6,6 +6,12 @@
:model="queryParams"
inline
>
<el-form-item label="用户ID" prop="user_id">
<el-input class="w-[280px]" v-model="queryParams.user_id" clearable placeholder="请输入用户ID" />
</el-form-item>
<el-form-item label="产品ID" prop="product_id">
<el-input class="w-[280px]" v-model="queryParams.product_id" clearable placeholder="请输入产品ID" />
</el-form-item>
<el-form-item label="设备编码" prop="code">
<el-input class="w-[280px]" v-model="queryParams.code" clearable placeholder="请输入设备编码" />
</el-form-item>
@ -63,14 +69,36 @@
</el-form>
</el-card>
<el-card class="!border-none" v-loading="pager.loading" shadow="never">
<el-button v-perms="['device.device/add']" type="primary" @click="handleAdd">
<template #icon>
<icon name="el-icon-Plus" />
</template>
新增
</el-button>
<el-button
v-perms="['device.device/delete']"
:disabled="!selectData.length"
@click="handleDelete(selectData)"
>
删除
</el-button>
<div class="mt-4">
<el-table :data="pager.lists" @selection-change="handleSelectionChange">
<el-table :data="pager.lists" @selection-change="handleSelectionChange" style="width: 100%">
<el-table-column type="selection" width="55" />
<el-table-column label="ID" prop="id" show-overflow-tooltip />
<el-table-column label="用户信息" width="200">
<template #default="{ row }">
<el-tag class="mr-2" type="success">ID: {{ row.user_id }}</el-tag>
<el-tag class="mr-2" type="success">账户: {{ row.user_id }}</el-tag>
</template>
</el-table-column>
<el-table-column label="产品信息">
<template #default="{ row }">
<el-tag class="mr-2" type="success">{{ row.user_id }}</el-tag>
</template>
</el-table-column>
<el-table-column label="设备编码" prop="code" show-overflow-tooltip />
<el-table-column label="设备名称" prop="name" show-overflow-tooltip />
<el-table-column label="土地ID" prop="land_id" show-overflow-tooltip />
<el-table-column label="土地名称" prop="title" show-overflow-tooltip />
<el-table-column label="设备类型" prop="type">
<template #default="{ row }">
<dict-value :options="dictData.device_type" :value="row.type" />
@ -191,19 +219,26 @@ import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const { query } = useRoute()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
//
const showEdit = ref(false)
let product_id = query.product_id
if (typeof product_id == 'undefined') {
product_id = ''
}
//
const queryParams = reactive({
user_id: '',
product_id: product_id,
code: '',
name: '',
type: '',
status: '',
is_online: '',
is_bind: '',
is_bind: ''
})
//

View File

@ -40,7 +40,7 @@
删除
</el-button>
<div class="mt-4">
<el-table :data="pager.lists" @selection-change="handleSelectionChange">
<el-table :data="pager.lists" @selection-change="handleSelectionChange" style="width: 100%">
<el-table-column type="selection" width="55" />
<el-table-column label="ID" prop="id" width="80" show-overflow-tooltip />
<el-table-column label="用户信息" width="200">

View File

@ -38,7 +38,8 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
public function setSearch(): array
{
return [
'=' => ['p.user_id', 'p.code', 'p.name', 'p.status'],
'%like%' => ['p.name'],
'=' => ['p.user_id', 'p.code', 'p.status'],
];
}

View File

@ -40,15 +40,21 @@ class DeviceLogic extends BaseLogic
{
Db::startTrans();
try {
Device::create([
'code' => $params['code'],
'name' => $params['name'],
'type' => $params['type'],
'status' => $params['status'],
'is_online' => $params['is_online'],
$device = Device::create([
'code' => $params['code'],
'name' => $params['name'],
'type' => $params['type'],
'status' => $params['status'],
'is_online' => $params['is_online'],
'is_bind' => $params['is_bind'],
]);
Db::name('product_device')->insert([
'product_id' => $params['product_id'],
'device_id' => $device['id'],
'device_type' => $params['type'],
'create_time' => time(),
'update_time' => time()
]);
Db::commit();
return true;
} catch (\Exception $e) {
@ -70,15 +76,22 @@ class DeviceLogic extends BaseLogic
{
Db::startTrans();
try {
Device::where('id', $params['id'])->update([
'code' => $params['code'],
'name' => $params['name'],
'type' => $params['type'],
'status' => $params['status'],
'is_online' => $params['is_online'],
$device = Device::where('id', $params['id'])->update([
'code' => $params['code'],
'name' => $params['name'],
'type' => $params['type'],
'status' => $params['status'],
'is_online' => $params['is_online'],
'is_bind' => $params['is_bind'],
]);
Db::name('product_device')->where('device_id', $params['id'])->delete();
Db::name('product_device')->insert([
'product_id' => $params['product_id'],
'device_id' => $params['id'],
'device_type' => $params['type'],
'create_time' => time(),
'update_time' => time()
]);
Db::commit();
return true;
} catch (\Exception $e) {

View File

@ -16,7 +16,7 @@ namespace app\adminapi\validate\land;
use app\common\validate\BaseValidate;
use think\facade\Db;
/**
* Product验证器
@ -31,11 +31,11 @@ class ProductValidate extends BaseValidate
* @var string[]
*/
protected $rule = [
'id' => 'require',
'user_id' => 'require',
'code' => 'require',
'name' => 'require',
'status' => 'require',
'id' => 'require',
'user_id' => 'require',
'code' => 'require',
'name' => 'require',
'status' => 'require',
];
@ -44,11 +44,11 @@ class ProductValidate extends BaseValidate
* @var string[]
*/
protected $field = [
'id' => 'id',
'user_id' => '用户id',
'code' => '产品编号',
'name' => '产品名称',
'status' => '产品状态',
'id' => 'id',
'user_id' => '用户id',
'code' => '产品编号',
'name' => '产品名称',
'status' => '产品状态',
];
@ -84,7 +84,7 @@ class ProductValidate extends BaseValidate
*/
public function sceneDelete()
{
return $this->only(['id']);
return $this->only(['id'])->append('id', 'require|checkProduct');
}
@ -99,4 +99,13 @@ class ProductValidate extends BaseValidate
return $this->only(['id']);
}
public function checkProduct($value, $rule, $data)
{
$productDevice = Db::name('product_device')->where('product_id', $value)->findOrEmpty();
if (!empty($productDevice)) {
return '存在关联设备,无法删除';
}
return true;
}
}