Lecturer.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. <?php
  2. namespace app\adminall\controller;
  3. use app\model\Miniprogram;
  4. use app\model\TrainClassAssign;
  5. use app\model\TrainClassView;
  6. use think\facade\Request;
  7. use think\facade\View;
  8. use think\facade\Db;
  9. use app\model\TrainClass;
  10. use app\model\TrainCourse;
  11. use app\model\TrainType;
  12. use app\model\TrainDoneLog;
  13. use app\model\TrainSumup;
  14. use app\model\Org;
  15. use app\logics\OrgLogic;
  16. use app\model\Employee;
  17. use app\event\Msg;
  18. use wx\miniprogram\Qrcode;
  19. use app\model\User;
  20. use app\model\Company;
  21. use app\model\TrainCourseView;
  22. use app\model\TrainClassCate;
  23. use app\model\CreditsSetting;
  24. use app\model\ActivityCollor;
  25. use app\model\Broadcast;
  26. use app\model\Lecturer as LecturerModel;
  27. use app\model\LecturerCompany;
  28. class Lecturer
  29. {
  30. private $root_id = 0;//总后台店面id默认0
  31. /**
  32. * 课件列表页面展示
  33. */
  34. public function course_index()
  35. {
  36. $type = LecturerCompany::where('1=1')->select()->toArray();
  37. View::assign('type', $type);
  38. return View::fetch();
  39. }
  40. /**
  41. * 课件列表
  42. */
  43. public function trainCourseList()
  44. {
  45. $param = request()->param();
  46. $condition[] = ['root_id', '=', $this->root_id];
  47. $condition[] = ['del', '=', 0];
  48. if (isset($param['keyword']) && !empty($param['keyword'])) $condition[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  49. if (isset($param['company_id']) && !empty($param['company_id'])) $condition[] = ['lecturer_company_id', '=', $param['company_id']];
  50. $data = LecturerModel::with(['company'=>function($query){
  51. $query->bind(['company_name'=>'name']);
  52. }])->withCount(['train'])->where($condition)->page($param['page'], $param['limit'])->order('id desc')->select()->toArray();
  53. $count = LecturerModel::where($condition)->count();
  54. $ids = array_column($data,'id');
  55. //观看人次
  56. $view_count = TrainClass::where([['lecturer_id','in',$ids]])->group('lecturer_id')->column('sum(view)','lecturer_id');
  57. //收藏数量
  58. $collor_count = ActivityCollor::where([['aid','in',$ids],['type','=',2]])->group('aid')->column('count(id)','aid');
  59. //直播次数
  60. $train_broadcast_view = Broadcast::where([['lecturer_id','in',$ids]])->group('lecturer_id')->column('count(id)','lecturer_id');
  61. foreach ($data as $key => $value) {
  62. $data[$key]['train_view'] = isset($view_count[$value['id']]) ? $view_count[$value['id']] : 0;
  63. $data[$key]['train_collor_view'] = isset($collor_count[$value['id']]) ? $collor_count[$value['id']] : 0;
  64. $data[$key]['train_broadcast_view'] = isset($train_broadcast_view[$value['id']]) ? $train_broadcast_view[$value['id']] : 0;
  65. }
  66. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  67. }
  68. /*
  69. * 课件场景列表
  70. */
  71. public function type_index()
  72. {
  73. if (!Request::isAjax()) {
  74. return View::fetch();
  75. }
  76. $param = request()->param();
  77. $condition = [
  78. 'root_id' => $this->root_id,
  79. 'from_type' => 0
  80. ];
  81. $data = TrainType::where($condition)->page($param['page'], $param['limit'])
  82. // ->order('train_course_count desc,id desc')
  83. ->order(['show'=>'asc','id'=>'desc'])
  84. ->select();
  85. $count = TrainType::where($condition)->count();
  86. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  87. }
  88. /*
  89. * 课件场景添加
  90. */
  91. public function add_train_type()
  92. {
  93. $type = Request::param('type');
  94. if (empty($type)) return json(['code' => 1, 'msg' => '请输入场景名称']);
  95. $isAdd = TrainType::where(['type' => $type, 'root_id' => $this->root_id])->count();
  96. if ($isAdd > 0) return json(['code' => 1, 'msg' => '场景已存在']);
  97. $objId = TrainType::insertGetId(['type' => $type, 'root_id' => $this->root_id]);
  98. return json(['code' => 0, 'msg' => '添加成功', 'data' => ['id' => $objId, 'type' => $type]]);
  99. }
  100. /*
  101. * 场景删除
  102. */
  103. public function del_train_type()
  104. {
  105. $id = Request::param('id');
  106. $data = TrainType::where(['id' => $id, 'root_id' => $this->root_id])->find();
  107. if (!$data) return json(['code' => 1, 'msg' => '删除失败']);
  108. $other_id = TrainType::where(['root_id'=>$this->root_id , 'show'=>1])->value('id');
  109. if(empty($other_id)) $other_id = TrainType::insertGetId(['type'=>'其它','root_id'=>$this->root_id,'show'=>1]);
  110. TrainCourse::where(['root_id'=>$this->root_id , 'type_id'=>$id])->update(['type_id'=>$other_id]);
  111. $data->delete();
  112. return json(['code' => 0, 'msg' => '删除成功']);
  113. }
  114. /*
  115. * 课件场景修改
  116. */
  117. public function edit_train_type()
  118. {
  119. $id = Request::param('id');
  120. $type = Request::param('type');
  121. if (empty($type)) return json(['code' => 1, 'msg' => '请输入场景名称']);
  122. if (TrainType::where(['id' => $id, 'root_id' => $this->root_id])->update(['type' => $type])) {
  123. return json(['code' => 0, 'msg' => '修改成功']);
  124. } else {
  125. return json(['code' => 1, 'msg' => '修改失败']);
  126. }
  127. }
  128. /**
  129. * 课件添加页面
  130. */
  131. public function course_add()
  132. {
  133. $condition = [
  134. 'root_id' => $this->root_id,
  135. 'from_type' => 0
  136. ];
  137. $type = TrainType::where($condition)->select()->toArray();
  138. View::assign('type', $type);
  139. return View::fetch();
  140. }
  141. /*
  142. * 课件保存
  143. */
  144. public function courseAddSave()
  145. {
  146. $data = Request::only(['title', 'type_id', 'type', 'content', 'video_img', 'file_image', 'file_video', 'file_audio', 'file_pdf', 'time_check', 'word_check', 'study_time', 'comment_word_num']);
  147. if ($data['time_check'] == 0 && $data['word_check'] == 0) return json(['code' => 1, 'msg' => '完成条件最少选择一项']);
  148. $ask = input('content');
  149. $ask = str_replace('&nbsp;', '', $ask);
  150. if (trim($ask) == '' && $data['type'] != 'pdf') {
  151. return json(['code' => 1, 'msg' => '课件内容不能为空']);
  152. }
  153. $newData = [
  154. 'root_id' => $this->root_id,
  155. 'title' => $data['title'],
  156. 'type_id' => $data['type_id'],
  157. 'content' => $data['content'],
  158. 'type' => $data['type'],
  159. 'study_time' => $data['time_check'] == 0 ? 0 : $data['study_time'],
  160. 'comment_word_num' => $data['word_check'] == 0 ? 0 : $data['comment_word_num']
  161. ];
  162. if ($data['type'] == 'video') {
  163. if (empty($data['file_video'])) return json(['code' => 1, 'msg' => '请选择视频']);
  164. $arr = explode(',', $data['file_video']);
  165. $newData['file'] = end($arr);
  166. $newData['video_img'] = !empty($data['video_img']) ? $data['video_img'] : null;
  167. } elseif ($data['type'] == 'image') {
  168. if (empty($data['file_image'])) return json(['code' => 1, 'msg' => '请选择图片']);
  169. $newData['file'] = $data['file_image'];
  170. } elseif ($data['type'] == 'pdf') {
  171. if (empty($data['file_pdf'])) return json(['code' => 1, 'msg' => '请选择pdf课程文件']);
  172. $arr = explode(',', $data['file_pdf']);
  173. $newData['file'] = end($arr);
  174. } else {
  175. if (empty($data['file_audio'])) return json(['code' => 1, 'msg' => '请选择音频']);
  176. $arr = explode(',', $data['file_audio']);
  177. $newData['file'] = end($arr);
  178. }
  179. if (TrainCourse::insert($newData)) {
  180. return json(['code' => 0, 'msg' => '添加成功']);
  181. } else {
  182. return json(['code' => 1, 'msg' => '添加失败']);
  183. }
  184. }
  185. /**
  186. * 课件富文本文件上传
  187. */
  188. public function fileupload()
  189. {
  190. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  191. $url = 'https://' . $ali_oss_bindurl . '/' . Request::param('file');
  192. return json(['code' => 0, 'data' => ['src' => $url]]);
  193. }
  194. /**
  195. * 讲师编辑页面
  196. */
  197. public function course_edit()
  198. {
  199. $id = Request::param('id');
  200. $data = LecturerModel::where(['id' => $id, 'root_id' => $this->root_id])->find();
  201. View::assign('data', $data);
  202. return View::fetch();
  203. }
  204. /**
  205. * 课件活动编辑保存
  206. */
  207. public function courseEditSave()
  208. {
  209. $data = Request::only(['title', 'type_id', 'type', 'content', 'video_img', 'file_image', 'file_audio', 'file_video', 'file_pdf', 'id', 'time_check', 'word_check', 'study_time', 'comment_word_num']);
  210. if ($data['time_check'] == 0 && $data['word_check'] == 0) return json(['code' => 1, 'msg' => '完成条件最少选择一项']);
  211. $ask = input('content');
  212. $ask = str_replace('&nbsp;', '', $ask);
  213. if (trim($ask) == '' && $data['type'] != 'pdf') {
  214. return json(['code' => 1, 'msg' => '课件内容不能为空']);
  215. }
  216. $newData = [
  217. 'root_id' => $this->root_id,
  218. 'title' => $data['title'],
  219. 'type_id' => $data['type_id'],
  220. 'content' => $data['content'],
  221. 'type' => $data['type'],
  222. 'study_time' => $data['time_check'] == 0 ? 0 : $data['study_time'],
  223. 'comment_word_num' => $data['word_check'] == 0 ? 0 : $data['comment_word_num']
  224. ];
  225. // 添加判断
  226. $course = TrainCourse::where(['root_id' => $this->root_id, 'id' => $data['id']])->find();
  227. if (empty($course)) return json(['code' => 1, 'msg' => '课件不存在']);
  228. // 判断是否更换类型,如果更换类型,则内容需要重新上传
  229. if ($course->type != $data['type'] && empty($data['file_' . $data['type']])) {
  230. $msgType = [
  231. 'video' => '视频',
  232. 'audio' => '音频',
  233. 'pdf' => 'pdf课程文件',
  234. 'image' => '图片',
  235. ];
  236. return json(['code' => 1, 'msg' => '请选择' . $msgType[$data['type']]]);
  237. }
  238. if (!empty($data['file_' . $data['type']])) {
  239. if ($data['type'] == 'image') {
  240. $newData['file'] = $data['file_image'];
  241. } else {
  242. $arr = explode(',', $data['file_' . $data['type']]);
  243. $newData['file'] = end($arr);
  244. if ('video' == $data['type'])
  245. $newData['video_img'] = !empty($data['video_img']) ? $data['video_img'] : null;
  246. }
  247. }
  248. $course->save($newData);
  249. // if ($data['type'] == 'video') {
  250. // if (empty($data['file_video'])) return json(['code' => 1, 'msg' => '请选择视频']);
  251. // $arr = explode(',', $data['file_video']);
  252. // $newData['file'] = end($arr);
  253. // $newData['video_img'] = !empty($data['video_img']) ? $data['video_img'] : null;
  254. // } elseif ($data['type'] == 'audio') {
  255. // if (empty($data['file_audio'])) return json(['code' => 1, 'msg' => '请选择音频']);
  256. // $arr = explode(',', $data['file_audio']);
  257. // $newData['file'] = end($arr);
  258. // } elseif ($data['type'] == 'pdf') {
  259. // if (empty($data['file_pdf'])) return json(['code' => 1, 'msg' => '请选择pdf课程文件']);
  260. // $arr = explode(',', $data['file_pdf']);
  261. // $newData['file'] = end($arr);
  262. // } else {
  263. // if (empty($data['file_image'])) return json(['code' => 1, 'msg' => '请选择图片']);
  264. // $newData['file'] = $data['file_image'];
  265. // }
  266. // TrainCourse::where(['root_id' => $this->root_id, 'id' => $data['id']])->update($newData);
  267. return json(['code' => 0, 'msg' => '修改成功']);
  268. }
  269. /**
  270. * 课件删除
  271. */
  272. public function del_course_train()
  273. {
  274. $id = Request::param('id');
  275. $where = [
  276. ['root_id','=',0],
  277. ['id','=',$id]
  278. ];
  279. LecturerModel::where($where)->update(['del'=>1]);
  280. return json(['code' => 0, 'msg' => '删除成功']);
  281. }
  282. /*
  283. * 课程培训
  284. * 新逻辑 区分指派前和指派后 以train_class表的train_employee字段区分,为空则为指派前阶段,有值为指派后阶段
  285. * 指派前阶段为自由学习阶段,所有人都可以看到课程并学习,指派后则为指派的人能学习
  286. * 量化考核中也要区分,指派前和指派后互相数据不关联
  287. */
  288. public function class_index()
  289. {
  290. if (!Request::isAjax()) {
  291. return View::fetch();
  292. }
  293. $param = request()->param();
  294. $condition[] = ['del', '=', 0];
  295. $condition[] = ['root_id', '=', $this->root_id];
  296. $condition[] = ['from_type', 'in', [0, 2]];
  297. if (isset($param['keyword'])) $condition[] = ['title', 'like', '%' . $param['keyword'] . '%'];
  298. if (isset($param['date'])) $condition[] = ['addtime', 'like', '%' . $param['date'] . '%'];
  299. $data = TrainClass::withCount(['doneLog' => function ($query, &$alias) {
  300. $query->where('done_percent', 100);
  301. $alias = 'completeCount';
  302. }])->withCount(['doneLog' => function ($query, &$alias) {
  303. $query->where([['done_percent', '<', 100], ['done_percent', '>', 0]]);
  304. $alias = 'noFinishCount';
  305. }])->where($condition)->page($param['page'], $param['limit'])->order('addtime desc')
  306. ->field('id,title,course_id,addtime,org_id,train_employee')->select()->toArray();
  307. $orgCount = Employee::where([['root_id', '=', $this->root_id], ['state', 'like', '%在职%'], ['org_id', '>', 0], ['uid', '>', 0]])->group('org_id')->column('count(org_id)', 'org_id');
  308. //总人数
  309. $total = Employee::where([['root_id','=',$this->root_id],['state','=','在职'],['uid','>',0]])->count();
  310. //课程总结
  311. $trainSummary = TrainSumup::where([['root_id', '=', $this->root_id], ['course_id', '=', 0]])->group('class_id,employee_id')->column('count(id),class_id');
  312. foreach ($data as &$v) {
  313. //培训人数
  314. $v['trainNumber'] = 0;
  315. if (!empty($orgCount) && !empty($v['org_id'])) {
  316. $trainArr = explode(',', $v['org_id']);
  317. foreach ($trainArr as $c) {
  318. isset($orgCount[$c]) ? $v['trainNumber'] += $orgCount[$c] : 0;
  319. }
  320. }
  321. //章节数
  322. $v['courseCount'] = empty($v['course_id']) ? 0 : count(explode(',', $v['course_id']));
  323. //指派人员
  324. $employee_id = explode(',', $v['train_employee']);
  325. $v['train_employee'] = count(array_filter(explode(',', $v['train_employee'])));
  326. //未开始人数
  327. $v['noStartCount'] = $v['train_employee'] - ($v['completeCount'] + $v['noFinishCount']);
  328. $v['noStartCount'] = $v['noStartCount'] >= 0 ? $v['noStartCount'] : 0;
  329. //完成观后感
  330. $v['summaryCount'] = isset($trainSummary[$v['id']]) ? $trainSummary[$v['id']] : 0;
  331. //新逻辑 区分指派前和指派后 ,这段删除就是之前的逻辑
  332. if ($v['train_employee']) {
  333. $v['trainNumber'] = $v['train_employee'];
  334. //指派后,完成人数
  335. $v['completeCount'] = TrainDoneLog::where([['class_id','=',$v['id']],['employee_id','in',$employee_id],['done_percent','=',100]])->count();
  336. $nofinish = TrainDoneLog::where([['class_id','=',$v['id']],['employee_id','in',$employee_id],['done_percent','in',[1,99]]])->count();
  337. //未完成人数
  338. $v['noFinishCount'] = count($employee_id) - $v['completeCount'];
  339. //未开始
  340. $v['noStartCount'] = count($employee_id) - $v['completeCount'] - $nofinish;
  341. //感悟数量
  342. $v['summaryCount'] = TrainSumup::where([['employee_id','in',$employee_id],['class_id','=', $v['id']], ['course_id', '=', 0]])->group('employee_id')->count();
  343. }else{
  344. //指派前,培训人数和学习人数都是所有人
  345. $v['trainNumber'] = $v['train_employee'] = $total;
  346. //未开始人数
  347. $v['noStartCount'] = $total - ($v['completeCount'] + $v['noFinishCount']);
  348. //未完成
  349. $v['noFinishCount'] = $total - $v['completeCount'];
  350. }
  351. }
  352. $count = TrainClass::where($condition)->count();
  353. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  354. }
  355. /**
  356. * 分类管理
  357. */
  358. public function class_cate()
  359. {
  360. return View::fetch();
  361. }
  362. /**
  363. * 分类管理添加
  364. */
  365. public function class_cate_add()
  366. {
  367. $root_id = $this->root_id;
  368. $param = Request::only(['name' => '']);
  369. $param['name'] = str_replace(' ', '', $param['name']);
  370. $w[] = ['root_id', '=', 0];
  371. $w[] = ['name', '=', $param['name']];
  372. $find = LecturerCompany::where($w)->findOrEmpty();
  373. if (!$find->isEmpty()) return json(['code' => 1, 'data' => '分类已存在', 'msg' => '分类已存在']);
  374. $id = LecturerCompany::insertGetId(['root_id' => $root_id, 'name' => $param['name']]);
  375. return json(['code' => 0, 'data' => '添加成功', 'msg' => '添加成功', 'id' => $id]);
  376. }
  377. /**
  378. * 讲师所属企业列表
  379. */
  380. public function class_cate_list()
  381. {
  382. $root_id = 0;
  383. $param = Request::only(['page' => 1, 'limit' => 10,'keyword'=>'']);
  384. $w[] = ['root_id', '=', $root_id];
  385. if($param['keyword']) $w[] = ['name','like','%'.$param['keyword'].'%'];
  386. $list = LecturerCompany::where($w)->page($param['page'], $param['limit'])->order('id asc')->select();
  387. $count = LecturerCompany::where($w)->count();
  388. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  389. }
  390. /**
  391. * 分类管理编辑
  392. */
  393. public function class_cate_edit()
  394. {
  395. $root_id = 0;
  396. $param = Request::only(['id' => 0, 'name' => '']);
  397. $w[] = ['root_id', '=', $root_id];
  398. $w[] = ['id', '<>', $param['id']];
  399. $w[] = ['name', '=', $param['name']];
  400. $find = LecturerCompany::where($w)->findOrEmpty();
  401. if(!$find->isEmpty()) return json(['code' => 1, 'data' => '企业已存在', 'msg' => '企业已存在']);
  402. $w1[] = ['root_id', '=', $root_id];
  403. $w1[] = ['id', '=', $param['id']];
  404. LecturerCompany::where($w1)->update(['name' => $param['name']]);
  405. return json(['code' => 0, 'data' => '修改成功', 'msg' => '修改成功']);
  406. }
  407. /**
  408. * 分类管理删除
  409. */
  410. public function class_cate_del()
  411. {
  412. $root_id = $this->root_id;
  413. $param = Request::only(['id' => 0]);
  414. $w[] = ['root_id', '=',0];
  415. $w[] = ['id', '=', $param['id']];
  416. $find = LecturerCompany::where($w)->delete();
  417. return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']);
  418. }
  419. /*
  420. * 删除标签或分类前,查询关联的内容数量
  421. */
  422. public function with_type_count()
  423. {
  424. $param = Request::param();
  425. if($param['type'] == 'cate')
  426. {
  427. $field = 'cate';
  428. }else{
  429. $field = 'label';
  430. }
  431. $where = [
  432. ['root_id','=',$this->root_id],
  433. ['del','=',0],
  434. [$field,'=',$param[$field]]
  435. ];
  436. $count = TrainClass::where($where)->count();
  437. if($count > 0)
  438. {
  439. $type = $field == 'cate' ? '分类' : '标签' ;
  440. $msg = '该'.$type.'关联'.$count.'个'.'课程,删除后该内容将归为其它分类。';
  441. }else{
  442. $msg = '确定删除该分类吗?';
  443. }
  444. return $msg;
  445. }
  446. /*
  447. * 分类、标签删除
  448. */
  449. public function class_cate_or_label_del()
  450. {
  451. $param = Request::param();
  452. $trainCate = TrainClassCate::where(['root_id'=>$this->root_id,'id'=>$param['id']])->find();
  453. if(empty($trainCate)) return json(['code' => 1, 'msg' => '数据不存在']);
  454. Db::startTrans();
  455. try {
  456. if($param['type'] == 'cate')
  457. {
  458. //查询是否存在其它分类
  459. $type_id = TrainClassCate::where(['root_id'=>$this->root_id,'pid'=>0,'show'=>1])->value('id');
  460. if(empty($type_id)) $type_id = TrainClassCate::insertGetId(['pid'=>0,'name'=>'其它','root_id'=>$this->root_id,'show'=>1]);
  461. //更新关联内容
  462. TrainClass::where(['root_id'=>$this->root_id,'cate'=>$trainCate['name']])->update(['cate'=>'其它']);
  463. TrainClassCate::where(['root_id'=>$this->root_id,'pid'=>$trainCate['id']])->update(['pid'=>$type_id]);
  464. //去重
  465. $groupId = TrainClassCate::where(['root_id'=>$this->root_id,'pid'=>$type_id,'show'=>1])->min('id');
  466. TrainClassCate::where([['root_id','=',$this->root_id],['pid','=',$type_id],['show','=',1],['id','<>',$groupId]])->delete();
  467. //删除分类
  468. $trainCate->delete();
  469. }else{
  470. $label_id = TrainClassCate::where(['root_id'=>$this->root_id,'pid'=>$trainCate['pid'],'show'=>1])->value('id');
  471. if(empty($label_id)) $label_id = TrainClassCate::insertGetId(['pid'=>$trainCate['pid'],'name'=>'其它','root_id'=>$this->root_id,'show'=>1]);
  472. //更新关联内容
  473. TrainClass::where(['root_id'=>$this->root_id,'label'=>$trainCate['name']])->update(['label'=>'其它']);
  474. //删除分类
  475. $trainCate->delete();
  476. }
  477. Db::commit();
  478. return json(['code' => 0, 'msg' => '删除成功']);
  479. } catch (\Exception $e) {
  480. Db::rollback();
  481. return json(['code' => 1, 'msg' => '删除失败']);
  482. }
  483. }
  484. /*
  485. * 课程培训添加页
  486. */
  487. public function class_add()
  488. {
  489. $w = [
  490. ['root_id', '=', $this->root_id]
  491. ];
  492. $type = LecturerCompany::where($w)->field('id,name')->select();
  493. View::assign('type', $type);
  494. return View::fetch();
  495. }
  496. /*
  497. * 讲师编辑页面
  498. */
  499. public function class_edit()
  500. {
  501. $id = input('id',0);
  502. $data = LecturerModel::where('id',$id)->find();
  503. View::assign('data', $data);
  504. $w = [
  505. ['root_id', '=', $this->root_id]
  506. ];
  507. $type = LecturerCompany::where($w)->field('id,name')->select();
  508. View::assign('type', $type);
  509. return View::fetch();
  510. }
  511. /**
  512. * 课件活动添加保存
  513. */
  514. public function classAddSave()
  515. {
  516. $data = Request::only(['name'=>'','lecturer_company_id'=>0,'speaker_type'=>'','position'=>'','introduce'=>'','photo'=>'','poster'=>'']);
  517. $data['root_id'] = $this->root_id;
  518. if(LecturerModel::insertGetId($data)){
  519. return json(['code' => 0, 'msg' => '添加成功']);
  520. }else{
  521. return json(['code' => 1, 'msg' => '添加失败']);
  522. };
  523. }
  524. /**
  525. * 课件活动添加保存
  526. */
  527. public function classEditSave()
  528. {
  529. $data = Request::only(['name'=>'','lecturer_company_id'=>0,'speaker_type'=>'','position'=>'','introduce'=>'','photo'=>'','poster'=>'']);
  530. $id = input('id',0);
  531. $info = LecturerModel::where([['root_id','=',0],['id','=',$id]])->findOrEmpty();
  532. if($info->isEmpty()) return json(['code' => 1, 'msg' => '编辑失败']);
  533. foreach ($data as $key => $value) {
  534. $info->$key = $value;
  535. }
  536. $info->save();
  537. return json(['code' => 0, 'msg' => '编辑成功']);
  538. }
  539. /*
  540. * 课程培训添加页面所需课件展示
  541. */
  542. public function class_choice_course()
  543. {
  544. $param = Request::only(['page'=>1,'limit'=>10,'keyword'=>'']);
  545. $type = TrainType::where(['root_id' => $this->root_id, 'from_type' => 0])->field('id,type,from_type')->select()->toArray();
  546. if (!Request::isAjax()) {
  547. $checkValId = request()->param('course_id');
  548. View::assign('type', $type);
  549. View::assign('course_id', isset($checkValId) ? $checkValId : '');
  550. return View::fetch();
  551. }
  552. $type_id = request()->param('type_id');
  553. $course_id = request()->param('course_id');
  554. $condition[] = ['root_id', '=', $this->root_id];
  555. if (isset($type_id) && !empty($type_id)) $condition[] = ['type_id', '=', $type_id];
  556. if (!empty($course_id)) {
  557. $arrId = explode(',', $course_id);
  558. $condition[] = ['id', 'not in', $arrId];
  559. }
  560. if($param['keyword']) $condition[] = ['title','like','%'.$param['keyword'].'%'];
  561. $data = TrainCourse::where($condition)->order('id desc')->page($param['page'],$param['limit'])->select();
  562. $count = TrainCourse::where($condition)->count();
  563. return json(['code' => 0, 'data' => $data,'count'=>$count]);
  564. }
  565. /*
  566. * 培训课程上下架
  567. */
  568. public function classPublish($id)
  569. {
  570. $obj = TrainClass::where(['id' => $id, 'root_id' => $this->root_id])->find();
  571. if (empty($obj)) return json(['code' => 1, 'msg' => '操作失败,数据不存在']);
  572. if (empty($obj['course_id']) && $obj->publish == 0) return json(['code' => 1, 'msg' => '请先绑定课件']);
  573. $obj->publish = $obj->publish == 1 ? 0 : 1;
  574. $obj->save();
  575. return json(['code' => 0, 'msg' => '操作成功']);
  576. }
  577. }