Browse Source

批量设置视频

wufeng 2 years ago
parent
commit
0eb327f11a

+ 7 - 1
app/admin/config/validate_rules.php

@@ -81,6 +81,12 @@ return [
         'weight|权重' => 'number|egt:0',
         'remark|备注' => 'max:255',
     ],
+    //【批量添加视频】
+    //添加
+    'videoGroupAdd' => [
+        'group_id|分组id' => 'require|number|gt:0',
+        'video_ids|视频id集合' => 'require|array|max:100',
+    ],
 
 
     //【卡类型】
@@ -145,7 +151,7 @@ return [
     'CompanyGoodAdd' => [
         'group_id|分组id' => 'require|number|gt:0',
         'good_id|商品id' => 'require|number|gt:0',
-        'is_top|是否置顶'=>'require|number|in:'.CommonModel::$top_no.','.CommonModel::$top_yes,
+        'is_top|是否置顶' => 'require|number|in:' . CommonModel::$top_no . ',' . CommonModel::$top_yes,
         'weight|权重' => 'number|egt:0',
     ],
 

+ 0 - 1
app/admin/controller/CompanyGood.php

@@ -5,7 +5,6 @@ namespace app\admin\controller;
 use app\admin\logic\CompanyGoodLogic;
 use app\BaseController;
 use app\model\CommonModel;
-use app\model\GroupModel;
 use think\exception\ValidateException;
 use think\facade\Config;
 use think\facade\Validate;

+ 45 - 0
app/admin/controller/VideoGroup.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace app\admin\controller;
+
+use app\admin\logic\VideoGroupLogic;
+use app\BaseController;
+use think\exception\ValidateException;
+use think\facade\Config;
+use think\facade\Validate;
+
+//批量设置视频
+class VideoGroup extends BaseController
+{
+
+    //列表
+    public function list()
+    {
+        $param = $this->request->only([
+            'page' => 1,
+            'size' => 10,
+            'company_title' => '',
+            'card_title' => '',
+            'code' => '',
+            'start_date' => '',
+            'end_date' => '',
+
+        ], 'post');
+
+        return VideoGroupLogic::list($param);
+    }
+
+    //添加
+    public function add()
+    {
+
+        $param = $this->request->only(['group_id', 'video_ids'], 'post');
+
+        $val = Validate::rule(Config::get('validate_rules.videoGroupAdd'));
+
+        if (!$val->check($param)) throw new ValidateException($val->getError());
+        return VideoGroupLogic::add($param);
+    }
+
+
+}

+ 106 - 0
app/admin/logic/VideoGroupLogic.php

@@ -0,0 +1,106 @@
+<?php
+
+namespace app\admin\logic;
+
+use app\model\AccountModel;
+use app\model\CommonModel;
+use app\model\GroupModel;
+use app\model\VideoGroupModel;
+use app\model\VideoModel;
+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('group b', 'b.id=a.group_id AND b.is_del=' . CommonModel::$del_normal)
+            ->leftJoin('company c', 'c.id=b.company_id AND c.is_del=' . CommonModel::$del_normal)
+            ->leftJoin('card d', 'd.id=b.card_id AND d.is_del=' . CommonModel::$del_normal)
+            ->where('a.is_del', CommonModel::$del_normal);
+
+        if ($data['company_title'] !== '') $db->whereLike('c.title', '%' . $data['company_title'] . '%');
+        if ($data['card_title']) $db->whereLike('d.title', '%' . $data['card_title'] . '%');
+        if ($data['code']) $db->where('a.code', $data['code']);
+        if (($data['start_date'] != '') && ($data['end_date' != ''])) $db->whereBetween('a.addtime', [$data['start_date'], $data['end_date']]);
+
+        $count = $db->count('a.id');
+
+        $video = $db
+            ->field('a.id,a.code,a.video_ids,a.addtime,c.title company_title,d.title card_title')
+            ->page($data['page'], $data['size'])
+            ->order('a.addtime', 'desc')
+            ->append(['video_name'])
+            ->withAttr('video_name', function ($val, $da) {
+                return VideoModel::where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
+                    ->whereIn('id', $da['video_ids'])->column('video_name');
+            })
+            ->select()
+            ->toArray();
+
+        return json_show(CommonModel::$success, '获取成功', ['list' => $video, 'count' => $count]);
+    }
+
+    //添加
+    public static function Add(array $data = []): Json
+    {
+
+        Db::startTrans();
+
+        try {
+
+            $rs = GroupModel::field('id,company_id,card_id,status')
+                ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['group_id']])
+                ->findOrEmpty();
+
+            if ($rs->isEmpty()) throw new Exception('该分组不存在');
+            if ($rs->status == CommonModel::$status_disable) throw new Exception('该分组已禁用');
+
+            //添加记录
+            $video_ids = VideoModel::where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
+                ->whereIn('id', $data['video_ids'])
+                ->column('id');
+            if (empty($video_ids)) throw new Exception('视频不存在或已禁用');
+
+            $date = date('Y-m-d H:i:s');
+
+            $data = [
+                'group_id' => $data['group_id'],
+                'code' => makeNo('VG'),
+                'video_ids' => implode(',', $video_ids),
+                'is_del' => CommonModel::$del_normal,
+                'addtime' => $date,
+                'updatetime' => $date,
+            ];
+
+            $res = VideoGroupModel::create($data)->save();
+
+            //更新对应的账户关联视频
+            AccountModel::where([
+                'is_del' => CommonModel::$del_normal,
+                'company_id' => $rs->company_id,
+                'card_id' => $rs->card_id
+            ])->save([
+                'video_ids' => implode(',', $video_ids),
+                'updaterid' => self::$uid,
+                'updater' => self::$uname,
+                'updatetime' => $date
+            ]);
+
+            Db::commit();
+
+            if ($res) return json_show(CommonModel::$success, '批量设置视频添加成功');
+            else throw new Exception();
+
+        } catch (Exception $exception) {
+            Db::rollback();
+            return json_show(CommonModel::$error_param, '批量视频添加失败,' . $exception->getMessage());
+        }
+    }
+
+
+}

+ 4 - 0
app/admin/route/app.php

@@ -53,6 +53,10 @@ Route::rule('videoRead', 'admin/Video/read');//获取视频详情
 Route::rule('videoEdit', 'admin/Video/rdit');//编辑视频
 Route::rule('videoChange', 'admin/Video/change');//视频启禁用
 Route::rule('videoDelete', 'admin/Video/delete');//删除视频
+//【批量设置视频】
+Route::rule('videoGroupList', 'admin/VideoGroup/list');//批量设置视频列表
+Route::rule('videoGroupAdd', 'admin/VideoGroup/add');//批量设置视频添加
+
 
 //【卡类型】
 Route::rule('cardList', 'admin/Card/list');//获取卡类型列表