$baseData['date']]; } else { $baseDateWhere = []; } // 切割规则 $sliceRuleUserIds = []; foreach ($parentUserIds as $userId) { $sliceRuleUserIds[static::sliceRule($userId)][] = $userId; } // 检查上级不存在的数据 $findParentUserIds = []; foreach ($sliceRuleUserIds as $tableNum => $sliceRuleUserIdsItem) { $baseDateWhere['user_id'] = $sliceRuleUserIdsItem; $userIdsColumn = static::setTableNumAndReturnSelf($tableNum) ->where($baseDateWhere) ->select('user_id') ->column() ?: []; $findParentUserIds[] = $userIdsColumn; } $findParentUserIds = array_merge(...$findParentUserIds); $now = time(); // 找差集 $noExistUserIds = array_diff($parentUserIds, $findParentUserIds); if ($noExistUserIds) { $batchInsertData = []; $batchInsertField = ['mall_id', 'user_id', 'created_at', 'updated_at']; if (static::IS_BY_DATE) { $batchInsertField[] = 'date'; } foreach ($noExistUserIds as $key => $noExistUserId) { $batchInsertData[self::sliceRule($noExistUserId)][$key] = [ 'mall_id' => $baseData['mall_id'], 'user_id' => $noExistUserId, 'created_at' => $now, 'updated_at' => $now, ]; if (static::IS_BY_DATE) { $batchInsertData[self::sliceRule($noExistUserId)][$key]['date'] = $baseData['date']; } } try { foreach ($batchInsertData as $tableNum => $values) { static::$currentTableNum = $tableNum; Yii::$app->db->createCommand()->batchInsert( static::tableName(), $batchInsertField, array_values($values) )->execute(); } } catch (Exception $e) { $desc = sprintf( '团队统计,插入基础统计数据失败,data: %s', json_encode([ 'class' => static::class, 'agrs' => func_get_args(), 'saveData' => array_merge(['tableName' => static::tableName()], compact('batchInsertField', 'batchInsertData')) ]) ); Yii::error(FormatHelper::exception($e, $desc)); } } [ 'allowSaveTeamData' => $allowSaveTeamData, 'allowSaveDirectData' => $allowSaveDirectData, 'allowSaveIndirectData' => $allowSaveIndirectData ] = self::getAllowSaveData($isIncr, $teamData, $directData, $indirectData); try { // 群组总业绩、群组产品总额、群组订单总数。(不包含自己) if ($allowSaveTeamData) { foreach ($sliceRuleUserIds as $tableNum => $sliceRuleUserIdsItem) { $baseDateWhere['user_id'] = $sliceRuleUserIdsItem; static::$currentTableNum = $tableNum; static::updateAllCounters($allowSaveTeamData, $baseDateWhere); } } // 保存直推数据 if ($parentId && $allowSaveDirectData) { $baseDateWhere['user_id'] = $parentId; static::$currentTableNum = self::sliceRule($parentId); static::updateAllCounters($allowSaveDirectData, $baseDateWhere); } // 保存间推数据 if ($secondParentId && $allowSaveIndirectData) { $baseDateWhere['user_id'] = $secondParentId; static::$currentTableNum = self::sliceRule($secondParentId); static::updateAllCounters($allowSaveIndirectData, $baseDateWhere); } } catch (Exception $e) { $desc = sprintf( '保存团队统计数据失败,data: %s', json_encode([ 'class' => static::class, 'agrs' => func_get_args(), 'saveData' => compact('allowSaveTeamData', 'allowSaveDirectData', 'allowSaveIndirectData') ]) ); Yii::error(FormatHelper::exception($e, $desc)); return false; } return true; } /** * 保存统计数据(整个上级都需要加)(只支持分表之后的表) * * @param array $baseData 基础数据(不需要保存的数据) * @param array $teamData 团队需要保存的数据 * @param bool $isIncr 是否是自增 * @param array $directData 直推需要保存的数据 * @param array $indirectData 间推需要保存的数据 * * @return bool */ public static function saveStatisticsTotal( array $baseData, array $teamData, bool $isIncr = true, array $directData = [], array $indirectData = [] ): bool { // 找出整个群组的人 $parentUserIds = $baseData['parents_user_ids'] ?? UserPartitionRelationship::getParentIdArr($baseData['user_id']); if (empty($parentUserIds)) { return true; } // 直推ID $parentId = end($parentUserIds); // 间推ID $secondParentId = prev($parentUserIds); if (static::IS_BY_DATE) { $baseDateWhere = ['date' => $baseData['date']]; } else { $baseDateWhere = []; } // 检查上级不存在的数据 $baseDateWhere['user_id'] = $parentUserIds; $findParentUserIds = static::find() ->where($baseDateWhere) ->select('user_id') ->column() ?: []; $now = time(); // 找差集 $noExistUserIds = array_diff($parentUserIds, $findParentUserIds); if ($noExistUserIds) { $batchInsertData = []; $batchInsertField = ['mall_id', 'user_id', 'created_at', 'updated_at']; if (static::IS_BY_DATE) { $batchInsertField[] = 'date'; } foreach ($noExistUserIds as $key => $noExistUserId) { $batchInsertData[$key] = [ 'mall_id' => $baseData['mall_id'], 'user_id' => $noExistUserId, 'created_at' => $now, 'updated_at' => $now, ]; if (static::IS_BY_DATE) { $batchInsertData[$key]['date'] = $baseData['date']; } } try { Yii::$app->db->createCommand()->batchInsert( static::tableName(), $batchInsertField, array_values($batchInsertData) )->execute(); } catch (Exception $e) { $desc = sprintf( '团队统计,插入基础统计数据失败,data: %s', json_encode([ 'class' => static::class, 'agrs' => func_get_args(), 'saveData' => array_merge(['tableName' => static::tableName()], compact('batchInsertField', 'batchInsertData')) ]) ); Yii::error(FormatHelper::exception($e, $desc)); } } [ 'allowSaveTeamData' => $allowSaveTeamData, 'allowSaveDirectData' => $allowSaveDirectData, 'allowSaveIndirectData' => $allowSaveIndirectData ] = self::getAllowSaveData($isIncr, $teamData, $directData, $indirectData); try { // 群组总业绩、群组产品总额、群组订单总数。(不包含自己) if ($allowSaveTeamData) { $baseDateWhere['user_id'] = $parentUserIds; static::updateAllCounters($allowSaveTeamData, $baseDateWhere); } // 保存直推数据 if ($parentId && $allowSaveDirectData) { $baseDateWhere['user_id'] = $parentId; static::updateAllCounters($allowSaveDirectData, $baseDateWhere); } // 保存间推数据 if ($secondParentId && $allowSaveIndirectData) { $baseDateWhere['user_id'] = $secondParentId; static::updateAllCounters($allowSaveIndirectData, $baseDateWhere); } } catch (Exception $e) { $desc = sprintf( '保存团队统计数据失败,data: %s', json_encode([ 'class' => static::class, 'agrs' => func_get_args(), 'saveData' => compact('allowSaveTeamData', 'allowSaveDirectData', 'allowSaveIndirectData') ]) ); Yii::error(FormatHelper::exception($e, $desc)); return false; } return true; } /** * 获取允许操作的字段 * * @param bool $isIncr * @param array $teamData * @param array $directData * @param array $indirectData * * @return array */ protected static function getAllowSaveData(bool $isIncr, array $teamData, array $directData, array $indirectData): array { $allowSaveTeamData = []; // 直推数据 $allowSaveDirectData = []; // 间推数据 $allowSaveIndirectData = []; // 允许操作的字段 $allowUpdateField = $isIncr ? static::ALLOW_INCR_FIELD : static::ALLOW_DECR_FIELD; $model = new static; foreach ($model as $field => $_) { !empty($teamData[$field]) && in_array($field, $allowUpdateField, true) && $allowSaveTeamData[$field] = $teamData[$field]; !empty($directData[$field]) && in_array($field, $allowUpdateField, true) && $allowSaveDirectData[$field] = $directData[$field]; !empty($indirectData[$field]) && in_array($field, $allowUpdateField, true) && $allowSaveIndirectData[$field] = $indirectData[$field]; } return compact('allowSaveTeamData', 'allowSaveDirectData', 'allowSaveIndirectData'); } }