Browse Source

'风场管理功能'

weich 2 years ago
parent
commit
68e342615c

+ 47 - 3
catch/wind/controller/Wind.php

@@ -33,7 +33,28 @@ class Wind extends CatchController
      */
     public function save(Request $request) : \think\Response
     {
-        return CatchResponse::success($this->windModel->storeBy($request->post()));
+        $post = $request->post();
+        
+        if (!isset($post['name']) || !$post['name']) {
+            return CatchResponse::fail('风场名称不能为空');
+        }
+        if (!isset($post['wind_shape']) || !$post['wind_shape']) {
+            return CatchResponse::fail('获取风场类型失败');
+        }
+        if (!isset($post['wind_info']) || !is_array($post['wind_info'])) {
+            return CatchResponse::fail('获取风场坐标数据失败');
+        }
+        if (!isset($post['department_id']) || !is_array($post['department_id'])) {
+            return CatchResponse::fail('获取风场所属部门失败');
+        }
+        // 检测名称重复
+        if($this->windModel->where('name',$post['name'])->count()){
+            return CatchResponse::fail('风场名称已存在');
+        }
+        $post['wind_info'] = json_encode($post['wind_info']);
+        $post['department_id'] = array_pop($post['department_id']);
+
+        return CatchResponse::success($this->windModel->storeBy($post));
     }
     
     /**
@@ -54,7 +75,30 @@ class Wind extends CatchController
      */
     public function update(Request $request, $id) : \think\Response
     {
-        return CatchResponse::success($this->windModel->updateBy($id, $request->post()));
+        $post = $request->post();
+        
+        if (!isset($post['name']) || !$post['name']) {
+            return CatchResponse::fail('风场名称不能为空');
+        }
+        if (!isset($post['wind_shape']) || !$post['wind_shape']) {
+            return CatchResponse::fail('获取风场类型失败');
+        }
+        if (!isset($post['wind_info']) || !is_array($post['wind_info'])) {
+            return CatchResponse::fail('获取风场坐标数据失败');
+        }
+        if (!isset($post['department_id']) || !$post['department_id']) {
+            return CatchResponse::fail('获取风场所属部门失败');
+        }
+        // 检测名称重复
+        $n_id = $this->windModel->where('name',$post['name'])->value('id');
+        if($n_id && $n_id != $id){
+            return CatchResponse::fail('风场名称已存在');
+        }
+        $post['wind_info'] = json_encode($post['wind_info']);
+        if(isset($post['department_id']) && is_array($post['department_id'])){
+            $post['department_id'] = array_pop($post['department_id']);
+        }
+        return CatchResponse::success($this->windModel->updateBy($id, $post));
     }
     
     /**
@@ -64,6 +108,6 @@ class Wind extends CatchController
      */
     public function delete($id) : \think\Response
     {
-        return CatchResponse::success($this->windModel->deleteBy($id));
+        return CatchResponse::success($this->windModel->deleteBy($id,true));
     }
 }

+ 4 - 0
catch/wind/database/migrations/20220427104913_wind.php

@@ -37,6 +37,10 @@ class Wind extends Migrator
 			->addColumn('maintenance_count', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '维保设备数量',])
 			->addColumn('pump_count', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '泵设备数量',])
 			->addColumn('remark', 'string', ['limit' => 100,'null' => true,'signed' => true,'comment' => '备注',])
+            ->addColumn('wind_shape', 'string', ['limit' => 20,'null' => false,'default' => '','signed' => true,'comment' => '风场形状',])
+            ->addColumn('side_color', 'string', ['limit' => 20,'null' => false,'default' => '#03D0F9','signed' => true,'comment' => '风场边颜色',])
+            ->addColumn('inside_color', 'string', ['limit' => 20,'null' => false,'default' => '#03D0F9','signed' => true,'comment' => '风场内颜色',])
+			->addColumn('wind_info', 'text', ['limit' => MysqlAdapter::TEXT_REGULAR,'null' => true,'signed' => true,'comment' => '风场区域信息',])
 			->addColumn('creator_id', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => false,'comment' => '创建人ID',])
 			->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => false,'comment' => '创建时间',])
 			->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => false,'comment' => '更新时间',])

+ 57 - 1
catch/wind/model/Wind.php

@@ -3,9 +3,11 @@
 namespace catchAdmin\wind\model;
 
 use catcher\base\CatchModel as Model;
-
+use catchAdmin\permissions\model\DataRangScopeTrait;
+use \think\facade\Db;
 class Wind extends Model
 {
+    use DataRangScopeTrait;
     // 表名
     public $name = 'wind';
     // 数据库字段映射
@@ -23,6 +25,14 @@ class Wind extends Model
         'maintenance_count',
         // 泵设备数量
         'pump_count',
+        //风场形状
+        'wind_shape',
+        //风场区域经纬度信息
+        'wind_info',
+        //风场边缘颜色
+        'side_color',
+        //风场内部颜色
+        'inside_color',
         // 备注
         'remark',
         // 创建人ID
@@ -34,4 +44,50 @@ class Wind extends Model
         // 软删除
         'deleted_at',
     );
+    public function getList()
+    {
+        // 不分页
+        if (property_exists($this, 'paginate') && $this->paginate === false) {
+            return $this->dataRange()
+                ->catchSearch()
+                ->field('*')
+                ->catchOrder()
+                ->append(['type_name','depart_name'])
+                ->creator()
+                ->select();
+        }
+
+        // 分页列表
+        return $this->dataRange()
+            ->catchSearch()
+            ->field('*')
+            ->catchOrder()
+            ->append(['type_name','depart_name'])
+            ->creator()
+            ->paginate();
+    }
+       /**
+     * 获取风场类型(文本)
+     */
+    public function getTypeNameAttr($value)
+    {
+        $type_arr = [
+            'circle' => '圆形围栏',
+            'polygon' => '多边形围栏',
+        ];
+        return $type_arr[$this->getData('wind_shape')] ? : '未知';     
+    }
+    public function getDepartmentIdAttr($value)
+    {
+        return json_decode($value,true);
+    }
+    public function getWindInfoAttr($value)
+    {
+        return json_decode($value, true);
+    }
+    public function getDepartNameAttr($value)
+    {
+        $id=$this->department_id;
+        return Db::table('departments')->where('id', $id)->value('department_name');
+    }
 }