195 lines
5.2 KiB
PHP
195 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\live;
|
|
|
|
use app\common\controller\Backend;
|
|
use think\Db;
|
|
|
|
/**
|
|
* 讲师管理
|
|
*
|
|
* @icon fa fa-user
|
|
*/
|
|
class Lecturer extends Backend
|
|
{
|
|
|
|
/**
|
|
* Lecturer模型对象
|
|
* @var \app\admin\model\live\Lecturer
|
|
*/
|
|
protected $model = null;
|
|
|
|
/**
|
|
* Admin模型对象
|
|
* @var \app\admin\model\Admin
|
|
*/
|
|
protected $adminModel = null;
|
|
|
|
protected $searchFields = 'id,username,name';
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = new \app\admin\model\live\Lecturer;
|
|
$this->adminModel = new \app\admin\model\Admin;
|
|
}
|
|
|
|
|
|
/**
|
|
* 查看讲师列表
|
|
*/
|
|
public function index()
|
|
{
|
|
//设置过滤方法
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
//如果发送的来源是Selectpage,则转发到Selectpage
|
|
if ($this->request->request('keyField')) {
|
|
return $this->selectpage();
|
|
}
|
|
|
|
//讲师角色写死在代码中,通过固定ID获取
|
|
$lecturerGroupId = \app\admin\model\auth\Group::LECTURER_GROUP_ID;
|
|
|
|
//获取属于讲师角色组的管理员ID
|
|
$lecturerUids = Db::name('auth_group_access')
|
|
->where('group_id', $lecturerGroupId)
|
|
->column('uid');
|
|
if (empty($lecturerUids)) {
|
|
return $this->success("获取成功", ["total" => 0, "rows" => []]);
|
|
}
|
|
|
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
|
$list = $this->adminModel
|
|
->where('id', 'in', $lecturerUids)
|
|
->where($where)
|
|
->order($sort, $order)
|
|
->paginate($limit);
|
|
|
|
$rows = $list->toArray();
|
|
|
|
if (!empty($rows['data'])) {
|
|
foreach ($rows['data'] as &$item) {
|
|
$item['lecturer_course_count'] = Db::name('live_lecturer')->where('admin_id', $item['id'])->count();
|
|
}
|
|
}
|
|
|
|
$result = ["total" => $rows['total'], "rows" => $rows['data']];
|
|
return $this->success("获取成功", $result);
|
|
}
|
|
|
|
|
|
/**
|
|
* 删除讲师
|
|
*/
|
|
public function del($ids = null)
|
|
{
|
|
if (false === $this->request->isPost()) {
|
|
$this->error(__("Invalid parameters"));
|
|
}
|
|
$ids = $ids ?: $this->request->post("ids");
|
|
if (empty($ids)) {
|
|
$this->error(__('Parameter %s can not be empty', 'ids'));
|
|
}
|
|
|
|
$list = $this->adminModel->where('id', 'in', $ids)->select();
|
|
|
|
$count = 0;
|
|
Db::startTrans();
|
|
try {
|
|
foreach ($list as $item) {
|
|
if ($item->id == 1) {
|
|
continue;
|
|
}
|
|
//清理讲师课程关联
|
|
Db::name('live_lecturer')->where('admin_id', $item->id)->delete();
|
|
//清理角色组关联
|
|
Db::name('auth_group_access')->where('uid', $item->id)->delete();
|
|
$count += $item->delete();
|
|
}
|
|
Db::commit();
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($count) {
|
|
$this->success("操作成功");
|
|
}
|
|
$this->error(__('No rows were deleted'));
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取讲师绑定的课程列表
|
|
*/
|
|
public function bindCourse()
|
|
{
|
|
$adminId = input('admin_id');
|
|
if (!$adminId) {
|
|
$this->error("参数错误,讲师ID不能为空");
|
|
}
|
|
|
|
//设置过滤方法
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
|
$list = $this->model
|
|
->with(['course'])
|
|
->where('admin_id', $adminId)
|
|
->where($where)
|
|
->order($sort, $order)
|
|
->paginate($limit);
|
|
|
|
$result = ["total" => $list->total(), "rows" => $list->items()];
|
|
return $this->success("获取成功", $result);
|
|
}
|
|
|
|
|
|
/**
|
|
* 添加讲师绑定课程
|
|
*/
|
|
public function bindCourseAdd()
|
|
{
|
|
$adminId = $this->request->post('admin_id');
|
|
$courseIds = $this->request->post('course_ids/a', []);
|
|
if (!$adminId) {
|
|
$this->error("参数错误,讲师ID不能为空");
|
|
}
|
|
if (empty($courseIds)) {
|
|
$this->error("请选择要绑定的课程");
|
|
}
|
|
|
|
foreach ($courseIds as $courseId) {
|
|
$exists = $this->model->where(['course_id' => $courseId, 'admin_id' => $adminId])->count();
|
|
if (!$exists) {
|
|
$this->model->create([
|
|
'course_id' => $courseId,
|
|
'admin_id' => $adminId,
|
|
]);
|
|
}
|
|
}
|
|
|
|
$this->success("操作成功");
|
|
}
|
|
|
|
|
|
/**
|
|
* 删除讲师绑定课程
|
|
*/
|
|
public function bindCourseDel()
|
|
{
|
|
if (false === $this->request->isPost()) {
|
|
$this->error(__("Invalid parameters"));
|
|
}
|
|
$id = $this->request->post('id');
|
|
$ids = $this->request->post('ids');
|
|
$deleteIds = $id ?: $ids;
|
|
if (empty($deleteIds)) {
|
|
$this->error("参数错误,请选择要删除的记录");
|
|
}
|
|
|
|
$this->model->where('id', 'in', $deleteIds)->delete();
|
|
$this->success("操作成功");
|
|
}
|
|
}
|