109 lines
3.3 KiB
PHP
109 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace app\api\model\app\recommend;
|
|
|
|
use think\Model;
|
|
use think\Db;
|
|
use app\api\model\app\agent\Level;
|
|
use think\Cache;
|
|
class Goods extends Model
|
|
{
|
|
|
|
|
|
// 表名
|
|
protected $name = 'app_recommend_goods';
|
|
|
|
|
|
// 自动写入时间戳字段
|
|
protected $autoWriteTimestamp = 'integer';
|
|
|
|
// 定义时间戳字段名
|
|
protected $createTime = 'createtime';
|
|
protected $updateTime = false;
|
|
protected $deleteTime = false;
|
|
|
|
// 追加属性
|
|
protected $append = [
|
|
];
|
|
|
|
|
|
/**
|
|
* 获取推荐商品列表
|
|
* @param $count
|
|
* @return void
|
|
*/
|
|
public function getGoodsList($count){
|
|
|
|
$cacheData = Cache::get(\app\common\constant\cache\Keys::COURSE_RECOMMEND);
|
|
|
|
if($cacheData){
|
|
foreach ($cacheData as &$item) {
|
|
$goodsType = $item['type'] ?? '';
|
|
$isVirtualPay = \app\common\library\pay\VirtualPayService::isUseVirtualPay($goodsType);
|
|
$item['is_virtual_pay'] = $isVirtualPay ? 1 : 0;
|
|
if ($isVirtualPay && !isset($item['_virtual_converted'])) {
|
|
$item = \app\common\library\pay\VirtualPayService::convertPriceFields($item, ['price', 'price_marking']);
|
|
$item['_virtual_converted'] = true;
|
|
}
|
|
}
|
|
unset($item);
|
|
return $cacheData;
|
|
}
|
|
|
|
$normalGoodsCondition = [
|
|
'course.status'=>1,
|
|
'sales_type'=>['like',"%alone%"],
|
|
'hide'=>0
|
|
];
|
|
|
|
$normalCount = self::with(['course'])->where($normalGoodsCondition)->count();
|
|
$start = 0;
|
|
|
|
if(($normalCount - $count) > 0){
|
|
$start = rand(0,(($normalCount - $count) - 1));
|
|
}
|
|
|
|
$list = self::with(['course'])
|
|
->where($normalGoodsCondition)
|
|
->field(['course.name','course.cover','course.price','course.price_marking','course.pay_type','course.type','course.id'])
|
|
->limit($start,$count)
|
|
->select();
|
|
|
|
if ($list){
|
|
|
|
$vipAppConfig = \app\common\model\app\Config::getConfig('vip');
|
|
|
|
foreach ($list as &$item){
|
|
unset($item['course']);
|
|
|
|
$item['is_vip_goods'] = false;
|
|
if($vipAppConfig['status'] == 1){
|
|
//判断是否为会员权益课程
|
|
$item['is_vip_goods'] = \app\api\model\app\vip\CardPrivilege::checkVipGoods($item['id']);
|
|
}
|
|
|
|
$goodsType = $item['type'] ?? '';
|
|
$isVirtualPay = \app\common\library\pay\VirtualPayService::isUseVirtualPay($goodsType);
|
|
$item['is_virtual_pay'] = $isVirtualPay ? 1 : 0;
|
|
if ($isVirtualPay) {
|
|
$item = \app\common\library\pay\VirtualPayService::convertPriceFields($item, ['price', 'price_marking']);
|
|
$item['_virtual_converted'] = true;
|
|
}
|
|
}
|
|
|
|
unset($item);
|
|
|
|
$list = collection($list)->toArray();
|
|
}
|
|
|
|
Cache::tag(\app\common\constant\cache\Keys::COURSE_TAG)->set(\app\common\constant\cache\Keys::COURSE_RECOMMEND,$list);
|
|
|
|
return $list;
|
|
}
|
|
|
|
|
|
public function course()
|
|
{
|
|
return $this->belongsTo('app\api\model\course\Course', 'goods_id', 'id', [], 'RIGHT')->setEagerlyType(0);
|
|
}
|
|
} |