初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model\course;
|
||||
|
||||
use think\Model;
|
||||
use app\admin\model\course\Course;
|
||||
class ColumnBind extends Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 表名
|
||||
protected $name = 'course_bind_column';
|
||||
|
||||
// 自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'integer';
|
||||
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = false;
|
||||
protected $deleteTime = false;
|
||||
|
||||
// 追加属性
|
||||
protected $append = [
|
||||
|
||||
];
|
||||
|
||||
|
||||
public function course()
|
||||
{
|
||||
return $this->belongsTo('Course', 'course_id', 'id', [], 'right')->setEagerlyType(0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 专栏绑定的课程
|
||||
* 考虑绑定类型可能还有目录
|
||||
* @return mixed
|
||||
*/
|
||||
public function bindcourse()
|
||||
{
|
||||
return $this->belongsTo('Course', 'course_id', 'id', [], 'left')->setEagerlyType(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 内容绑定专栏
|
||||
* @param $courseId 内容ID
|
||||
* @param $column 专栏
|
||||
* @return void
|
||||
*/
|
||||
public function setCourseBindColumn($courseId,$columns){
|
||||
|
||||
$columnIds = [];
|
||||
|
||||
if($columns){
|
||||
foreach ($columns as $column){
|
||||
$columnIds[] = $column['id'];
|
||||
}
|
||||
}
|
||||
|
||||
$courseBindColumnIds = $this->getCourseBindColumnIds($courseId);
|
||||
|
||||
//判断已经绑定的专栏列表 是不是在这次更新的列表中,不在则删除
|
||||
if(!empty($courseBindColumnIds)){
|
||||
foreach ($courseBindColumnIds as $columnId){
|
||||
if(!in_array($columnId,$columnIds)){
|
||||
self::where([
|
||||
'column_id'=>$columnId,
|
||||
'course_id'=>$courseId,
|
||||
])->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(!empty($columnIds)){
|
||||
foreach ($columnIds as $columnId){
|
||||
$this->setColumnBindCourse($columnId,[$courseId]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取内容绑定的专栏
|
||||
* @param $courseId
|
||||
* @return array
|
||||
*/
|
||||
public function getCourseBindColumn($courseId){
|
||||
|
||||
$CourseModel = new \app\admin\model\course\Course();
|
||||
|
||||
$ids = $this->getCourseBindColumnIds($courseId);
|
||||
|
||||
return $CourseModel->where('id','in',$ids)->field(['id','name','createtime'])->select();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取内容绑定的专栏ID
|
||||
* @param $courseId
|
||||
* @return array
|
||||
*/
|
||||
public function getCourseBindColumnIds($courseId){
|
||||
|
||||
$columnIds = self::where(['course_id'=>$courseId])->field('column_id')->select();
|
||||
|
||||
if(!$columnIds){
|
||||
return [];
|
||||
}
|
||||
|
||||
$ids = [];
|
||||
foreach ($columnIds as $id){
|
||||
$ids[] = $id['column_id'];
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定内容
|
||||
* @param $columnId
|
||||
* @param $courseIds
|
||||
* @param $dir
|
||||
* @return bool
|
||||
*/
|
||||
public function setColumnBindCourse($columnId,$courseIds,$p_id = 0,$type = 1,$title = ''){
|
||||
|
||||
$bindCourseId = $this->getBindCourseId($columnId);
|
||||
|
||||
//获取专栏已经绑定过的课程,如果需要被绑定的课程不再已经绑定的列表中,则添加
|
||||
if(!empty($courseIds)){
|
||||
foreach ($courseIds as $item){
|
||||
if(!in_array($item,$bindCourseId)){
|
||||
$row = [
|
||||
'course_id'=>$item,
|
||||
'column_id'=>$columnId,
|
||||
'try'=>0,
|
||||
'type'=>$type,
|
||||
'title'=>$title,
|
||||
'p_id'=>$p_id,
|
||||
'uniacid'=>UNIACID,
|
||||
'createtime'=>time(),
|
||||
];
|
||||
$row['sort']=$this->defineSort($columnId,$p_id);
|
||||
|
||||
//改变课程的sales_type
|
||||
$courseInfo = Course::where([
|
||||
'id'=>$item
|
||||
])->field("sales_type")->find();
|
||||
|
||||
if($courseInfo){
|
||||
$salesType = json_decode($courseInfo['sales_type'],true);
|
||||
if(!in_array("column",$salesType)){
|
||||
$salesType[] = "column";
|
||||
}
|
||||
Course::where([
|
||||
'id'=>$item
|
||||
])->update([
|
||||
'sales_type'=>json_encode($salesType)
|
||||
]);
|
||||
}
|
||||
|
||||
self::insert($row);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取绑定的内容ID
|
||||
* @param $columnId
|
||||
* @return mixed
|
||||
*/
|
||||
public function getBindCourseId($columnId){
|
||||
$list = self::where([
|
||||
'column_id'=>$columnId,
|
||||
'type'=>1
|
||||
])->field('course_id')->select();
|
||||
|
||||
$ids = [];
|
||||
if(!empty($list)){
|
||||
foreach ($list as $item){
|
||||
$ids[] = $item['course_id'];
|
||||
}
|
||||
}
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取新绑定项的排序值
|
||||
* 根据专栏(Course)配置的 column_update 决定新条目落在同层的头部还是尾部:
|
||||
* - column_update = 'desc':最近添加的在前 → 取同层 min(sort) - 1
|
||||
* - column_update = 'asc' 或未设置:最近添加的在后 → 取同层 max(sort) + 1
|
||||
* @param int $columnId 专栏ID(tuzhi_course.id)
|
||||
* @param int $p_id 上级目录ID
|
||||
* @return int
|
||||
*/
|
||||
public function defineSort($columnId,$p_id = 0){
|
||||
//读取专栏的更新顺序配置
|
||||
$columnUpdate = Course::where(['id'=>$columnId])->value('column_update');
|
||||
$columnUpdate = $columnUpdate === 'desc' ? 'desc' : 'asc';
|
||||
|
||||
if($columnUpdate === 'desc'){
|
||||
//最近添加的在前:排到同层最前面
|
||||
$minItem = self::where([
|
||||
'column_id'=>$columnId,
|
||||
'p_id'=>$p_id
|
||||
])->field("sort")->order("sort asc")->find();
|
||||
if(!$minItem){
|
||||
return 0;
|
||||
}
|
||||
return (int)$minItem['sort'] - 1;
|
||||
}
|
||||
|
||||
//最近添加的在后:排到同层最后面
|
||||
$maxItem = self::where([
|
||||
'column_id'=>$columnId,
|
||||
'p_id'=>$p_id
|
||||
])->field("sort")->order("sort desc")->find();
|
||||
if(!$maxItem){
|
||||
return 0;
|
||||
}
|
||||
return (int)$maxItem['sort'] + 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user