Commit 9e7121ac by zhufx

1.重构设置实体表名代码

2.订单错误码更新
parent b8d02185
......@@ -8,9 +8,11 @@
namespace Hdll\Services\Common\Entity;
use Swoft\App;
use Swoft\Core\RequestContext;
use Swoft\Db\Bean\Collector\EntityCollector;
use Swoft\Db\Model;
use Swoft\Exception\Exception;
use Swoole\Http\Request;
class CommonEntity extends Model
{
......@@ -23,20 +25,16 @@ class CommonEntity extends Model
//获取注解里的table
$res = new \ReflectionClass(static::class);
self::$commentString = $res->getDocComment();
if ( false !== strpos(self::$commentString,"{split}") ) {
$dbNum = self::getDbNum();
self::setTableName($dbNum);
}
//分表逻辑
$this->split();
$this->init();
}
/**
* 初始化
*/
public function init()
{
......@@ -48,23 +46,68 @@ class CommonEntity extends Model
$res = new \ReflectionClass(static::class);
self::$commentString = $res->getDocComment();
if ( false !== strpos(self::$commentString,"{split}") ) {
$dbNum = self::getDbNum();
self::setTableName($dbNum);
//分表逻辑
self::split();
return new static;
}
/**
* 设置分表表名
* @throws Exception
*/
protected function split()
{
$request = RequestContext::getRequest();
if ( empty($request) ) {//初始化不做处理
return;
}
return new static();
$testFlag = $request->query('flag');
if ( false === strpos(self::$commentString,"{split}") || $testFlag == 'hdllTest' ) {
return;
}
$dbNum = self::getDbNum();
$tableName = self::getTableName();
self::setTableName($tableName.'_'.$dbNum);
}
/**
* 获取分库表明
* 获取分库表名
*
* @return string|void
* @throws Exception
* @throws \ReflectionException
*/
public static function tableName()
{
$num = self::getDbNum();
return self::getTableName().'_'.$num;
$request = RequestContext::getRequest();
if ( empty($request) ) {//初始化不做处理
return;
}
$testFlag = $request->query('flag');
//获取注解里的table
$res = new \ReflectionClass(static::class);
self::$commentString = $res->getDocComment();
if ( false !== strpos(self::$commentString,"{split}") && $testFlag != 'hdllTest') {
$dbNum = self::getDbNum();
$tableName = self::getTableName().'_'.$dbNum;
self::setTableName($tableName);
return $tableName;
} else {
return self::getTableName();
}
}
/**
......@@ -89,13 +132,11 @@ class CommonEntity extends Model
* 设置表名
* @param $dbNum
*/
private static function setTableName($dbNum)
protected static function setTableName($tableName)
{
$tableName = self::getTableName();
EntityCollector::collect(
static::class,
new \Swoft\Db\Bean\Annotation\Table(['name' => $tableName.'_'.$dbNum])
new \Swoft\Db\Bean\Annotation\Table(['name' => $tableName])
);
}
......@@ -104,7 +145,7 @@ class CommonEntity extends Model
* 获取原始表名
* @return string
*/
private static function getTableName()
protected static function getTableName()
{
preg_match('/(?<=Table\(name=").*(?="\))/', self::$commentString, $matches);
return $tableName = isset($matches[0])?$matches[0]:'';
......
......@@ -43,12 +43,6 @@ class OrderError
const DB_ERROR = ["msg" => "数据库操作错误", "code" => 500, "errorCode" => 22118];
public static function atranslate($message,$param)
{
if ( empty($param) ) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment