123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace catchAdmin\alarm\controller;
- use catcher\base\CatchRequest as Request;
- use catcher\CatchResponse;
- use catcher\base\CatchController;
- use catchAdmin\alarm\model\RfidWithBw as rfidWithBwModel;
- use catcher\Utils;
- use catcher\library\excel\Excel;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use think\facade\Db;
- class RfidWithBw extends CatchController
- {
- protected $rfidWithBwModel;
-
- public function __construct(RfidWithBwModel $rfidWithBwModel)
- {
- $this->rfidWithBwModel = $rfidWithBwModel;
- }
-
- /**
- * 列表
- * @time 2022年10月26日 13:53
- * @param Request $request
- */
- public function index(Request $request) : \think\Response
- {
- return CatchResponse::paginate($this->rfidWithBwModel->getList());
- }
-
- /**
- * 保存信息
- * @time 2022年10月26日 13:53
- * @param Request $request
- */
- public function save(Request $request) : \think\Response
- {
- //$this->rfidWithBwModel->storeBy($request->post())
- $data=$request->post();
- $creator_id=$data['creator_id'];
- $arr=[];
- foreach($data as $key=>$val){
- if($key==='creator_id'){
- continue;
- }
- $val['creator_id']=$creator_id;
- $val['created_at']=time();
-
- array_push($arr,$val);
- }
- // var_dump($arr);
- array_unique($data, SORT_REGULAR);
- $success = $this->rfidWithBwModel->insertAll($arr);
- return CatchResponse::success($success);
- }
-
- /**
- * 读取
- * @time 2022年10月26日 13:53
- * @param $id
- */
- public function read($id) : \think\Response
- {
- return CatchResponse::success($this->rfidWithBwModel->findBy($id));
- }
-
- /**
- * 更新
- * @time 2022年10月26日 13:53
- * @param Request $request
- * @param $id
- */
- public function update(Request $request, $id) : \think\Response
- {
- return CatchResponse::success($this->rfidWithBwModel->updateBy($id, $request->post()));
- }
-
- /**
- * 删除
- * @time 2022年10月26日 13:53
- * @param $id
- */
- public function delete($id) : \think\Response
- {
- return CatchResponse::success($this->rfidWithBwModel->deleteBy($id));
- }
- /**
- * 导入标签
- *
- * @time 2022年02月15日
- * @param Excel $excel
- * @param DeviceExport $deviceExport
- * @throws \PhpOffice\PhpSpreadsheet\Exception
- * @return \think\response\Json
- */
- public function importRfid(Request $request)
- {
- $url = $request->post('url');
- if (!$url) {
- return CatchResponse::fail('请上传文件');
- }
- $bw_id = $request->post('bw_id');
- if (!$bw_id) {
- 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 <= 2) {
- continue;
- }
- $total += $highestRow - 2;
- for ($j = 3; $j <= $highestRow; $j++) {
-
- $arr = array(); //每条设备信息
- $arr['rfid'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
- $arr['rfid_type'] = trim($sheet->getCell("B" . $j)->getFormattedValue());
- $arr['created_at']=time();
- $arr['creator_id']=$creator_id;
- $arr['bw_id']=$bw_id;
- $exist=Db::table('rfid_with_bw')->where('rfid',$arr['rfid'])->where('bw_id',$bw_id)->count();
- if($exist){
- continue;
- }
- array_push($data,$arr);
-
- }
- }
-
- array_unique($data, SORT_REGULAR);
- // var_dump($data);return CatchResponse::success();
- $count = Db::table('rfid_with_bw')->limit(100)->insertAll($data);
- if ($count) {
- return CatchResponse::success('共' . $total . '条数据,成功' . $count . '条,失败' . ($total-$count) . '条');
- }
- return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
- }
- }
|