VideoGroupLogic.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_title'] != '') $db->whereLike('b.title', '%' . $data['company_title'] . '%');
  20. if ($data['card_title'] != '') $db->whereLike('c.title', '%' . $data['card_title'] . '%');
  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. $rs = VideoGroupModel::field('id')
  33. ->where([
  34. 'is_del' => CommonModel::$del_normal,
  35. 'company_id' => $data['company_id'],
  36. 'card_id' => $data['card_id']
  37. ])
  38. ->findOrEmpty()
  39. ->isEmpty();
  40. if (!$rs) return json_show(CommonModel::$error_param, '该分组已存在');
  41. $res = VideoGroupModel::create(array_merge($data, [
  42. 'num' => 0,
  43. 'is_top' => CommonModel::$top_no,
  44. 'status' => CommonModel::$status_normal,
  45. 'is_del' => CommonModel::$del_normal,
  46. 'createrid' => self::$uid,
  47. 'creater' => self::$uname,
  48. 'addtime' => date('Y-m-d H:i:s'),
  49. 'updaterid' => self::$uid,
  50. 'updater' => self::$uname,
  51. 'updatetime' => date('Y-m-d H:i:s'),
  52. ]))->save();
  53. return $res ? json_show(CommonModel::$success, '添加视频分组成功') : json_show(CommonModel::$success, '添加视频分组失败');
  54. }
  55. //获取视频分组详情
  56. public static function read(int $id = 0): Json
  57. {
  58. $res = VideoGroupModel::field(true)
  59. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  60. ->findOrEmpty()
  61. ->toArray();
  62. if (empty($res)) throw new ValidateException('该视频分组为空');
  63. return json_show(CommonModel::$success, '获取视频分组详情成功', $res);
  64. }
  65. //编辑视频分组
  66. public static function edit(array $data = []): Json
  67. {
  68. $rs = VideoGroupModel::field('id,company_id,card_id')
  69. ->where([
  70. 'id' => $data['id'],
  71. 'is_del' => CommonModel::$del_normal,
  72. ])
  73. ->findOrEmpty();
  74. if ($rs->isEmpty()) return json_show(CommonModel::$error_param, '该视频分组不存在');
  75. if (($rs->company_id != $data['company_id']) || ($rs->card_id != $data['card_id'])) {
  76. $temp = VideoGroupModel::field('id')
  77. ->where([
  78. 'is_del' => CommonModel::$del_normal,
  79. 'company_id' => $data['company_id'],
  80. 'card_id' => $data['card_id']
  81. ])
  82. ->findOrEmpty()
  83. ->isEmpty();
  84. if (!$temp) return json_show(CommonModel::$error_param, '该视频分组已存在');
  85. }
  86. $res = VideoGroupModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  87. ->save(array_merge($data, ['updatetime' => date('Y-m-d H:i:s'), 'updaterid' => self::$uid, 'updater' => self::$uid]));
  88. return $res ? json_show(CommonModel::$success, '编辑视频分组成功') : json_show(CommonModel::$error_param, '编辑视频分组失败');
  89. }
  90. //视频分组启禁用
  91. public static function status(array $data = []): Json
  92. {
  93. $res = VideoGroupModel::where('id', $data['id'])
  94. ->where('status', '<>', $data['status'])
  95. ->save([
  96. 'status' => $data['status'],
  97. 'updatetime' => date('Y-m-d H:i:s'),
  98. 'updaterid' => self::$uid,
  99. 'updater' => self::$uid
  100. ]);
  101. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败,该视频分组不存在或重复操作');
  102. }
  103. //删除视频分组
  104. public static function delete(array $ids = []): Json
  105. {
  106. Db::startTrans();
  107. try {
  108. VideoGroupModel::whereIn('id', $ids)
  109. ->where('is_del', CommonModel::$del_normal)
  110. ->save([
  111. 'is_del' => CommonModel::$del_deleted,
  112. 'updatetime' => date('Y-m-d H:i:s'),
  113. 'updaterid' => self::$uid,
  114. 'updater' => self::$uid
  115. ]);
  116. Db::name('video_group_item')
  117. ->whereIn('video_group_id', $ids)
  118. ->where('is_del', CommonModel::$del_normal)
  119. ->save([
  120. 'is_del' => CommonModel::$del_deleted,
  121. 'updatetime' => date('Y-m-d H:i:s'),
  122. ]);
  123. Db::commit();
  124. return json_show(CommonModel::$success, '删除成功');
  125. } catch (Exception $exception) {
  126. Db::rollback();
  127. return json_show(CommonModel::$error_param, '删除失败,' . $exception->getMessage());
  128. }
  129. }
  130. //视频分组置顶
  131. public static function top(array $data = []): Json
  132. {
  133. $res = VideoGroupModel::where('id', $data['id'])
  134. ->where('is_top', '<>', $data['is_top'])
  135. ->save([
  136. 'is_top' => $data['is_top'],
  137. 'updatetime' => date('Y-m-d H:i:s'),
  138. 'updaterid' => self::$uid,
  139. 'updater' => self::$uid
  140. ]);
  141. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$success, '操作失败,该视频分组不存在或重复操作');
  142. }
  143. }