tinyqc/app/Models/Qc/ResultCommentsModel.php

79 lines
2.4 KiB
PHP
Raw Normal View History

<?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();
}
/**
* 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);
}
}
}