107 lines
3.7 KiB
PHP
107 lines
3.7 KiB
PHP
|
<?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);
|
||
|
}
|
||
|
}
|