Device.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace catchAdmin\device\controller;
  3. use catcher\base\CatchRequest as Request;
  4. use catcher\CatchResponse;
  5. use catcher\base\CatchController;
  6. use catcher\Utils;
  7. use catcher\library\excel\Excel;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use catchAdmin\device\excel\DeviceExport;
  10. use catchAdmin\device\model\Device as deviceModel;
  11. class Device extends CatchController
  12. {
  13. protected $deviceModel;
  14. public function __construct(DeviceModel $deviceModel)
  15. {
  16. $this->deviceModel = $deviceModel;
  17. }
  18. /**
  19. * 列表
  20. * @time 2022年01月20日 09:47
  21. * @param Request $request
  22. */
  23. public function index(Request $request) : \think\Response
  24. {
  25. $field = $request->get('field')?:'id';
  26. $order = $request->get('order')?:'desc';
  27. return CatchResponse::paginate($this->deviceModel->getDeviceList($field,$order));
  28. }
  29. /**
  30. * 保存信息
  31. * @time 2022年01月20日 09:47
  32. * @param Request $request
  33. */
  34. public function save(Request $request) : \think\Response
  35. {
  36. $data = $request->post();
  37. if(!$data['department_id']){
  38. return CatchResponse::fail('请选择部门');
  39. }
  40. $data['imei'] = trim($data['imei']);
  41. $data['rfid'] = trim($data['rfid']);
  42. if(!$data['imei'] && !$data['rfid']){
  43. return CatchResponse::fail('GPS编号和RFID编号必填一项');
  44. }
  45. //检测重复
  46. if($data['imei']){
  47. if($this->deviceModel->where('imei',$data['imei'])->limit(0,1)->find()){
  48. return CatchResponse::fail('GPS编号已存在');
  49. }
  50. }
  51. if($data['rfid']){
  52. if($this->deviceModel->where('rfid',$data['rfid'])->limit(0,1)->find()){
  53. return CatchResponse::fail('RFID编号已存在');
  54. }
  55. }
  56. $data['name'] = '设备'.substr($data['imei']?:$data['rfid'],-4);
  57. return CatchResponse::success($this->deviceModel->storeBy($data));
  58. }
  59. /**
  60. * 读取
  61. * @time 2022年01月20日 09:47
  62. * @param $id
  63. */
  64. public function read($id) : \think\Response
  65. {
  66. return CatchResponse::success($this->deviceModel->findBy($id));
  67. }
  68. /**
  69. * 更新
  70. * @time 2022年01月20日 09:47
  71. * @param Request $request
  72. * @param $id
  73. */
  74. public function update(Request $request, $id) : \think\Response
  75. {
  76. $data = $request->post();
  77. if(!$data['department_id']){
  78. return CatchResponse::fail('请选择部门');
  79. }
  80. $data['imei'] = trim($data['imei']);
  81. $data['rfid'] = trim($data['rfid']);
  82. if(!$data['imei'] && !$data['rfid']){
  83. return CatchResponse::fail('GPS编号和RFID编号必填一项');
  84. }
  85. //检测重复
  86. if($data['imei']){
  87. $oid = $this->deviceModel->where('imei',$data['imei'])->limit(0,1)->value('id');
  88. if( $oid && $oid != $id){
  89. return CatchResponse::fail('GPS编号已存在');
  90. }
  91. }
  92. if($data['rfid']){
  93. $oid = $this->deviceModel->where('rfid',$data['rfid'])->limit(0,1)->value('id');
  94. if($oid && $oid != $id){
  95. return CatchResponse::fail('RFID编号已存在');
  96. }
  97. }
  98. $data['name'] = $data['imei']?:$data['rfid'];
  99. return CatchResponse::success($this->deviceModel->updateBy($id,$data));
  100. }
  101. /**
  102. * 删除
  103. * @time 2022年01月20日 09:47
  104. * @param $id
  105. */
  106. public function delete($id) : \think\Response
  107. {
  108. return CatchResponse::success($this->deviceModel->deleteBy($id,true));
  109. }
  110. /**
  111. * 导出
  112. *
  113. * @time 2022年02月15日
  114. * @param Excel $excel
  115. * @param DeviceExport $deviceExport
  116. * @throws \PhpOffice\PhpSpreadsheet\Exception
  117. * @return \think\response\Json
  118. */
  119. public function export_device(Excel $excel, DeviceExport $DeviceExport)
  120. {
  121. // var_dump(Utils::publicPath('export/users'));//导出路径
  122. return CatchResponse::success($excel->save($DeviceExport, Utils::publicPath('export/devices'), 'local', '设备列表'));
  123. }
  124. /**
  125. * 导入设备
  126. *
  127. * @time 2022年02月15日
  128. * @param Excel $excel
  129. * @param DeviceExport $deviceExport
  130. * @throws \PhpOffice\PhpSpreadsheet\Exception
  131. * @return \think\response\Json
  132. */
  133. public function import_device(Request $request)
  134. {
  135. $url = $request->post('url');
  136. if (!$url) {
  137. return CatchResponse::fail('请上传文件');
  138. }
  139. $depart_id = $request->post('depart_id');
  140. if (!$depart_id) {
  141. return CatchResponse::fail('请选择部门');
  142. }
  143. $model_id = $request->post('model_id');
  144. if (!$model_id) {
  145. return CatchResponse::fail('请选择型号');
  146. }
  147. $creator_id = $request->post('creator_id');
  148. //解析地址
  149. $parse_url = parse_url($url)['path'];
  150. //载入excel表格
  151. $objPHPExcel = IOFactory::load(public_path() . $parse_url);
  152. // var_dump($objPHPExcel);
  153. //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
  154. // $nameArr = $objPHPExcel->getSheetNames();
  155. // var_dump($nameArr);
  156. //获取表的数量
  157. $sheetCount = $objPHPExcel->getSheetCount();
  158. $fail = 0; //失败条数
  159. $success = 0; //成功条数
  160. $total = 0; //总共设备数
  161. $data = []; //设备数据
  162. //循环读取每一张表
  163. for ($index = 0; $index < $sheetCount; $index++) {
  164. //设置当前要读取的表
  165. $sheet = $objPHPExcel->getSheet($index); //excel中的第一张sheet
  166. // var_dump($sheet);exit;
  167. $highestRow = $sheet->getHighestRow(); // 取得总行数
  168. // var_dump($highestRow);
  169. if ($highestRow <= 2) {
  170. continue;
  171. }
  172. $total += $highestRow - 2;
  173. for ($j = 3; $j <= $highestRow; $j++) {
  174. $arr = array(); //每条设备信息
  175. $arr['imei'] = trim($sheet->getCell("A" . $j)->getFormattedValue()); //imei
  176. if ($arr['imei'] && mb_strlen($arr['imei']) != 15) {
  177. $fail++;
  178. $msg = '导入设备:' . $arr['imei'] . '失败:imei编号格式不正确';
  179. $this->importFailLog($msg);
  180. continue;
  181. }
  182. //检测重复
  183. if($arr['imei']){
  184. $isHas = $this->deviceModel->where('imei',$arr['imei'])->count();
  185. if($isHas){
  186. $fail++;
  187. $msg = '导入设备imei:' . $arr['imei'] . '失败:imei编号已存在';
  188. $this->importFailLog($msg);
  189. continue;
  190. }
  191. }
  192. $arr['rfid'] = trim($sheet->getCell("B" . $j)->getFormattedValue()); //rfid
  193. //检测重复
  194. if($arr['rfid']){
  195. $isHas = $this->deviceModel->where('rfid',$arr['rfid'])->count();
  196. if($isHas){
  197. $fail++;
  198. $msg = '导入设备rfid:' . $arr['rfid'] . '失败:rfid编号已存在';
  199. $this->importFailLog($msg);
  200. continue;
  201. }
  202. }
  203. $arr['remark'] = trim($sheet->getCell("C" . $j)->getFormattedValue()); //备注
  204. $arr['department_id'] = $depart_id;
  205. $arr['creator_id'] = $creator_id;
  206. $arr['model'] = $model_id;
  207. $arr['name'] = '设备'.substr($arr['imei']?:$arr['rfid'],-4);
  208. $arr['created_at'] = time();
  209. $arr['updated_at'] = time();
  210. array_push($data,$arr);
  211. }
  212. }
  213. array_unique($data, SORT_REGULAR);
  214. // var_dump($data);return CatchResponse::success();
  215. $count = $this->deviceModel->limit(100)->insertAll($data);
  216. if ($success = $count) {
  217. return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . $fail . '条');
  218. }
  219. return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
  220. }
  221. /**
  222. * 导入设备失败日志
  223. */
  224. public function importFailLog($msg)
  225. {
  226. $file = runtime_path() . '/log/' . date("Ymd", time()) . "/import_devices_fail.log";
  227. $folder = dirname($file);
  228. if (!is_dir($folder)) {
  229. mkdir($folder, 0777, true);
  230. }
  231. file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $msg . PHP_EOL, FILE_APPEND);
  232. }
  233. }
  234. // ┏┛ ┻━━━━━┛ ┻┓
  235. // ┃       ┃
  236. // ┃   ━   ┃
  237. // ┃ ┳┛  ┗┳ ┃
  238. // ┃       ┃
  239. // ┃   ┻   ┃
  240. // ┃       ┃
  241. // ┗━┓   ┏━━━┛
  242. // ┃   ┃ 神兽保佑
  243. // ┃   ┃ 代码无BUG!
  244. // ┃   ┗━━━━━━━━━┓
  245. // ┃        ┣┓
  246. // ┃     ┏┛
  247. // ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛
  248. // ┃ ┫ ┫ ┃ ┫ ┫
  249. // ┗━┻━┛ ┗━┻━┛