tinyqc/app/Controllers/Api/EntryApiController.php

253 lines
8.8 KiB
PHP
Raw Normal View History

<?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;
$yearMonth = $input['yearMonth'] ?? '';
$operation = $input['operation'] ?? 'replace';
$tests = $input['tests'] ?? [];
$results = [];
$statistics = [];
$validations = [];
foreach ($tests as $testData) {
$testId = $testData['testId'] ?? 0;
$resvalues = $testData['resvalue'] ?? [];
$rescomments = $testData['rescomment'] ?? [];
$controlTest = $this->controlTestModel->getByControlAndTest($controlId, $testId);
$mean = $controlTest['mean'] ?? 0;
$sd = $controlTest['sd'] ?? 0;
$sdLimit = $sd > 0 ? $sd * 2 : 0;
$testValues = [];
$validCount = 0;
$validSum = 0;
$validSqSum = 0;
foreach ($resvalues as $day => $value) {
if (!empty($value)) {
$resdate = $yearMonth . '-' . str_pad($day, 2, '0', STR_PAD_LEFT);
$rescomment = $rescomments[$day] ?? '';
$resultData = [
'control_ref_id' => $controlId,
'test_ref_id' => $testId,
'resdate' => $resdate,
'resvalue' => $value,
'rescomment' => $rescomment,
];
if ($operation === 'replace') {
$this->resultModel->saveResult($resultData);
} else {
$existing = $this->resultModel->checkExisting($controlId, $testId, $resultData['resdate']);
if (!$existing) {
$this->resultModel->saveResult($resultData);
}
}
$numValue = (float) $value;
$testValues[] = $numValue;
$validCount++;
$validSum += $numValue;
$validSqSum += $numValue * $numValue;
$withinLimit = true;
if ($sdLimit > 0) {
$withinLimit = abs($numValue - $mean) <= $sdLimit;
}
if (!$withinLimit) {
$validations[] = [
'testId' => $testId,
'day' => $day,
'value' => $value,
'mean' => $mean,
'sd' => $sd,
'status' => 'out_of_range'
];
}
}
}
if ($validCount > 0) {
$calcMean = $validSum / $validCount;
$calcSd = 0;
if ($validCount > 1) {
$variance = ($validSqSum - ($validSum * $validSum) / $validCount) / ($validCount - 1);
$calcSd = $variance > 0 ? sqrt($variance) : 0;
}
$cv = $calcMean > 0 ? ($calcSd / $calcMean) * 100 : 0;
$statistics[] = [
'testId' => $testId,
'n' => $validCount,
'mean' => round($calcMean, 3),
'sd' => round($calcSd, 3),
'cv' => round($cv, 2)
];
}
$results[] = [
'testId' => $testId,
'saved' => count($resvalues)
];
}
return $this->respond([
'status' => 'success',
'message' => 'save success',
'data' => [
'results' => $results,
'statistics' => $statistics,
'validations' => $validations
]
]);
} 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 = [];
$comments = [];
foreach ($results as $row) {
$day = (int)date('j', strtotime($row['resdate']));
$formValues[$day] = $row['resvalue'];
if (!empty($row['rescomment'])) {
$comments[$day] = $row['rescomment'];
}
}
return $this->respond([
'status' => 'success',
'message' => 'fetch success',
'data' => [
'formValues' => $formValues,
'comments' => $comments,
'comment' => $comment ? $comment['comtext'] : ''
]
], 200);
} catch (\Exception $e) {
return $this->failServerError('Something went wrong: ' . $e->getMessage());
}
}
}