alias('a') ->leftJoin('video b', 'b.id=a.video_id AND b.is_del=' . CommonModel::$del_normal) ->where(['a.is_del' => CommonModel::$del_normal, 'video_group_id' => $data['video_group_id']]); if ($data['video_title'] != '') $db->whereLike('b.video_name', '%' . $data['video_title'] . '%'); $count = $db->count('a.id'); $list = $db->field('a.id,b.video_name,b.remark video_remark,a.is_top') ->page($data['page'], $data['size']) ->order(['a.is_top' => 'desc', 'a.id' => 'desc']) ->select() ->toArray(); return json_show(CommonModel::$success, '获取视频分组明细列表成功', ['count' => $count, 'list' => $list]); } //添加 public static function add(array $data = []): Json { Db::startTrans(); try { $rs = VideoGroupModel::field('id') ->where(['id' => $data['video_group_id'], 'is_del' => CommonModel::$del_normal]) ->findOrEmpty() ->isEmpty(); if ($rs) throw new Exception('该视频分组不存在'); $rs = VideoModel::field('id') ->where(['id' => $data['video_id'], 'is_del' => CommonModel::$del_normal]) ->findOrEmpty() ->isEmpty(); if ($rs) throw new Exception('该视频不存在'); $rs = Db::name('video_group_item') ->field('id') ->where([ 'is_del' => CommonModel::$del_normal, 'video_group_id' => $data['video_group_id'], 'video_id' => $data['video_id']]) ->findOrEmpty(); if (!empty($rs)) throw new Exception('该视频已存在,不能在同一视频分组下重复添加'); $res = Db::name('video_group_item') ->insert(array_merge($data, [ 'is_top' => CommonModel::$top_no, 'is_del' => CommonModel::$del_normal, 'addtime' => date('Y-m-d H:i:s'), 'updatetime' => date('Y-m-d H:i:s'), ])); Db::name('video_group') ->where(['id' => $data['video_group_id'], 'is_del' => CommonModel::$del_normal]) ->inc('num', $res) ->update(['updatetime' => date('Y-m-d H:i:s')]); Db::commit(); return $res ? json_show(CommonModel::$success, '添加视频分组明细成功') : json_show(CommonModel::$success, '添加视频分组明细失败'); } catch (Exception $exception) { Db::rollback(); return json_show(CommonModel::$error_param, '添加视频分组明细失败,' . $exception->getMessage()); } } //获取详情 public static function read(int $id = 0): Json { $res = Db::name('video_group_item') ->field(true) ->where(['id' => $id, 'is_del' => CommonModel::$del_normal]) ->findOrEmpty(); if (empty($res)) throw new ValidateException('该视频分组明细为空'); return json_show(CommonModel::$success, '获取视频分组明细详情成功', $res); } //编辑 public static function edit(array $data = []): Json { $rs = VideoModel::field('id') ->where(['id' => $data['video_id'], 'is_del' => CommonModel::$del_normal]) ->findOrEmpty() ->isEmpty(); if ($rs) return json_show(CommonModel::$error_param, '该视频不存在'); $res = Db::name('video_group_item') ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal]) ->save(array_merge($data, ['updatetime' => date('Y-m-d H:i:s')])); return $res ? json_show(CommonModel::$success, '编辑视频分组明细成功') : json_show(CommonModel::$error_param, '编辑视频分组明细失败'); } //删除 public static function delete(array $ids = []): Json { Db::startTrans(); try { $video_group_id = Db::name('video_group_item') ->whereIn('id', $ids) ->where('is_del', CommonModel::$del_normal) ->group('video_group_id') ->column('video_group_id'); if (count($video_group_id) > 1) throw new Exception('不能删除不同分组下的视频'); $num = Db::name('video_group_item') ->whereIn('id', $ids) ->where('is_del', CommonModel::$del_normal) ->update([ 'is_del' => CommonModel::$del_deleted, 'updatetime' => date('Y-m-d H:i:s'), ]); Db::name('video_group') ->where(['id' => $video_group_id[0], 'is_del' => CommonModel::$del_normal]) ->dec('num', $num) ->update(['updatetime' => date('Y-m-d H:i:s')]); Db::commit(); return $num ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败,该视频分组明细不存在'); } catch (Exception $exception) { Db::rollback(); return json_show(CommonModel::$error_param, '添加视频分组明细失败,' . $exception->getMessage()); } } //置顶 public static function top(array $data = []): Json { $res = Db::name('video_group_item') ->where('id', $data['id']) ->where('is_top', '<>', $data['is_top']) ->update([ 'is_top' => $data['is_top'], 'updatetime' => date('Y-m-d H:i:s'), ]); return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败,该视频分组明细不存在或重复操作'); } }