2023-11-22 18:03:28 +08:00
< ? php
namespace app\api\controller ;
use app\common\model\action\Action ;
2023-11-23 11:57:48 +08:00
use app\common\model\land\Land ;
use app\common\model\plant\Plant ;
use dh2y\qrcode\QRcode ;
2023-11-22 18:03:28 +08:00
use think\facade\Db ;
2023-11-23 11:57:48 +08:00
use think\response\Json ;
2023-11-22 18:03:28 +08:00
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' ], //除虫操作字段
2023-11-23 11:57:48 +08:00
5 => [ 'user' , 'pic' , 'remark' ], //收获操作字段
2023-11-22 18:03:28 +08:00
];
protected array $actionMessage = [
1 => '施肥操作' ,
2 => '除草操作' ,
3 => '灌溉操作' ,
4 => '除虫操作' ,
5 => '收获操作' ,
];
//添加操作
2023-11-23 11:57:48 +08:00
public function add () : Json
{
$params = $this -> request -> post ([ 'plant_id' , 'type' , 'detail' ]);
if ( empty ( $params [ 'plant_id' ]) || empty ( $params [ 'type' ]) || empty ( $params [ 'detail' ])){
2023-11-22 18:03:28 +08:00
return $this -> fail ( '缺少必要参数' );
}
2023-11-23 11:57:48 +08:00
if ( ! in_array ( $params [ 'type' ],[ 1 , 2 , 3 , 4 , 5 ])){
2023-11-22 18:03:28 +08:00
return $this -> fail ( '操作类型错误' );
}
$detail = json_decode ( $params [ 'detail' ], true );
if ( empty ( $detail )){
2023-11-23 11:57:48 +08:00
return $this -> fail ( '操作内容格式错误' );
2023-11-22 18:03:28 +08:00
}
foreach ( $this -> actionFields [ $params [ 'type' ]] as $v ) {
if ( empty ( $detail [ $v ])){
return $this -> fail ( $this -> actionMessage [ $params [ 'type' ]] . '缺少必要参数' );
}
}
2023-11-23 11:57:48 +08:00
if ( ! is_array ( $detail [ 'pic' ])){
return $this -> fail ( $this -> actionMessage [ $params [ 'type' ]] . '图片字段格式错误' );
}
foreach ( $detail as $k => $v ){
if ( ! in_array ( $k , $this -> actionFields [ $params [ 'type' ]])){
unset ( $detail [ $k ]);
}
}
$params [ 'detail' ] = json_encode ( $detail );
$plant = Plant :: where ( 'id' , $params [ 'plant_id' ]) -> findOrEmpty ();
if ( $plant -> isEmpty ()){
return $this -> fail ( '土地种植信息错误' );
}
if ( $plant [ 'status' ] == 2 ){
return $this -> fail ( '当前种植信息为已收获状态,不能添加操作信息' );
}
$land = Land :: where ( 'id' , $plant [ 'land_id' ]) -> findOrEmpty ();
if ( $land -> isEmpty ()){
return $this -> fail ( '土地信息错误' );
}
Db :: transaction ( function () use ( $params , $plant , $land ){
Action :: create ([
2023-11-25 11:04:14 +08:00
'user_id' => $plant [ 'user_id' ],
2023-11-23 11:57:48 +08:00
'plant_id' => $params [ 'plant_id' ],
2023-11-22 18:03:28 +08:00
'type' => $params [ 'type' ],
2023-11-23 11:57:48 +08:00
'type_text' => $this -> actionMessage [ $params [ 'type' ]],
2023-11-22 18:03:28 +08:00
'detail' => $params [ 'detail' ],
2023-11-23 11:57:48 +08:00
'create_time' => time (),
'update_time' => time (),
2023-11-22 18:03:28 +08:00
]);
2023-11-23 11:57:48 +08:00
if ( $params [ 'type' ] == 5 ){
$qrCode = new QRcode ();
$qrCodeUrl = env ( 'project.project_url' ) . '/api/index/suYuan?id=' . $params [ 'plant_id' ];
$qrCodeFile = $qrCode -> png ( $qrCodeUrl , false , 6 ) -> entry ();
Plant :: where ( 'id' , $params [ 'plant_id' ]) -> update ([
'status' => 2 ,
'qr_code' => empty ( $qrCodeFile ) ? '' : $qrCodeFile
]);
Land :: where ( 'id' , $plant [ 'land_id' ]) -> update ([
'residual_area' => $land [ 'residual_area' ] + $plant [ 'area' ],
'update_time' => time ()
]);
}
});
return $this -> success ( '操作成功' );
}
//操作列表
public function list () : Json
{
$params = $this -> request -> get ([ 'plant_id' ]);
if ( empty ( $params [ 'plant_id' ])){
return $this -> fail ( '缺少必要参数' );
2023-11-22 18:03:28 +08:00
}
2023-11-29 17:15:47 +08:00
$data = Db :: query ( " SELECT * FROM zzsy_land_plant_action WHERE id IN (SELECT max( id ) FROM zzsy_land_plant_action WHERE plant_id = { $params [ 'plant_id' ] } GROUP BY type) " );
2023-11-29 17:27:35 +08:00
foreach ( $data as $k => $v ){
$data [ $k ][ 'detail' ] = json_decode ( $v [ 'detail' ], true );
}
2023-11-29 17:15:47 +08:00
return $this -> success ( '请求成功' , $data );
}
//单一操作列表
public function listForType () : Json
{
$params = $this -> request -> get ([ 'plant_id' , 'type' ]);
if ( empty ( $params [ 'plant_id' ]) || empty ( $params [ 'type' ])){
return $this -> fail ( '缺少必要参数' );
}
2023-11-29 17:59:24 +08:00
$data = Action :: where ( 'plant_id' , $params [ 'plant_id' ]) -> where ( 'type' , $params [ 'type' ]) -> order ( 'create_time desc' ) -> select () -> each ( function ( $item ){
2023-11-29 17:45:19 +08:00
$item [ 'detail' ] = json_decode ( $item [ 'detail' ], true );
return $item ;
}) -> toArray ();
2023-11-23 11:57:48 +08:00
return $this -> success ( '请求成功' , $data );
2023-11-22 18:03:28 +08:00
}
}