<?php
	
	namespace app\api\controller;

	use app\common\model\action\Action;
	use app\common\model\land\Land;
	use app\common\model\plant\Plant;
	use dh2y\qrcode\QRcode;
	use think\facade\Db;
	use think\response\Json;
	
	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 => ['user','pic','remark'],//收获操作字段
		];
		protected array $actionMessage = [
			1 => '施肥操作',
			2 => '除草操作',
			3 => '灌溉操作',
			4 => '除虫操作',
			5 => '收获操作',
		];
		
		//添加操作
		public function add(): Json
		{
			$params = $this->request->post(['plant_id','type','detail']);
			if(empty($params['plant_id']) || empty($params['type']) || empty($params['detail'])){
				return $this->fail('缺少必要参数');
			}
			if(!in_array($params['type'],[1,2,3,4,5])){
				return $this->fail('操作类型错误');
			}
			$detail = json_decode($params['detail'],true);
			if(empty($detail)){
				return $this->fail('操作内容格式错误');
			}
			foreach($this->actionFields[$params['type']] as $v) {
				if($v == 'remark'){
					continue;
				}
				if(empty($detail[$v])){
					return $this->fail($this->actionMessage[$params['type']].'缺少必要参数');
				}
			}
			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([
					'user_id' => $plant['user_id'],
					'plant_id' => $params['plant_id'],
					'type' => $params['type'],
					'type_text' => $this->actionMessage[$params['type']],
					'detail' => $params['detail'],
					'create_time' => time(),
					'update_time' => time(),
				]);
				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,
						'harvest_date' => time(),
					]);
					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('缺少必要参数');
			}
			$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)");
			foreach($data as $k=>$v){
				$data[$k]['detail'] = json_decode($v['detail'],true);
				$data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
				$data[$k]['update_time'] = date('Y-m-d H:i:s',$v['update_time']);
			}
			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('缺少必要参数');
			}
			$data = Action::where('plant_id',$params['plant_id'])->where('type',$params['type'])->order('create_time desc')->select()->each(function($item){
				$item['detail'] = json_decode($item['detail'],true);
				return $item;
			})->toArray();
			return $this->success('请求成功',$data);
		}
	}