VideoGroupLogic.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\AccountModel;
  4. use app\model\CommonModel;
  5. use app\model\GroupModel;
  6. use app\model\VideoGroupModel;
  7. use app\model\VideoModel;
  8. use think\Exception;
  9. use think\exception\ValidateException;
  10. use think\facade\Db;
  11. use think\response\Json;
  12. class VideoGroupLogic extends BaseLogic
  13. {
  14. //列表
  15. public static function List(array $data = []): Json
  16. {
  17. $db = VideoGroupModel::alias('a')
  18. ->leftJoin('group b', 'b.id=a.group_id AND b.is_del=' . CommonModel::$del_normal)
  19. ->leftJoin('company c', 'c.id=b.company_id AND c.is_del=' . CommonModel::$del_normal)
  20. ->leftJoin('card d', 'd.id=b.card_id AND d.is_del=' . CommonModel::$del_normal)
  21. ->where('a.is_del', CommonModel::$del_normal);
  22. if ($data['company_title'] !== '') $db->whereLike('c.title', '%' . $data['company_title'] . '%');
  23. if ($data['card_title']) $db->whereLike('d.title', '%' . $data['card_title'] . '%');
  24. if ($data['code']) $db->where('a.code', $data['code']);
  25. if (($data['start_date'] != '') && ($data['end_date' != ''])) $db->whereBetween('a.addtime', [$data['start_date'], $data['end_date']]);
  26. $count = $db->count('a.id');
  27. $video = $db
  28. ->field('a.id,a.code,a.video_ids,a.addtime,c.title company_title,d.title card_title')
  29. ->page($data['page'], $data['size'])
  30. ->order('a.addtime', 'desc')
  31. ->append(['video_name'])
  32. ->withAttr('video_name', function ($val, $da) {
  33. return VideoModel::where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  34. ->whereIn('id', $da['video_ids'])->column('video_name');
  35. })
  36. ->select()
  37. ->toArray();
  38. return json_show(CommonModel::$success, '获取成功', ['list' => $video, 'count' => $count]);
  39. }
  40. //添加
  41. public static function Add(array $data = []): Json
  42. {
  43. Db::startTrans();
  44. try {
  45. $rs = GroupModel::field('id,company_id,card_id,status')
  46. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['group_id']])
  47. ->findOrEmpty();
  48. if ($rs->isEmpty()) throw new Exception('该分组不存在');
  49. if ($rs->status == CommonModel::$status_disable) throw new Exception('该分组已禁用');
  50. //添加记录
  51. $video_ids = VideoModel::where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  52. ->whereIn('id', $data['video_ids'])
  53. ->column('id');
  54. if (empty($video_ids)) throw new Exception('视频不存在或已禁用');
  55. $date = date('Y-m-d H:i:s');
  56. $data = [
  57. 'group_id' => $data['group_id'],
  58. 'code' => make_no('VG'),
  59. 'video_ids' => implode(',', $video_ids),
  60. 'is_del' => CommonModel::$del_normal,
  61. 'addtime' => $date,
  62. 'updatetime' => $date,
  63. ];
  64. $res = VideoGroupModel::create($data)->save();
  65. //更新对应的账户关联视频
  66. AccountModel::where([
  67. 'is_del' => CommonModel::$del_normal,
  68. 'company_id' => $rs->company_id,
  69. 'card_id' => $rs->card_id
  70. ])->save([
  71. 'video_ids' => implode(',', $video_ids),
  72. 'updaterid' => self::$uid,
  73. 'updater' => self::$uname,
  74. 'updatetime' => $date
  75. ]);
  76. Db::commit();
  77. if ($res) return json_show(CommonModel::$success, '批量设置视频添加成功');
  78. else throw new Exception();
  79. } catch (Exception $exception) {
  80. Db::rollback();
  81. return json_show(CommonModel::$error_param, '批量视频添加失败,' . $exception->getMessage());
  82. }
  83. }
  84. }