from(Shop::tableName()); $query->where(['ptStyle' => 1]); $query->orderBy('addTime ASC'); foreach ($query->batch(20) as $shopList) { foreach ($shopList as $shop) { $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $shopId = $shop['id'] ?? 0; $query = new \yii\db\Query(); $query->from(Custom::tableName()); $query->where(['shopId' => $shopId]); $query->orderBy('addTime ASC'); foreach ($query->batch(50) as $customList) { foreach ($customList as $custom) { $customId = $custom['id'] ?? 0; $debtAmount = $custom['debtAmount'] ?? 0; $hdId = $custom['hdId'] ?? 0; $customName = $custom['name'] ?? ''; if (empty($hdId) || empty($customId)) { echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 的ID是空的 \n"; util::stop(); } if ($debtAmount > 0) { echo "shopId:" . $shopId . " | " . $customName . " customId:" . $customId . " hdId:" . $hdId . " 有欠款 ==== \n\n"; $custom = CustomClass::getById($customId, true); $hd = HdClass::getById($hdId, true); if (empty($custom) || empty($hd)) { echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 的对象是空的哦!! \n"; util::stop(); } $balance = $custom->balance ?? 0; $hdBalance = $hd->balance ?? 0; if (floatval($balance) != $hdBalance) { echo $customName . " customId:" . $customId . " hdId:" . $hdId . " 二边余额不一致 \n"; util::stop(); } $newBalance = bcsub($balance, $debtAmount, 2); $custom->balance = $newBalance; $custom->debtAmount = 0; $custom->isDebt = 0; $custom->debtNum = 0; $custom->save(); $hd->balance = $newBalance; $hd->debtAmount = 0; $hd->debt = 1; $hd->debtNum = 0; $hd->save(); $hdName = $hd->name ?? ''; $mainId = $shop['mainId'] ?? 0; $event = '系统操作,合并欠款到余额'; $capitalType = dict::getDict('capitalType', 'system', 'id'); $change = [ 'customId' => $customId, 'customName' => $customName, 'hdId' => $hdId, 'hdName' => $hdName, 'relateId' => 0, 'onlinePay' => 1, 'capitalType' => $capitalType, 'amount' => $debtAmount, 'balance' => $newBalance, 'io' => 0, 'side' => 0, 'payWay' => 0, 'event' => $event, 'staffId' => 0, 'staffName' => '', 'shopId' => $shopId, 'mainId' => $mainId, 'remark' => '', ]; BalanceChangeClass::add($change, true); } else { //echo "shopId:".$shopId." | ".$customName . " customId:" . $customId . " hdId:" . $hdId . " 没有欠款 ok ok \n"; } } } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); $msg = $e->getMessage(); echo "报错了:" . $msg . "\n"; } } } } }