| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Products\Discount; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
class DiscountTaskModel |
| 7 |
{ |
| 8 |
private $wpdb; |
| 9 |
private $tableName; |
| 10 |
|
| 11 |
public const STATUS_PENDING = 'pending'; |
| 12 |
public const STATUS_PROCESSING = 'processing'; |
| 13 |
public const STATUS_COMPLETED = 'completed'; |
| 14 |
public const STATUS_FAILED = 'failed'; |
| 15 |
|
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
global $wpdb; |
| 19 |
$this->wpdb = $wpdb; |
| 20 |
$this->tableName = $wpdb->prefix . 'sync_basalam_discount_tasks'; |
| 21 |
} |
| 22 |
|
| 23 |
public function create(array $data) |
| 24 |
{ |
| 25 |
$productId = $data['product_id'] ?? null; |
| 26 |
$variationId = $data['variation_id'] ?? null; |
| 27 |
$discountPercent = $data['discount_percent'] ?? 0; |
| 28 |
$activeDays = $data['active_days'] ?? 7; |
| 29 |
$action = $data['action']; |
| 30 |
$scheduledAt = $data['scheduled_at'] ?? current_time('mysql'); |
| 31 |
$status = $data['status'] ?? self::STATUS_PENDING; |
| 32 |
|
| 33 |
$result = $this->wpdb->insert( |
| 34 |
$this->tableName, |
| 35 |
[ |
| 36 |
'product_id' => $productId, |
| 37 |
'variation_id' => $variationId, |
| 38 |
'discount_percent' => $discountPercent, |
| 39 |
'active_days' => $activeDays, |
| 40 |
'action' => $action, |
| 41 |
'status' => $status, |
| 42 |
'scheduled_at' => $scheduledAt, |
| 43 |
'created_at' => current_time('mysql'), |
| 44 |
], |
| 45 |
['%s', '%s', '%f', '%d', '%s', '%s', '%s', '%s'] |
| 46 |
); |
| 47 |
|
| 48 |
return $result ? $this->wpdb->insert_id : false; |
| 49 |
} |
| 50 |
|
| 51 |
public function getPendingTasks() |
| 52 |
{ |
| 53 |
$sql = $this->wpdb->prepare( |
| 54 |
"SELECT * FROM {$this->tableName} |
| 55 |
WHERE status = %s |
| 56 |
AND scheduled_at <= %s |
| 57 |
ORDER BY scheduled_at ASC", |
| 58 |
self::STATUS_PENDING, |
| 59 |
current_time('mysql') |
| 60 |
); |
| 61 |
|
| 62 |
return $this->wpdb->get_results($sql); |
| 63 |
} |
| 64 |
|
| 65 |
public function getTasksByDiscountPercent($discountPercent) |
| 66 |
{ |
| 67 |
$sql = $this->wpdb->prepare( |
| 68 |
"SELECT * FROM {$this->tableName} |
| 69 |
WHERE status = %s |
| 70 |
AND discount_percent = %f |
| 71 |
AND scheduled_at <= %s |
| 72 |
ORDER BY created_at ASC", |
| 73 |
self::STATUS_PENDING, |
| 74 |
$discountPercent, |
| 75 |
current_time('mysql') |
| 76 |
); |
| 77 |
|
| 78 |
return $this->wpdb->get_results($sql); |
| 79 |
} |
| 80 |
|
| 81 |
public function getGroupedPendingTasks() |
| 82 |
{ |
| 83 |
$sql = $this->wpdb->prepare( |
| 84 |
"SELECT discount_percent, active_days, action, COUNT(*) as count, |
| 85 |
GROUP_CONCAT(DISTINCT id) as task_ids, |
| 86 |
GROUP_CONCAT(DISTINCT product_id) as product_ids, |
| 87 |
GROUP_CONCAT(DISTINCT variation_id) as variation_ids |
| 88 |
FROM {$this->tableName} |
| 89 |
WHERE status = %s |
| 90 |
AND scheduled_at <= %s |
| 91 |
GROUP BY discount_percent, active_days, action |
| 92 |
ORDER BY action ASC, discount_percent ASC", |
| 93 |
self::STATUS_PENDING, |
| 94 |
current_time('mysql') |
| 95 |
); |
| 96 |
|
| 97 |
return $this->wpdb->get_results($sql); |
| 98 |
} |
| 99 |
|
| 100 |
public function getRunnableTasks() |
| 101 |
{ |
| 102 |
$sql = $this->wpdb->prepare( |
| 103 |
"SELECT discount_percent, active_days, action, COUNT(*) as count, |
| 104 |
GROUP_CONCAT(DISTINCT id) as task_ids, |
| 105 |
GROUP_CONCAT(DISTINCT product_id) as product_ids, |
| 106 |
GROUP_CONCAT(DISTINCT variation_id) as variation_ids |
| 107 |
FROM {$this->tableName} |
| 108 |
WHERE status = %s |
| 109 |
AND scheduled_at <= %s |
| 110 |
GROUP BY discount_percent, active_days, action |
| 111 |
ORDER BY action ASC, scheduled_at ASC, discount_percent ASC |
| 112 |
LIMIT 1", |
| 113 |
self::STATUS_PENDING, |
| 114 |
current_time('mysql') |
| 115 |
); |
| 116 |
|
| 117 |
return $this->wpdb->get_row($sql); |
| 118 |
} |
| 119 |
|
| 120 |
public function updateStatus($id, $status, $errorMessage = null) |
| 121 |
{ |
| 122 |
$data = [ |
| 123 |
'status' => $status, |
| 124 |
]; |
| 125 |
|
| 126 |
$format = ['%s']; |
| 127 |
|
| 128 |
if ($status === self::STATUS_COMPLETED || $status === self::STATUS_FAILED) { |
| 129 |
$data['processed_at'] = current_time('mysql'); |
| 130 |
$format[] = '%s'; |
| 131 |
} |
| 132 |
|
| 133 |
if ($errorMessage) { |
| 134 |
$data['error_message'] = $errorMessage; |
| 135 |
$format[] = '%s'; |
| 136 |
} |
| 137 |
|
| 138 |
return $this->wpdb->update( |
| 139 |
$this->tableName, |
| 140 |
$data, |
| 141 |
['id' => $id], |
| 142 |
$format, |
| 143 |
['%d'] |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
public function updateMultipleStatus($ids, $status, $errorMessage = null) |
| 148 |
{ |
| 149 |
if (empty($ids)) return false; |
| 150 |
|
| 151 |
$idsPlaceholders = implode(',', array_fill(0, count($ids), '%d')); |
| 152 |
|
| 153 |
$dataParts = ["status = %s"]; |
| 154 |
$prepareValues = array_merge([$status], $ids); |
| 155 |
|
| 156 |
if ($status === self::STATUS_COMPLETED || $status === self::STATUS_FAILED) { |
| 157 |
$dataParts[] = "processed_at = %s"; |
| 158 |
array_splice($prepareValues, 1, 0, current_time('mysql')); |
| 159 |
} |
| 160 |
|
| 161 |
if ($errorMessage) { |
| 162 |
$dataParts[] = "error_message = %s"; |
| 163 |
array_splice($prepareValues, -count($ids), 0, $errorMessage); |
| 164 |
} |
| 165 |
|
| 166 |
$sql = $this->wpdb->prepare( |
| 167 |
"UPDATE {$this->tableName} |
| 168 |
SET " . implode(', ', $dataParts) . " |
| 169 |
WHERE id IN ($idsPlaceholders)", |
| 170 |
$prepareValues |
| 171 |
); |
| 172 |
|
| 173 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom plugin table; identifier from $wpdb->prefix and generated %d/%s placeholders, not user input; values are prepared. |
| 174 |
return $this->wpdb->query($sql); |
| 175 |
} |
| 176 |
|
| 177 |
public function deleteMultipleTasks($ids) |
| 178 |
{ |
| 179 |
if (empty($ids)) return false; |
| 180 |
|
| 181 |
$idsPlaceholders = implode(',', array_fill(0, count($ids), '%d')); |
| 182 |
|
| 183 |
$sql = $this->wpdb->prepare( |
| 184 |
"DELETE FROM {$this->tableName} |
| 185 |
WHERE id IN ($idsPlaceholders)", |
| 186 |
$ids |
| 187 |
); |
| 188 |
|
| 189 |
return $this->wpdb->query($sql); |
| 190 |
} |
| 191 |
|
| 192 |
public function getTaskById($id) |
| 193 |
{ |
| 194 |
$sql = $this->wpdb->prepare( |
| 195 |
"SELECT * FROM {$this->tableName}WHERE id = %d", |
| 196 |
$id |
| 197 |
); |
| 198 |
|
| 199 |
return $this->wpdb->get_row($sql); |
| 200 |
} |
| 201 |
|
| 202 |
public function getTasksCountByStatus($status = null) |
| 203 |
{ |
| 204 |
if ($status) { |
| 205 |
$sql = $this->wpdb->prepare( |
| 206 |
"SELECT COUNT(*) FROM {$this->tableName} WHERE status = %s", |
| 207 |
$status |
| 208 |
); |
| 209 |
} else { |
| 210 |
$sql = "SELECT COUNT(*) FROM {$this->tableName}"; |
| 211 |
} |
| 212 |
|
| 213 |
return $this->wpdb->get_var($sql); |
| 214 |
} |
| 215 |
} |
| 216 |
|