168 lines
5.4 KiB
PHP
168 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Api;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\DictControlModel;
|
|
use App\Models\ControlTestModel;
|
|
use App\Models\DailyResultModel;
|
|
use App\Models\ResultModel;
|
|
use App\Models\ResultCommentModel;
|
|
|
|
class EntryApiController extends BaseController
|
|
{
|
|
protected $dictControlModel;
|
|
protected $controlTestModel;
|
|
protected $dailyResultModel;
|
|
protected $resultModel;
|
|
protected $commentModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->dictControlModel = new \App\Models\DictControlModel();
|
|
$this->controlTestModel = new \App\Models\ControlTestModel();
|
|
$this->dailyResultModel = new \App\Models\DailyResultModel();
|
|
$this->resultModel = new \App\Models\ResultModel();
|
|
$this->commentModel = new \App\Models\ResultCommentModel();
|
|
}
|
|
|
|
public function getControls()
|
|
{
|
|
try {
|
|
$date = $this->request->getGet('date');
|
|
$deptId = $this->request->getGet('deptId');
|
|
|
|
$rows = $this->dictControlModel->getActiveByDate($date, $deptId);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'fetch success',
|
|
'data' => $rows
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getTests()
|
|
{
|
|
try {
|
|
$controlId = $this->request->getGet('controlId');
|
|
|
|
$rows = $this->controlTestModel->getByControl($controlId);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'fetch success',
|
|
'data' => $rows
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function saveDaily()
|
|
{
|
|
$input = $this->request->getJSON(true);
|
|
|
|
try {
|
|
$resultData = [
|
|
'control_ref_id' => $input['controlId'] ?? 0,
|
|
'test_ref_id' => $input['testId'] ?? 0,
|
|
'resdate' => $input['resdate'] ?? date('Y-m-d'),
|
|
'resvalue' => $input['resvalue'] ?? '',
|
|
'rescomment' => $input['rescomment'] ?? '',
|
|
];
|
|
|
|
$this->dailyResultModel->saveResult($resultData);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'save success'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function saveMonthly()
|
|
{
|
|
$input = $this->request->getJSON(true);
|
|
|
|
try {
|
|
$controlId = $input['controlId'] ?? 0;
|
|
$testId = $input['testId'] ?? 0;
|
|
$dates = $input['dates'] ?? '';
|
|
$resvalues = $input['resvalue'] ?? [];
|
|
|
|
foreach ($resvalues as $day => $value) {
|
|
if (!empty($value)) {
|
|
$resultData = [
|
|
'control_ref_id' => $controlId,
|
|
'test_ref_id' => $testId,
|
|
'resdate' => $dates . '-' . str_pad($day, 2, '0', STR_PAD_LEFT),
|
|
'resvalue' => $value,
|
|
'rescomment' => '',
|
|
];
|
|
$this->resultModel->saveResult($resultData);
|
|
}
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'save success'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function saveComment()
|
|
{
|
|
$input = $this->request->getJSON(true);
|
|
|
|
try {
|
|
$commentData = [
|
|
'control_ref_id' => $input['controlId'] ?? 0,
|
|
'test_ref_id' => $input['testId'] ?? 0,
|
|
'commonth' => $input['commonth'] ?? '',
|
|
'comtext' => $input['comtext'] ?? '',
|
|
];
|
|
|
|
$this->commentModel->saveComment($commentData);
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'save success'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getMonthlyData()
|
|
{
|
|
$controlId = $this->request->getGet('controlId');
|
|
$testId = $this->request->getGet('testId');
|
|
$yearMonth = $this->request->getGet('yearMonth');
|
|
|
|
try {
|
|
$results = $this->resultModel->getByMonth($controlId, $testId, $yearMonth);
|
|
$comment = $this->commentModel->getByControlTestMonth($controlId, $testId, $yearMonth);
|
|
|
|
$formValues = [];
|
|
foreach ($results as $row) {
|
|
$day = (int)date('j', strtotime($row['resdate']));
|
|
$formValues[$day] = $row['resvalue'];
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 'success',
|
|
'message' => 'fetch success',
|
|
'data' => [
|
|
'formValues' => $formValues,
|
|
'comment' => $comment ? $comment['comtext'] : ''
|
|
]
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Something went wrong: ' . $e->getMessage());
|
|
}
|
|
}
|
|
}
|