|
|
@@ -740,104 +740,114 @@ class OrderCgClearClass extends BaseClass
|
|
|
return '(' . $prefix . 'amount IS NULL OR ' . $prefix . 'amount = 0 OR ' . $prefix . 'amount = \'0\' OR ' . $prefix . 'amount = \'0.00\')';
|
|
|
}
|
|
|
|
|
|
- protected static function pairAmountKey($orderId, $cgId)
|
|
|
- {
|
|
|
- return intval($orderId) . '_' . intval($cgId);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * @return array<string,string> orderId_cgId => amount
|
|
|
+ * 只查 amount=0 的关系行(带 clearStyle,避免再查整单 xhClear)
|
|
|
+ * @return array[] id, clearId, orderId, cgId, clearStyle
|
|
|
*/
|
|
|
- public static function buildPairAmountMap($clear)
|
|
|
+ public static function fetchZeroAmountRelationBatch($cursorId, $limit)
|
|
|
{
|
|
|
- $pairs = self::buildPairsFromClear($clear);
|
|
|
- $map = [];
|
|
|
- foreach ($pairs as $pair) {
|
|
|
- $amount = self::normalizeAmount($pair['amount'] ?? 0);
|
|
|
- if (bccomp($amount, '0', 2) <= 0) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- $map[self::pairAmountKey($pair['orderId'] ?? 0, $pair['cgId'] ?? 0)] = $amount;
|
|
|
+ $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');
|
|
|
+ $params = [];
|
|
|
+ if ($cursorId !== null) {
|
|
|
+ $sql .= ' AND r.id < :cursorId';
|
|
|
+ $params[':cursorId'] = intval($cursorId);
|
|
|
}
|
|
|
- return $map;
|
|
|
+ $sql .= ' ORDER BY r.id DESC LIMIT ' . intval($limit);
|
|
|
+ return Yii::$app->db->createCommand($sql)->bindValues($params)->queryAll();
|
|
|
}
|
|
|
|
|
|
- public static function resolveAmountForRelationRow(array $row, array $pairAmountMap)
|
|
|
+ /**
|
|
|
+ * 按关系行 orderId/cgId + clearStyle 反查应对应的 actPrice(不展开整单 saleIds/purchaseIds)
|
|
|
+ */
|
|
|
+ public static function resolveAmountForRelationRowDirect(array $row)
|
|
|
{
|
|
|
+ $clearStyle = intval($row['clearStyle'] ?? 0);
|
|
|
$orderId = intval($row['orderId'] ?? 0);
|
|
|
$cgId = intval($row['cgId'] ?? 0);
|
|
|
- $key = self::pairAmountKey($orderId, $cgId);
|
|
|
- if (isset($pairAmountMap[$key])) {
|
|
|
- return $pairAmountMap[$key];
|
|
|
- }
|
|
|
- if ($cgId === 0 && $orderId > 0) {
|
|
|
- $legacyKey = self::pairAmountKey(0, $orderId);
|
|
|
- if (isset($pairAmountMap[$legacyKey])) {
|
|
|
- return $pairAmountMap[$legacyKey];
|
|
|
+ $gys2KmGys = intval(dict::getDict('clearStyle', 'gys2KmGys'));
|
|
|
+ $gys2Hd = intval(dict::getDict('clearStyle', 'gys2Hd'));
|
|
|
+ $hd2Gys = intval(dict::getDict('clearStyle', 'hd2Gys'));
|
|
|
+
|
|
|
+ if ($clearStyle === $gys2KmGys) {
|
|
|
+ $ghsCgId = $cgId > 0 ? $cgId : $orderId;
|
|
|
+ return self::amountFromGhsCgOrderId($ghsCgId);
|
|
|
+ }
|
|
|
+ if ($clearStyle === $gys2Hd && $orderId > 0) {
|
|
|
+ $amount = self::amountFromGhsOrderId($orderId);
|
|
|
+ if ($amount !== null) {
|
|
|
+ return $amount;
|
|
|
}
|
|
|
- $cg = PurchaseOrderClass::getById($orderId);
|
|
|
- if (!empty($cg)) {
|
|
|
- $amount = self::resolveDebtAmount($cg);
|
|
|
- if (bccomp($amount, '0', 2) > 0) {
|
|
|
- return $amount;
|
|
|
- }
|
|
|
+ }
|
|
|
+ if ($clearStyle === $hd2Gys && $cgId > 0) {
|
|
|
+ $amount = self::amountFromHdPurchaseId($cgId);
|
|
|
+ if ($amount !== null) {
|
|
|
+ return $amount;
|
|
|
}
|
|
|
}
|
|
|
if ($orderId > 0) {
|
|
|
- $order = OrderClass::getById($orderId);
|
|
|
- if (!empty($order)) {
|
|
|
- $amount = self::resolveDebtAmount($order);
|
|
|
- if (bccomp($amount, '0', 2) > 0) {
|
|
|
- return $amount;
|
|
|
- }
|
|
|
+ $amount = self::amountFromGhsOrderId($orderId);
|
|
|
+ if ($amount !== null) {
|
|
|
+ return $amount;
|
|
|
}
|
|
|
}
|
|
|
if ($cgId > 0) {
|
|
|
- $purchase = PurchaseClass::getById($cgId);
|
|
|
- if (!empty($purchase)) {
|
|
|
- $amount = self::resolveDebtAmount($purchase);
|
|
|
- if (bccomp($amount, '0', 2) > 0) {
|
|
|
- return $amount;
|
|
|
- }
|
|
|
- }
|
|
|
- $ghsCg = PurchaseOrderClass::getById($cgId);
|
|
|
- if (!empty($ghsCg)) {
|
|
|
- $amount = self::resolveDebtAmount($ghsCg);
|
|
|
- if (bccomp($amount, '0', 2) > 0) {
|
|
|
- return $amount;
|
|
|
- }
|
|
|
+ $amount = self::amountFromHdPurchaseId($cgId);
|
|
|
+ if ($amount !== null) {
|
|
|
+ return $amount;
|
|
|
}
|
|
|
+ return self::amountFromGhsCgOrderId($cgId);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public static function countZeroAmountRelationRows()
|
|
|
+ protected static function amountFromGhsOrderId($orderId)
|
|
|
{
|
|
|
- $table = self::$baseFile::tableName();
|
|
|
- return intval(Yii::$app->db->createCommand(
|
|
|
- 'SELECT COUNT(*) FROM ' . $table . ' WHERE ' . self::zeroAmountWhereSql()
|
|
|
- )->queryScalar());
|
|
|
+ $orderId = intval($orderId);
|
|
|
+ if ($orderId <= 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ $order = OrderClass::getById($orderId);
|
|
|
+ return self::amountFromEntity($order);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @return array[] id, clearId, orderId, cgId
|
|
|
- */
|
|
|
- public static function fetchZeroAmountRelationBatch($cursorId, $limit)
|
|
|
+ protected static function amountFromHdPurchaseId($cgId)
|
|
|
{
|
|
|
- $table = self::$baseFile::tableName();
|
|
|
- $sql = 'SELECT id, clearId, orderId, cgId FROM ' . $table
|
|
|
- . ' WHERE ' . self::zeroAmountWhereSql();
|
|
|
- $params = [];
|
|
|
- if ($cursorId !== null) {
|
|
|
- $sql .= ' AND id < :cursorId';
|
|
|
- $params[':cursorId'] = intval($cursorId);
|
|
|
+ $cgId = intval($cgId);
|
|
|
+ if ($cgId <= 0) {
|
|
|
+ return null;
|
|
|
}
|
|
|
- $sql .= ' ORDER BY id DESC LIMIT ' . intval($limit);
|
|
|
- return Yii::$app->db->createCommand($sql)->bindValues($params)->queryAll();
|
|
|
+ $purchase = PurchaseClass::getById($cgId);
|
|
|
+ return self::amountFromEntity($purchase);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static function amountFromGhsCgOrderId($cgId)
|
|
|
+ {
|
|
|
+ $cgId = intval($cgId);
|
|
|
+ if ($cgId <= 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ $cg = PurchaseOrderClass::getById($cgId);
|
|
|
+ return self::amountFromEntity($cg);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static function amountFromEntity($entity)
|
|
|
+ {
|
|
|
+ if (empty($entity)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ $amount = self::resolveDebtAmount($entity);
|
|
|
+ if (bccomp($amount, '0', 2) <= 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return $amount;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param array[] $rows
|
|
|
+ * @param array[] $rows fetchZeroAmountRelationBatch 的结果
|
|
|
* @return array{updated:int,skipped:int}
|
|
|
*/
|
|
|
public static function patchZeroAmountRows(array $rows, $dryRun = false)
|
|
|
@@ -845,45 +855,35 @@ class OrderCgClearClass extends BaseClass
|
|
|
if (empty($rows)) {
|
|
|
return ['updated' => 0, 'skipped' => 0];
|
|
|
}
|
|
|
- $clearIds = array_values(array_unique(array_filter(array_map('intval', array_column($rows, 'clearId')))));
|
|
|
- $clearList = ClearClass::getAllByCondition(['id' => ['in', $clearIds]], null, '*', null);
|
|
|
- $clearMap = [];
|
|
|
- if (!empty($clearList)) {
|
|
|
- foreach ($clearList as $clear) {
|
|
|
- $id = is_array($clear) ? ($clear['id'] ?? 0) : ($clear->id ?? 0);
|
|
|
- if ($id > 0) {
|
|
|
- $clearMap[intval($id)] = $clear;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- $pairMaps = [];
|
|
|
- foreach ($clearIds as $clearId) {
|
|
|
- if (isset($clearMap[$clearId])) {
|
|
|
- $pairMaps[$clearId] = self::buildPairAmountMap($clearMap[$clearId]);
|
|
|
- } else {
|
|
|
- $pairMaps[$clearId] = [];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
$table = self::$baseFile::tableName();
|
|
|
$updated = 0;
|
|
|
$skipped = 0;
|
|
|
foreach ($rows as $row) {
|
|
|
$relId = intval($row['id'] ?? 0);
|
|
|
- $clearId = intval($row['clearId'] ?? 0);
|
|
|
- if ($relId <= 0 || $clearId <= 0) {
|
|
|
+ if ($relId <= 0) {
|
|
|
$skipped++;
|
|
|
continue;
|
|
|
}
|
|
|
- $amount = self::resolveAmountForRelationRow($row, $pairMaps[$clearId] ?? []);
|
|
|
- if ($amount === null || bccomp($amount, '0', 2) <= 0) {
|
|
|
+ $amount = self::resolveAmountForRelationRowDirect($row);
|
|
|
+ if ($amount === null) {
|
|
|
$skipped++;
|
|
|
continue;
|
|
|
}
|
|
|
if (!$dryRun) {
|
|
|
- Yii::$app->db->createCommand()->update($table, ['amount' => $amount], ['id' => $relId])->execute();
|
|
|
+ $affected = Yii::$app->db->createCommand(
|
|
|
+ 'UPDATE ' . $table . ' SET amount = :amount WHERE id = :id AND ' . self::zeroAmountWhereSql()
|
|
|
+ )->bindValues([
|
|
|
+ ':amount' => $amount,
|
|
|
+ ':id' => $relId,
|
|
|
+ ])->execute();
|
|
|
+ if ($affected > 0) {
|
|
|
+ $updated++;
|
|
|
+ } else {
|
|
|
+ $skipped++;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $updated++;
|
|
|
}
|
|
|
- $updated++;
|
|
|
}
|
|
|
return ['updated' => $updated, 'skipped' => $skipped];
|
|
|
}
|