Merge pull request '修改产品绑定逻辑' (#18) from zhangwei into dev

Reviewed-on: #18
This commit is contained in:
weiz 2023-11-27 13:36:41 +08:00
commit 85d7feffe4
1 changed files with 66 additions and 0 deletions

View File

@ -2,6 +2,7 @@
namespace app\api\controller; namespace app\api\controller;
use app\common\model\device\Device;
use app\common\model\geo\City; use app\common\model\geo\City;
use app\common\model\geo\County; use app\common\model\geo\County;
use app\common\model\geo\Group; use app\common\model\geo\Group;
@ -9,6 +10,10 @@
use app\common\model\geo\Town; use app\common\model\geo\Town;
use app\common\model\geo\Village; use app\common\model\geo\Village;
use app\common\model\land\Land; 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; use think\response\Json;
class LandController extends BaseApiController class LandController extends BaseApiController
@ -119,4 +124,65 @@
]); ]);
return $landRes->id ? $this->success('土地添加成功') : $this->fail('土地添加失败'); 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('绑定成功');
}
} }