Commit 37a28dcce831e3165b29d360954d76fdbacac5bc

Authored by xu
1 parent 27252b60
Exists in master

1. F 授权接口调整,有重大的bug

2. F 版本管理代码被覆盖了
app-api/controllers/AuthDeviceController.php
... ... @@ -72,16 +72,7 @@ class AuthDeviceController extends BaseController
72 72 $e->message = '签名有误';
73 73 return $e;
74 74 }
75   - // 检测是否授权过了的设备
76   - // 这里还得判读删除之后恢复的,状态是未授权
77   - $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'is_delete' => 0]);
78   - if ($deviceModel && DeviceStatus::HAS_AUTH == $deviceModel->status) {
79   - $e->mac = $deviceModel->mac;
80   - $e->serial_no = $deviceModel->serial_no;
81   - $e->message = 'ok';
82   - $e->status = 5;
83   - return $e;
84   - }
  75 +
85 76 $authResult = Device::authDevice($deviceId, $manufactureNo, $projectNo, $modelNo, $productionNo);
86 77  
87 78 if ($authResult->success) {
... ... @@ -93,6 +84,9 @@ class AuthDeviceController extends BaseController
93 84 $e->status = 4;
94 85 $e->message = $authResult->message;
95 86 }
  87 + if ($authResult->status > 0) {
  88 + $e->status = $authResult->status;
  89 + }
96 90  
97 91 return $e;
98 92 }
... ...
app-ht/modules/datas/views/device/device-list.php
... ... @@ -61,7 +61,7 @@ $this->params['breadcrumbs'][] = $this->title;
61 61 </div>
62 62 </div>
63 63 <div class="panel" style="margin-bottom: 0">
64   - <div style="padding:10px ;20px;box-sizing: border-box">><b>厂商:</b><?=$gets['manufacture']?> <b>项目:</b><?=$gets['project']?> <b>型号:</b><?=$gets['model']?> <b>生产日期:</b><?=$gets['production']?>
  64 + <div style="padding:10px ;20px;box-sizing: border-box"><b>厂商:</b><?=$gets['manufacture']?> <b>项目:</b><?=$gets['project']?> <b>型号:</b><?=$gets['model']?> <b>生产日期:</b><?=$gets['production']?>
65 65 <b> 批次编号:</b><?=$gets['batch_no']?>
66 66 </div>
67 67 </div>
... ...
app-ht/modules/device/controllers/DeviceController.php
... ... @@ -28,6 +28,12 @@ class DeviceController extends BaseController
28 28 */
29 29 public function actionIndex()
30 30 {
  31 + $params = $this->dataList(1);
  32 + return $this->render('index', $params);
  33 + }
  34 +
  35 + private function dataList($type)
  36 + {
31 37 $request = Yii::$app->request;
32 38 $serialNo = $request->get('serial_no');
33 39 $mac = $request->get('mac');
... ... @@ -95,11 +101,16 @@ class DeviceController extends BaseController
95 101 if (0 >= $page) {
96 102 $page = 1;
97 103 }
98   - $pageSize = 20;
99   - $page = ($page -1) * $pageSize;
100   - // DeviceRepository::getList($where, $pageSize, $page);
101   - $deviceData = DeviceRepository::getList($where, $pageSize, $page);
102   - $pages = new Pagination(['totalCount' => DeviceRepository::getListCount($where), 'pageSize' => $pageSize]);
  104 + if (1 == $type) {
  105 + $pageSize = 20;
  106 + $page = ($page -1) * $pageSize;
  107 + $deviceData = DeviceRepository::getList($where, $pageSize, $page);
  108 + $pages = new Pagination(['totalCount' => DeviceRepository::getListCount($where), 'pageSize' => $pageSize]);
  109 + } else {
  110 + $deviceData = DeviceRepository::getList($where, 0, 0);
  111 + $pages = null;
  112 + }
  113 +
103 114 $statusList = DeviceStatus::statusLabels(); //
104 115  
105 116 $params['statusList'] = $statusList;
... ... @@ -121,7 +132,7 @@ class DeviceController extends BaseController
121 132 'status' => $status
122 133 ];
123 134  
124   - return $this->render('index', $params);
  135 + return $params;
125 136 }
126 137  
127 138 /**
... ... @@ -319,16 +330,21 @@ class DeviceController extends BaseController
319 330  
320 331 return $this->renderJson($e);
321 332 }
  333 +
322 334 /**
323   - * 导出订单数据
  335 + * 导出数据
324 336 * @return string
325 337 */
326   - public function actionExportDa()
  338 + public function actionExport()
327 339 {
328   - $request = Yii::$app->request;
  340 + $params = $this->dataList(0);
  341 + return $this->renderPartial('export', $params);
329 342  
330 343 }
331 344  
  345 + /**
  346 + * @return string
  347 + */
332 348 public function actionAuthFailIndex()
333 349 {
334 350 $request = Yii::$app->request;
... ... @@ -429,6 +445,55 @@ class DeviceController extends BaseController
429 445 return $this->renderJson($e);
430 446 }
431 447  
  448 +
  449 + public function actionBatchAuthDevice()
  450 + {
  451 + $req = Yii::$app->request;
  452 + $ids = $req->post('ids');
  453 + $e = new stdClass();
  454 + $e->success = false;
  455 + $e->message = 'fail';
  456 + $ids = explode(',', $ids);
  457 + $deviceModels = DeviceRepository::findAll(['id' => $ids]);
  458 + if (empty($deviceModels)) {
  459 + $e->message = '找不到该设备';
  460 + return $this->renderJson($e);
  461 + }
  462 +
  463 + $isAuthCount = 0;
  464 + $emptyDeviceIdCount = 0;
  465 + $updateIds = [];
  466 + foreach ($deviceModels as $k => $deviceModel ) {
  467 + if(DeviceStatus::HAS_AUTH == $deviceModel->status) {
  468 + $isAuthCount++;
  469 + }
  470 + if (empty($deviceModel->device_id)) {
  471 + $emptyDeviceIdCount++;
  472 + }
  473 + if (DeviceStatus::HAS_AUTH != $deviceModel->status && $deviceModel->device_id) {
  474 + $updateIds[] = $deviceModel->id;
  475 + }
  476 + }
  477 +
  478 + $tt = time();
  479 + if ($updateIds) {
  480 + $attr = [
  481 + 'auth_at' => $tt,
  482 + 'status' => DeviceStatus::HAS_AUTH
  483 + ];
  484 + $condition = [
  485 + 'id' => $updateIds
  486 + ];
  487 + DeviceModel::updateAll($attr, $condition);
  488 + $e->success = true;
  489 + $e->message = '成功授权'.count($updateIds).'个设备';
  490 + } else {
  491 + $e->message = '设备ID都为空或部分设备已经授权过,授权失败';
  492 + }
  493 +
  494 + return $this->renderJson($e);
  495 + }
  496 +
