suyuan/app/api/controller/ActionController.php

61 lines
1.9 KiB
PHP

<?php
namespace app\api\controller;
use app\common\model\action\Action;
use app\common\model\action\ActionPic;
use think\facade\Db;
class ActionController extends BaseApiController
{
protected array $actionFields = [
1 => ['kind','breed','dosage','start_date','end_date','area','user','pic','remark'],//施肥操作字段
2 => ['kind','breed','dosage','start_date','end_date','area','user','pic','remark'],//除草操作字段
3 => ['type','start_date','end_date','area','user','pic','remark'],//灌溉操作字段
4 => ['kind','breed','dosage','start_date','end_date','area','user','pic','remark'],//除虫操作字段
5 => ['area','user','pic','remark'],//收获操作字段
];
protected array $actionMessage = [
1 => '施肥操作',
2 => '除草操作',
3 => '灌溉操作',
4 => '除虫操作',
5 => '收获操作',
];
//添加操作
public function add() {
$params = $this->request->post(['land_id','type','detail']);
if(empty($params['land_id']) || empty($params['type']) || empty($params['detail'])){
return $this->fail('缺少必要参数');
}
if(!in_array($params['type'],[1,2,3,4,5,6])){
return $this->fail('操作类型错误');
}
$detail = json_decode($params['detail'],true);
if(empty($detail)){
return $this->fail('请填写操作格式错误');
}
foreach($this->actionFields[$params['type']] as $v) {
if(empty($detail[$v])){
return $this->fail($this->actionMessage[$params['type']].'缺少必要参数');
}
}
Db::startTrans();
try {
$actionModel = new Action();
$actionPicModel = new ActionPic();
$actionModel->save([
'land_id' => $params['land_id'],
'type' => $params['type'],
'detail' => $params['detail'],
'create_time' => time()
]);
Db::commit();
return $this->success('操作添加成功');
} catch (\Exception $e) {
Db::rollback();
return $this->fail($e->getMessage());
}
}
}