123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace catchAdmin\device\controller;
- use catcher\base\CatchRequest as Request;
- use catcher\CatchResponse;
- use catcher\base\CatchController;
- // use catchAdmin\device\model\RfRecord as rfRecordModel;
- // use catchAdmin\device\model\Station as stationModel;
- // use catchAdmin\device\model\StationPhoto;
- use catcher\Utils;
- use catcher\library\excel\Excel;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use think\facade\Env;
- use think\facade\Db;
- use PDO;
- class Station extends CatchController
- {
- // protected $stationModel;
- // protected $rfRecordModel;
-
- // public function __construct(rfRecordModel $rfRecordModel,stationModel $stationModel)
- // {
- // $this->rfRecordModel = $rfRecordModel;
- // $this->stationModel = $stationModel;
- // }
-
- /**
- * 列表
- * @time 2022年01月20日 10:09
- * @param Request $request
- */
- public function index(Request $request)
- {
- $param=$request->param();
-
- $cond=[];
- if($param['shortcode']){
- $cond['a.DEVICE_CODE']=['like',$param['shortcode']];
- }
- if($param['name']){
- // $param['name'] = mb_convert_encoding($param['name'], 'GBK','UTF-8');
- $cond['a.DEVICE_NAME']=['like',$param['name']];
- }
- $count=queryOracleCount('DSSC2.ADM_DEV',$cond);
- $cond['page']=isset($param['page'])?$param['page']:1;
- $cond['limit']=isset($param['limit'])?$param['limit']:10;
- $rows=queryOracleSelect('(SELECT * FROM DSSC2.ADM_DEV ORDER BY ID DESC) a',$cond,'a.ID,a.IS_ONLINE,a.LOGIN_NAME,a.DEVICE_CODE,a.DEVICE_NAME,a.OWNER_CODE,a.DEVICE_IP,a.DEVICE_PORT,to_char(a.CREATE_DATE,\'yyyy-mm-dd hh24:mi:ss\') CREATE_DATE');
- foreach($rows as &$val){
- $val['IS_ONLINE_TEXT']=$val['IS_ONLINE']?'在线':'离线';
- $findCond=[
- 'DEVICE_CODE'=>['=',$val['DEVICE_CODE']]
- ];
- $info=queryOracleFind('DSSC2.ADM_DEV_RFID_CHN',$findCond);
- $val['longitude']=$info['GPS_X'];
- $val['latitude']=$info['GPS_Y'];
- }
-
- $response=[
- 'code'=>10000,
- 'message'=>'查询成功',
- 'count'=>$count,
- 'data'=>$rows,
- 'current'=>isset($param['page'])?(int)$param['page']:1,
- 'limit'=>isset($param['limit'])?(int)$param['limit']:10,
- ];
- return $response;
-
- }
- /**
- * 保存信息
- * @time 2022年01月20日 10:55
- * @param Request $request
- */
- public function save(Request $request)
- {
- $params=$request->param();
- $params['LOGIN_NAME']=strtoupper($params['LOGIN_NAME']);
- $r=$this->execSaveStation($params);
- return CatchResponse::success($r);
- }
-
- private function execSaveStation($data){
- $conn=getOracleConnect();
- $sql='select DEVICE_CODE from "DSSC2"."ADM_DEV" where DEVICE_CODE in (select max(DEVICE_CODE) from "DSSC2"."ADM_DEV")';
- $stid = oci_parse($conn, $sql);
- $r = oci_execute($stid);
- $result = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
- $deivce_code=$result['DEVICE_CODE']+1;
-
- $time=date('Y-m-d H:i:s');
- //插入基站信息表
- $sql='INSERT INTO "DSSC2"."ADM_DEV"("ID", "DEVICE_CODE","DEVICE_NAME", "DEVICE_CATEGORY", "DEVICE_TYPE","OWNER_CODE","LOGIN_TYPE","LOGIN_NAME","IS_ONLINE","CREATE_DATE","UPDATE_DATE")
- VALUES(DSSC2.SEQ_ADM_DEV.nextval,\''.$deivce_code.'\',\''.$data['DEVICE_NAME'].'\',32,\'13\',\'001\',\'6\',\''.$data['LOGIN_NAME'].'\',0,TO_DATE(\''.$time.'\', \'SYYYY-MM-DD HH24:MI:SS\'),TO_DATE(\''.$time.'\', \'SYYYY-MM-DD HH24:MI:SS\'))';
- $stid = oci_parse($conn, $sql);
- $r = oci_execute($stid);
-
- //插入基站信息表
- $sql='INSERT INTO "DSSC2"."ADM_DEV_RFID_CHN"("ID", "DEVICE_CODE","CHANNEL_NAME", "STAT" ,"GPS_X","GPS_Y","TYPE")
- VALUES(DSSC2.SEQ_ADM_DEV_RFID_CHN.nextval,\''.$deivce_code.'\',\''.$data['DEVICE_NAME'].'\',1,\''.$data['longitude'].'\',\''.$data['latitude'].'\',1)';
- $stid = oci_parse($conn, $sql);
- $r = oci_execute($stid);
- oci_free_statement($stid);
- return $r;
- }
- /**
- * 更新
- * @time 2022年01月20日 10:55
- * @param Request $request
- * @param $id
- */
- public function update(Request $request, $id)
- {
- $params=$request->param();
- $params['LOGIN_NAME']=strtoupper($params['LOGIN_NAME']);
- //UPDATE DSSC2.ADM_DEV A SET A.DEVICE_NAME = '1014测试插入4' WHERE A.DEVICE_CODE = '79933';
- //UPDATE DSSC2.ADM_DEV_RFID_CHN A SET A.CHANNEL_NAME = '1014测试插入4' WHERE A.DEVICE_CODE ='79933';
- $conn=getOracleConnect();
- //更新
- $sql='UPDATE DSSC2.ADM_DEV SET DEVICE_NAME = \''.$params['DEVICE_NAME'].'\',LOGIN_NAME=\''.$params['LOGIN_NAME'].'\' WHERE DEVICE_CODE = \''.$id.'\' ';
- $stid = oci_parse($conn, $sql);
- $r = oci_execute($stid);
-
- $sql='UPDATE DSSC2.ADM_DEV_RFID_CHN SET CHANNEL_NAME = \''.$params['DEVICE_NAME'].'\',GPS_X='.$params['longitude'].',GPS_Y='.$params['latitude'].' WHERE DEVICE_CODE = \''.$id.'\' ';
- $stid2 = oci_parse($conn, $sql);
- $r2 = oci_execute($stid2);
-
- return CatchResponse::success('修改成功');
- }
-
- /**
- * 删除
- * @time 2022年01月20日 10:55
- * @param $id
- */
- public function delete($id) : \think\Response
- {
- // $ids=Utils::stringToArrayBy($id);
- $conn=getOracleConnect();
- $sql='DELETE FROM DSSC2.ADM_DEV WHERE DEVICE_CODE in ('.$id.')';
-
- $stid = oci_parse($conn, $sql);
- $r = oci_execute($stid);
- $sql='DELETE FROM DSSC2.ADM_DEV_RFID_CHN WHERE DEVICE_CODE in ('.$id.')';
- $stid = oci_parse($conn, $sql);
- $r = oci_execute($stid);
- return CatchResponse::success($r);
- }
- public function getdeviceListByStation(Request $request){
- $params=$request->param();
- if(!$params['station_code']){
- return CatchResponse::success('');
- }
- $cond=[];
- if($params['station_code']){
- $cond['RF_ID']=['=',$params['station_code']];
- }
- $start_time = date('Y-m-d 00:00:00',time());
- $end_time = date('Y-m-d 23:59:59',time());
- if(isset($params['timeRange']) && $params['timeRange'] != ''){
- $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
- $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
- $cond['RF_DATE']=['timeRange',$start_time,$end_time];
- }
- $count=queryOracleCount('DSSC2.W_DW_RF_RECORD',$cond);
- $cond['page']=isset($param['page'])?$param['page']:1;
- $cond['limit']=isset($param['limit'])?$param['limit']:10;
-
- $rows=queryOracleSelect('(SELECT * FROM DSSC2.W_DW_RF_RECORD ORDER BY ID DESC) a',$cond,'a.RF_FLAGID,a.RF_STAT,to_char(a.RF_DATE,\'yyyy-mm-dd hh24:mi:ss\') RF_DATE');
- foreach($rows as &$val){
- //状态: 0- 未知,1 - 进入,2 - 离开
- if($val['RF_STAT']==1){
- $val['RF_STAT_TEXT']='进入';
- }elseif($val['RF_STAT']==2){
- $val['RF_STAT_TEXT']='离开';
- }else{
- $val['RF_STAT_TEXT']='未知';
- }
- }
- $response=[
- 'code'=>10000,
- 'message'=>'查询成功',
- 'count'=>$count,
- 'data'=>$rows,
- 'current'=>isset($params['page'])?(int)$params['page']:1,
- 'limit'=>isset($params['limit'])?(int)$params['limit']:10,
- ];
- return $response;
- // $cond=[];
- // $params=$request->param();
- // if(!$params['station_code']){
- // return CatchResponse::success('');
- // }
- // if($params['station_code']){
- // $cond[]=['RF_ID','=',$params['station_code']];
- // }
- // $start_time = date('Y-m-d 00:00:00',time());
- // $end_time = date('Y-m-d 23:59:59',time());
- // if(isset($params['timeRange']) && $params['timeRange'] != ''){
- // $start_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][0]));
- // $end_time=date('Y-m-d H:i:s',strtotime($params['timeRange'][1]));
- // $cond[]=['RF_DATE','between',[$start_time,$end_time]];
- // }
- // $list=$this->rfRecordModel->getList($cond);
-
- // return CatchResponse::paginate($list);
-
- }
-
-
- /**
- * 导入设备
- *
- * @time 2022年02月15日
- * @param Excel $excel
- * @param DeviceExport $deviceExport
- * @throws \PhpOffice\PhpSpreadsheet\Exception
- * @return \think\response\Json
- */
- public function importStation(Request $request)
- {
- $url = $request->post('url');
- if (!$url) {
- return CatchResponse::fail('请上传文件');
- }
-
- $creator_id = $request->post('creator_id');
- //解析地址
- $parse_url = parse_url($url)['path'];
- //载入excel表格
- $objPHPExcel = IOFactory::load(public_path() . $parse_url);
- //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
- // $nameArr = $objPHPExcel->getSheetNames();
- //获取表的数量
- $sheetCount = $objPHPExcel->getSheetCount();
- $fail = 0; //失败条数
- $success = 0; //成功条数
- $total = 0; //总共设备数
- $data = []; //设备数据
- //循环读取每一张表
- for ($index = 0; $index < $sheetCount; $index++) {
- //设置当前要读取的表
- $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
- // var_dump($sheet);exit;
- $highestRow = $sheet->getHighestRow(); // 取得总行数
- // var_dump($highestRow);
- if ($highestRow <= 1) {
- continue;
- }
- $total += $highestRow - 1;
- for ($j = 2; $j <= $highestRow; $j++) {
- $arr = array(); //每条设备信息
- $arr['LOGIN_NAME'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
- $arr['longitude'] = trim($sheet->getCell("B" . $j)->getFormattedValue());
- $arr['latitude'] = trim($sheet->getCell("C" . $j)->getFormattedValue());
- $arr['DEVICE_NAME'] = trim($sheet->getCell("D" . $j)->getFormattedValue());
-
- $r=$this->execSaveStation($arr);
- if($r){
- $success++;
- }else{
- $fail++;
- }
- // var_dump($arr);
- // array_push($data,$arr);
- }
- }
- // array_unique($data, SORT_REGULAR);
- // // var_dump($data);return CatchResponse::success();
- // $count = $this->deviceModel->limit(100)->insertAll($data);
- // if ($success = $count) {
- return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
- // }
- // return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
- }
- }
|