VideoGroupLogic.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CommonModel;
  4. use app\model\VideoGroupModel;
  5. use think\Exception;
  6. use think\exception\ValidateException;
  7. use think\facade\Db;
  8. use think\response\Json;
  9. class VideoGroupLogic extends BaseLogic
  10. {
  11. //获取视频分组列表
  12. public static function list(array $data = []): Json
  13. {
  14. $db = VideoGroupModel::alias('a')
  15. ->leftJoin('company b', 'b.id=a.company_id')
  16. ->leftJoin('card c', 'c.id=a.card_id')
  17. ->where('a.is_del', CommonModel::$del_normal);
  18. if ($data['status'] != '') $db->where('a.status', $data['status']);
  19. if ($data['company_id'] != '') $db->where('a.company_id', $data['company_id']);
  20. if ($data['card_id'] != '') $db->where('a.card_id', $data['card_id']);
  21. $count = $db->count('a.id');
  22. $list = $db->field('a.id,b.title company_title,c.title card_title,a.num,a.status,a.addtime,a.is_top')
  23. ->page($data['page'], $data['size'])
  24. ->order(['a.is_top' => 'desc', 'a.id' => 'desc'])
  25. ->select()
  26. ->toArray();
  27. return json_show(CommonModel::$success, '获取视频分组列表成功', ['count' => $count, 'list' => $list]);
  28. }
  29. //添加视频分组
  30. public static function add(array $data = []): Json
  31. {
  32. Db::startTrans();
  33. try {
  34. $rs = VideoGroupModel::field('id')
  35. ->where([
  36. 'is_del' => CommonModel::$del_normal,
  37. 'company_id' => $data['company_id'],
  38. 'card_id' => $data['card_id']
  39. ])
  40. ->findOrEmpty()
  41. ->isEmpty();
  42. if (!$rs) throw new Exception('该分组已存在');
  43. $date = date('Y-m-d H:i:s');
  44. $video_group_id = Db::name('video_group')
  45. ->strict(false)
  46. ->insertGetId(array_merge($data, [
  47. 'num' => count($data['video_list']),
  48. 'is_top' => CommonModel::$top_no,
  49. 'status' => CommonModel::$status_normal,
  50. 'is_del' => CommonModel::$del_normal,
  51. 'createrid' => self::$uid,
  52. 'creater' => self::$uname,
  53. 'addtime' => $date,
  54. 'updaterid' => self::$uid,
  55. 'updater' => self::$uname,
  56. 'updatetime' => $date,
  57. ]));
  58. $da = [];
  59. foreach ($data['video_list'] as $item) {
  60. $da[] = [
  61. 'video_group_id' => $video_group_id,
  62. 'video_id' => $item['video_id'],
  63. 'is_top' => $item['is_top'],
  64. 'is_del' => CommonModel::$del_normal,
  65. 'addtime' => $date,
  66. 'updatetime' => $date,
  67. ];
  68. }
  69. Db::name('video_group_item')->insertAll($da);
  70. Db::commit();
  71. return json_show(CommonModel::$success, '添加视频分组成功');
  72. } catch (Exception $exception) {
  73. Db::rollback();
  74. return json_show(CommonModel::$error_param, '添加视频分组失败,' . $exception->getMessage());
  75. }
  76. }
  77. //获取视频分组详情
  78. public static function read(int $id = 0): Json
  79. {
  80. $res = VideoGroupModel::field(true)
  81. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  82. ->findOrEmpty()
  83. ->toArray();
  84. if (empty($res)) throw new ValidateException('该视频分组为空');
  85. $res['child'] = Db::name('video_group_item')
  86. ->alias('a')
  87. ->field('a.id,a.video_id,a.is_top,a.is_del,b.video_name,b.remark')
  88. ->leftJoin('video b', 'b.id=a.video_id AND b.is_del=' . CommonModel::$del_normal)
  89. ->where(['a.is_del' => CommonModel::$del_normal, 'a.video_group_id' => $id])
  90. ->order(['a.is_top' => 'desc', 'a.id' => 'desc'])
  91. ->select()
  92. ->toArray();
  93. return json_show(CommonModel::$success, '获取视频分组详情成功', $res);
  94. }
  95. //编辑视频分组
  96. public static function edit(array $data = []): Json
  97. {
  98. Db::startTrans();
  99. try {
  100. $rs = VideoGroupModel::field('id,company_id,card_id')
  101. ->where([
  102. 'is_del' => CommonModel::$del_normal,
  103. 'id' => $data['id']
  104. ])
  105. ->findOrEmpty();
  106. if ($rs->isEmpty()) throw new Exception('该视频分组不存在');
  107. if (($rs->company_id != $data['company_id']) || ($rs->card_id != $data['card_id'])) {
  108. $temp = VideoGroupModel::field('id')
  109. ->where([
  110. 'is_del' => CommonModel::$del_normal,
  111. 'company_id' => $data['company_id'],
  112. 'card_id' => $data['card_id']
  113. ])
  114. ->findOrEmpty()
  115. ->isEmpty();
  116. if (!$temp) throw new Exception('该分组已存在');
  117. }
  118. $date = date('Y-m-d H:i:s');
  119. VideoGroupModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  120. ->strict(false)
  121. ->save(array_merge($data, ['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uid]));
  122. $del = $ins = [];
  123. foreach ($data['video_list'] as $item) {
  124. if (isset($item['id']) && $item['id'] != 0) {
  125. if ($item['is_del'] == CommonModel::$del_deleted) $del[] = $item['id'];
  126. else Db::name('video_group_item')->where('is_del', CommonModel::$del_normal)->where('id', $item['id'])->save(array_merge($item, ['updatetime' => $date]));
  127. } else $ins[] = [
  128. 'video_group_id' => $data['id'],
  129. 'video_id' => $item['video_id'],
  130. 'is_top' => $item['is_top'],
  131. 'is_del' => CommonModel::$del_normal,
  132. 'addtime' => $date,
  133. 'updatetime' => $date,
  134. ];
  135. }
  136. if ($del) Db::name('video_group_item')
  137. ->whereIn('id', $del)
  138. ->where('is_del', CommonModel::$del_normal)
  139. ->update([
  140. 'is_del' => CommonModel::$del_deleted,
  141. 'updatetime' => $date,
  142. ]);
  143. if ($ins) Db::name('video_group_item')->insertAll($ins);
  144. Db::commit();
  145. return json_show(CommonModel::$success, '编辑视频分组成功');
  146. } catch (Exception $exception) {
  147. Db::rollback();
  148. return json_show(CommonModel::$error_param, '编辑视频分组失败,' . $exception->getMessage());
  149. }
  150. }
  151. //视频分组启禁用
  152. public static function status(array $data = []): Json
  153. {
  154. $res = VideoGroupModel::where('id', $data['id'])
  155. ->where('status', '<>', $data['status'])
  156. ->save([
  157. 'status' => $data['status'],
  158. 'updatetime' => date('Y-m-d H:i:s'),
  159. ]);
  160. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败');
  161. }
  162. //删除视频分组
  163. public static function delete(int $id = 0): Json
  164. {
  165. $res = VideoGroupModel::where('id', $id)
  166. ->where('is_del', CommonModel::$del_normal)
  167. ->save([
  168. 'is_del' => CommonModel::$del_deleted,
  169. 'updatetime' => date('Y-m-d H:i:s'),
  170. ]);
  171. return $res ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败');
  172. }
  173. //视频分组置顶
  174. public static function top(array $data = []): Json
  175. {
  176. $res = VideoGroupModel::where('id', $data['id'])
  177. ->where('is_top', '<>', $data['is_top'])
  178. ->save([
  179. 'is_top' => $data['is_top'],
  180. 'updatetime' => date('Y-m-d H:i:s'),
  181. ]);
  182. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败');
  183. }
  184. }