From e746730d841616c8dcdac94761c62ef2b4549de5 Mon Sep 17 00:00:00 2001 From: weiz Date: Mon, 27 Nov 2023 13:35:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=A7=E5=93=81=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/LandController.php | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/app/api/controller/LandController.php b/app/api/controller/LandController.php index 53a10f2c..61854a62 100644 --- a/app/api/controller/LandController.php +++ b/app/api/controller/LandController.php @@ -2,6 +2,7 @@ namespace app\api\controller; + use app\common\model\device\Device; use app\common\model\geo\City; use app\common\model\geo\County; use app\common\model\geo\Group; @@ -9,6 +10,10 @@ use app\common\model\geo\Town; use app\common\model\geo\Village; use app\common\model\land\Land; + use app\common\model\land\LandProduct; + use app\common\model\land\Product; + use app\common\model\product\ProductDevice; + use think\facade\Db; use think\response\Json; class LandController extends BaseApiController @@ -119,4 +124,65 @@ ]); return $landRes->id ? $this->success('土地添加成功') : $this->fail('土地添加失败'); } + + //产品列表 + public function product(): Json + { + $data = Product::field('id as product_id,code,name')->where('status',1)->select()->toArray(); + foreach ($data as $k=>$v) { + $productDevice = ProductDevice::where('product_id',$v['product_id'])->select()->toArray(); + if(empty($productDevice)){ + unset($data[$k]); + } + } + return $this->success('请求成功',$data); + } + + //绑定产品 + public function bind(): Json + { + $params = $this->request->post(['land_id','product_id']); + if(empty($params['land_id']) || empty($params['product_id'])){ + return $this->fail('缺少必要参数'); + } + $land = Land::where('id',$params['land_id'])->findOrEmpty(); + if($land->isEmpty()){ + return $this->fail('土地信息错误'); + } + $landProduct = LandProduct::where('land_id',$params['land_id'])->findOrEmpty(); + if(!$landProduct->isEmpty()){ + return $this->fail('当前土地已绑定其他产品'); + } + $product = Product::where('id',$params['product_id'])->findOrEmpty(); + if($product->isEmpty()){ + return $this->fail('产品信息错误'); + } + $productDevice = ProductDevice::where('product_id',$params['product_id'])->column('device_id'); + if(empty($productDevice)){ + return $this->fail('该产品没有绑定设备,请先给该产品绑定设备'); + } + $productLand = LandProduct::where('product_id',$params['product_id'])->findOrEmpty(); + if(!$productLand->isEmpty()){ + return $this->fail('该产品已被其他土地绑定'); + } + //创建数据 + Db::transaction(function()use($land,$product,$productDevice) { + LandProduct::create([ + 'land_id' => $land['id'], + 'product_id' => $product['id'], + 'create_time' => time(), + 'update_time' => time(), + ]); + Product::where('id',$product['id'])->update([ + 'user_id' => $land['user_id'], + 'status' => 2, + 'update_time' => time(), + ]); + Device::where('id','in',$productDevice)->update([ + 'user_id' => $land['user_id'], + 'update_time' => time() + ]); + }); + return $this->success('绑定成功'); + } } \ No newline at end of file