123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace app\admin\logic;
- use app\model\CommonModel;
- use app\model\VideoGroupModel;
- use think\Exception;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\response\Json;
- class VideoGroupLogic extends BaseLogic
- {
- //获取视频分组列表
- public static function list(array $data = []): Json
- {
- $db = VideoGroupModel::alias('a')
- ->leftJoin('company b', 'b.id=a.company_id')
- ->leftJoin('card c', 'c.id=a.card_id')
- ->where('a.is_del', CommonModel::$del_normal);
- if ($data['status'] != '') $db->where('a.status', $data['status']);
- if ($data['company_id'] != '') $db->where('a.company_id', $data['company_id']);
- if ($data['card_id'] != '') $db->where('a.card_id', $data['card_id']);
- $count = $db->count('a.id');
- $list = $db->field('a.id,b.title company_title,c.title card_title,a.num,a.status,a.addtime,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([
- 'is_del' => CommonModel::$del_normal,
- 'company_id' => $data['company_id'],
- 'card_id' => $data['card_id']
- ])
- ->findOrEmpty()
- ->isEmpty();
- if (!$rs) throw new Exception('该分组已存在');
- $date = date('Y-m-d H:i:s');
- $video_group_id = Db::name('video_group')
- ->strict(false)
- ->insertGetId(array_merge($data, [
- 'num' => count($data['video_list']),
- 'is_top' => CommonModel::$top_no,
- 'status' => CommonModel::$status_normal,
- 'is_del' => CommonModel::$del_normal,
- 'createrid' => self::$uid,
- 'creater' => self::$uname,
- 'addtime' => $date,
- 'updaterid' => self::$uid,
- 'updater' => self::$uname,
- 'updatetime' => $date,
- ]));
- $da = [];
- foreach ($data['video_list'] as $item) {
- $da[] = [
- 'video_group_id' => $video_group_id,
- 'video_id' => $item['video_id'],
- 'is_top' => $item['is_top'],
- 'is_del' => CommonModel::$del_normal,
- 'addtime' => $date,
- 'updatetime' => $date,
- ];
- }
- Db::name('video_group_item')->insertAll($da);
- Db::commit();
- return 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 = VideoGroupModel::field(true)
- ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty()
- ->toArray();
- if (empty($res)) throw new ValidateException('该视频分组为空');
- $res['child'] = Db::name('video_group_item')
- ->field(true)
- ->where(['is_del' => CommonModel::$del_normal, 'video_group_id' => $id])
- ->order(['is_top' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取视频分组详情成功', $res);
- }
- //编辑视频分组
- public static function edit(array $data = []): Json
- {
- Db::startTrans();
- try {
- $rs = VideoGroupModel::field('id,company_id,card_id')
- ->where([
- 'is_del' => CommonModel::$del_normal,
- 'id' => $data['id']
- ])
- ->findOrEmpty();
- if ($rs->isEmpty()) throw new Exception('该视频分组不存在');
- if (($rs->company_id != $data['company_id']) || ($rs->card_id != $data['card_id'])) {
- $temp = VideoGroupModel::field('id')
- ->where([
- 'is_del' => CommonModel::$del_normal,
- 'company_id' => $data['company_id'],
- 'card_id' => $data['card_id']
- ])
- ->findOrEmpty()
- ->isEmpty();
- if (!$temp) throw new Exception('该分组已存在');
- }
- $date = date('Y-m-d H:i:s');
- VideoGroupModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
- ->strict(false)
- ->save(array_merge($data, ['updatetime' => $date]));
- $del = $ins = [];
- foreach ($data['video_list'] as $item) {
- if (isset($item['id']) && $item['id'] != 0) {
- if ($item['is_del'] == CommonModel::$del_deleted) $del[] = $item['id'];
- else Db::name('video_group_item')->where('is_del', CommonModel::$del_normal)->where('id', $item['id'])->save(array_merge($item, ['updatetime' => $date]));
- } else $ins[] = [
- 'video_group_id' => $data['id'],
- 'video_id' => $item['video_id'],
- 'is_top' => $item['is_top'],
- 'is_del' => CommonModel::$del_normal,
- 'addtime' => $date,
- 'updatetime' => $date,
- ];
- }
- if ($del) Db::name('video_group_item')
- ->whereIn('id', $del)
- ->where('is_del', CommonModel::$del_normal)
- ->update([
- 'is_del' => CommonModel::$del_deleted,
- 'updatetime' => $date,
- ]);
- if ($ins) Db::name('video_group_item')->insertAll($ins);
- Db::commit();
- return json_show(CommonModel::$success, '编辑视频分组成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(CommonModel::$error_param, '编辑视频分组失败,' . $exception->getMessage());
- }
- }
- //视频分组启禁用
- public static function status(array $data = []): Json
- {
- $res = VideoGroupModel::where('id', $data['id'])
- ->where('status', '<>', $data['status'])
- ->save([
- 'status' => $data['status'],
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败');
- }
- //删除视频分组
- public static function delete(int $id = 0): Json
- {
- $res = VideoGroupModel::where('id', $id)
- ->where('is_del', CommonModel::$del_normal)
- ->save([
- 'is_del' => CommonModel::$del_deleted,
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- return $res ? json_show(CommonModel::$success, '删除成功') : json_show(CommonModel::$error_param, '删除失败');
- }
- //视频分组置顶
- public static function top(array $data = []): Json
- {
- $res = VideoGroupModel::where('id', $data['id'])
- ->where('is_top', '<>', $data['is_top'])
- ->save([
- 'is_top' => $data['is_top'],
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败');
- }
- }
|