|
|
@@ -734,34 +734,57 @@ class OrderCgClearClass extends BaseClass
|
|
|
return array_values(array_unique(array_filter(array_map('intval', $arr))));
|
|
|
}
|
|
|
|
|
|
- protected static function zeroAmountWhereSql($alias = '')
|
|
|
+ /**
|
|
|
+ * xhOrderCgClear.amount 是否视为 0(含 NULL、0.00)
|
|
|
+ */
|
|
|
+ protected static function isZeroAmountSql($alias = '')
|
|
|
{
|
|
|
$prefix = $alias !== '' ? $alias . '.' : '';
|
|
|
return '(' . $prefix . 'amount IS NULL OR ' . $prefix . 'amount = 0 OR ' . $prefix . 'amount = \'0\' OR ' . $prefix . 'amount = \'0.00\')';
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 只查 amount=0 的关系行(带 clearStyle,避免再查整单 xhClear)
|
|
|
- * @return array[] id, clearId, orderId, cgId, clearStyle
|
|
|
+ * 步骤1:只从 xhOrderCgClear 查 amount=0 的行
|
|
|
+ * @return array[] id, clearId, orderId, cgId
|
|
|
*/
|
|
|
public static function fetchZeroAmountRelationBatch($cursorId, $limit)
|
|
|
{
|
|
|
$relTable = self::$baseFile::tableName();
|
|
|
- $clearTable = Clear::tableName();
|
|
|
- $sql = 'SELECT r.id, r.clearId, r.orderId, r.cgId, c.clearStyle FROM ' . $relTable . ' r'
|
|
|
- . ' INNER JOIN ' . $clearTable . ' c ON c.id = r.clearId'
|
|
|
- . ' WHERE ' . self::zeroAmountWhereSql('r');
|
|
|
+ $sql = 'SELECT id, clearId, orderId, cgId FROM ' . $relTable
|
|
|
+ . ' WHERE ' . self::isZeroAmountSql();
|
|
|
$params = [];
|
|
|
if ($cursorId !== null) {
|
|
|
- $sql .= ' AND r.id < :cursorId';
|
|
|
+ $sql .= ' AND id < :cursorId';
|
|
|
$params[':cursorId'] = intval($cursorId);
|
|
|
}
|
|
|
- $sql .= ' ORDER BY r.id DESC LIMIT ' . intval($limit);
|
|
|
+ $sql .= ' ORDER BY id DESC LIMIT ' . intval($limit);
|
|
|
return Yii::$app->db->createCommand($sql)->bindValues($params)->queryAll();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 按关系行 orderId/cgId + clearStyle 反查应对应的 actPrice(不展开整单 saleIds/purchaseIds)
|
|
|
+ * 步骤2 辅助:批量取 clearStyle(仅处理步骤1命中的 clearId)
|
|
|
+ * @param int[] $clearIds
|
|
|
+ * @return array<int,int> clearId => clearStyle
|
|
|
+ */
|
|
|
+ public static function getClearStyleMap(array $clearIds)
|
|
|
+ {
|
|
|
+ $clearIds = array_values(array_unique(array_filter(array_map('intval', $clearIds))));
|
|
|
+ if (empty($clearIds)) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ $rows = Yii::$app->db->createCommand(
|
|
|
+ 'SELECT id, clearStyle FROM ' . Clear::tableName()
|
|
|
+ . ' WHERE id IN (' . implode(',', $clearIds) . ')'
|
|
|
+ )->queryAll();
|
|
|
+ $map = [];
|
|
|
+ foreach ($rows as $row) {
|
|
|
+ $map[intval($row['id'])] = intval($row['clearStyle'] ?? 0);
|
|
|
+ }
|
|
|
+ return $map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 步骤2:反查本行 amount 应写入的值(源单 actPrice)
|
|
|
*/
|
|
|
public static function resolveAmountForRelationRowDirect(array $row)
|
|
|
{
|
|
|
@@ -847,45 +870,91 @@ class OrderCgClearClass extends BaseClass
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 步骤1~3:xhOrderCgClear amount=0 → 反查应填金额 → 按 id 更新
|
|
|
* @param array[] $rows fetchZeroAmountRelationBatch 的结果
|
|
|
- * @return array{updated:int,skipped:int}
|
|
|
+ * @return array{updated:int,skipped:int,details:array[]}
|
|
|
*/
|
|
|
- public static function patchZeroAmountRows(array $rows, $dryRun = false)
|
|
|
+ public static function patchZeroAmountRows(array $rows, $dryRun = false, $logDetails = false)
|
|
|
{
|
|
|
if (empty($rows)) {
|
|
|
- return ['updated' => 0, 'skipped' => 0];
|
|
|
+ return ['updated' => 0, 'skipped' => 0, 'details' => []];
|
|
|
}
|
|
|
+
|
|
|
+ $clearIds = array_values(array_unique(array_filter(array_map('intval', array_column($rows, 'clearId')))));
|
|
|
+ $clearStyleMap = self::getClearStyleMap($clearIds);
|
|
|
+
|
|
|
$table = self::$baseFile::tableName();
|
|
|
$updated = 0;
|
|
|
$skipped = 0;
|
|
|
+ $details = [];
|
|
|
+
|
|
|
foreach ($rows as $row) {
|
|
|
$relId = intval($row['id'] ?? 0);
|
|
|
+ $clearId = intval($row['clearId'] ?? 0);
|
|
|
if ($relId <= 0) {
|
|
|
$skipped++;
|
|
|
continue;
|
|
|
}
|
|
|
+
|
|
|
+ $row['clearStyle'] = $clearStyleMap[$clearId] ?? 0;
|
|
|
$amount = self::resolveAmountForRelationRowDirect($row);
|
|
|
if ($amount === null) {
|
|
|
$skipped++;
|
|
|
+ if ($logDetails) {
|
|
|
+ $details[] = [
|
|
|
+ 'id' => $relId,
|
|
|
+ 'clearId' => $clearId,
|
|
|
+ 'orderId' => intval($row['orderId'] ?? 0),
|
|
|
+ 'cgId' => intval($row['cgId'] ?? 0),
|
|
|
+ 'status' => 'skip',
|
|
|
+ 'reason' => '反查不到有效 actPrice',
|
|
|
+ ];
|
|
|
+ }
|
|
|
continue;
|
|
|
}
|
|
|
+
|
|
|
if (!$dryRun) {
|
|
|
$affected = Yii::$app->db->createCommand(
|
|
|
- 'UPDATE ' . $table . ' SET amount = :amount WHERE id = :id AND ' . self::zeroAmountWhereSql()
|
|
|
+ 'UPDATE ' . $table . ' SET amount = :amount WHERE id = :id AND ' . self::isZeroAmountSql()
|
|
|
)->bindValues([
|
|
|
':amount' => $amount,
|
|
|
':id' => $relId,
|
|
|
])->execute();
|
|
|
if ($affected > 0) {
|
|
|
$updated++;
|
|
|
+ if ($logDetails) {
|
|
|
+ $details[] = [
|
|
|
+ 'id' => $relId,
|
|
|
+ 'clearId' => $clearId,
|
|
|
+ 'amount' => $amount,
|
|
|
+ 'status' => 'updated',
|
|
|
+ ];
|
|
|
+ }
|
|
|
} else {
|
|
|
$skipped++;
|
|
|
+ if ($logDetails) {
|
|
|
+ $details[] = [
|
|
|
+ 'id' => $relId,
|
|
|
+ 'status' => 'skip',
|
|
|
+ 'reason' => '更新时 amount 已非 0',
|
|
|
+ ];
|
|
|
+ }
|
|
|
}
|
|
|
} else {
|
|
|
$updated++;
|
|
|
+ if ($logDetails) {
|
|
|
+ $details[] = [
|
|
|
+ 'id' => $relId,
|
|
|
+ 'clearId' => $clearId,
|
|
|
+ 'orderId' => intval($row['orderId'] ?? 0),
|
|
|
+ 'cgId' => intval($row['cgId'] ?? 0),
|
|
|
+ 'amount' => $amount,
|
|
|
+ 'status' => 'would_update',
|
|
|
+ ];
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- return ['updated' => $updated, 'skipped' => $skipped];
|
|
|
+ return ['updated' => $updated, 'skipped' => $skipped, 'details' => $details];
|
|
|
}
|
|
|
|
|
|
}
|