update api open door

This commit is contained in:
2022-10-18 08:38:54 +07:00
parent d6d7dc3719
commit c8e0bb7795
4 changed files with 103 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ use yii\helpers\FileHelper;
use app\models\CaptureLogs;
use app\models\ListManagement;
use app\models\common;
use app\models\Schedule;
/**
* CardController implements the CRUD actions for Card model.
@@ -582,6 +583,12 @@ class ApiController extends Controller {
if ($value['action'] == "remove_info")
$result = $this->removeInfo($value);
if ($value['action'] == "insert_door_open")
$result = $this->insertSchedule($value);
if ($value['action'] == "remove_door_open")
$result = $this->removeSchedule($value);
if ($value['action'] == "reset_all") {
\Yii::$app->db->createCommand()->truncateTable('capture_logs')->execute();
\Yii::$app->db->createCommand()->truncateTable('list_management')->execute();
@@ -592,7 +599,7 @@ class ApiController extends Controller {
if (Yii::$app->params['autoSyncLog'])
file_put_contents("logs.txt", $count . "\t" . $value['action'] . "\t" . $value['files_name'][0] . "\t" . ($result ? "true" : "false") . "\t" . $processTime . "\t" . $value['name'] . "\n", FILE_APPEND);
if (in_array($value['action'], ['insert_image', 'remove_image', 'insert_info', 'remove_info', 'reset_all'])) {
if (in_array($value['action'], ['insert_image', 'remove_image', 'insert_info', 'remove_info', 'reset_all', 'insert_door_open', 'remove_door_open'])) {
if ($result)
$infomation[] = [
"obj_Log" => $value['obj_Log'],
@@ -764,4 +771,92 @@ class ApiController extends Controller {
return false;
}
public function insertSchedule($data) {
$model = new Schedule();
$info = ListManagement::findOne(['staff_id' => $data['id']]);
$schedule = Schedule::findOne(['staff_id' => $info->id, 'id_door_calendar' => $data['id_door_calendar']]);
if ($schedule) {
$schedule->from_time = $data['time_in'];
$schedule->to_time = $data['time_out'];
$schedule->from_date = $this->formatDateSchedule($data['day_in']);
$schedule->to_date = $this->formatDateSchedule($data['day_out']);
$schedule->date_of_week = $this->formatDayOfWeekSchedule($data['day_of_week']);
return $schedule->save();
} else {
return $model->create([
'staff_id' => $info->id,
'from_time' => $data['time_in'],
'to_time' => $data['time_out'],
'from_date' => $this->formatDateSchedule($data['day_in']),
'to_date' => $this->formatDateSchedule($data['day_out']),
'date_of_week' => $this->formatDayOfWeekSchedule($data['day_of_week']),
'id_door_calendar' => $data['id_door_calendar']
]);
}
}
public function removeSchedule($data) {
$info = ListManagement::findOne(['staff_id' => $data['id']]);
if ($info) {
$schedule = \app\models\Schedule::findOne([
'id_door_calendar' => $data['id_door_calendar'],
'staff_id' => $info->id
]);
if ($schedule)
$schedule->delete();
}
return true;
}
public function formatDateSchedule($date) {
$temp = explode(" ", $date);
$parse = explode("-", $temp[0]);
return $parse[2] . "/" . $parse[1] . "/" . $parse[0];
}
public function formatDayOfWeekSchedule($dayOfWeek) {
$days = [];
$temp = explode("|", $dayOfWeek);
foreach ($temp as $key => $value) {
if ($value != "")
$days[] = intval($value) + 2;
}
return implode(",", $days);
}
public function actionCheckOpenDoor() {
if (Yii::$app->request->post()) {
$post = Yii::$app->request->bodyParams;
$info = ListManagement::findOne(["staff_id" => $post["object_id"]]);
$timeGet = date_format(date_create_from_format('Y-m-d H:i:s', $post['timeget']), 'U');
$openDoor = 0;
if ($info) {
$schedules = Schedule::find()->andWhere(["staff_id" => $info->id])->all();
foreach ($schedules as $key => $value) {
$fromDate = date_format(date_create_from_format('H:i:s d/m/Y', "00:00:00 " . $value->from_date), 'U');
$toDate = date_format(date_create_from_format('H:i:s d/m/Y', "23:59:59 " . $value->to_date), 'U');
if ($timeGet > $fromDate && $timeGet < $toDate) {
$dateOfWeek = explode(",", $value->date_of_week);
$weekDay = intval(date("w", $timeGet)) + 1;
if ($weekDay == 1)
$weekDay = 8;
if (in_array($weekDay, $dateOfWeek)) {
$fromTime = date_format(date_create_from_format('H:i:s d/m/Y', $value->from_time . ":00 " . date("d/m/Y", $timeGet)), 'U');
$toTime = date_format(date_create_from_format('H:i:s d/m/Y', $value->to_time . ":59 " . date("d/m/Y", $timeGet)), 'U');
if ($timeGet > $fromTime && $timeGet < $toTime)
$openDoor = 1;
}
}
}
}
Yii::$app->response->format = "json";
return [
"status" => 10000,
"data" => [
"open_door" => $openDoor
]
];
}
}
}