123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace app\common\model;
- use app\common\constants\SpecialPageConstants;
- use app\common\library\Redis;
- use app\main\service\ActivityService;
- use app\main\service\BookService;
- use think\Model;
- class SpecialPage extends Model
- {
- // 表名
- protected $table = 'special_page';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
-
- // 追加属性
- protected $append = [
- 'type_text',
- 'status_text'
- ];
-
-
- public function getTypeList()
- {
- return ['1' => __('Type 1'),'2' => __('Type 2'),'3' => __('Type 3'),'4' => __('Type 4')];
- }
- public function getStatusList()
- {
- return ['normal' => __('Status normal'),'hidden' => __('Status hidden')];
- }
- public function getTypeTextAttr($value, $data)
- {
- $value = $value ? $value : $data['type'];
- $list = $this->getTypeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : $data['status'];
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- /**
- * 返回详情
- * @param $id
- * @return array
- */
- public function getInfo($id)
- {
- $result = [];
- $redis = Redis::instance();
- $key = SpecialPageConstants::REDIS_KEY. $id; //书单信息缓存
- if ($redis->exists($key)) {
- $result = json_decode($redis->get($key), true);
- } else {
- $page = $this->where('id', 'EQ', $id)->find();
- if (!empty($page)) {
- $result = $page->toArray();
- //拉取资源
- $blocks = collection(model("SpecialBlock")->where('page_id', 'EQ', $id)->order("weigh desc")->select())->toArray();
- $result['items'] = $blocks;
- if (!empty($result['items'])) {
- foreach ($result['items'] as &$item) {
- $bookIds = explode(",", str_replace([" ", " ", "\s", "\r", "\n"], [",", ",", ",", ",", ","], trim($item['book_id'])));
- $bookIds = array_values(array_filter($bookIds));
- if ($bookIds) {
- $books = [];
- foreach ($bookIds as $bookId) {
- $bookInfo = BookService::instance()->getBookModel()->BookInfo($bookId);
- if(!empty($bookInfo)){
- $books[$bookId]['word_count'] = formatNumber($bookInfo['word_count']);
- $books[$bookId]['read_num'] = formatNumber($bookInfo['read_num']);
- }
- }
- $item['books'] = $books;
- }
- if ($item['activity_id']) {
- $res = ActivityService::instance()->getActivityModel()->alias('a')->join(['resource' => 'r'], 'r.activity_id = a.id')
- ->field(['r.id', 'a.id' => 'aid'])
- ->where('a.id', 'eq', $item['activity_id'])
- ->find();
- if ($res) {
- $item['activity_rid'] = $res['id'];
- }
- }
- }
- }
- $redis->set($key, json_encode($result, 256));
- }
- }
- return $result;
- }
- /**
- * 清除缓存
- * @param $id
- * @return bool
- */
- public function rmCache($id)
- {
- if (!empty($id)) {
- $redis = Redis::instance();
- $key = 'SPP:'. $id; //专题页缓存
- if ($redis->exists($key)) {
- $redis->del($key);
- }
- }
- return true;
- }
- }
|