diff --git a/app/adminapi/controller/action/ActionController.php b/app/adminapi/controller/action/ActionController.php new file mode 100644 index 00000000..6f5db916 --- /dev/null +++ b/app/adminapi/controller/action/ActionController.php @@ -0,0 +1,107 @@ +request->post(['type_id','farmer_type','name','detail']); + //验证基本参数 + if(empty($params['type_id']) || empty($params['farmer_type']) || !in_array($params['farmer_type'],[1,2,3,4]) || empty($params['name'])){ + return $this->fail('参数错误'); + } + //验证json数据 + $detail = json_decode($params['detail'],true); + if(empty($detail)){ + return $this->fail('相关信息数据错误'); + } + //验证数据是否存在 + $hasRes = Db::name($this->actionTableName)->where('type_id',$params['type_id'])->where('farmer_type',$params['farmer_type'])->where('name',$params['name'])->findOrEmpty(); + if(!empty($hasRes)){ + return $this->fail('数据已存在'); + } + //写入数据 + Db::transaction(function () use($params,$detail) { + $actionId = Db::name($this->actionTableName)->insertGetId([ + 'type_id' => $params['type_id'], + 'farmer_type' => $params['farmer_type'], + 'name' => $params['name'] + ]); + foreach($detail as $k => $v){ + $detail[$k]['action_id'] = $actionId; + } + Db::name($this->actionDetailTableName)->insertAll($detail); + }); + return $this->success('添加成功'); + } + + //删除用户操作 + public function delete(): Json + { + //获取参数 + $params = $this->request->post(['action_id']); + if(empty($params['action_id'])){ + return $this->fail('参数错误'); + } + //删除数据 + Db::transaction(function () use($params) { + //先删除相关信息 + Db::name($this->actionDetailTableName)->where('action_id','in',$params['action_id'])->delete(); + //在删除操作信息 + Db::name($this->actionTableName)->where('id',$params['action_id'])->delete(); + }); + return $this->success('删除成功'); + } + + //删除相关信息 + public function deleteDetail(): Json + { + //获取参数 + $params = $this->request->post(['action_detail_id']); + if(empty($params['action_detail_id'])){ + return $this->fail('参数错误'); + } + //删除数据 + $delRes = Db::name($this->actionDetailTableName)->where('id',$params['action_detail_id'])->delete(); + //返回数据 + return $delRes ? $this->success('删除成功') : $this->fail('删除失败'); + } + + //操作列表 + public function list(): Json + { + //获取参数 + $params = $this->request->get(['type_id','farmer_type','name','page_no','page_size']); + //设置搜索条件 + $where = []; + if(!empty($params['type_id'])){ + $where[] = ['type_id','=',$params['type_id']]; + } + if(!empty($params['farmer_type'])){ + $where[] = ['farmer_type','=',$params['farmer_type']]; + } + if(!empty($params['name'])){ + $where[] = ['name','like','%'.$params['name'].'%']; + } + //设置分页属性 + $pageNo = empty($params['page_no']) || $params['page_no'] < 0 ? 1 : $params['page_no']; + $pageSize = empty($params['page_size']) || $params['page_size'] < 0 ? 15 : $params['page_size']; + //查询数据 + $data = Db::name($this->actionTableName)->where($where)->page($pageNo,$pageSize)->order('id desc')->select()->each(function($item){ + $item['type_name'] = Db::name('farmer_action_type')->where('id',$item['type_id'])->column('name')[0]; + $item['detail'] = Db::name($this->actionDetailTableName)->where('action_id','in',$item['id'])->select(); + return $item; + })->toArray(); + //返回数据 + return $this->success('请求成功',$data); + } + } \ No newline at end of file diff --git a/app/adminapi/controller/action/ActionTypeController.php b/app/adminapi/controller/action/ActionTypeController.php index 65bc2b7d..d9456c84 100644 --- a/app/adminapi/controller/action/ActionTypeController.php +++ b/app/adminapi/controller/action/ActionTypeController.php @@ -1,5 +1,4 @@ fail('参数错误'); } //判断操作类型是否存在 - $hasRes = Db::name('farmer_action_type')->field('id')->where('name',$params['name'])->findOrEmpty(); + $hasRes = Db::name($this->actionTypeTableName)->field('id')->where('name',$params['name'])->findOrEmpty(); if(!empty($hasRes)){ return $this->fail('数据已存在'); } //添加数据 - $addRes = Db::name('farmer_action_type')->insert(['name'=>$params['name']]); + $addRes = Db::name($this->actionTypeTableName)->insert(['name'=>$params['name']]); //返回数据 return $addRes ? $this->success('添加成功') : $this->fail('添加失败'); } @@ -38,12 +38,12 @@ return $this->fail('参数错误'); } //判断操作类型是否存在 - $hasRes = Db::name('farmer_action_type')->where('id','<>',$params['id'])->where('name',$params['name'])->field('id')->findOrEmpty(); + $hasRes = Db::name($this->actionTypeTableName)->where('id','<>',$params['id'])->where('name',$params['name'])->field('id')->findOrEmpty(); if(!empty($hasRes)){ return $this->fail('数据已存在'); } //更新数据 - $ediRes = Db::name('farmer_action_type')->where('id',$params['id'])->update(['name'=>$params['name']]); + $ediRes = Db::name($this->actionTypeTableName)->where('id',$params['id'])->update(['name'=>$params['name']]); //返回数据 return $ediRes ? $this->success('修改成功') : $this->fail('修改失败'); } @@ -55,7 +55,7 @@ $params = $this->request->get(['name']); $where = empty($params['name']) ? [] : [['name','like','%'.$params['name'].'%']]; //获取数据 - $data = Db::name('farmer_action_type')->where($where)->order('id desc')->select(); + $data = Db::name($this->actionTypeTableName)->where($where)->order('id desc')->select(); //返回数据 return $this->success('请求成功',$data->toArray()); }