2026-01-19 06:37:37 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Models\Qc;
|
|
|
|
|
|
|
|
|
|
use App\Models\BaseModel;
|
|
|
|
|
|
|
|
|
|
class ResultCommentsModel extends BaseModel {
|
|
|
|
|
protected $table = 'result_comments';
|
|
|
|
|
protected $primaryKey = 'result_comment_id';
|
|
|
|
|
protected $allowedFields = [
|
|
|
|
|
'control_id',
|
|
|
|
|
'test_id',
|
|
|
|
|
'comment_month',
|
|
|
|
|
'com_text',
|
|
|
|
|
'created_at',
|
|
|
|
|
'updated_at',
|
|
|
|
|
'deleted_at'
|
|
|
|
|
];
|
|
|
|
|
protected $useTimestamps = true;
|
|
|
|
|
protected $useSoftDeletes = true;
|
|
|
|
|
|
|
|
|
|
public function search($keyword = null) {
|
|
|
|
|
if ($keyword) {
|
|
|
|
|
return $this->groupStart()
|
|
|
|
|
->like('comment_month', $keyword)
|
|
|
|
|
->orLike('com_text', $keyword)
|
|
|
|
|
->groupEnd()
|
|
|
|
|
->findAll();
|
|
|
|
|
}
|
|
|
|
|
return $this->findAll();
|
|
|
|
|
}
|
2026-01-20 16:47:11 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get comments by control, test and month
|
|
|
|
|
*/
|
|
|
|
|
public function getByControlTestMonth(int $controlId, int $testId, string $month): ?array {
|
|
|
|
|
return $this->where('control_id', $controlId)
|
|
|
|
|
->where('test_id', $testId)
|
|
|
|
|
->where('comment_month', $month)
|
|
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->first();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all comments for a test and month
|
|
|
|
|
*/
|
|
|
|
|
public function getByTestMonth(int $testId, string $month): array {
|
|
|
|
|
return $this->where('test_id', $testId)
|
|
|
|
|
->where('comment_month', $month)
|
|
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->findAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Upsert comment (insert or update based on control/test/month)
|
|
|
|
|
*/
|
|
|
|
|
public function upsertComment(array $data): int {
|
|
|
|
|
$existing = $this->where('control_id', $data['control_id'])
|
|
|
|
|
->where('test_id', $data['test_id'])
|
|
|
|
|
->where('comment_month', $data['comment_month'])
|
|
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($existing) {
|
|
|
|
|
if (empty($data['com_text'])) {
|
|
|
|
|
// If text is empty, soft delete
|
|
|
|
|
$this->update($existing['result_comment_id'], ['deleted_at' => date('Y-m-d H:i:s')]);
|
|
|
|
|
return $existing['result_comment_id'];
|
|
|
|
|
}
|
|
|
|
|
$this->update($existing['result_comment_id'], $data);
|
|
|
|
|
return $existing['result_comment_id'];
|
|
|
|
|
} else {
|
|
|
|
|
if (empty($data['com_text'])) {
|
|
|
|
|
return 0; // Don't insert empty comments
|
|
|
|
|
}
|
|
|
|
|
return $this->insert($data, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-19 06:37:37 +07:00
|
|
|
}
|