This commit is contained in:
Default
2025-04-01 17:32:09 +08:00
parent 521347c20f
commit 90fb138082
6 changed files with 372 additions and 125 deletions

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace app\model;
use DateTime;
use storage\StorageCore;
use think\facade\Cache;
use think\Model;
@@ -14,4 +15,76 @@ use think\Model;
class ChapterModel extends MongoModel
{
protected $strCollection = 'chapter';
static public $arrChapterOptions = [
'typeMap' => [
'root' => 'array',
'document' => 'array',
'array' => 'array',
],
'projection' => [
'_id' => 0,
'c_page_title' => 0,
'c_page_keywords' => 0,
'c_page_description' => 0,
'c_source_uri' => 0,
'c_source_id' => 0,
'c_site_name' => 0,
'c_site_code' => 0,
'c_site_uuid' => 0,
],
];
/**
* Undocumented function
*
* @param integer $intNId
* @return null|array
*/
public function getLatestChapter(int $intNId): null|array
{
$arrFilter = ['n_id' => $intNId, 'c_page' => 0];
$arrOptions = self::$arrChapterOptions;
$arrOptions['sort'] = ['c_sort_num' => -1];
$arrChapter = $this->getOne($arrFilter, $arrOptions);
if (!empty($arrChapter)) {
applyToKeys($arrChapter, ['created_at', 'updated_at'], 'formatMongoDate');
$arrChapter['content'] = StorageCore::getInstance()->read($arrChapter['c_content_path']);
}
return $arrChapter;
}
/**
* Undocumented function
*
* @param integer $intNId
* @return null|array
*/
public function getChapterAll(array $arrFilter, array $arrOptions = []): null|array
{
// $arrFilter = ['n_id' => $intNId, 'c_page' => 0];
// $arrOptions = self::$arrChapterOptions;
// $arrOptions['sort'] = ['c_sort_num' => -1];
// $arrChapter = $this->getOne($arrFilter, $arrOptions);
// if (!empty($arrChapter)) {
// applyToKeys($arrChapter, ['created_at', 'updated_at'], 'formatMongoDate');
// $arrChapter['content'] = StorageCore::getInstance()->read($arrChapter['c_content_path']);
// }
// return $arrChapter;
}
}

View File

@@ -7,8 +7,6 @@ namespace app\model;
use db\mongo\MongoBase;
use ddl\DDLManage;
use MongoDB\Collection;
use think\facade\Cache;
use think\Model;
/**
* @mixin think\Model
@@ -68,4 +66,15 @@ abstract class MongoModel
{
return $this->strCollection;
}
/**
* Undocumented function
*
* @param array $arrFilter
* @return array|object|null
*/
public function getOne(array $arrFilter = [], array $arrOptions = []): array|object|null
{
return $this->getCol()->findOne($arrFilter, $arrOptions);
}
}

View File

@@ -64,4 +64,40 @@ class NovelClicksModel extends MongoModel
}
return true;
}
public function getStats(int $intNId): array
{
$Now = new DateTime();
$strDailyDate = $Now->format('Y-m-d');
$strWeeklyDate = $Now->modify('monday this week')->format('Y-m-d');
$strMonthlyDate = $Now->format('Y-m-01');
$query = [
'n_id' => $intNId,
'$or' => [
['nc_type' => 'daily', 'nc_date' => $strDailyDate],
['nc_type' => 'weekly', 'nc_date' => $strWeeklyDate],
['nc_type' => 'monthly', 'nc_date' => $strMonthlyDate],
['nc_type' => 'total', 'nc_date' => null]
]
];
$arrOptions = [
'projection' => ['nc_type' => 1, 'nc_clicks' => 1, '_id' => 0],
'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']
];
$arrResults = $this->getCol()->find($query, $arrOptions)->toArray();
$arrStats = array_column($arrResults, 'nc_clicks', 'nc_type');
$defaultStats = [
'daily' => 0,
'weekly' => 0,
'monthly' => 0,
'total' => 0
];
$arrStats = array_merge($defaultStats, $arrStats);
return $arrStats;
}
}

View File

@@ -36,7 +36,7 @@ class NovelModel extends MongoModel
try {
$arrConditions = array_map(function ($strKeyword) {
return ['og_title' => ['$regex' => trim($strKeyword), '$arrOptions' => 'i']];
return ['og_title' => ['$regex' => trim($strKeyword), '$options' => 'i']];
}, $arrKeywords);
$UpdateResult = $this->getCol()->updateMany(
@@ -53,6 +53,86 @@ class NovelModel extends MongoModel
return true;
}
static public $arrNovelOptions = [
'typeMap' => [
'root' => 'array',
'document' => 'array',
'array' => 'array',
],
'projection' => [
'_id' => 0,
'og_title' => 0,
'n_site_name' => 0,
'n_site_code' => 0,
'n_site_uuid' => 0,
'n_source_id' => 0,
'n_page_title' => 0,
'og_description' => 0,
'og_novel_latest_chapter_name' => 0,
'og_image' => 0,
'og_novel_latest_chapter_url' => 0,
'og_novel_read_url' => 0,
'og_type' => 0,
'og_url' => 0,
],
];
/**
* Undocumented function
*
* @param integer $intNId
* @return array
*/
public function getNovelInfo(int $intNId): null|array
{
$arrFilter = ['n_id' => $intNId];
$arrOptions = self::$arrNovelOptions;
$arrNovel = $this->getOne($arrFilter, $arrOptions);
applyToKeys($arrNovel, ['created_at', 'updated_at'], 'formatMongoDate');
return $arrNovel;
}
/**
* Undocumented function
*
* @param string $strKey
* @param integer $intCount
* @param int $intLifeTime
* @param array $arrFilter
* @return void
*/
public function getRandNovel(string $strKey, $intCount = 10, int $intLifeTime = 24 * 3600, array $arrFilter = []): array
{
try {
$arrResult = Cache::get($strKey);
// if (empty($arrResult)) {
if (true) {
$pipeline = [
['$match' => $arrFilter],
['$sample' => ['size' => $intCount]]
];
$Novel = $this->getCol()->aggregate($pipeline);
$arrResult = iterator_to_array($Novel);
if (!empty($arrResult)) {
Cache::set($strKey, $arrResult, $intLifeTime);
}
}
return $arrResult;
} catch (\Exception $e) {
return [];
}
}
/**
* Undocumented function
*