diff --git a/controllers/DeviceController.php b/controllers/DeviceController.php index d7bcdfcf..21185fe0 100644 --- a/controllers/DeviceController.php +++ b/controllers/DeviceController.php @@ -347,10 +347,11 @@ class DeviceController extends Controller { } } - public function actionGetDataSync() { + public function actionSyncSchedule() { if (Yii::$app->request->post()) { Yii::$app->response->format = "json"; $device_id = Yii::$app->request->post("data"); + $deviceInfo = $this->findModel($device_id); $doors = Door::find()->andWhere(["device_id" => $device_id])->all(); $filter = ["OR"]; foreach ($doors as $key => $value) { @@ -362,20 +363,8 @@ class DeviceController extends Controller { foreach ($staffs as $key => $value) { $schedule[] = $value->schedule_id; } + $schedule_lists = array_unique($schedule); - $schedule = array_unique($schedule); - return [ - "IP" => $this->findModel($device_id)->ip_address, - "schedule" => $schedule - ]; - } - } - - public function actionSyncSchedule() { - if (Yii::$app->request->post()) { - Yii::$app->response->format = "json"; - $post = Yii::$app->request->post(); - $schedule_lists = $post["data"]; $TimeZoneInfos = []; foreach ($schedule_lists as $key => $value) { $schedule = Schedule::findOne($value); @@ -394,28 +383,29 @@ class DeviceController extends Controller { ]; } $res = json_decode(common::requestToCardService("/SetDeviceData/TimeZone", [ - "DeviceIP" => $post["ip"], + "DeviceIP" => $deviceInfo->ip_address, "TimeZoneInfos" => $TimeZoneInfos ]), true); return [ + "IP" => $deviceInfo->ip_address, "res" => $res, - "IP" => $post["ip"] + "staffs" => count($staffs) ]; } } - public function actionSyncStaffs() { + public function actionSyncStaff($page = 1) { if (Yii::$app->request->post()) { Yii::$app->response->format = "json"; - $post = Yii::$app->request->post(); - $device_id = Device::findOne(['ip_address' => $post['ip']])->id; + $device_id = Yii::$app->request->post("data"); + $deviceInfo = $this->findModel($device_id); $doors = Door::find()->andWhere(["device_id" => $device_id])->all(); $filter = ["OR"]; foreach ($doors as $key => $value) { $filter[] = ["LIKE", "door_access", '"' . $value->id . '"']; } - $staff_lists = Staff::find()->andWhere($filter)->all(); - + $offset = ($page - 1) * 200; + $staff_lists = Staff::find()->andWhere($filter)->limit(200)->offset($offset)->all(); $UserInfos = []; $UserAuthorizeInfos = []; @@ -441,17 +431,19 @@ class DeviceController extends Controller { } } } - $resUser = json_decode(common::requestToCardService("/SetDeviceData/User", [ - "DeviceIP" => $post["ip"], + json_decode(common::requestToCardService("/SetDeviceData/User", [ + "DeviceIP" => $deviceInfo->ip_address, "UserInfos" => $UserInfos ]), true); common::requestToCardService("/SetDeviceData/UserAuthorize", [ - "DeviceIP" => $post["ip"], + "DeviceIP" => $deviceInfo->ip_address, "UserAuthorizeInfos" => $UserAuthorizeInfos ]); return [ - "resUser" => $resUser, - "IP" => $post["ip"] + "IP" => $deviceInfo->ip_address, + "staff" => count($UserInfos), + "from" => $offset + 1, + "to" => $offset + count($UserInfos) ]; } } diff --git a/helpers/DeviceGrid.php b/helpers/DeviceGrid.php index b73bc949..ed29d67b 100644 --- a/helpers/DeviceGrid.php +++ b/helpers/DeviceGrid.php @@ -24,4 +24,10 @@ class DeviceGrid extends CommonGrid { }; } + public static function checkbox($type, $haveRoot = true) { + return function($model) use ($type) { + return ""; + }; + } + } diff --git a/views/device/index.tpl b/views/device/index.tpl index bf681f9f..f5b47415 100644 --- a/views/device/index.tpl +++ b/views/device/index.tpl @@ -6,6 +6,9 @@ {DeviceAsset::register($this)|void} {block name='content'}
+ + + @@ -61,7 +64,7 @@ 'format' => 'raw', 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center', 'style' => 'width:3%'], - 'value' => \app\helpers\CommonGrid::checkbox("device", false) + 'value' => \app\helpers\DeviceGrid::checkbox("device", false) ], 'name', 'serial', diff --git a/web/js/device.js b/web/js/device.js index de57235b..3e12e08c 100644 --- a/web/js/device.js +++ b/web/js/device.js @@ -248,10 +248,18 @@ function changeIP(e) { var progress = 0; var totals = 0; -function _sync(e) { +var index = 0; +var totalPage = 0; +var currentPage = 1; +var secondIncrement = 0; + +async function _sync(e) { var lists = []; $.each($("input[name='checkbox-device']:checked"), function () { - lists.push($(this).val()); + lists.push({ + id: $(this).val(), + ip: $(this).attr("data") + }); }); if (lists.length == 0) { alert("Vui lòng lựa chọn đối tượng!"); @@ -269,10 +277,8 @@ function _sync(e) { common.modalOpen(data.form, false, data.title); $("#close-modal").attr("disabled", true); $("#modalHeader").find("button").remove(); - totals = data.lists.length * 2; - for (var i = 0; i < data.lists.length; i++) { - getDataSync(data.lists[i]); - } + totals = data.lists.length; + syncSchedule(data.lists[index].id, data.lists[index].ip, data); }, error: function (jqXHR, textStatus, errorThrown) { common.modalBlock(false); @@ -281,6 +287,71 @@ function _sync(e) { }); } +function syncSchedule(device_id, device_ip, fullData) { + appendLogs("[" + device_ip + "] Bắt đầu đồng bộ dữ liệu tới thiết bị", "blue", "minus"); + $.ajax({ + url: $("input[name='URL_sync_schedule']").val(), + type: 'POST', + data: { + data: device_id + }, + success: function (data) { + var totalsStaff = data.staffs; + totalPage = parseInt(totalsStaff / 200); + if (totalsStaff % 200 > 0) + totalPage++; + secondIncrement = 1 / (totalPage + 1); + if (data.res.ErrorCode == "-1") { + appendLogs("[" + data.IP + "] Đồng bộ lịch trình tới thiết bị thất bại", "red", "remove"); + index++; + if (index < totals) + syncSchedule(fullData.lists[index].id, fullData.lists[index].ip, fullData); + else { + index = 0; + processProgress(1); + } + } else { + appendLogs("[" + data.IP + "] Đồng bộ lịch trình tới thiết bị thành công", "green", "check"); + syncStaffs(device_id, device_ip, currentPage, fullData); + processProgress(secondIncrement); + } + }, + error: function (jqXHR, textStatus, errorThrown) { + common.ajaxError(); + } + }); +} + +function syncStaffs(device_id, device_ip, page, fullData) { + appendLogs("[" + device_ip + "] Đồng bộ dữ liệu nhân viên tới thiết bị", "", "minus"); + $.ajax({ + url: $("input[name='URL_sync_staff']").val() + "?page=" + page, + type: 'POST', + data: { + data: device_id + }, + success: function (data) { + appendLogs("[" + data.IP + "] Đồng bộ dữ liệu " + data.staff + " nhân viên từ " + data.from + " đến " + data.to + " tới thiết bị", "green", "check"); + processProgress(secondIncrement); + currentPage++; + if (currentPage <= totalPage) + syncStaffs(device_id, device_ip, currentPage, fullData); + else { + currentPage = 1; + index++; + if (index < totals) + syncSchedule(fullData.lists[index].id, fullData.lists[index].ip, fullData); + else { + index = 0; + } + } + }, + error: function (jqXHR, textStatus, errorThrown) { + common.ajaxError(); + } + }); +} + function getDataSync(data) { $.ajax({ url: $("input[name='get_data_sync_url']").val(), @@ -315,72 +386,6 @@ function getDataSync(data) { }); } -function syncSchedule(schedule, ip) { - $.ajax({ - url: $("input[name='sync_schedule_url']").val(), - type: 'POST', - data: { - data: schedule, - ip: ip - }, - success: function (data) { - console.log("sync schedule: ", data.res); - var html = ""; - html = " Đồng bộ lịch trình tới thiết bị " + data.IP + ".
"; - progress++; - if (data.res.ErrorCode == "-1") { - html = " Đồng bộ tới thiết bị " + data.IP + " thất bại.
"; - progress++; - } else { - syncStaffs(ip); - } - $("#logs-response").append(html); - var percent = parseInt(progress / totals * 100); - $("#progress").attr("aria-valuenow", percent); - $("#progress").attr("style", "width: " + percent + "%"); - $("#progress").html(percent + "%"); - if (percent >= 100) { - progress = 0; - $("#close-modal").attr("disabled", false); - } - }, - error: function (jqXHR, textStatus, errorThrown) { - common.ajaxError(); - } - }); -} - -function syncStaffs(ip) { - $.ajax({ - url: $("input[name='sync_staffs_url']").val(), - type: 'POST', - data: { - ip: ip - }, - success: function (data) { - console.log("sync user: ", data.resUser); - var html = ""; - html = " Đồng bộ nhân viên tới thiết bị " + data.IP + ".
"; - if (data.res.ErrorCode == "-1") { - html = " Đồng bộ nhân viên tới thiết bị " + data.IP + " thất bại.
"; - } - $("#logs-response").append(html); - progress++; - var percent = parseInt(progress / totals * 100); - $("#progress").attr("aria-valuenow", percent); - $("#progress").attr("style", "width: " + percent + "%"); - $("#progress").html(percent + "%"); - if (percent >= 100) { - progress = 0; - $("#close-modal").attr("disabled", false); - } - }, - error: function (jqXHR, textStatus, errorThrown) { - common.ajaxError(); - } - }); -} - var progressLogs = 0; var totalsLogs = 0; function _getLogs(e) { @@ -443,3 +448,21 @@ function syncLogs(data) { }); } +function appendLogs(text, cls, icon) { + var html = " " + text + "
"; + $("#logs-response").prepend(html); +} + +function processProgress(ai) { + progress = progress + ai; + var percent = parseInt(progress / totals * 100); + if (percent >= 98) + percent = 100; + $("#progress").attr("aria-valuenow", percent); + $("#progress").attr("style", "width: " + percent + "%"); + $("#progress").html(percent + "%"); + if (percent >= 100) { + progress = 0; + $("#close-modal").attr("disabled", false); + } +}