432 497 /**
433 498 * @return string
434 499 */
... ... @@ -444,9 +509,11 @@ class DeviceController extends BaseController
444 509 $e->message = '找不到该设备';
445 510 return $this->renderJson($e);
446 511 }
447   -
  512 + if (empty($deviceModel->device_id) || DeviceStatus::HAS_AUTH != $deviceModel->status) {
  513 + $e->message = '必须是授权成功且有设备ID的才能删除';
  514 + return $this->renderJson($e);
  515 + }
448 516 $tt = time();
449   -
450 517 $deviceModel->is_delete = 1;
451 518 $result = $deviceModel->save();
452 519 if ($result) {
... ... @@ -461,6 +528,50 @@ class DeviceController extends BaseController
461 528 /**
462 529 * @return string
463 530 */
  531 + public function actionBatchDelDevice()
  532 + {
  533 + $req = Yii::$app->request;
  534 + $ids = $req->post('ids');
  535 + $e = new stdClass();
  536 + $e->success = false;
  537 + $e->message = 'fail';
  538 + $ids = explode(',', $ids);
  539 + $deviceModels = DeviceRepository::findAll(['id' => $ids]);
  540 + if (empty($deviceModels)) {
  541 + $e->message = '找不到该设备';
  542 + return $this->renderJson($e);
  543 + }
  544 +
  545 + $tt = time();
  546 + $delCount = 0;
  547 + $deleteIds = [];
  548 + foreach ($deviceModels as $k => $deviceModel) {
  549 + if ($deviceModel->device_id && DeviceStatus::HAS_AUTH == $deviceModel->status) {
  550 + $deleteIds[] = $deviceModel->id;
  551 + $delCount++;
  552 + }
  553 + }
  554 + $attr = [
  555 + 'is_delete' => 1
  556 + ];
  557 + $condition = [
  558 + 'id' => $deleteIds
  559 + ];
  560 + DeviceModel::updateAll($attr, $condition);
  561 +
  562 + if ($delCount > 0) {
  563 + $e->message = '成功删除'.$delCount .'个设备';
  564 + $e->success = true;
  565 + } else {
  566 + $e->message = '必须是授权成功的设备才能删除,删除失败';
  567 + }
  568 +
  569 + return $this->renderJson($e);
  570 + }
  571 +
  572 + /**
  573 + * @return string
  574 + */
464 575 public function actionDoEdit()
465 576 {
466 577 $req = Yii::$app->request;
... ... @@ -564,11 +675,11 @@ class DeviceController extends BaseController
564 675 $batchNo = Device::getBatchNo($deviceFailModel->manufacture_no, $deviceFailModel->project_no, $deviceFailModel->model_no, $deviceFailModel->production_no);
565 676 $batchInfo = CreateBatchRepository::findOne(['batch_no' => $batchNo]);
566 677 if (empty($batchInfo)) {
567   - $e->message = '该厂商未生产过该批次的设备无法生成';
  678 + $e->message = '该厂商未生产过该批次的设备无法处理';
568 679 return $this->renderJson($e);
569 680 }
570 681 $batchId = $batchInfo->id;
571   - $deviceModel = DeviceRepository::findOne(['device_id' => $deviceFailModel->device_id]);
  682 + $deviceModel = DeviceRepository::findOne(['device_id' => $deviceFailModel->device_id , 'is_delete' => 0]);
572 683 if ($deviceModel) {
573 684 $e->message = '该设备已经授权过';
574 685 return $this->renderJson($e);
... ... @@ -576,71 +687,224 @@ class DeviceController extends BaseController
576 687 $count = DeviceRepository::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]);
577 688  
578 689 if (($count *1) >= ($batchInfo->num * 1)) {
579   - $exitDeviceModel = DeviceModel::find();
580   - $exitDeviceModel->where(['batch_id' => $batchId]);
581   - $exitDeviceModel->orderBy('serial_no desc');
582   - $exitDevice = $exitDeviceModel->one();
583   - $serialNo = '';
584   - if ($exitDevice) {
585   - $numNo = mb_substr($exitDevice->serial_no, -4);
586   - $no1 = hexdec($numNo);
587   - $no =($no1 * 1) + 1;
588   - $newNo = sprintf('%04X', $no);
589   - $serialNo = $batchNo.$newNo;
  690 + $trans = Yii::$app->getDb()->beginTransaction();
  691 + try {
  692 +
  693 + Device::createWithMacSerialNo($batchId, $batchNo, $deviceFailModel->device_id, $deviceFailModel->apply_at, 1, DeviceStatus::HAS_AUTH);
  694 + // 设备改为删除了
  695 + $deviceFailModel->is_delete = 1;
  696 + $deviceFailModel->save();
  697 + $trans->commit();
  698 + $e->success = true;
  699 + return $this->renderJson($e);
  700 + } catch (Exception $exception) {
  701 + $trans->rollBack();
  702 + $e->message = '处理失败,请再尝试一次';
  703 + return $this->renderJson($e);
590 704 }
591 705  
592   - $macAddress = Utils::macGenerate();
593   - $deviceFind = DeviceModel::find();
594   - $deviceFind->where(
595   - [
596   - 'or',
597   - ['=', 'serial_no', $serialNo],
598   - ['=', 'mac', $macAddress]
599   - ]
600   - );
601   - $checkDevice = $deviceFind->one();
602   - if ($checkDevice) {
603   - $e->message = 'MAC地址或序列号已经存在,请再授权一次';
  706 + } else {
  707 + $newDeviceModel = DeviceRepository::findOne(['device_id'=> null, 'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]);
  708 + if (!$newDeviceModel) {
  709 + $trans = Yii::$app->getDb()->beginTransaction();
  710 + try {
  711 + Device::createWithMacSerialNo($batchId, $batchNo, $deviceFailModel->device_id, $deviceFailModel->apply_at, 1, DeviceStatus::HAS_AUTH);
  712 + $deviceFailModel->is_delete = 1;
  713 + $deviceFailModel->save();
  714 + $trans->commit();
  715 + $e->success = true;
  716 + $e->message = '处理成功';
  717 + } catch (Exception $exception) {
  718 + $trans->rollBack();
  719 + $e->message = '处理失败, 生成的设备序列号用完,请再点击处理一次';
  720 + }
604 721 return $this->renderJson($e);
605 722 }
  723 +
606 724 $trans = Yii::$app->getDb()->beginTransaction();
607 725 try {
608   - $newDevice = new DeviceModel();
609   - $newDevice->serial_no = $serialNo;
610   - $newDevice->mac = $macAddress;
611   - $newDevice->device_id = $deviceFailModel->device_id;
612   - $newDevice->batch_id = $batchId;
613   - $newDevice->status = DeviceStatus::HAS_AUTH;
614   - $newDevice->has_re_auth = 1;
615   - $newDevice->apply_at = time();
616   - $newDevice->save();
  726 + $newDeviceModel->device_id = $deviceFailModel->device_id;
  727 + $newDeviceModel->status = DeviceStatus::HAS_AUTH;
  728 + $newDeviceModel->apply_at = $deviceFailModel->apply_at;
  729 + $newDeviceModel->auth_at = time();
  730 + $newDeviceModel->status = DeviceStatus::HAS_AUTH;
  731 + $newDeviceModel->save();
  732 +
617 733 $deviceFailModel->is_delete = 1;
618 734 $deviceFailModel->save();
619 735 $trans->commit();
620 736 $e->success = true;
  737 + $e->message = '处理成功';
621 738 return $this->renderJson($e);
622   - } catch (Exception $exception){
623   -
  739 + } catch (Exception $exception) {
624 740 $trans->rollBack();
625   - $e->message = '授权失败';
  741 + $e->message = '处理失败,系统错误';
626 742 return $this->renderJson($e);
627 743 }
  744 + }
  745 + }
628 746  
629   - } else {
630   - $newDeviceModel = DeviceRepository::findOne(['device_id'=> null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]);
631   - $newDeviceModel->device_id = $deviceFailModel->device_id;
632   - $newDeviceModel->status = DeviceStatus::HAS_AUTH;
633   - $newDeviceModel->apply_at = time();
634   - $newDeviceModel->auth_at = time();
635   - $newDeviceModel->status = DeviceStatus::HAS_AUTH;
636   - $saveResult = $newDeviceModel->save();
637   - if ($saveResult) {
638   - $e->success = true;
  747 + /**
  748 + * @return string
  749 + * @throws \yii\db\Exception
  750 + */
  751 + public function actionBatchReAuth()
  752 + {
  753 + $req = Yii::$app->request;
  754 + $ids = $req->post('ids');
  755 + $e = new stdClass();
  756 + $e->success = false;
  757 + $e->message = 'fail';
  758 + $ids = explode(',', $ids);
  759 + $deviceFailModels = DeviceAuthFailRepository::findAll(['id' => $ids, 'is_delete' => 0]);
  760 + if (empty($deviceFailModels)) {
  761 + $e->message = '未找到记录';
  762 + return $this->renderJson($e);
  763 + }
  764 + $exitCount = 0;
  765 + $successCount = 0;
  766 + $failCount = 0;
  767 + $notExitCount = 0;
  768 + $deviceExitCount = 0;
  769 + foreach ($deviceFailModels as $k => $deviceFailModel) {
  770 + $batchNo = Device::getBatchNo($deviceFailModel->manufacture_no, $deviceFailModel->project_no, $deviceFailModel->model_no, $deviceFailModel->production_no);
  771 +
  772 + $batchInfo = CreateBatchRepository::findOne(['batch_no' => $batchNo]);
  773 + if (empty($batchInfo)) {
  774 + //该厂商未生产过该批次的设备无法生成
  775 + $notExitCount++;
  776 + continue;
  777 + }
  778 + $batchId = $batchInfo->id;
  779 + $deviceModel = DeviceRepository::findOne(['device_id' => $deviceFailModel->device_id , 'is_delete' => 0]);
  780 + if ($deviceModel) {
  781 + //该设备已经授权过
  782 + $deviceExitCount++;
  783 + continue;
  784 + }
  785 + $count = DeviceRepository::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]);
  786 +
  787 + if (($count *1) >= ($batchInfo->num * 1)) {
  788 + $exitDeviceModel = DeviceModel::find();
  789 + $exitDeviceModel->where(['batch_id' => $batchId]);
  790 + $exitDeviceModel->orderBy('serial_no desc');
  791 + $exitDevice = $exitDeviceModel->one();
  792 + $serialNo = '';
  793 + if ($exitDevice) {
  794 + $numNo = mb_substr($exitDevice->serial_no, -4);
  795 + $no1 = hexdec($numNo);
  796 + $no =($no1 * 1) + 1;
  797 + $newNo = sprintf('%04X', $no);
  798 + $serialNo = $batchNo.$newNo;
  799 + }
  800 +
  801 + $macAddress = Utils::macGenerate();
  802 + $deviceFind = DeviceModel::find();
  803 + $deviceFind->where(
  804 + [
  805 + 'or',
  806 + ['=', 'serial_no', $serialNo],
  807 + ['=', 'mac', $macAddress]
  808 + ]
  809 + );
  810 + $checkDevice = $deviceFind->one();
  811 + if ($checkDevice) {
  812 + //'MAC地址或序列号已经存在,请再处理一次';
  813 + $exitCount++;
  814 + continue;
  815 + }
  816 +
  817 + $trans = Yii::$app->getDb()->beginTransaction();
  818 + try {
  819 + $item = [
  820 + 'serial_no' => $serialNo,
  821 + 'mac' => $macAddress,
  822 + 'device_id' => $deviceFailModel->device_id,
  823 + 'batch_id' => $batchId,
  824 + 'status' => DeviceStatus::HAS_AUTH,
  825 + 'has_re_auth' => 1,
  826 + 'apply_at' => $deviceFailModel->apply_at,
  827 + 'auth_at' => time(),
  828 + ];
  829 + Device::create($item);
  830 + // 设备改为删除了
  831 + $deviceFailModel->is_delete = 1;
  832 + $deviceFailModel->save();
  833 + $trans->commit();
  834 + $successCount++;
  835 +
  836 + } catch (Exception $exception) {
  837 + $trans->rollBack();
  838 + $failCount++;
  839 + }
639 840 } else {
640   - $e->message = '授权失败';
  841 +
  842 + $newDeviceModel = DeviceRepository::findOne(['device_id'=> null, 'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]);
  843 + if (!$newDeviceModel) {
  844 +
  845 + $trans = Yii::$app->getDb()->beginTransaction();
  846 + try {
  847 + Device::createWithMacSerialNo($batchId, $batchNo, $deviceFailModel->device_id, $deviceFailModel->apply_at, 1, DeviceStatus::HAS_AUTH);
  848 + $deviceFailModel->is_delete = 1;
  849 + $deviceFailModel->save();
  850 + $trans->commit();
  851 + $successCount++;
  852 + } catch (Exception $exception) {
  853 + $trans->rollBack();
  854 + $failCount++;
  855 + }
  856 +
  857 + } else {
  858 + $trans = Yii::$app->getDb()->beginTransaction();
  859 + try {
  860 + $newDeviceModel->device_id = $deviceFailModel->device_id;
  861 + $newDeviceModel->status = DeviceStatus::HAS_AUTH;
  862 + $newDeviceModel->apply_at = $deviceFailModel->apply_at;
  863 + $newDeviceModel->auth_at = time();
  864 + $newDeviceModel->status = DeviceStatus::HAS_AUTH;
  865 + $newDeviceModel->save();
  866 +
  867 + $deviceFailModel->is_delete = 1;
  868 + $deviceFailModel->save();
  869 + $trans->commit();
  870 + $successCount++;
  871 + } catch (Exception $exception) {
  872 + $trans->rollBack();
  873 + $failCount++;
  874 + }
  875 + }
  876 +
641 877 }
642   - return $this->renderJson($e);
643 878 }
  879 +
  880 + if ($successCount > 0) {
  881 + $e->message = '成功处理了'.$successCount.'个设备。';
  882 + if ($failCount > 0) {
  883 + $e->message = $e->message .'另外有'.$failCount.'个处理失败。';
  884 + }
  885 + if ($exitCount > 0) {
  886 + $e->message = '有'.$exitCount.'个生成序列号失败,请重试';
  887 + }
  888 + $e->success = true;
  889 + } else {
  890 + $e->message = '处理失败。';
  891 + if ($failCount > 0) {
  892 + $e->message = $e->message .'共有'.$failCount.'个处理失败';
  893 + }
  894 + if ($notExitCount > 0) {
  895 + $e->message = '发现有'.$notExitCount.'个没有厂商批次的设备。';
  896 + }
  897 + if ($deviceExitCount > 0) {
  898 + $e->message = '发现有'.$deviceExitCount.'个设备已经授权过了';
  899 + }
  900 + //$exitCount
  901 + if ($exitCount > 0) {
  902 + $e->message = '有'.$exitCount.'个生成序列号失败,请重试';
  903 + }
  904 +
  905 + }
  906 +
  907 + return $this->renderJson($e);
644 908 }
645 909  
646 910 /**
... ... @@ -655,7 +919,100 @@ class DeviceController extends BaseController
655 919 $e->success = false;
656 920 $e->message = 'fail';
657 921 $delDevice = DeviceRepository::findOne(['id' => $id, 'is_delete' => 1]);
  922 + if (!$delDevice) {
  923 + $e->message = '未找到该设备';
  924 + return $this->renderJson($e);
  925 + }
  926 + // 检测当前批次是否已经满了,能否恢复
  927 + $batchModel = CreateBatchRepository::findOne(['id' => $delDevice->batch_id]);
  928 + if (empty($batchModel)) {
  929 + $e->message = '未找到厂商对应的批次无法恢复';
  930 + return $this->renderJson($e);
  931 + }
  932 + $count = DeviceRepository::rowsCount(['batch_id' => $batchModel->id, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]);
658 933  
  934 + if (($count *1) >= ($batchModel->num * 1)) {
  935 + $e->message = '该厂商的批次已经满了无法恢复该设备';
  936 + return $this->renderJson($e);
  937 + }
  938 + // 检测当前设备ID是否存在表里面
  939 + $exitDevice = DeviceRepository::findOne(['device_id' => $delDevice->device_id, 'is_delete' => 0]);
  940 + if ($exitDevice) {
  941 + $e->message = '该设备ID已经存在序列号表里面,无法恢复';
  942 + return $this->renderJson($e);
  943 + }
  944 +
  945 + $delDevice->is_delete = 0;
  946 + $delDevice->auth_at = null;
  947 + $delDevice->status = DeviceStatus::NO_AUTH;
  948 + $saveResult = $delDevice->save();
  949 + if ($saveResult) {
  950 + $e->success = true;
  951 + $e->message = '恢复成功';
  952 + } else {
  953 + $e->message = '恢复失败';
  954 + }
  955 +
  956 + return $this->renderJson($e);
  957 + }
  958 +
  959 + public function actionBatchRestoreDevice()
  960 + {
  961 + $req = Yii::$app->request;
  962 + $ids = $req->post('ids');
  963 + $e = new stdClass();
  964 + $e->success = false;
  965 + $e->message = 'fail';
  966 + $ids = explode(',', $ids);
  967 + $delDevices = DeviceRepository::findAll(['id' => $ids, 'is_delete' => 1]);
  968 + if (!$delDevices) {
  969 + $e->message = '未找到设备';
  970 + return $this->renderJson($e);
  971 + }
  972 + $fullCount = 0;
  973 + $exitCount = 0;
  974 + $restoreArr = [];
  975 + // 检测当前批次是否已经满了,能否恢复
  976 + foreach ($delDevices as $k => $delDevice) {
  977 + $batchModel = CreateBatchRepository::findOne(['id' => $delDevice->batch_id]);
  978 + if (empty($batchModel)) {
  979 + continue;
  980 + }
  981 + $count = DeviceRepository::rowsCount(['batch_id' => $batchModel->id, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]);
  982 + if (($count *1) >= ($batchModel->num * 1)) {
  983 + $fullCount++;
  984 + }
  985 + // 检测当前设备ID是否存在表里面
  986 + $exitDevice = DeviceRepository::findOne(['device_id' => $delDevice->device_id, 'is_delete' => 0]);
  987 + if ($exitDevice) {
  988 + $exitCount++;
  989 + }
  990 + if (!$exitDevice && ($count *1) < ($batchModel->num * 1)) {
  991 + $restoreArr[] = $delDevice->id;
  992 + }
  993 + }
  994 + $restoreArr = array_unique($restoreArr);
  995 + if (!$restoreArr) {
  996 + $e->message = '设备不满足恢复条件,无法恢复';
  997 + return $this->renderJson($e);
  998 + }
  999 + $attr = [
  1000 + 'is_delete' => 0,
  1001 + 'auth_at' => null,
  1002 + 'status' => DeviceStatus::NO_AUTH
  1003 + ];
  1004 +
  1005 + $condition = [
  1006 + 'id' => $restoreArr
  1007 + ];
  1008 + $saveResult = DeviceModel::updateAll($attr, $condition);
  1009 +
  1010 + if ($saveResult) {
  1011 + $e->success = true;
  1012 + $e->message = '成功恢复'.count($restoreArr).'台设备';
  1013 + } else {
  1014 + $e->message = '恢复失败,系统错误';
  1015 + }
659 1016  
660 1017 return $this->renderJson($e);
661 1018 }
... ...
app-ht/modules/device/views/device/auth-fail-index.php
1 1 <?php
2 2 use yii\helpers\Url;
3 3 use app\ht\widgets\LinkPager;
4   -use domain\device\DeviceStatus;
  4 +use domain\device\Device;
5 5  
6 6 $this->title = '授权失败管理';
7 7 $this->params['breadcrumbs'][] = '失败序列号管理';
... ... @@ -61,11 +61,12 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
61 61 <tr>
62 62 <th width="4%"></th>
63 63 <th width="5%">ID</th>
64   - <th width="15%">厂商</th>
  64 + <th width="10%">批次编码</th>
  65 + <th width="10%">厂商</th>
65 66 <th width="6%">项目</th>
66 67 <th width="8%">设备型号</th>
67 68 <th width="8%">生产日期</th>
68   - <th width="15%">设备ID</th>
  69 + <th width="10%">设备ID</th>
69 70 <th width="10%">申请时间</th>
70 71 <th width="20%">操作</th>
71 72 </tr>
... ... @@ -75,11 +76,13 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
75 76 <?php if ($deviceList) { ?>
76 77 <?php foreach ($deviceList as $item) : ?>
77 78 <tr>
78   - <td><input type="checkbox" class="check-cls" value="<?= $item['id'] ?>"/></td>
  79 + <td><input type="checkbox" class="check-cls check-item" value="<?= $item['id'] ?>"/></td>
79 80 <td class="td-cls">
80 81 <?= $item['id'] ?>
81 82 </td>
82   -
  83 + <td class="td-cls">
  84 + <?= Device::getBatchNo($item['manufacture_no'], $item['project_no'], $item['model_no'], $item['production_no']) ?>
  85 + </td>
83 86 <td class="td-cls">
84 87 <?= $item['manufacture'] ?>
85 88 </td>
... ... @@ -132,6 +135,7 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
132 135  
133 136 <script>
134 137 var authURL = "<?=Url::toRoute('/device/device/re-auth')?>";
  138 + var batchReAuthURL = "<?=Url::toRoute('/device/device/batch-re-auth')?>";
135 139 var delURL = "<?=Url::toRoute('/device/device/del-device')?>";
136 140 var doEditURL = "<?=Url::toRoute('/device/device/do-edit-fail')?>";
137 141 $(function () {
... ... @@ -183,5 +187,37 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
183 187 }
184 188  
185 189 })
  190 +
  191 + $('.check-all').click(function(e) {
  192 + var checkVal = $(this).prop('checked');
  193 + $('.check-item').prop('checked', checkVal)
  194 +
  195 + })
  196 +
  197 + $('.btn_batch_auth').click(function(e) {
  198 + var checkItems = $('.check-item:checked');
  199 + if ( 0 == checkItems.length) {
  200 + alert('请选择要操作的行');
  201 + return false;
  202 + }
  203 + var ids = [];
  204 + $.each(checkItems, function(i,n){
  205 + var id = $(n).val();
  206 + if ('' != id && 0 != id) {
  207 + ids.push(id);
  208 + }
  209 + })
  210 + ids = ids.join(',');
  211 + $.post(batchReAuthURL,{ids:ids},function(res){
  212 + if (res.success) {
  213 + alert(res.message);
  214 + var _body = window.parent;
  215 + var _iframe1=_body.document.getElementById('x-iframe');
  216 + _iframe1.contentWindow.location.reload(true);
  217 + } else {
  218 + alert(res.message);
  219 + }
  220 + }, 'json')
  221 + })
186 222 });
187 223 </script>
188 224 \ No newline at end of file
... ...
app-ht/modules/device/views/device/delete-index.php
... ... @@ -148,7 +148,7 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
148 148 <?php endforeach; ?>
149 149 <?php } else { ?>
150 150 <tr>
151   - <td colspan="12">
  151 + <td colspan="13">
152 152 <center>暂无记录</center>
153 153 </td>
154 154 </tr>
... ... @@ -174,6 +174,7 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
174 174 var authURL = "<?=Url::toRoute('/device/device/auth-device')?>";
175 175 var delURL = "<?=Url::toRoute('/device/device/del-device')?>";
176 176 var restoreURL = "<?=Url::toRoute('/device/device/restore-device')?>";
  177 + var batchRestoreURL = "<?=Url::toRoute('/device/device/batch-restore-device')?>";
177 178 var doEditURL = "<?=Url::toRoute('/device/device/do-edit')?>";
178 179 $(function () {
179 180  
... ... @@ -223,10 +224,50 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
223 224  
224 225 })
225 226  
226   - $('.btn_restore').click(function(){
  227 + $('.btn_restore').click(function() {
  228 + var id = $(this).attr('aid');
  229 + $.post(restoreURL, {id:id} ,function(res) {
  230 + if (res.success) {
  231 + alert(res.message);
  232 + var _body = window.parent;
  233 + var _iframe1=_body.document.getElementById('x-iframe');
  234 + _iframe1.contentWindow.location.reload(true);
  235 + } else {
  236 + alert(res.message);
  237 + }
  238 + } , 'json')
  239 + })
  240 + $('.check-all').click(function(e) {
  241 + var checkVal = $(this).prop('checked');
  242 + $('.check-item').prop('checked', checkVal)
227 243  
228 244 })
229 245  
  246 + $('.btn_batch_auth').click(function(){
  247 + var checkItems = $('.check-item:checked');
  248 + if ( 0 == checkItems.length) {
  249 + alert('请选择要操作的行');
  250 + return false;
  251 + }
  252 + var ids = [];
  253 + $.each(checkItems, function(i,n){
  254 + var id = $(n).val();
  255 + if ('' != id && 0 != id) {
  256 + ids.push(id);
  257 + }
  258 + })
  259 + ids = ids.join(',');
  260 + $.post(batchRestoreURL,{ids:ids},function(res){
  261 + if (res.success) {
  262 + alert(res.message);
  263 + var _body = window.parent;
  264 + var _iframe1=_body.document.getElementById('x-iframe');
  265 + _iframe1.contentWindow.location.reload(true);
  266 + } else {
  267 + alert(res.message);
  268 + }
  269 + }, 'json')
  270 + })
230 271  
231 272 });
232 273 </script>
233 274 \ No newline at end of file
... ...
app-ht/modules/device/views/device/export.php 0 → 100644
... ... @@ -0,0 +1,91 @@
  1 +<?php
  2 +
  3 +header('Content-Type: application/vnd.ms-excel;charset=utf-8');
  4 +$title = date('Y-m-d') . '_设备';
  5 +$name = $title . ".xls";
  6 +header('Content-Disposition: attachment;filename=' . $name . '');
  7 +header('Cache-Control: max-age=0');
  8 +$fp = fopen('php://output', 'a');
  9 +$limit = 10000;
  10 +$cnt = 0;
  11 +
  12 +?>
  13 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  14 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  15 +<html xmlns:o="urn:schemas-microsoft-com:office:office"
  16 + xmlns:x="urn:schemas-microsoft-com:office:excel"
  17 + xmlns="http://www.w3.org/TR/REC-html40">
  18 +
  19 +<head>
  20 + <meta http-equiv=Content-Type content="text/html; charset=utf-8">
  21 +
  22 +</head>
  23 +<body>
  24 +<div id="Classeur1_16681" align='center' x:publishsource="Excel">
  25 + <table x:str border='1' cellpadding='0' cellspacing='0' width='100%' style="border-collapse: collapse">
  26 + <thead>
  27 + <tr>
  28 + <th width="6%">ID</th>
  29 + <th width="5%">序列号</th>
  30 + <th width="8%">厂商</th>
  31 + <th width="6%">项目</th>
  32 + <th width="8%">设备型号</th>
  33 + <th width="6%">生产日期</th>
  34 + <th>MAC地址</th>
  35 + <th width="7%">设备ID</th>
  36 + <th width="7%">申请时间</th>
  37 + <th width="7%">授权时间</th>
  38 + <th width="7%">状态</th>
  39 + </tr>
  40 + </thead>
  41 + <tbody>
  42 + <?php foreach ($deviceList as $key => $item) : ?>
  43 + <tr>
  44 + <td class="td-cls">
  45 + <?= $item['id'] ?>
  46 + </td>
  47 + <td class="td-cls">
  48 + <div class="cell-cls"><?= $item['serial_no'] ?></div>
  49 + </td>
  50 + <td class="td-cls">
  51 + <?= $item['manufacture'] ?>
  52 + </td>
  53 + <td class="td-cls">
  54 + <?= $item['project'] ?>
  55 + </td>
  56 + <td class="td-cls">
  57 + <?= $item['model'] ?>
  58 + </td>
  59 + <td class="td-cls">
  60 + <?= $item['production'] ?>
  61 + </td>
  62 + <td class="td-cls">
  63 + <?= $item['mac'] ?>
  64 + </td>
  65 + <td class="td-cls">
  66 + <?= $item['device_id']? $item['device_id']:'暂无'?>
  67 + </td>
  68 + <td class="td-cls">
  69 + <?= $item['apply_at']?date('Y-m-d H:i:s', $item['apply_at']):'暂无' ?>
  70 + </td>
  71 + <td class="td-cls">
  72 + <?= $item['auth_at']? date('Y-m-d H:i:s', $item['auth_at']):'暂无' ?>
  73 + </td>
  74 + <td class="td-cls">
  75 + <?= $statusList[$item['status']] ?>
  76 + </td>
  77 + </tr>
  78 + <?php
  79 + $cnt++;
  80 + if (1000 == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题
  81 + ob_flush();
  82 + flush();
  83 + $cnt = 0;
  84 + }
  85 + ?>
  86 + <?php endforeach; ?>
  87 + </tbody>
  88 + </table>
  89 +</div>
  90 +</body>
  91 +</html>
0 92 \ No newline at end of file
... ...
app-ht/modules/device/views/device/index.php
... ... @@ -80,6 +80,7 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
80 80  
81 81 <div class="form-group col-sm-12" style="text-align: center;">
82 82 <button type="submit" class="btn btn-primary font-1" id="submitFilterBtn">查询</button>
  83 + <a class="btn btn-default" style="float: right;" href="javascript:void(0)" id="btn-export"> 导出数据 </a>
83 84 </div>
84 85 </form>
85 86 </div>
... ... @@ -186,7 +187,9 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
186 187  
187 188 <script>
188 189 var authURL = "<?=Url::toRoute('/device/device/auth-device')?>";
  190 + var batchAuthURL = "<?=Url::toRoute('/device/device/batch-auth-device')?>";
189 191 var delURL = "<?=Url::toRoute('/device/device/del-device')?>";
  192 + var batchDelURL = "<?=Url::toRoute('/device/device/batch-del-device')?>";
190 193 var doEditURL = "<?=Url::toRoute('/device/device/do-edit')?>";
191 194 $(function () {
192 195 $('.btn_auth').click(function(e) {
... ... @@ -258,11 +261,89 @@ $this-&gt;params[&#39;breadcrumbs&#39;][] = $this-&gt;title;
258 261  
259 262 })
260 263  
261   - $('.check-all').click(function(e){
  264 + $('.check-all').click(function(e) {
262 265 var checkVal = $(this).prop('checked');
263 266 $('.check-item').prop('checked', checkVal)
264 267  
265 268 })
  269 + $('.btn_batch_auth').click(function() {
  270 + var checkItems = $('.check-item:checked');
  271 + if ( 0 == checkItems.length) {
  272 + alert('请选择要操作的行');
  273 + return false;
  274 + }
  275 + var ids = [];
  276 + $.each(checkItems, function(i,n){
  277 + var id = $(n).val();
  278 + if ('' != id && 0 != id) {
  279 + ids.push(id);
  280 + }
  281 + })
  282 + ids = ids.join(',');
  283 + $.post(batchAuthURL,{ids:ids},function(res){
  284 + if (res.success) {
  285 + alert(res.message);
  286 + var _body = window.parent;
  287 + var _iframe1=_body.document.getElementById('x-iframe');
  288 + _iframe1.contentWindow.location.reload(true);
  289 + } else {
  290 + alert(res.message);
  291 + }
  292 + }, 'json')
  293 + })
  294 + //btn_batch_del
  295 + $('.btn_batch_del').click(function() {
  296 + if (!confirm('确定删除设备')){
  297 + return false;
  298 + }
  299 + var checkItems = $('.check-item:checked');
  300 + if ( 0 == checkItems.length) {
  301 + alert('请选择要操作的行');
  302 + return false;
  303 + }
  304 + var ids = [];
  305 + $.each(checkItems, function(i,n){
  306 + var id = $(n).val();
  307 + if ('' != id && 0 != id) {
  308 + ids.push(id);
  309 + }
  310 + })
  311 + ids = ids.join(',');
  312 + $.post(batchDelURL,{ids:ids},function(res){
  313 + if (res.success) {
  314 + alert(res.message);
  315 + var _body = window.parent;
  316 + var _iframe1=_body.document.getElementById('x-iframe');
  317 + _iframe1.contentWindow.location.reload(true);
  318 + } else {
  319 + alert(res.message);
  320 + }
  321 + }, 'json')
  322 + })
266 323  
  324 + window.queryParams = function(params) {
  325 + $("#search-form").find('input[name]').each(function () {
  326 + var val = $(this).val();
  327 + var name = $(this).attr('name');
  328 + if(val){
  329 + params[name] = val;
  330 + }
  331 + });
  332 + return params;
  333 + }
  334 + $('#btn-export').click(function(e){
  335 + var params = {};
  336 + window.queryParams(params);
  337 +
  338 + $strQuery = "?";
  339 + if (params) {
  340 + for (var p in params) {
  341 + $strQuery += p + "=" + params[p] + "&";
  342 + }
  343 + }
  344 + $strQuery = $strQuery.substring(0, $strQuery.length-1);
  345 + window.location.href = "export" + $strQuery;
  346 + return false;
  347 + })
267 348 });
268 349 </script>
269 350 \ No newline at end of file
... ...
app-ht/modules/upgrade/controllers/UpgradeController.php
... ... @@ -159,10 +159,25 @@ class UpgradeController extends BaseController
159 159 $request = Yii::$app->request;
160 160 $id = $request->post("id"); //
161 161 $e = new stdClass();
  162 + $e->success = false;
  163 + $e->message = 'fail';
162 164 if (empty($id)) {
163   -
  165 + $e->message = 'ID为空';
  166 + return $this->renderJson($e);
  167 + }
  168 + $upgradeModel = UpgradeRepository::findOne(['id' => $id]);
  169 + if (empty($upgradeModel)) {
  170 + $e->message = '未找到版本记录';
164 171 return $this->renderJson($e);
165 172 }
  173 + $upgradeModel->status = UpgradeStatus::STATUS_WAIT;
  174 + if ($upgradeModel->save()) {
  175 + $e->success = true;
  176 + } else {
  177 + $e->message = '取消失败';
  178 + }
  179 +
  180 + return $this->renderJson($e);
166 181  
167 182 }
168 183  
... ... @@ -566,6 +581,14 @@ class UpgradeController extends BaseController
566 581 if (isset($manufacture["name"])) {
567 582 $info["manufacture_name"] = $manufacture["name"];
568 583 }
  584 + $project = ProjectRepository::selectOne($info["project_id"]);
  585 + if (isset($project["name"])) {
  586 + $info["project_name"] = $project["name"];
  587 + }
  588 + $model = ModelRepository::selectOne($info["model_id"]);
  589 + if (isset($model["name"])) {
  590 + $info["model_name"] = $model["name"];
  591 + }
569 592 }
570 593 return $this->render('push-app', ["info" => $info]);
571 594 }
... ... @@ -584,6 +607,14 @@ class UpgradeController extends BaseController
584 607 if (isset($manufacture["name"])) {
585 608 $info["manufacture_name"] = $manufacture["name"];
586 609 }
  610 + $project = ProjectRepository::selectOne($info["project_id"]);
  611 + if (isset($project["name"])) {
  612 + $info["project_name"] = $project["name"];
  613 + }
  614 + $model = ModelRepository::selectOne($info["model_id"]);
  615 + if (isset($model["name"])) {
  616 + $info["model_name"] = $model["name"];
  617 + }
587 618 }
588 619 return $this->render('push-ota', ["info" => $info]);
589 620 }
... ...
console/controllers/TestController.php
... ... @@ -35,15 +35,15 @@ class TestController extends Controller
35 35 public function actionDeviceAuth()
36 36 {
37 37 $url = 'http://kingb:8012/app-api/web/authDevice';
38   - $manufactureNo = '0002';
39   - $device_id = 'SZDEVICE000037';
40   - $projectNo = '002';
41   - $modelNo = '002';
42   - $productionNo = '0001';
43   -
  38 + //$url = 'http://47.107.95.101/app-api/web/authDevice';
  39 + $manufactureNo = '0003';
  40 + $device_id = 'HZ5DEVICE00007';
  41 + $projectNo = '0003';
  42 + $modelNo = '0003';
  43 + $productionNo = '0003';
44 44 $timestamp = time();
45 45 $salt = 13456;
46   - $sign = md5($manufactureNo. $projectNo. $modelNo . $productionNo . $timestamp . $salt);
  46 + $sign = md5($manufactureNo. $projectNo. $modelNo . $productionNo . $timestamp .$device_id. $salt);
47 47 $params = [
48 48 'manufacture' => $manufactureNo,
49 49 'device_id' => $device_id,
... ... @@ -54,7 +54,7 @@ class TestController extends Controller
54 54 'sign' => $sign,
55 55 ];
56 56 $params = json_encode($params);
57   - $params = '{"manufacture":"0001","device_id":"4705BF36C376","project":"0001","model":"0001","production":"0001","timestamp":"1500000000","sign":"f16b3f969714a400e155e04e0ea29938"}';
  57 + //$params = '{"manufacture":"0002","device_id":"6705BF36C37F","project":"0002","model":"0003","production":"0002","timestamp":"1500000000","sign":"2b67361ba972020496f1a1b3df388499"}';
58 58 $postResult = Http::POST($url, $params);
59 59 echo $postResult;
60 60  
... ...
domain/device/Device.php
... ... @@ -2,9 +2,73 @@
2 2  
3 3 namespace domain\device;
4 4  
  5 +use Yii;
  6 +use common\helpers\Utils;
  7 +use domain\device\models\Device as DeviceModel;
5 8 use stdClass;
  9 +
6 10 class Device
7 11 {
  12 +
  13 +
  14 + /**
  15 + * @param $item
  16 + * @return DeviceModel
  17 + */
  18 + static function create($item)
  19 + {
  20 + $newDevice = new DeviceModel();
  21 + $newDevice->serial_no = $item['serial_no'];
  22 + $newDevice->mac = $item['mac'];
  23 + $newDevice->device_id = $item['device_id'];
  24 + $newDevice->batch_id = $item['batch_id'];
  25 + $newDevice->status = $item['status'];
  26 + $newDevice->has_re_auth = $item['has_re_auth'];
  27 + $newDevice->apply_at = $item['apply_at'];
  28 + $newDevice->auth_at = $item['auth_at'];
  29 + $newDevice->save();
  30 +
  31 + return $newDevice;
  32 + }
  33 +
  34 + /**
  35 + * @param $batchId
  36 + * @param $batchNo
  37 + * @param $deviceId
  38 + * @param $applyAt
  39 + * @param $hasReAuth
  40 + * @return DeviceModel
  41 + */
  42 + static function createWithMacSerialNo($batchId, $batchNo, $deviceId, $applyAt, $hasReAuth, $status)
  43 + {
  44 + $exitDeviceModel = DeviceModel::find();
  45 + $exitDeviceModel->where(['batch_id' => $batchId]);
  46 + $exitDeviceModel->orderBy('serial_no desc');
  47 + $exitDevice = $exitDeviceModel->one();
  48 + $serialNo = '';
  49 + if ($exitDevice) {
  50 + $numNo = mb_substr($exitDevice->serial_no, -4);
  51 + $no1 = hexdec($numNo);
  52 + $no =($no1 * 1) + 1;
  53 + $newNo = sprintf('%04X', $no);
  54 + $serialNo = $batchNo.$newNo;
  55 + }
  56 +
  57 + $macAddress = Utils::macGenerate();
  58 + $item = [
  59 + 'serial_no' => $serialNo,
  60 + 'mac' => $macAddress,
  61 + 'device_id' => $deviceId,
  62 + 'batch_id' => $batchId,
  63 + 'status' => $status,
  64 + 'has_re_auth' => $hasReAuth,
  65 + 'apply_at' => $applyAt,
  66 + 'auth_at' => time(),
  67 + ];
  68 +
  69 + return self::create($item);
  70 + }
  71 +
8 72 /**
9 73 * 序列号前缀,也是batchNo
10 74 * @param $manufacture
... ... @@ -49,6 +113,7 @@ class Device
49 113 $e->message = '';
50 114 $e->serial_no = '';
51 115 $e->mac = '';
  116 + $e->status = 0;
52 117 $tt = time();
53 118 $batchNo = self::getBatchNo($manufactureNo, $projectNo, $modelNo, $productionNo);
54 119 $batchModel = CreateBatchRepository::findOne(['batch_no' => $batchNo]);
... ... @@ -57,11 +122,34 @@ class Device
57 122 return $e;
58 123 }
59 124 $batchId = $batchModel->id;
60   - $count = DeviceRepository::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]);
61 125  
62   - if (($count *1) >= ($batchModel->num * 1)) {
  126 + $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'batch_id' => $batchId, 'is_delete' => 0]);
  127 + if ($deviceModel) {
  128 + if (DeviceStatus::HAS_AUTH == $deviceModel->status) {
  129 + $e->mac = $deviceModel->mac;
  130 + $e->serial_no = $deviceModel->serial_no;
  131 + $e->message = '授权成功';
  132 + $e->success = true;
  133 + $e->status = 5;
  134 + } elseif(DeviceStatus::NO_AUTH == $deviceModel->status) {
  135 + $deviceModel->status = DeviceStatus::HAS_AUTH;
  136 + $deviceModel->auth_at = time();
  137 + $deviceModel->save();
  138 + $e->mac = $deviceModel->mac;
  139 + $e->serial_no = $deviceModel->serial_no;
  140 + $e->message = '授权成功';
  141 + $e->success = true;
  142 + $e->status = 5;
  143 + } else {
  144 + $e->message = '授权失败';
  145 + }
  146 +
  147 + return $e;
  148 + }
  149 + $needRecord = DeviceRepository::checkNeedRecordFailRecord($batchId, $batchModel->num);
  150 +
  151 + if ($needRecord) {
63 152 // 超过了限制数,记录到另外一个表里面
64   - // to do 记录到表里面
65 153 $failRecord = DeviceAuthFailRepository::findOne(['device_id' => $deviceId, 'is_delete' => 0]);
66 154 if (!$failRecord) {
67 155 $item = [
... ... @@ -73,14 +161,32 @@ class Device
73 161 ];
74 162 DeviceAuthFail::create($item);
75 163 }
  164 +
76 165 $e->message = '授权失败';
77 166 return $e;
78 167 }
79 168  
80   -
81   - $deviceModel = DeviceRepository::findOne(['device_id' => $deviceId, 'batch_id' => $batchId, 'is_delete' => 0]);
82 169 if (empty($deviceModel)) {
83   - $newDeviceModel = DeviceRepository::findOne(['device_id'=> null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]);
  170 + $needGen = DeviceRepository::checkNeedAutoGen($batchId, $batchModel->num);
  171 + if ($needGen) {
  172 + $genDeviceModel = Device::createWithMacSerialNo($batchId, $batchNo, $deviceId, $tt, 0, DeviceStatus::HAS_AUTH);
  173 + if ($genDeviceModel) {
  174 + $e->message = '授权成功';
  175 + $e->success = true;
  176 + $e->serial_no = $genDeviceModel->serial_no;
  177 + $e->mac = $genDeviceModel->mac;
  178 + } else {
  179 + $e->message = '授权失败, 下次重试授权!';
  180 + $e->success = false;
  181 + }
  182 + return $e;
  183 + }
  184 + // 找到未空白未绑定的设备序列号
  185 + $newDeviceModel = DeviceRepository::findOne(['device_id' => null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]);
  186 + if (empty($newDeviceModel)) {
  187 + $e->message = '授权失败,系统参数有误';
  188 + return $e;
  189 + }
84 190 $newDeviceModel->device_id = $deviceId;
85 191 $newDeviceModel->status = DeviceStatus::HAS_AUTH;
86 192 $newDeviceModel->apply_at = $tt ;
... ... @@ -93,26 +199,9 @@ class Device
93 199  
94 200 return $e;
95 201 }
96   - if (DeviceStatus::HAS_AUTH == $deviceModel->status) {
97   - $e->message = '授权成功';
98   - $e->success = true;
99   - $e->serial_no = $deviceModel->serial_no;
100   - $e->mac = $deviceModel->mac;
101   - } elseif (DeviceStatus::FAIL_AUTH == $deviceModel->status) {
102   - $e->message = '授权失败';
103   - } elseif (DeviceStatus::NO_AUTH == $deviceModel->status) {
104   - // 删除之后恢复的数据
105   - $deviceModel->status = DeviceStatus::HAS_AUTH;
106   - $deviceModel->auth_at = $tt;
107   - $deviceModel->save();
108   - $e->message = '授权成功';
109   - $e->success = true;
110   - $e->serial_no = $deviceModel->serial_no;
111   - $e->mac = $deviceModel->mac;
112   - } else {
113   - $e->message = '授权失败!';
114   - }
115 202  
116 203 return $e;
117 204 }
  205 +
  206 +
118 207 }
119 208 \ No newline at end of file
... ...
domain/device/DeviceAuthFailRepository.php
... ... @@ -62,4 +62,17 @@ class DeviceAuthFailRepository
62 62  
63 63 return $all;
64 64 }
  65 +
  66 + /**
  67 + * @param $condition
  68 + * @return array|\yii\db\ActiveRecord[]
  69 + */
  70 + static function findAll($condition)
  71 + {
  72 + $deviceModel = DeviceAuthFailModel::find();
  73 + $deviceModel->where($condition);
  74 + $list = $deviceModel->all();
  75 +
  76 + return $list;
  77 + }
65 78 }
66 79 \ No newline at end of file
... ...
domain/device/DeviceRepository.php
... ... @@ -2,11 +2,21 @@
2 2  
3 3 namespace domain\device;
4 4  
  5 +use common\helpers\Log as AppLog;
5 6 use domain\device\models\CreateBatch as CreateBatchModel;
6 7 use domain\device\models\Device as DeviceModel;
  8 +use domain\device\models\DeviceStats;
7 9  
8 10 class DeviceRepository
9 11 {
  12 + const DEBUG = true;
  13 + private static function myLog($str)
  14 + {
  15 + if (false == self::DEBUG) {
  16 + return '';
  17 + }
  18 + AppLog::DEBUG($str);
  19 + }
10 20 /**
11 21 * @param $where
12 22 * @return array|\yii\db\ActiveRecord[]
... ... @@ -85,6 +95,62 @@ class DeviceRepository
85 95 $deviceModel = DeviceModel::find();
86 96 $deviceModel->where($condition);
87 97 $count = $deviceModel->count();
  98 + self::myLog('SQL:'.$deviceModel->createCommand()->rawSql);
88 99 return $count;
89 100 }
  101 +
  102 + /**
  103 + * 判断是否要登记到授权失败表
  104 + * @param $batchId
  105 + * @param $batchNum
  106 + * @return bool
  107 + */
  108 + static function checkNeedRecordFailRecord($batchId, $batchNum)
  109 + {
  110 + // 第1个情况,全部授权满了
  111 + $authCount = self::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]);
  112 +
  113 + if (($authCount *1) >= ($batchNum * 1)) {
  114 + return true;
  115 + }
  116 +
  117 + // 第2个情况,授权数目 + 删除后恢复数满了
  118 + $where = ['and',
  119 + ['=', 'batch_id', $batchId],
  120 + ['is not', 'device_id', null],
  121 + ['in', 'status', [DeviceStatus::NO_AUTH, DeviceStatus::FAIL_AUTH]],
  122 + ['=', 'is_delete', 0],
  123 + ];
  124 + $restoreCount = self::rowsCount($where);
  125 + if (($restoreCount *1 + $authCount) >= ($batchNum * 1)) {
  126 + return true;
  127 + }
  128 +
  129 + return false;
  130 + }
  131 +
  132 + /**
  133 + * 判断是否要自动产生序列号
  134 + * @param $batchId
  135 + * @param $batchNum
  136 + * @return bool
  137 + */
  138 + static function checkNeedAutoGen($batchId, $batchNum)
  139 + {
  140 + $where = [
  141 + 'and',
  142 + ['=', 'batch_id' ,$batchId],
  143 + ['=', 'is_delete', 0],
  144 + ['=', 'status', DeviceStatus::NO_AUTH],
  145 + ['is', 'device_id', null]
  146 + ];
  147 +
  148 + $authCount = self::rowsCount($where);
  149 + self::myLog('checkNeedAutoGen data:'.$authCount.' __'. $batchNum);
  150 + if (empty($authCount)) {
  151 + return true;
  152 + }
  153 +
  154 + return false;
  155 + }
90 156 }
91 157 \ No newline at end of file
... ...