diff --git a/app/adminapi/controller/device/DeviceController.php b/app/adminapi/controller/device/DeviceController.php index 58b45c59..0732988e 100644 --- a/app/adminapi/controller/device/DeviceController.php +++ b/app/adminapi/controller/device/DeviceController.php @@ -1,10 +1,148 @@ request->post(['device_no','device_type','farmer_phone']); + if(empty($params['device_no']) || empty($params['device_type']) || !in_array($params['device_type'],[1,2,3]) || empty($params['farmer_phone'])){ + return $this->fail('参数错误'); + } + //获取农户信息 + $url = env('project.worker_domain').'/api/information/farmerInfoByPhone'; + $farmer = curl_post($url,[],['phone'=>$params['farmer_phone']]); + if(empty($farmer) || $farmer['code'] == 0){ + return $this->fail('未找到农户'); + } + $result = Db::name('farmer_device')->insert([ + 'user_id' => $farmer['data']['id'], + 'user_name' => $farmer['data']['name'], + 'user_phone' => $farmer['data']['phone'], + 'user_address' => $farmer['data']['area_name'].$farmer['data']['street_name'].$farmer['data']['village_name'].$farmer['data']['brigade_name'].$farmer['data']['address'], + 'code' => $params['device_no'], + 'type' => $params['device_type'], + 'status' => 1, + 'create_time' => time() + ]); + return $result ? $this->success('添加成功',[],1,1) : $this->fail('添加失败',[],1,1); + } + public function lists(): Json + { + $params = $this->request->get(['device_no','farmer_name','farmer_phone','page_no','page_size']); + //设置分页条件 + $pageNo = empty($params['page_no']) || $params['page_no'] < 0 ? 1 : $params['page_no']; + $pageSize = empty($params['page_size']) || $params['page_size'] < 0 ? 10 : $params['page_size']; + //设置搜索条件 + $search = []; + if(!empty($params['device_no'])){ + $search[] = ['code','like','%'.$params['device_no'].'%']; + } + if(!empty($params['farmer_name'])){ + $search[] = ['user_name','like','%'.$params['farmer_name'].'%']; + } + if(!empty($params['farmer_phone'])){ + $search[] = ['user_phone','like','%'.$params['farmer_phone'].'%']; + } + $data = Db::name('farmer_device')->where($search)->page($pageNo,$pageSize)->order('create_time desc')->select()->each(function($item){ + $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']); + return $item; + })->toArray(); + $count = Db::name('farmer_device')->where($search)->count(); + $result = [ + 'count' => $count, + 'page_no' => $pageNo, + 'page_size' => $pageSize, + 'lists' => $data + ]; + return $this->success('请求成功',$result); + } + + public function detail(): Json + { + $params = $this->request->get(['id']); + if(empty($params['id'])){ + return $this->fail('参数错误'); + } + $data = Db::name('farmer_device')->where('id',$params['id'])->findOrEmpty(); + if(!empty($data)){ + $data['create_time'] = date('Y-m-d H:i:s',$data['create_time']); + } + return $this->success('请求成功',$data); + } + + public function deviceEnvData(): Json + { + $params = $this->request->get(['id','start_time','end_time']); + if(empty($params['id']) || empty($params['start_time']) || empty($params['end_time'])){ + return $this->fail('参数错误'); + } + $device = $data = Db::name('farmer_device')->where('id',$params['id'])->findOrEmpty(); + if(empty($device)){ + return $this->fail('未找到相关设备'); + } + switch ($device['type']) { + case 1: + //获取数据 + $data = Db::name('farmer_land_env_data')->where('user_id',$device['user_id'])->whereBetweenTime('create_time', $params['start_time'], $params['end_time'])->order('id asc')->select()->each(function($item){ + $item['create_time'] = date('m-d',$item['create_time']); + return $item; + })->toArray(); + if(empty($data)){ + return $this->fail('暂无数据'); + } + $result = [ + ['soil_temp' => '土地温度','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'soil_temp')],'max'=>max(array_column($data,'soil_temp'))], + ['soil_mois' => '土地湿度','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'soil_mois')],'max'=>max(array_column($data,'soil_mois'))], + ['p_content' => '土壤磷含量','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'p_content')],'max'=>max(array_column($data,'p_content'))], + ['n_content' => '土壤氮含量','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'n_content')],'max'=>max(array_column($data,'n_content'))], + ['k_content' => '土壤钾含量','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'k_content')],'max'=>max(array_column($data,'k_content'))], + ]; + break; + case 2: + //获取数据 + $data = Db::name('farmer_animal_env_data')->where('user_id',$device['user_id'])->whereBetweenTime('create_time', $params['start_time'], $params['end_time'])->order('id asc')->select()->each(function($item){ + $item['create_time'] = date('m-d',$item['create_time']); + return $item; + })->toArray(); + if(empty($data)){ + return $this->fail('暂无数据'); + } + $result = [ + ['hj_temp' => '环境温度','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'hj_temp')],'max'=>max(array_column($data,'hj_temp'))], + ['hj_humi' => '环境湿度','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'hj_humi')],'max'=>max(array_column($data,'hj_humi'))], + ['o_content' => '氧气浓度','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'o_content')],'max'=>max(array_column($data,'o_content'))], + ['c_content' => '二氧化碳浓度','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'c_content')],'max'=>max(array_column($data,'c_content'))], + ['light' => '光照强调','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'light')],'max'=>max(array_column($data,'light'))], + ['dqy' => '大气压强','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'dqy')],'max'=>max(array_column($data,'dqy'))], + ]; + break; + case 3: + //获取数据 + $data = Db::name('farmer_pond_env_data')->where('user_id',$device['user_id'])->whereBetweenTime('create_time', $params['start_time'], $params['end_time'])->order('id asc')->select()->each(function($item){ + $item['create_time'] = date('m-d',$item['create_time']); + return $item; + })->toArray(); + if(empty($data)){ + return $this->fail('暂无数据'); + } + $result = [ + ['water_temp' => '水温','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'water_temp')],'max'=>max(array_column($data,'water_temp'))], + ['ph_content' => '酸碱度','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'ph_content')],'max'=>max(array_column($data,'ph_content'))], + ['dissolved_oxygen' => '溶解氧','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'dissolved_oxygen')],'max'=>max(array_column($data,'dissolved_oxygen'))], + ['electric_conductivity' => '电导率','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'electric_conductivity')],'max'=>max(array_column($data,'electric_conductivity'))], + ['tds' => 'TDS','categories' => array_column($data,'create_time'),'series'=>['name'=>'目标值','data'=>array_column($data,'tds')],'max'=>max(array_column($data,'tds'))], + ]; + break; + default: + $result = []; + } + return $this->success('请求成功',$result); + } } \ No newline at end of file