| 1 |
- <?php
/**
* 广告
* @author wesmielr
*/
namespace app\index\service;
use think\Db;
class AdvertService
{
/**
* 获取对应位置轮播列表
* @param $slideId 广告位ID
* @param int $num 获取数量
* @param string $field 字段
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function getListBySlide($slideId, $num=6, $field=''){
$cacheKey = "cache:advert:byslide_".$slideId.'_'.$num;
$dataList = RedisService::get($cacheKey);
if ($dataList) {
return $dataList;
}
$field = $field? $field : 'id,title,image,url,target';
$dataList = Db::name('slide_item')->where(['status'=> 1])
->where(function($query) use ($slideId){
if(is_array($slideId)){
$query->whereIn('slide_id', $slideId);
}else{
$query->where('slide_id', $slideId);
}
})
->field($field)
->order('list_order')
->limit($num)
->select();
$dataList = $dataList ? $dataList->toArray() : [];
if ($dataList) {
RedisService::set($cacheKey, $dataList, 7 * 24 * 3600);
}
return $dataList;
}
/**
* 获取对应位置轮播列表
* @param $slideId 广告位ID
* @param int $num 获取数量
* @param string $field 字段
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function getListBySlides($slideIds, $num=6, $field=''){
$cacheKey = "cache:advert:byslide_".$num."_".md5(json_encode($slideIds));
$dataList = RedisService::get($cacheKey);
if ($dataList) {
// return $dataList;
}
$field = $field? $field : 'a.id,a.slide_id,a.title,a.image,a.url,a.target';
$subQuery = Db::name('slide_item')->alias('b')
->where('b.slide_id = a.slide_id and b.id <= a.id')
->field(Db::raw("count(b.slide_id)"))
->group('b.slide_id')
->buildSql();
$dataList = Db::name('slide_item')->alias('a')
->where($subQuery." <= {$num}")
->where(['status'=> 1])
->field($field)
->order('list_order')
->select();
$dataList = $dataList ? $dataList->toArray() : [];
if ($dataList) {
$datas = [];
foreach ($dataList as $item){
$datas[$item['slide_id']][] = $item;
}
$dataList = $datas;
RedisService::set($cacheKey, $dataList, 7 * 24 * 3600);
}
return $dataList;
}
}
|