Station.php 8.8 KB

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