debug
This commit is contained in:
147
code/app/model/ConfigSubjectFomartModel.php
Normal file
147
code/app/model/ConfigSubjectFomartModel.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\model;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
|
||||
/**
|
||||
* @mixin think\Model
|
||||
*/
|
||||
class ConfigSubjectFomartModel extends MongoModel
|
||||
{
|
||||
/**
|
||||
* Undocumented variable
|
||||
*
|
||||
* @var self
|
||||
*/
|
||||
static public $obj;
|
||||
|
||||
static public function getInstance(): self
|
||||
{
|
||||
if (self::$obj == NULL) {
|
||||
self::$obj = new self;
|
||||
}
|
||||
return self::$obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented variable
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $strCollection = 'config_subject_fomart';
|
||||
|
||||
static public $arrOptions = [
|
||||
'typeMap' => [
|
||||
'root' => 'array',
|
||||
'document' => 'array',
|
||||
'array' => 'array',
|
||||
],
|
||||
'projection' => [
|
||||
'_id' => 0,
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 直接读库查询大量小说
|
||||
*
|
||||
* @param integer $intNId
|
||||
* @return null|array
|
||||
*/
|
||||
public function findMany(array $arrFilter, int $intCount = 10, array $arrSort = [], array $arrOptions = []): null|array
|
||||
{
|
||||
if (empty($arrOptions)) {
|
||||
$arrOptions = self::$arrOptions;
|
||||
}
|
||||
|
||||
if ($intCount > 0) {
|
||||
$arrOptions['limit'] = $intCount;
|
||||
}
|
||||
|
||||
if (empty($arrSort)) {
|
||||
$arrOptions['sort'] = $arrSort;
|
||||
}
|
||||
|
||||
$Cursor = $this->getCol()->find($arrFilter, $arrOptions);
|
||||
|
||||
$arrResult = iterator_to_array($Cursor);
|
||||
|
||||
return $arrResult;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询多条数据并缓存
|
||||
*
|
||||
* @param array $arrFilter
|
||||
* @param integer $intCount
|
||||
* @param array $arrSort
|
||||
* @param array $arrOptions
|
||||
* @param string $strKey
|
||||
* @param integer $intLifeTime
|
||||
* @return null|array
|
||||
*/
|
||||
public function findManyWithCache(
|
||||
array $arrFilter,
|
||||
int $intCount = 0,
|
||||
array $arrSort = [],
|
||||
array $arrOptions = [],
|
||||
string $strKey = '',
|
||||
int $intLifeTime = 24 * 3600,
|
||||
): null|array {
|
||||
try {
|
||||
|
||||
$arrResult = $this->checkCacheByKey($strKey);
|
||||
if ($arrResult !== NULL) {
|
||||
return $arrResult;
|
||||
}
|
||||
|
||||
$arrResult = $this->findMany($arrFilter, $intCount, $arrSort, $arrOptions);
|
||||
|
||||
$this->setCacheByKey($strKey, $arrResult, $intLifeTime);
|
||||
|
||||
return $arrResult;
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* find one novel
|
||||
*
|
||||
* @param array $arrFilter
|
||||
* @param array $arrOptions
|
||||
* @return null|array
|
||||
*/
|
||||
public function findOne(array $arrFilter, array $arrOptions = []): null|array
|
||||
{
|
||||
if (empty($arrOptions)) {
|
||||
$arrOptions = self::$arrOptions;
|
||||
}
|
||||
|
||||
$arrNovel = $this->getOne($arrFilter, $arrOptions);
|
||||
|
||||
|
||||
return $arrNovel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据小说ID查询指定小说
|
||||
*
|
||||
* @param integer $intNId
|
||||
* @return array
|
||||
*/
|
||||
public function getConfigSubjectFomartByCfsKey(string $intNId): null|array
|
||||
{
|
||||
$arrFilter = ['cfs_key' => $intNId];
|
||||
return $this->findOne($arrFilter);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -94,22 +94,46 @@ class DomainModel extends BaseModel
|
||||
* @param string $strKey
|
||||
* @return void
|
||||
*/
|
||||
public function getFomartSubject($strKey) //, $intNum = 1, $boolRand = false)
|
||||
public function getFomartSubject($strKey, $strDiff = '')
|
||||
{
|
||||
$intSfgId = $this->t_cfg->{$strKey}['sfg_id'];
|
||||
|
||||
// $SubjectFomartModel = SubjectFomartModel::where('sfg_id', $intSfgId)->select()->first();
|
||||
$SubjectFomartModel = NULL;
|
||||
|
||||
$arrAllSubjectFomartModel = SubjectFomartModel::getAllSubjectFomartOnCacheBySfgId($intSfgId);
|
||||
if (empty($arrAllSubjectFomartModel)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$SubjectFomartModel = (is_array($arrAllSubjectFomartModel) && !empty($arrAllSubjectFomartModel)) ? reset($arrAllSubjectFomartModel) : null;
|
||||
$strDomain = request()->DomainModel->d_domain;
|
||||
$strController = request()->controller();
|
||||
$strAction = request()->action();
|
||||
|
||||
return empty($SubjectFomartModel) ? '' : $SubjectFomartModel->sf_val;
|
||||
$strUUId = sprintf('%s-%s-%s-%s-%s', $strDomain, $strController, $strAction, $strKey, $strDiff);
|
||||
|
||||
// echo '<pre>';
|
||||
// print_r($SubjectFomartModel);
|
||||
// exit;
|
||||
$arrConfigSubjectFomart = ConfigSubjectFomartModel::getInstance()->getConfigSubjectFomartByCfsKey($strUUId);
|
||||
|
||||
// return $SubjectFomartModel->sf_val;
|
||||
if (empty($arrConfigSubjectFomart)) {
|
||||
|
||||
$intKey = array_rand($arrAllSubjectFomartModel);
|
||||
|
||||
$SubjectFomartModel = $arrAllSubjectFomartModel[$intKey];
|
||||
|
||||
ConfigSubjectFomartModel::getInstance()->insert([
|
||||
'cfs_key' => $strUUId,
|
||||
'sf_id' => $SubjectFomartModel->sf_id,
|
||||
]);
|
||||
} else {
|
||||
$intSfId = $arrConfigSubjectFomart['sf_id'];
|
||||
|
||||
foreach ($arrAllSubjectFomartModel as $objTemp) {
|
||||
if ($objTemp->sf_id == $intSfId) {
|
||||
$SubjectFomartModel = $objTemp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $SubjectFomartModel->sf_val;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user