新增农户操作类型功能模块

This commit is contained in:
weiz 2023-10-21 10:45:43 +08:00
parent 581510f8ea
commit b7a4ae51e7
1 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,62 @@
<?php
namespace app\adminapi\controller\action;
use app\adminapi\controller\BaseAdminController;
use think\facade\Db;
use think\response\Json;
class ActionTypeController extends BaseAdminController
{
//添加操作类型
public function add(): Json
{
//获取参数
$params = $this->request->post(['name']);
//验证参数
if(empty($params['name'])){
return $this->fail('参数错误');
}
//判断操作类型是否存在
$hasRes = Db::name('farmer_action_type')->field('id')->where('name',$params['name'])->findOrEmpty();
if(!empty($hasRes)){
return $this->fail('数据已存在');
}
//添加数据
$addRes = Db::name('farmer_action_type')->insert(['name'=>$params['name']]);
//返回数据
return $addRes ? $this->success('添加成功') : $this->fail('添加失败');
}
//修改操作类型
public function edit(): Json
{
//获取参数
$params = $this->request->post(['id','name']);
//验证参数
if(empty($params['id']) || empty($params['name'])){
return $this->fail('参数错误');
}
//判断操作类型是否存在
$hasRes = Db::name('farmer_action_type')->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']]);
//返回数据
return $ediRes ? $this->success('修改成功') : $this->fail('修改失败');
}
//操作记录列表
public function list(): Json
{
//获取参数
$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();
//返回数据
return $this->success('请求成功',$data->toArray());
}
}