246 lines
6.9 KiB
PHP
246 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace app\admin\model\course;
|
|
|
|
use think\Model;
|
|
use app\admin\model\course\GroupBind;
|
|
|
|
class Course extends Model
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
// 表名
|
|
protected $name = 'course';
|
|
|
|
// 自动写入时间戳字段
|
|
protected $autoWriteTimestamp = 'integer';
|
|
|
|
// 定义时间戳字段名
|
|
protected $createTime = 'createtime';
|
|
protected $updateTime = 'updatetime';
|
|
protected $deleteTime = false;
|
|
|
|
//需要转换的数组参数
|
|
protected $arrayField = ['unshelf_status','hide','live_video','try_listen_course','try_watch_content','shelf_type_time_pause','try_read_status','audio_path','try_listen_status','try_listen_content','try_watch_course','try_watch_status','video_path','try_watch_status','shelf_pause','hide','limit_watch','bind_data','sales_type'];
|
|
|
|
protected $timeField = ['validity_fix_time','shelf_time','unshelf_time','live_start_time','live_end_time'];
|
|
|
|
// 追加属性
|
|
protected $append = [
|
|
// 'type_text',
|
|
// 'pay_type_text',
|
|
// 'validity_type_text',
|
|
// 'validity_diy_time_text',
|
|
// 'validity_fix_time_text',
|
|
// 'shelf_time_text',
|
|
// 'unshelf_time_text'
|
|
];
|
|
|
|
|
|
/**
|
|
* 检查状态
|
|
* @param $params
|
|
* @return void
|
|
*/
|
|
public function checkStatus($params){
|
|
|
|
//立即上架
|
|
if($params['shelf_type'] == 'now'){
|
|
return 1;
|
|
}
|
|
|
|
//暂不上架
|
|
if($params['shelf_type'] == 'no'){
|
|
return 0;
|
|
}
|
|
|
|
//定时上架
|
|
if($params['shelf_type'] == 'time' && $params['shelf_time'] > time()){
|
|
return 1;
|
|
}
|
|
|
|
//定时下架
|
|
if(!empty($params['unshelf_status']) && $params['unshelf_time'] > time()){
|
|
return 0;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/**
|
|
* 转换参数格式
|
|
* @param $params 参数
|
|
* @param $type 类型 编码encode 解码decode
|
|
* @return array
|
|
*/
|
|
public function structrue($params,$type = 'encode'){
|
|
$groupBindModel = new GroupBind();
|
|
$data = [];
|
|
foreach ($params as $key => $item){
|
|
if(in_array($key,$this->arrayField)){
|
|
if($type=='encode'){
|
|
$data[$key] = json_encode($item);
|
|
}else{
|
|
$data[$key] = json_decode($item,true);
|
|
}
|
|
|
|
}elseif(in_array($key,$this->timeField)){
|
|
if($type=='decode'){
|
|
|
|
if($item){
|
|
$time = $item;
|
|
}else{
|
|
$time = time();
|
|
}
|
|
|
|
$data[$key] = date('Y-m-d H:i:s',$time);
|
|
$data[$key.'_num'] = $time;
|
|
}else{
|
|
$data[$key] = is_string($item) ? strtotime($item) : ceil($item / 1000);
|
|
}
|
|
}elseif($key=='group'){
|
|
if($type=='decode'){
|
|
$data[$key] = [];
|
|
if(isset($params['id'])){
|
|
$data[$key] = $groupBindModel->getCourseBindGroup($params['id']);
|
|
}
|
|
}else{
|
|
$data[$key] = $item;
|
|
}
|
|
}else{
|
|
$data[$key] = $item;
|
|
}
|
|
}
|
|
|
|
|
|
if($type=='encode'){
|
|
foreach ($this->timeField as $timeField){
|
|
unset($data[$timeField.'_num']);
|
|
}
|
|
|
|
if(isset($data['live_type']) && $data['live_type'] == 2){
|
|
$data['live_end_time'] = $data['live_start_time'] + $data['live_video_duration'];
|
|
}
|
|
}
|
|
|
|
|
|
$data['uniacid'] = UNIACID;
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function getTypeList()
|
|
{
|
|
return ['article' => __('Article'), 'video' => __('Video'), 'audio' => __('Audio'), 'column' => __('Column')];
|
|
}
|
|
|
|
public function getPayTypeList()
|
|
{
|
|
return ['free' => __('Free'), 'pay' => __('Pay'), 'password' => __('Password')];
|
|
}
|
|
|
|
public function getValidityTypeList()
|
|
{
|
|
return ['diy' => __('Diy'), 'fix' => __('Fix'), 'long' => __('Long')];
|
|
}
|
|
|
|
|
|
public function getTypeTextAttr($value, $data)
|
|
{
|
|
$value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
|
|
$list = $this->getTypeList();
|
|
return isset($list[$value]) ? $list[$value] : '';
|
|
}
|
|
|
|
|
|
public function getPayTypeTextAttr($value, $data)
|
|
{
|
|
$value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
|
|
$list = $this->getPayTypeList();
|
|
return isset($list[$value]) ? $list[$value] : '';
|
|
}
|
|
|
|
|
|
public function getValidityTypeTextAttr($value, $data)
|
|
{
|
|
$value = $value ? $value : (isset($data['validity_type']) ? $data['validity_type'] : '');
|
|
$list = $this->getValidityTypeList();
|
|
return isset($list[$value]) ? $list[$value] : '';
|
|
}
|
|
|
|
|
|
public function getValidityDiyTimeTextAttr($value, $data)
|
|
{
|
|
$value = $value ? $value : (isset($data['validity_diy_time']) ? $data['validity_diy_time'] : '');
|
|
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
|
|
}
|
|
|
|
|
|
public function getValidityFixTimeTextAttr($value, $data)
|
|
{
|
|
$value = $value ? $value : (isset($data['validity_fix_time']) ? $data['validity_fix_time'] : '');
|
|
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
|
|
}
|
|
|
|
|
|
public function getShelfTimeTextAttr($value, $data)
|
|
{
|
|
$value = $value ? $value : (isset($data['shelf_time']) ? $data['shelf_time'] : '');
|
|
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
|
|
}
|
|
|
|
|
|
public function getUnshelfTimeTextAttr($value, $data)
|
|
{
|
|
$value = $value ? $value : (isset($data['unshelf_time']) ? $data['unshelf_time'] : '');
|
|
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
|
|
}
|
|
|
|
protected function setValidityDiyTimeAttr($value)
|
|
{
|
|
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
|
|
}
|
|
|
|
protected function setValidityFixTimeAttr($value)
|
|
{
|
|
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
|
|
}
|
|
|
|
protected function setShelfTimeAttr($value)
|
|
{
|
|
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
|
|
}
|
|
|
|
protected function setUnshelfTimeAttr($value)
|
|
{
|
|
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
|
|
}
|
|
|
|
|
|
public function agentGoods()
|
|
{
|
|
return $this->belongsTo('app\admin\model\app\agent\Goods', 'id', 'goods_id', [], 'left')->setEagerlyType(0);
|
|
}
|
|
|
|
|
|
public function study()
|
|
{
|
|
return $this->belongsTo('app\admin\model\data\Study', 'id','course_id', [], 'LEFT')->setEagerlyType(0);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取直播课程 IDS
|
|
* @return void
|
|
*/
|
|
public function getLiveCourseIds(){
|
|
return self::where([
|
|
'type'=>'live'
|
|
])->order('id','desc')->column('id');
|
|
}
|
|
}
|