RfidWithBw.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace catchAdmin\alarm\controller;
  3. use catcher\base\CatchRequest as Request;
  4. use catcher\CatchResponse;
  5. use catcher\base\CatchController;
  6. use catchAdmin\alarm\model\RfidWithBw as rfidWithBwModel;
  7. use catcher\Utils;
  8. use catcher\library\excel\Excel;
  9. use PhpOffice\PhpSpreadsheet\IOFactory;
  10. use think\facade\Db;
  11. class RfidWithBw extends CatchController
  12. {
  13. protected $rfidWithBwModel;
  14. public function __construct(RfidWithBwModel $rfidWithBwModel)
  15. {
  16. $this->rfidWithBwModel = $rfidWithBwModel;
  17. }
  18. /**
  19. * 列表
  20. * @time 2022年10月26日 13:53
  21. * @param Request $request
  22. */
  23. public function index(Request $request) : \think\Response
  24. {
  25. return CatchResponse::paginate($this->rfidWithBwModel->getList());
  26. }
  27. /**
  28. * 保存信息
  29. * @time 2022年10月26日 13:53
  30. * @param Request $request
  31. */
  32. public function save(Request $request) : \think\Response
  33. {
  34. //$this->rfidWithBwModel->storeBy($request->post())
  35. $data=$request->post();
  36. $creator_id=$data['creator_id'];
  37. $arr=[];
  38. foreach($data as $key=>$val){
  39. if($key==='creator_id'){
  40. continue;
  41. }
  42. $val['creator_id']=$creator_id;
  43. $val['created_at']=time();
  44. array_push($arr,$val);
  45. }
  46. // var_dump($arr);
  47. array_unique($data, SORT_REGULAR);
  48. $success = $this->rfidWithBwModel->insertAll($arr);
  49. return CatchResponse::success($success);
  50. }
  51. /**
  52. * 读取
  53. * @time 2022年10月26日 13:53
  54. * @param $id
  55. */
  56. public function read($id) : \think\Response
  57. {
  58. return CatchResponse::success($this->rfidWithBwModel->findBy($id));
  59. }
  60. /**
  61. * 更新
  62. * @time 2022年10月26日 13:53
  63. * @param Request $request
  64. * @param $id
  65. */
  66. public function update(Request $request, $id) : \think\Response
  67. {
  68. return CatchResponse::success($this->rfidWithBwModel->updateBy($id, $request->post()));
  69. }
  70. /**
  71. * 删除
  72. * @time 2022年10月26日 13:53
  73. * @param $id
  74. */
  75. public function delete($id) : \think\Response
  76. {
  77. return CatchResponse::success($this->rfidWithBwModel->deleteBy($id));
  78. }
  79. /**
  80. * 导入标签
  81. *
  82. * @time 2022年02月15日
  83. * @param Excel $excel
  84. * @param DeviceExport $deviceExport
  85. * @throws \PhpOffice\PhpSpreadsheet\Exception
  86. * @return \think\response\Json
  87. */
  88. public function importRfid(Request $request)
  89. {
  90. $url = $request->post('url');
  91. if (!$url) {
  92. return CatchResponse::fail('请上传文件');
  93. }
  94. $bw_id = $request->post('bw_id');
  95. if (!$bw_id) {
  96. return CatchResponse::fail('缺少必要参数');
  97. }
  98. $creator_id = $request->post('creator_id');
  99. //解析地址
  100. $parse_url = parse_url($url)['path'];
  101. //载入excel表格
  102. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  103. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  104. // $nameArr = $objPHPExcel->getSheetNames();
  105. //获取表的数量
  106. $sheetCount = $objPHPExcel->getSheetCount();
  107. $fail = 0; //失败条数
  108. $success = 0; //成功条数
  109. $total = 0; //总共设备数
  110. $data = []; //设备数据
  111. //循环读取每一张表
  112. for ($index = 0; $index < $sheetCount; $index++) {
  113. //设置当前要读取的表
  114. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  115. // var_dump($sheet);exit;
  116. $highestRow = $sheet->getHighestRow(); // 取得总行数
  117. // var_dump($highestRow);
  118. if ($highestRow <= 2) {
  119. continue;
  120. }
  121. $total += $highestRow - 2;
  122. for ($j = 3; $j <= $highestRow; $j++) {
  123. $arr = array(); //每条设备信息
  124. $arr['rfid'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue()));
  125. $arr['rfid_type'] = trim($sheet->getCell("B" . $j)->getFormattedValue());
  126. $arr['created_at']=time();
  127. $arr['creator_id']=$creator_id;
  128. $arr['bw_id']=$bw_id;
  129. $exist=Db::table('rfid_with_bw')->where('rfid',$arr['rfid'])->where('bw_id',$bw_id)->count();
  130. if($exist){
  131. continue;
  132. }
  133. array_push($data,$arr);
  134. }
  135. }
  136. array_unique($data, SORT_REGULAR);
  137. // var_dump($data);return CatchResponse::success();
  138. $count = Db::table('rfid_with_bw')->limit(100)->insertAll($data);
  139. if ($count) {
  140. return CatchResponse::success('共' . $total . '条数据,成功' . $count . '条,失败' . ($total-$count) . '条');
  141. }
  142. return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  143. }
  144. }