Soldier.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\AdminBaseController;
  4. use app\common\model\SoldierModel;
  5. use app\common\model\SoldierVideoModel;
  6. use app\common\model\SoldierVideoSeriesModel;
  7. use app\common\model\SoldierVideoWatchModel;
  8. use app\common\validate\SoldierUserValidate;
  9. use app\common\validate\SoldierVideoSeriesValidate;
  10. use app\common\validate\SoldierVideoValidate;
  11. use think\exception\ValidateException;
  12. class Soldier extends AdminBaseController
  13. {
  14. /**
  15. * 用户列表
  16. */
  17. public function user()
  18. {
  19. return view('', [
  20. 'status_list' => SoldierModel::STATUS,
  21. ]);
  22. }
  23. public function listUser()
  24. {
  25. $map = $this->dealEqualInput(['status'], $this->dealLikeInput(['keywords' => 'name|mobile']));
  26. $list = SoldierModel::where($map)
  27. ->order('id', 'desc')
  28. ->limit(input('limit'))
  29. ->page(input('page'))
  30. ->append(['status_text'])->select();
  31. $count = SoldierModel::where($map)->count();
  32. if ($count == 0) {
  33. ajax_return(1, '未查询到数据');
  34. }
  35. list_return($list, $count);
  36. }
  37. public function delUser()
  38. {
  39. $id = input('id/d', 0);
  40. if (empty($id)) {
  41. ajax_return(1, '未查询到数据');
  42. }
  43. SoldierModel::destroy($id);
  44. SoldierVideoWatchModel::destroy(['user_id' => $id]);
  45. ajax_return();
  46. }
  47. /**
  48. * 编辑用户
  49. */
  50. public function userForm()
  51. {
  52. $id = input('id/d', 0);
  53. $info = SoldierModel::find($id);
  54. return view('', [
  55. 'info' => $info,
  56. 'status_list' => SoldierModel::STATUS,
  57. ]);
  58. }
  59. public function editUser()
  60. {
  61. $data = input('post.');
  62. try {
  63. validate(SoldierUserValidate::class)->check($data);
  64. } catch (ValidateException $e) {
  65. ajax_return(1, $e->getError());
  66. }
  67. //手机号
  68. $check_mobile_where = [['mobile', '=', $data['mobile']]];
  69. if (!empty($data['id'])) {
  70. $check_mobile_where[] = ['id', '<>', $data['id']];
  71. }
  72. $check_mobile = SoldierModel::where($check_mobile_where)->find();
  73. if (!empty($check_mobile)) {
  74. ajax_return(1, '手机号已存在');
  75. }
  76. //密码
  77. if (empty($data['id']) && empty($data['password'])) {
  78. ajax_return(1, '请输入一个初始密码');
  79. }
  80. if (empty($data['password'])) {
  81. unset($data['password']);
  82. } else {
  83. $data['salt'] = rand_str();
  84. $data['password'] = md5(md5($data['salt']) . $data['password']);
  85. }
  86. if (empty($data['id'])) {
  87. SoldierModel::create($data);
  88. } else {
  89. SoldierModel::update($data, ['id' => $data['id']]);
  90. }
  91. ajax_return();
  92. }
  93. /**
  94. * 用户导入
  95. */
  96. public function importUser()
  97. {
  98. return view('public/import', [
  99. 'url' => url('soldier/importUserPost'),
  100. 'last_table' => 'lay-soldier-user-table',
  101. 'template_file' => '/static/common/exl/soldier_user.xls',
  102. ]);
  103. }
  104. /**
  105. * 用户导入提交
  106. */
  107. public function importUserPost()
  108. {
  109. $file_url = input('file_url/s', "");
  110. if (!file_exists($file_url)) {
  111. ajax_return(1, '文件不存在');
  112. }
  113. //初始化数据
  114. $data = ['name', 'mobile', 'password'];
  115. $list = import_exl($file_url, $data, 1);
  116. if (empty($list)) {
  117. ajax_return(1, '请上传有数据的文件');
  118. }
  119. $empty_check = [
  120. 'name' => '姓名',
  121. 'mobile' => '手机号',
  122. 'password' => '密码',
  123. ];
  124. //获取手机号
  125. $mobile_list = array_column($list, 'mobile');
  126. $mobile_check_list = SoldierModel::where('mobile', 'in', $mobile_list)->column('mobile');
  127. //错误判断
  128. $validate = \think\facade\Validate::rule('mobile', 'mobile');
  129. foreach ($list as $k => $v) {
  130. foreach ($empty_check as $key => $value) {
  131. if (empty($v[$key])) {
  132. return ajax_return(1, '第' . ($k + 2) . '行的' . $value . '不能为空');
  133. }
  134. }
  135. if (!$validate->check($v)) {
  136. return ajax_return(1, '第' . ($k + 2) . '行的手机号格式不对');
  137. }
  138. if (!empty(in_array($v['mobile'], $mobile_check_list))) {
  139. return ajax_return(1, '第' . ($k + 2) . '行的手机号已存在');
  140. }
  141. $list[$k]['salt'] = rand_str();
  142. $list[$k]['password'] = md5(md5($list[$k]['salt']) . $v['password']);
  143. }
  144. SoldierModel::insertAll($list);
  145. ajax_return(0);
  146. }
  147. /**
  148. * 视频系列列表
  149. */
  150. public function videoSeries()
  151. {
  152. return view('', [
  153. 'status_list' => SoldierVideoSeriesModel::STATUS,
  154. ]);
  155. }
  156. public function listVideoSeries()
  157. {
  158. $map = $this->dealEqualInput(['status'], $this->dealLikeInput(['title']));
  159. $list = SoldierVideoSeriesModel::where($map)
  160. ->order('priority desc,id desc')
  161. ->limit(input('limit'))
  162. ->page(input('page'))
  163. ->append(['status_text'])->select();
  164. $count = SoldierVideoSeriesModel::where($map)->count();
  165. if ($count == 0) {
  166. ajax_return(1, '未查询到数据');
  167. }
  168. list_return($list, $count);
  169. }
  170. public function delVideoSeries()
  171. {
  172. $id = input('id/d', 0);
  173. if (empty($id)) {
  174. ajax_return(1, '未查询到数据');
  175. }
  176. $check = SoldierVideoModel::where('series_id', $id)->find();
  177. if (!empty($check)) {
  178. ajax_return(1, '该系列下有视频,无法删除');
  179. }
  180. SoldierVideoSeriesModel::destroy($id);
  181. ajax_return();
  182. }
  183. /**
  184. * 编辑视频系列
  185. */
  186. public function videoSeriesForm()
  187. {
  188. $id = input('id/d', 0);
  189. $info = SoldierVideoSeriesModel::find($id);
  190. return view('', [
  191. 'info' => $info,
  192. 'status_list' => SoldierVideoSeriesModel::STATUS,
  193. ]);
  194. }
  195. public function editVideoSeries()
  196. {
  197. $data = input('post.');
  198. try {
  199. validate(SoldierVideoSeriesValidate::class)->check($data);
  200. } catch (ValidateException $e) {
  201. ajax_return(1, $e->getError());
  202. }
  203. if (empty($data['id'])) {
  204. SoldierVideoSeriesModel::create($data);
  205. } else {
  206. SoldierVideoSeriesModel::update($data);
  207. }
  208. ajax_return();
  209. }
  210. /**
  211. * 视频列表
  212. */
  213. public function video()
  214. {
  215. $series_list = SoldierVideoSeriesModel::where('status', SoldierVideoSeriesModel::STATUS_SHOW)->order('priority desc')->select();
  216. return view('', [
  217. 'status_list' => SoldierVideoModel::STATUS,
  218. 'is_must_list' => SoldierVideoModel::IS_MUST,
  219. 'series_list' => $series_list,
  220. ]);
  221. }
  222. public function listVideo()
  223. {
  224. $map = $this->dealEqualInput(['status', 'series_id', 'is_must'], $this->dealLikeInput(['title']));
  225. $list = SoldierVideoModel::with(['series'])
  226. ->where($map)
  227. ->order('priority desc,id desc')
  228. ->limit(input('limit'))
  229. ->page(input('page'))
  230. ->append(['status_text', 'is_must_text'])->select();
  231. $count = SoldierVideoModel::where($map)->count();
  232. if ($count == 0) {
  233. ajax_return(1, '未查询到数据');
  234. }
  235. list_return($list, $count);
  236. }
  237. public function fieldVideo()
  238. {
  239. $id = input('id/d');
  240. $cate = SoldierVideoModel::find($id);
  241. if (empty($cate)) {
  242. ajax_return(1, '视频信息不存在');
  243. } else {
  244. $cate->save([
  245. input('field/s') => input('value/s', ""),
  246. ]);
  247. }
  248. ajax_return();
  249. }
  250. public function delVideo()
  251. {
  252. $id = input('id/d', 0);
  253. if (empty($id)) {
  254. ajax_return(1, '未查询到数据');
  255. }
  256. $check = SoldierVideoWatchModel::where('video_id', $id)->find();
  257. if (!empty($check)) {
  258. ajax_return(1, '该视频下有观看记录,无法删除');
  259. }
  260. SoldierVideoModel::destroy($id);
  261. ajax_return();
  262. }
  263. /**
  264. * 编辑视频
  265. */
  266. public function videoForm()
  267. {
  268. $id = input('id/d', 0);
  269. $series_list = SoldierVideoSeriesModel::where('status', SoldierVideoSeriesModel::STATUS_SHOW)->order('priority desc')->select();
  270. $info = SoldierVideoModel::find($id);
  271. return view('', [
  272. 'info' => $info,
  273. 'status_list' => SoldierVideoModel::STATUS,
  274. 'is_must_list' => SoldierVideoModel::IS_MUST,
  275. 'series_list' => $series_list,
  276. ]);
  277. }
  278. public function editVideo()
  279. {
  280. $data = input('post.');
  281. try {
  282. validate(SoldierVideoValidate::class)->check($data);
  283. } catch (ValidateException $e) {
  284. ajax_return(1, $e->getError());
  285. }
  286. if (empty($data['id'])) {
  287. SoldierVideoModel::create($data);
  288. } else {
  289. SoldierVideoModel::update($data);
  290. }
  291. ajax_return();
  292. }
  293. /**
  294. * 观看记录
  295. */
  296. public function videoWatch()
  297. {
  298. $id = input('id/d', 0);
  299. if (empty($id)) {
  300. return '请选择视频';
  301. }
  302. return view('', [
  303. 'id' => $id,
  304. ]);
  305. }
  306. public function listVideoWatch()
  307. {
  308. $id = input('id/d', 0);
  309. if (empty($id)) {
  310. ajax_return(1, '请选择视频');
  311. }
  312. $list = SoldierVideoWatchModel::with(['user'])
  313. ->where('video_id', $id)
  314. ->limit(input('limit'))
  315. ->page(input('page'))
  316. ->append(['status_text'])
  317. ->select();
  318. $count = SoldierVideoWatchModel::where('video_id', $id)->count();
  319. if ($count == 0) {
  320. ajax_return(1, '未查询到数据');
  321. }
  322. list_return($list, $count);
  323. }
  324. public function exportVideoWatch()
  325. {
  326. $id = input('id/d', 0);
  327. if (empty($id)) {
  328. ajax_return(1, '请选择视频');
  329. }
  330. $video = SoldierVideoModel::find($id);
  331. $list = SoldierVideoWatchModel::with(['user'])->where('video_id', $id)->append(['status_text'])->select();
  332. foreach ($list as $v) {
  333. $v['user_name'] = $v['user']['name'];
  334. }
  335. $xlsCell = [
  336. ['id', '表ID'],
  337. ['user_name', '姓名'],
  338. ['status_text', '状态'],
  339. ['create_time', '首次学习时间'],
  340. ['update_time', '最后学习时间'],
  341. ];
  342. export_exl($video['title'] . "的观看记录", $xlsCell, $list);
  343. }
  344. public function exportUserVideoWatch()
  345. {
  346. $id = input('id/d', 0);
  347. if (empty($id)) {
  348. ajax_return(1, '请选择视频');
  349. }
  350. $user = SoldierModel::find($id);
  351. $video_list = SoldierVideoModel::with('series')->where('status', SoldierVideoModel::STATUS_SHOW)->append(['is_must_text'])->order('priority desc,id desc')->select();
  352. $watch_list = SoldierVideoWatchModel::where('user_id', $id)->column('*', 'video_id');
  353. foreach ($video_list as $v) {
  354. $v['series_name'] = $v['series']['title'];
  355. if (empty($watch_list[$v['id']])) {
  356. $v['watch_status_text'] = SoldierVideoWatchModel::STATUS[SoldierVideoWatchModel::STATUS_NO_STARTED];
  357. $v['fist_time'] = $v['last_time'] = '';
  358. } else {
  359. $v['watch_status_text'] = SoldierVideoWatchModel::STATUS[$watch_list[$v['id']]['status']];
  360. $v['fist_time'] = date('Y-m-d H:i:s', $watch_list[$v['id']]['create_time']);
  361. $v['last_time'] = date('Y-m-d H:i:s', $watch_list[$v['id']]['update_time']);
  362. }
  363. }
  364. $xlsCell = [
  365. ['series_name', '系列'],
  366. ['title', '标题'],
  367. ['is_must_text', '是否必修'],
  368. ['watch_status_text', '观看状态'],
  369. ['fist_time', '首次学习时间'],
  370. ['last_time', '最后学习时间'],
  371. ];
  372. export_exl($user['name'] . "的观看记录", $xlsCell, $video_list);
  373. }
  374. }