where([ 'order_id' => $params['order_id'], 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id, ]) ->select('id') ->column(); if (!$orderDetailIds) { $this->setError('未查询到订单'); return false; } $existScoreLogId = ScoreExpansionInvertLog::find() ->where([ 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'addons_name' => $params['addons_name'], 'source_table' => $sourceTable, 'source_table_id' => $orderDetailIds ]) ->select('score_log_id') ->column(); CapitalLog::$capitalType = 'score'; $scoreList = CapitalLog::lists([ 'where' => [ [ 'source_table_id' => $orderDetailIds, 'mall_id' => $this->mall_id, 'source_table' => $sourceTable, 'from_type' => $params['addons_name'], 'change_type' => 'add', ], ['not in', 'id', $existScoreLogId] ] ]); if (!$scoreList) { $this->setError('未查询到积分记录'); return false; } // 循环扣减积分,及转为积分拓客的冻结积分 $t = Yii::$app->db->beginTransaction(); try { $insertWallet = []; $desc = sprintf( '订单(%d)%s积分奖励转化为积分拓客冻结积分', $params['order_id'], [AddonsEnum::ADDONS_NAME_DISTRIBUTION => '分销商', AddonsEnum::ADDONS_NAME_AGENT => '渠道分红'][$params['addons_name']] ); $insetInvertLog = []; $now = time(); foreach ($scoreList as $item) { // 插入用户积分表 $wallet = ScoreExpansionWallet::setData([ 'mall_id' => $item['mall_id'], 'user_id' => $item['user_id'], 'is_frozen' => true, 'integral' => $item['change_num'], 'source_table' => CapitalLog::tableName(), 'source_table_id' => $item['id'], 'from_type' => ScoreExpansionEnum::FROM_SCORE_CONVERSION, 'desc' => $desc, ]); if ($wallet === false) { throw new RuntimeException(ScoreExpansionWallet::getStaticError()); } $insertWallet[] = [ 'mall_id' => $item['mall_id'], 'user_id' => $item['user_id'], 'is_frozen' => false, 'wallet_type' => 'score', 'money' => -$item['change_num'], 'desc' => $desc, 'source_table' => ScoreExpansionWalletLog::tableName(), 'source_table_id' => $wallet['log']['id'] ?? 0, 'from_type' => ScoreExpansionEnum::ADDONS_NAME, 'capital_type' => 'score_expansion_score_conversion', 'order_no' => $item['order_no'], ]; $insetInvertLog[] = [ 'mall_id' => $this->mall_id, 'addons_name' => $params['addons_name'], 'source_table' => $item['source_table'], 'source_table_id' => $item['source_table_id'], 'score_log_id' => $item['id'], 'created_at' => $now, 'updated_at' => $now, ]; } !empty($insetInvertLog) && Yii::$app ->db ->createCommand() ->batchInsert( ScoreExpansionInvertLog::tableName(), array_keys($insetInvertLog[0]), $insetInvertLog ) ->execute(); $res = UserWalletService::batchHandleByQueue($insertWallet); if (empty($res)) { throw new RuntimeException(UserWalletService::getStaticError()); } $t->commit(); }catch (Exception $e) { $t->rollBack(); $this->setError($e->getMessage()); return false; } return true; } }