add
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace app\model;
|
||||
namespace app\common\library;
|
||||
|
||||
use app\DbCoroutineContext;
|
||||
use think\Collection;
|
||||
use think\db\Connection;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
use support\Log;
|
||||
|
||||
class DatabaseRoute
|
||||
{
|
||||
@@ -29,7 +28,7 @@ class DatabaseRoute
|
||||
int $count = null,
|
||||
): array
|
||||
{
|
||||
$dbMap = config('database.db_map');
|
||||
$dbMap = config('think-orm.db_map');
|
||||
$counts = [];
|
||||
$total = 0;
|
||||
|
||||
@@ -192,7 +191,7 @@ class DatabaseRoute
|
||||
bool $isRecords = false
|
||||
): array
|
||||
{
|
||||
$dbMap = config('database.db_map');
|
||||
$dbMap = config('think-orm.db_map');
|
||||
$counts = [];
|
||||
$total = 0;
|
||||
|
||||
@@ -367,7 +366,7 @@ class DatabaseRoute
|
||||
*/
|
||||
public static function deleteAllDbDirect(string $table, \Closure $builder): int
|
||||
{
|
||||
$dbMap = config('database.db_master_map');
|
||||
$dbMap = config('think-orm.db_master_map');
|
||||
$totalDeleted = 0;
|
||||
|
||||
foreach ($dbMap as $connName) {
|
||||
@@ -396,8 +395,8 @@ class DatabaseRoute
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->builder = $builder;
|
||||
$this->dbMap = config('database.db_map');
|
||||
$this->masterDbMap = config('database.db_master_map');
|
||||
$this->dbMap = config('think-orm.db_map');
|
||||
$this->masterDbMap = config('think-orm.db_master_map');
|
||||
}
|
||||
|
||||
public function __call($method, $args)
|
||||
@@ -473,12 +472,12 @@ class DatabaseRoute
|
||||
|
||||
private static function getIndex()
|
||||
{
|
||||
$INDEX = \cache('db_index');
|
||||
$INDEX = cache('db_index');
|
||||
if ($INDEX == null) {
|
||||
$INDEX = 0;
|
||||
}
|
||||
$INDEX = ++$INDEX % 4;
|
||||
\cache('db_index', $INDEX);
|
||||
cache('db_index', $INDEX);
|
||||
return $INDEX;
|
||||
}
|
||||
|
||||
@@ -492,7 +491,12 @@ class DatabaseRoute
|
||||
*/
|
||||
public static function getConnection($table, $data = [], $isWrite = false)
|
||||
{
|
||||
|
||||
|
||||
$routeConfig = config('think-orm.route');
|
||||
if (!isset($routeConfig[$table])) {
|
||||
return $isWrite ? 'duanju-master' : 'duanju-slave';
|
||||
}
|
||||
$keyField = strpos($table, 'user') !== false || in_array($table, [
|
||||
'orders', 'course_collect', 'pay_details', 'disc_spinning_record',
|
||||
'cash_out', 'course_user', 'tb_user', 'task_center_record',
|
||||
@@ -502,7 +506,7 @@ class DatabaseRoute
|
||||
|
||||
if (!isset($data[$keyField])) {
|
||||
Log::warning("分库警告: 表={$table}, 数据中缺少 {$keyField} 字段");
|
||||
return $isWrite ? 'duanju_master' : 'duanju_slave';
|
||||
return $isWrite ? 'duanju-master' : 'duanju-slave';
|
||||
}
|
||||
|
||||
$index = abs($data[$keyField] % 5);
|
||||
@@ -511,6 +515,19 @@ class DatabaseRoute
|
||||
: $routeConfig[$table]['slave'];
|
||||
|
||||
$connectionName = str_replace("{\${$keyField}%5}", $index, $connectionTemplate);
|
||||
|
||||
if (strpos($connectionName, 'slave')) {
|
||||
$INDEX = self::getIndex();
|
||||
$connectionName = str_replace("duanju", 'duanju'.$INDEX, $connectionName);
|
||||
}
|
||||
// 验证连接是否存在
|
||||
$connections = config('think-orm.connections');
|
||||
if (!isset($connections[$connectionName])) {
|
||||
Log::error("分库错误: 表={$table}, 连接={$connectionName} 不存在");
|
||||
return $isWrite ? 'duanju-master' : 'duanju-slave';
|
||||
}
|
||||
|
||||
// Log::info("分库成功: 表={$table}, 键={$keyField}, 值={$data[$keyField]}, 索引={$index}, 连接={$connectionName}");
|
||||
return $connectionName;
|
||||
}
|
||||
}
|
||||
@@ -5,18 +5,16 @@ namespace app\controller;
|
||||
use app\api\model\CourseCollect;
|
||||
use app\api\model\CourseDetails;
|
||||
use app\api\model\TbUser;
|
||||
use app\model\DatabaseRoute;
|
||||
use app\common\library\DatabaseRoute;
|
||||
use app\model\Test;
|
||||
use support\Request;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
|
||||
|
||||
\support\Log::info('来了' . date('Y-m-d H:i:s'));
|
||||
$get['courseId'] = $course_id = '1877654905222135809';
|
||||
$user['user_id'] = $user_id = '14240';
|
||||
$user = DatabaseRoute::getDb('tb_user', $user_id)->find();
|
||||
@@ -132,6 +130,7 @@ class IndexController
|
||||
$detailsId = $det_db->name('course_user')->where(['course_id' => $courseId, 'classify' => 2])->column('course_details_id');
|
||||
$det_db->close();
|
||||
$detailsId = array_flip(array_flip($detailsId)); // 去重
|
||||
\support\Log::info('啦啦啦' . date('Y-m-d H:i:s'));
|
||||
}
|
||||
// 处理剧集列表
|
||||
$current = null;
|
||||
@@ -162,7 +161,6 @@ class IndexController
|
||||
->where('classify', 2)
|
||||
->limit(1)
|
||||
->count();
|
||||
$isGood_db->close();
|
||||
$s['isGood'] = empty($isGood) || $isGood == 0 ? 0 : 1;
|
||||
}
|
||||
}
|
||||
@@ -184,6 +182,7 @@ class IndexController
|
||||
'collect' => empty($collect) || $collect == 0 ? 0 : 1,
|
||||
'list' => $courseDetailsSetVos
|
||||
];
|
||||
\support\Log::info('即将返回' . date('Y-m-d H:i:s'));
|
||||
return json($map);
|
||||
} catch (\Exception $e) {
|
||||
return json($e->getMessage());
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace app\model;
|
||||
|
||||
use support\Log;
|
||||
use think\model;
|
||||
use think\facade\Db;
|
||||
use app\common\library\DatabaseRoute;
|
||||
|
||||
class Test extends Model
|
||||
{
|
||||
@@ -51,7 +53,7 @@ class Test extends Model
|
||||
}else{
|
||||
$isExpire = true;
|
||||
}
|
||||
|
||||
Log::info('嘿嘿' . date('Y-m-d H:i:s'));
|
||||
return !$isExpire;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user