$params['user_id'], 'code' => $params['code'], 'name' => $params['name'], 'type' => $params['type'], 'monitor_item' => $params['monitor_item'], '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) { 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']); } Db::startTrans(); try { $device = Device::where('id', $params['id'])->update([ 'user_id' => $params['user_id'], 'code' => $params['code'], 'name' => $params['name'], 'type' => $params['type'], 'monitor_item' => $params['monitor_item'], '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) { 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 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'); } }