adminInfo)['user_id']; if (!empty($params['product_id'])) { $userId = Db::name('product')->where('id', $params['product_id'])->value('user_id'); } Db::startTrans(); try { $device = Device::create([ 'user_id' => $userId, 'code' => $params['code'], 'name' => $params['name'], 'type' => $params['type'], 'monitor_item' => $params['monitor_item'], 'video_url' => $params['video_url'], 'status' => $params['status'], 'is_online' => $params['is_online'] ]); if (!empty($params['product_id'])) { 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) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 编辑 * @param array $params * @return bool * @author likeadmin * @date 2023/11/24 15:30 */ public static function edit(array $params): bool { if (is_array($params['monitor_item'])) { $params['monitor_item'] = implode(',', $params['monitor_item']); } else { $params['monitor_item'] = trim($params['monitor_item']); } $userId = (request()->adminInfo)['user_id']; if (!empty($params['product_id'])) { $userId = Db::name('product')->where('id', $params['product_id'])->value('user_id'); } Db::startTrans(); try { $device = Device::where('id', $params['id'])->update([ 'user_id' => $userId, 'code' => $params['code'], 'name' => $params['name'], 'type' => $params['type'], 'monitor_item' => $params['monitor_item'], 'video_url' => $params['video_url'], 'status' => $params['status'], 'is_online' => $params['is_online'] ]); Db::name('product_device')->where('device_id', $params['id'])->delete(); if (!empty($params['product_id'])) { 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) { Db::rollback(); self::setError($e->getMessage()); return false; } } public function datas($params): array { $userWhere['d.user_id'] = -1; if (!empty($params['user_id'])) { $userWhere['d.user_id'] = $params['user_id']; } if (!request()->adminInfo['root'] && request()->adminInfo['user_id']) { $userWhere['d.user_id'] = request()->adminInfo['user_id']; } if (request()->adminInfo['root']) { unset($userWhere['d.user_id']); } $queryWhere = []; if (!empty($params['name'])) { $queryWhere[] = ['d.name', 'like', '%' . $params['name'] . '%']; } $deviceIdArray = Db::name('product_device')->column('device_id'); $lists = Db::name('device')->alias('d') ->where($userWhere)->where($queryWhere)->whereNotIn('d.id', $deviceIdArray) ->leftJoin('product_device pd','pd.device_id = d.id') ->leftJoin('product p','p.id = pd.product_id') ->leftJoin('user u','u.id = p.user_id') ->field('d.*') ->limit(0, 100) ->order(['d.id' => 'desc']) ->select()->toArray(); foreach ($lists as &$item) { $item['deviceinfo'] = 'ID:' . $item['id'] . ' / 名称:' . $item['name']; } return $lists; } /** * @notes 删除 * @param array $params * @return bool * @author likeadmin * @date 2023/11/24 15:30 */ public static function delete(array $params): bool { Db::name('product_device')->where('device_id', $params['id'])->delete(); Db::name('device_monitor_data')->where('device_id', $params['id'])->delete(); Db::name('monitor_alarm')->where('device_id', $params['id'])->delete(); return Device::destroy($params['id']); } /** * @notes 获取详情 * @param $params * @return array * @author likeadmin * @date 2023/11/24 15:30 */ public static function detail($params): array { $detail = Db::name('device')->alias('d') ->where('d.id', $params['id']) ->leftJoin('user u','u.id = d.user_id') ->leftJoin('product_device pd','pd.device_id = d.id') ->leftJoin('product p','p.id = pd.product_id') ->leftJoin('land_product lp','lp.product_id = pd.product_id') ->leftJoin('land l','l.id = lp.land_id') ->field('d.*, u.account, pd.product_id, p.name as product_name, lp.land_id, l.title as land_title') ->findOrEmpty(); $monitorItemArray = []; if (!empty($detail['monitor_item'])) { $monitorItemArray = explode(',', $detail['monitor_item']); } $dictTypeId = Db::name('dict_type')->where('type', 'monitor_item')->value('id'); $monitorItemArray = Db::name('dict_data')->where('type_id', $dictTypeId)->whereIn('value', $monitorItemArray)->field(['name', 'value'])->select()->toArray(); foreach($monitorItemArray as $k=>$v) { $monitorItemArray[$k]['device_id'] = $params['id']; $monitorItemArray[$k]['monitor_value'] = '-'; $monitorItemArray[$k]['last_time'] = '-'; $monitorData = Db::name('device_monitor_data')->where('device_id', $params['id'])->where('monitor_item', $v['value'])->order(['id' => 'desc'])->find(); if (!empty($monitorData)) { $monitorItemArray[$k]['monitor_value'] = $monitorData['monitor_value']; $monitorItemArray[$k]['last_time'] = date('Y-m-d H:i:s', $monitorData['create_time']); } } $detail['monitor_item'] = $monitorItemArray; return $detail; } public static function monitorData($params): array { $pageSizeMax = Config::get('project.lists.page_size_max'); $pageSize = Config::get('project.lists.page_size'); $pageNo = $page_no = request()->get('page_no', 1) ?: 1; $pageSize = $page_size = request()->get('page_size', $pageSize) ?: $pageSize; $limitOffset = ($pageNo - 1) * $pageSize; $limitLength = $pageSize; $where['device_id'] = $params['id']; $where['monitor_item'] = $params['monitor_type']; $lists = Db::name('device_monitor_data') ->where($where) ->limit($limitOffset, $limitLength) ->order(['id' => 'desc']) ->select() ->toArray(); $count = Db::name('device_monitor_data')->where($where)->count(); return compact('lists', 'count', 'page_no', 'page_size'); } }