This commit is contained in:
weiz 2023-10-21 18:00:48 +08:00
parent b7a4ae51e7
commit 82ab411cef
2 changed files with 113 additions and 6 deletions

View File

@ -0,0 +1,107 @@
<?php
namespace app\adminapi\controller\action;
use app\adminapi\controller\BaseAdminController;
use think\facade\Db;
use think\response\Json;
class ActionController extends BaseAdminController
{
private string $actionTableName = 'farmer_action';
private string $actionDetailTableName = 'farmer_action_detail';
//添加用戶操作
public function add(): Json
{
//获取參數
$params = $this->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);
}
}

View File

@ -1,5 +1,4 @@
<?php <?php
namespace app\adminapi\controller\action; namespace app\adminapi\controller\action;
use app\adminapi\controller\BaseAdminController; use app\adminapi\controller\BaseAdminController;
@ -8,6 +7,7 @@
class ActionTypeController extends BaseAdminController class ActionTypeController extends BaseAdminController
{ {
private string $actionTypeTableName = 'farmer_action_type';
//添加操作类型 //添加操作类型
public function add(): Json public function add(): Json
{ {
@ -18,12 +18,12 @@
return $this->fail('参数错误'); return $this->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)){ if(!empty($hasRes)){
return $this->fail('数据已存在'); 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('添加失败'); return $addRes ? $this->success('添加成功') : $this->fail('添加失败');
} }
@ -38,12 +38,12 @@
return $this->fail('参数错误'); 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)){ if(!empty($hasRes)){
return $this->fail('数据已存在'); 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('修改失败'); return $ediRes ? $this->success('修改成功') : $this->fail('修改失败');
} }
@ -55,7 +55,7 @@
$params = $this->request->get(['name']); $params = $this->request->get(['name']);
$where = empty($params['name']) ? [] : [['name','like','%'.$params['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()); return $this->success('请求成功',$data->toArray());
} }