stationModel = $stationModel; } /** * 列表 * @time 2022年01月20日 10:09 * @param Request $request */ public function index(Request $request) : \think\Response { $field = $request->get('field')?:'id'; $order = $request->get('order')?:'desc'; return CatchResponse::paginate($this->stationModel->getStationList($field,$order)); } /** * 保存信息 * @time 2022年01月20日 10:09 * @param Request $request */ public function save(Request $request) : \think\Response { $data = $request->post(); //判断参数 if(!$data['department_id']){ return CatchResponse::fail('请选择所属部门'); } if(!$data['model']){ return CatchResponse::fail('请选择基站型号'); } $data['mac'] = trim($data['mac']); if(!$data['mac']){ return CatchResponse::fail('请输入基站Mac'); } if (!preg_match('/^[0-9a-zA-Z]{6,15}$/i',$data['mac'])){ return CatchResponse::fail('基站为6-15位字母数字'); } //基站简码 $data['shortcode'] = substr($data['mac'],-6,6); //校验重复 if($this->stationModel->where('mac',$data['mac'])->count()){ return CatchResponse::fail('该基站已存在'); } //开启事务 Db::startTrans(); //开局状态 if($data['type'] == 1){ $data['open_status'] = 0; }else{ $data['open_status'] = 1; //开局时间 $data['open_time'] = date('Y-m-d H:i:s'); //开局员工 $data['open_user_id'] = $data['creator_id']; } $station_id = $this->stationModel->storeBy($data); if(!$station_id){ return CatchResponse::fail('添加失败'); } //基站图片 if(isset($data['install_photo'])){ $photo = array( 'url'=>$data['install_photo'], 'type'=>'station', 'station_id'=>$station_id, 'creator_id'=>$data['creator_id'] ); $res = (new StationPhoto())->storeBy($photo); if(!$res){ Db::rollback(); return CatchResponse::fail('添加失败'); } } Db::commit(); return CatchResponse::success(); } /** * 读取 * @time 2022年01月20日 10:09 * @param $id */ public function read($id) : \think\Response { return CatchResponse::success($this->stationModel->findBy($id)); } /** * 更新 * @time 2022年01月20日 10:09 * @param Request $request * @param $id */ public function update(Request $request, $id) : \think\Response { return CatchResponse::success($this->stationModel->updateBy($id, $request->post())); } /** * 删除 * @time 2022年01月20日 10:09 * @param $id */ public function delete($id) : \think\Response { return CatchResponse::success($this->stationModel->deleteBy($id,true)); } /** * 导出 * * @time 2022年02月15日 * @param Excel $excel * @param StationExport $stationExport * @throws \PhpOffice\PhpSpreadsheet\Exception * @return \think\response\Json */ public function export_station(Excel $excel, StationExport $StationExport) { // var_dump(Utils::publicPath('export/users'));//导出路径 return CatchResponse::success($excel->save($StationExport, Utils::publicPath('export/stations'), 'local', '基站列表')); } /** * 导入设备 * * @time 2022年02月15日 * @param Excel $excel * @param DeviceExport $deviceExport * @throws \PhpOffice\PhpSpreadsheet\Exception * @return \think\response\Json */ public function import_station(Request $request) { $url = $request->post('url'); if (!$url) { return CatchResponse::fail('请上传文件'); } $depart_id = $request->post('depart_id'); if (!$depart_id) { return CatchResponse::fail('请选择部门'); } $model_id = $request->post('model_id'); if (!$model_id) { return CatchResponse::fail('请选择型号'); } $creator_id = $request->post('creator_id'); //解析地址 $parse_url = parse_url($url)['path']; //载入excel表格 $objPHPExcel = IOFactory::load(public_path() . $parse_url); // var_dump($objPHPExcel); //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3') // $nameArr = $objPHPExcel->getSheetNames(); // var_dump($nameArr); //获取表的数量 $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 <= 2) { continue; } $total += $highestRow - 2; for ($j = 3; $j <= $highestRow; $j++) { $arr = array(); //每条基站信息 $arr['mac'] = trim($sheet->getCell("A" . $j)->getFormattedValue()); //imei if ($arr['mac'] && mb_strlen($arr['mac']) != 12) { $fail++; $msg = '导入基站:' . $arr['mac'] . '失败:mac编号格式不正确'; $this->importFailLog($msg); continue; } if ($arr['mac']) { $isHas = $this->stationModel->where('mac',$arr['mac'])->count(); if($isHas){ $fail++; $msg = '导入基站:' . $arr['mac'] . '失败:mac编号已存在'; $this->importFailLog($msg); continue; } } $arr['remark'] = trim($sheet->getCell("B" . $j)->getFormattedValue()); //备注 $arr['department_id'] = $depart_id; $arr['shortcode'] = substr($arr['mac'],-6); $arr['creator_id'] = $creator_id; $arr['model'] = $model_id; $arr['name'] = '基站'.substr($arr['mac'],-4); $arr['created_at'] = time(); $arr['updated_at'] = time(); array_push($data,$arr); } } array_unique($data, SORT_REGULAR); // var_dump($data);return CatchResponse::success(); $count = $this->stationModel->limit(100)->insertAll($data); if ($success = $count) { return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条'); } return CatchResponse::success(['error' => true, 'msg' => '导入失败']); } /** * 导入设备失败日志 */ public function importFailLog($msg) { $file = runtime_path() . '/log/' . date("Ymd", time()) . "/import_stations_fail.log"; $folder = dirname($file); if (!is_dir($folder)) { mkdir($folder, 0777, true); } file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $msg . PHP_EOL, FILE_APPEND); } // ┏┛ ┻━━━━━┛ ┻┓ // ┃       ┃ // ┃   ━   ┃ // ┃ ┳┛  ┗┳ ┃ // ┃       ┃ // ┃   ┻   ┃ // ┃       ┃ // ┗━┓   ┏━━━┛ // ┃   ┃ 神兽保佑 // ┃   ┃ 代码无BUG! // ┃   ┗━━━━━━━━━┓ // ┃        ┣┓ // ┃     ┏┛ // ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛ // ┃ ┫ ┫ ┃ ┫ ┫ // ┗━┻━┛ ┗━┻━┛ }