Merge pull request 'update' (#7) from zhangwei into dev

Reviewed-on: #7
This commit is contained in:
weiz 2023-11-22 18:05:07 +08:00
commit ed00ed2786
5 changed files with 163 additions and 9 deletions

View File

@ -0,0 +1,61 @@
<?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());
}
}
}

View File

@ -13,20 +13,36 @@
class LandController extends BaseApiController
{
//添加土地
public function addLand(): Json
//土地列表
public function list(): Json
{
$fields = ['title','area','province_code','city_code','county_code','town_code','village_code','group_code','master_name','master_phone'];
$params = $this->request->post(['page_no','page_size']);
$pageNo = empty($params['page_no']) ? 1 : $params['page_no'];
$pageSize = empty($params['page_size']) ? 6 : $params['page_size'];
$data = Land::where('user_id',$this->userId)->page($pageNo,$pageSize)->order('create_time desc')->select()->each(function($item){
$item['pic'] = json_decode($item['pic']);
return $item;
})->toArray();
return $this->success('请求成功',$data);
}
//添加土地
public function add(): Json
{
//获取参数并验证
$fields = ['title','area','province_code','city_code','county_code','town_code','village_code','group_code','master_name','master_phone','pic'];
$params = $this->request->post($fields);
foreach($fields as $v){
if(!isset($params[$v]) || $params[$v] == ''){
return $this->fail('缺少必要参数');
}
}
//验证土地是否存在
$land = Land::where('user_id',$this->userId)->where('title',$params['title'])->findOrEmpty();
if(!$land->isEmpty()){
return $this->fail('土地名称已存在');
}
//获取省市县镇村组名称
$province = Province::field('province_name')->where('province_code',$params['province_code'])->findOrEmpty();
if($province->isEmpty()){
return $this->fail('省份编码错误');
@ -51,7 +67,13 @@
if($group->isEmpty()){
return $this->fail('小组编码错误');
}
$land = Land::create([
//验证土地图片字段是否是json数组
$pic = json_decode($params['pic'],true);
if(empty($pic)){
return $this->fail('图片数据格式错误');
}
//创建数据
$landRes = Land::create([
'user_id' => $this->userId,
'title' => $params['title'],
'total_area' => $params['area'],
@ -70,13 +92,10 @@
'group_name' => $group['group_name'],
'master_name' => $params['master_name'],
'master_phone' => $params['master_phone'],
'pic' => $params['pic'],
'create_time' => time(),
'update_time' => time(),
]);
if($land->id){
return $this->success('土地添加成功');
}else{
return $this->fail('土地添加失败');
}
return $landRes->id ? $this->success('土地添加成功') : $this->fail('土地添加失败');
}
}

View File

@ -0,0 +1,54 @@
<?php
namespace app\api\controller;
use app\common\model\land\Land;
use app\common\model\plant\Plant;
use think\facade\Db;
use think\response\Json;
class PlantController extends BaseApiController
{
//添加种植
public function add(): Json
{
//获取参数
$params = $this->request->post(['land_id','kind','breed','area','user','date','pic','remark']);
if(empty($params['land_id']) || empty($params['kind']) || empty($params['breed']) || empty($params['area']) || empty($params['user']) || empty($params['date']) || empty($params['pic'])){
return $this->fail('缺少必要参数');
}
if(!strtotime($params['date'])){
return $this->fail('日期格式错误');
}
$pics = json_decode($params['pic'],true);
if(empty($pics)){
return $this->fail('图片参数格式错误');
}
//获取土地信息
$land = Land::field('residual_area')->where('id',$params['land_id'])->findOrEmpty();
if($land->isEmpty()){
return $this->fail('土地信息错误');
}
if($params['area'] > $land['residual_area']){
return $this->fail('种植面积超过当前土地可种植面积');
}
//创建数据
Db::transaction(function()use($params,$land) {
Plant::create([
'land_id' => $params['land_id'],
'kind' => $params['kind'],
'breed' => $params['breed'],
'area' => $params['area'],
'user' => $params['user'],
'remark' => $params['remark'],
'pic' => $params['pic'],
'plant_date' => strtotime($params['date']),
'status' => 1,
'create_time' => time()
]);
$residual_area = $land['residual_area']-$params['area'];
Land::where('id',$params['land_id'])->update(['residual_area'=>$residual_area]);
});
return $this->success('种植添加成功');
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace app\common\model\action;
use app\common\model\BaseModel;
class Action extends BaseModel
{
protected $name = 'land_plant_action';
}

View File

@ -0,0 +1,10 @@
<?php
namespace app\common\model\plant;
use app\common\model\BaseModel;
class Plant extends BaseModel
{
protected $name = 'land_plant';
}