VideoGroupItemLogic.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\VideoGroupModel;
  5. use app\model\VideoModel;
  6. use think\Exception;
  7. use think\exception\ValidateException;
  8. use think\facade\Db;
  9. use think\response\Json;
  10. class VideoGroupItemLogic extends BaseLogic
  11. {
  12. //列表
  13. public static function list(array $data = []): Json
  14. {
  15. $db = Db::name('video_group_item')
  16. ->alias('a')
  17. ->leftJoin('video b', 'b.id=a.video_id AND b.is_del=' . CommonModel::$del_normal)
  18. ->where(['a.is_del' => CommonModel::$del_normal, 'video_group_id' => $data['video_group_id']]);
  19. if ($data['video_title'] != '') $db->whereLike('b.video_name', '%' . $data['video_title'] . '%');
  20. $count = $db->count('a.id');
  21. $list = $db->field('a.id,b.video_name,b.remark video_remark,a.is_top')
  22. ->page($data['page'], $data['size'])
  23. ->order(['a.is_top' => 'desc', 'a.id' => 'desc'])
  24. ->select()
  25. ->toArray();
  26. return json_show(CommonModel::$success, '获取视频分组明细列表成功', ['count' => $count, 'list' => $list]);
  27. }
  28. //添加
  29. public static function add(array $data = []): Json
  30. {
  31. Db::startTrans();
  32. try {
  33. $rs = VideoGroupModel::field('id')
  34. ->where(['id' => $data['video_group_id'], 'is_del' => CommonModel::$del_normal])
  35. ->findOrEmpty()
  36. ->isEmpty();
  37. if ($rs) throw new Exception('该视频分组不存在');
  38. $rs = VideoModel::field('id')
  39. ->where(['id' => $data['video_id'], 'is_del' => CommonModel::$del_normal])
  40. ->findOrEmpty()
  41. ->isEmpty();
  42. if ($rs) throw new Exception('该视频不存在');
  43. $rs = Db::name('video_group_item')
  44. ->field('id')
  45. ->where([
  46. 'is_del' => CommonModel::$del_normal,
  47. 'video_group_id' => $data['video_group_id'],
  48. 'video_id' => $data['video_id']])
  49. ->findOrEmpty();
  50. if (!empty($rs)) throw new Exception('该视频已存在,不能在同一视频分组下重复添加');
  51. $res = Db::name('video_group_item')
  52. ->insert(array_merge($data, [
  53. 'is_top' => CommonModel::$top_no,
  54. 'is_del' => CommonModel::$del_normal,
  55. 'addtime' => date('Y-m-d H:i:s'),
  56. 'updatetime' => date('Y-m-d H:i:s'),
  57. ]));
  58. Db::name('video_group')
  59. ->where(['id' => $data['video_group_id'], 'is_del' => CommonModel::$del_normal])
  60. ->inc('num', $res)
  61. ->update(['updatetime' => date('Y-m-d H:i:s')]);
  62. Db::commit();
  63. return $res ? json_show(CommonModel::$success, '添加视频分组明细成功') : json_show(CommonModel::$success, '添加视频分组明细失败');
  64. } catch (Exception $exception) {
  65. Db::rollback();
  66. return json_show(CommonModel::$error_param, '添加视频分组明细失败,' . $exception->getMessage());
  67. }
  68. }
  69. //获取详情
  70. public static function read(int $id = 0): Json
  71. {
  72. $res = Db::name('video_group_item')
  73. ->field(true)
  74. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  75. ->findOrEmpty();
  76. if (empty($res)) throw new ValidateException('该视频分组明细为空');
  77. return json_show(CommonModel::$success, '获取视频分组明细详情成功', $res);
  78. }
  79. //编辑
  80. public static function edit(array $data = []): Json
  81. {
  82. $rs = VideoModel::field('id')
  83. ->where(['id' => $data['video_id'], 'is_del' => CommonModel::$del_normal])
  84. ->findOrEmpty()
  85. ->isEmpty();
  86. if ($rs) return json_show(CommonModel::$error_param, '该视频不存在');
  87. $res = Db::name('video_group_item')
  88. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  89. ->save(array_merge($data, ['updatetime' => date('Y-m-d H:i:s')]));
  90. return $res ? json_show(CommonModel::$success, '编辑视频分组明细成功') : json_show(CommonModel::$error_param, '编辑视频分组明细失败');
  91. }
  92. //删除
  93. public static function delete(array $ids = []): Json
  94. {
  95. Db::startTrans();
  96. try {
  97. $video_group_id = Db::name('video_group_item')
  98. ->whereIn('id', $ids)
  99. ->where('is_del', CommonModel::$del_normal)
  100. ->group('video_group_id')
  101. ->column('video_group_id');
  102. if (count($video_group_id) > 1) throw new Exception('不能删除不同分组下的视频');
  103. $num = Db::name('video_group_item')
  104. ->whereIn('id', $ids)
  105. ->where('is_del', CommonModel::$del_normal)
  106. ->update([
  107. 'is_del' => CommonModel::$del_deleted,
  108. 'updatetime' => date('Y-m-d H:i:s'),
  109. ]);
  110. Db::name('video_group')
  111. ->where(['id' => $video_group_id[0], 'is_del' => CommonModel::$del_normal])
  112. ->dec('num', $num)
  113. ->update(['updatetime' => date('Y-m-d H:i:s')]);
  114. Db::commit();
  115. return $num ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败,该视频分组明细不存在');
  116. } catch (Exception $exception) {
  117. Db::rollback();
  118. return json_show(CommonModel::$error_param, '添加视频分组明细失败,' . $exception->getMessage());
  119. }
  120. }
  121. //置顶
  122. public static function top(array $data = []): Json
  123. {
  124. $res = Db::name('video_group_item')
  125. ->where('id', $data['id'])
  126. ->where('is_top', '<>', $data['is_top'])
  127. ->update([
  128. 'is_top' => $data['is_top'],
  129. 'updatetime' => date('Y-m-d H:i:s'),
  130. ]);
  131. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败,该视频分组明细不存在或重复操作');
  132. }
  133. }