adminInfo)['root']; $userId = (request()->adminInfo)['user_id']; if ($root && !empty($params['user_id'])) { $userId = $params['user_id']; } try { Product::create([ 'user_id' => $userId, 'fence_house_id' => $params['fence_house_id'], 'code' => $params['code'], 'name' => $params['name'], 'desc' => $params['desc'], 'image' => $params['image'], 'status' => 2, ]); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } /** * @notes 编辑 * @param array $params * @return bool * @author likeadmin * @date 2023/11/25 16:16 */ public static function edit(array $params): bool { $root = (request()->adminInfo)['root']; $userId = (request()->adminInfo)['user_id']; if ($root && !empty($params['user_id'])) { $userId = $params['user_id']; } try { Product::where('id', $params['id'])->update([ 'user_id' => $userId, 'fence_house_id' => $params['fence_house_id'], 'code' => $params['code'], 'name' => $params['name'], 'desc' => $params['desc'], 'image' => $params['image'], 'status' => 2, ]); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } public function datas($params): array { $userWhere['p.user_id'] = -1; if (!empty($params['user_id'])) { $userWhere['p.user_id'] = $params['user_id']; } if (!request()->adminInfo['root'] && request()->adminInfo['user_id']) { $userWhere['p.user_id'] = request()->adminInfo['user_id']; } if (request()->adminInfo['root']) { unset($userWhere['p.user_id']); } $queryWhere = []; if (!empty($params['name'])) { $queryWhere[] = ['p.name', 'like', '%' . $params['name'] . '%']; } $productIdArray = Db::name('land_product')->column('product_id'); if (!empty($params['all'])) { $productIdArray= []; } $lists = Db::name('product')->alias('p') ->where($userWhere)->where($queryWhere)->whereNotIn('p.id', $productIdArray) ->leftJoin('user u','u.id = p.user_id') ->field('p.*') ->limit(0, 100) ->order(['p.id' => 'desc']) ->select()->toArray(); foreach ($lists as &$item) { $item['productinfo'] = 'ID:' . $item['id'] . ' / 名称:' . $item['name']; } return $lists; } /** * @notes 删除 * @param array $params * @return bool * @author likeadmin * @date 2023/11/25 16:16 */ public static function delete(array $params): bool { Db::name('land_product')->where('product_id', $params['id'])->delete(); Db::name('monitor_data')->where('product_id', $params['id'])->delete(); return Product::destroy($params['id']); } /** * @notes 获取详情 * @param $params * @return array * @author likeadmin * @date 2023/11/25 16:16 */ public static function detail($params): array { return Product::findOrEmpty($params['id'])->toArray(); } public static function bind($params): bool { $root = (request()->adminInfo)['root']; $userId = (request()->adminInfo)['user_id']; if ($root && !empty($params['user_id'])) { $userId = $params['user_id']; } if (!empty($params['id'])) { $product = Product::findOrEmpty($params['id'])->toArray(); $userId = $product['user_id'] ?? 0; } Db::startTrans(); try { Db::name('product_device')->whereIn('device_id', $params['device_id'])->delete(); Db::name('product_device')->where('product_id', $params['id'])->delete(); Db::name('device')->whereIn('id', $params['device_id'])->update([ 'user_id' => $userId ]); $deviceList = Db::name('device')->whereIn('id', $params['device_id'])->select()->toArray(); $insertData = []; foreach($deviceList as $d) { $insertData[] = [ 'product_id' => $params['id'], 'device_id' => $d['id'], 'device_type' => $d['type'], 'create_time' => time(), 'update_time' => time() ]; } Db::name('product_device')->insertAll($insertData); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } }