làm lại quy trình đồng bộ dữ liệu

This commit is contained in:
dongpd 2020-10-20 18:07:57 +07:00
parent 9719e836f8
commit 9112eaaff9
4 changed files with 123 additions and 99 deletions

View File

@ -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)
];
}
}

View File

@ -24,4 +24,10 @@ class DeviceGrid extends CommonGrid {
};
}
public static function checkbox($type, $haveRoot = true) {
return function($model) use ($type) {
return "<input type='checkbox' value='{$model->id}' data='{$model->ip_address}' name='checkbox-{$type}' class='checkbox-{$type}'>";
};
}
}

View File

@ -6,6 +6,9 @@
{DeviceAsset::register($this)|void}
{block name='content'}
<div class="device-index">
<input type='hidden' name='URL_sync_schedule' value="{Url::to(['sync-schedule'])}">
<input type='hidden' name='URL_sync_staff' value="{Url::to(['sync-staff'])}">
<input type='hidden' name='get_data_sync_url' value="{Url::to(['get-data-sync'])}">
<input type='hidden' name='sync_schedule_url' value="{Url::to(['sync-schedule'])}">
<input type='hidden' name='sync_staffs_url' value="{Url::to(['sync-staffs'])}">
@ -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',

View File

@ -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 = "<span class='text-green'><i class='fa fa-check'></i> Đồng bộ lịch trình tới thiết bị <b>" + data.IP + "</b>.</span><br>";
progress++;
if (data.res.ErrorCode == "-1") {
html = "<span class='text-red'><i class='fa fa-remove'></i> Đồng bộ tới thiết bị <b>" + data.IP + "</b> thất bại.</span><br>";
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 = "<span class='text-green'><i class='fa fa-check'></i> Đồng bộ nhân viên tới thiết bị <b>" + data.IP + "</b>.</span><br>";
if (data.res.ErrorCode == "-1") {
html = "<span class='text-red'><i class='fa fa-remove'></i> Đồng bộ nhân viên tới thiết bị <b>" + data.IP + "</b> thất bại.</span><br>";
}
$("#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 = "<span class='text-" + cls + "'><i class='fa fa-" + icon + "'></i> " + text + "</span><br>";
$("#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);
}
}