VideoCategoryService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\VideoCategoryModel;
  13. use App\Models\ActionLogModel;
  14. use App\Services\BaseService;
  15. use Illuminate\Support\Facades\DB;
  16. /**
  17. * 服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class VideoCategoryService
  21. * @package App\Services\Common
  22. */
  23. class VideoCategoryService extends BaseService
  24. {
  25. public static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * VideoCategoryService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new VideoCategoryModel();
  35. }
  36. /**
  37. * 静态入口
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = new static();
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 列表
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['mark' => 1];
  55. // 状态筛选
  56. if (isset($params['status']) && $params['status'] !== '') {
  57. $where['status'] = intval($params['status']);
  58. }
  59. $query = $this->model
  60. ->where($where)
  61. ->where(function ($query) use ($params) {
  62. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  63. if ($keyword) {
  64. $query->where('name', 'like', "%{$keyword}%");
  65. }
  66. })
  67. ->select([
  68. 'id',
  69. 'pid',
  70. 'name',
  71. 'status',
  72. 'sort',
  73. 'create_time'
  74. ])
  75. ->orderBy('sort', 'asc')
  76. ->orderBy('id', 'desc');
  77. $list = $query->paginate($pageSize > 0 ? $pageSize : 9999999);
  78. $list = $list ? $list->toArray() : [];
  79. if ($list) {
  80. foreach ($list['data'] as &$item) {
  81. $item['create_time'] = $item['create_time']
  82. ? datetime($item['create_time'], 'Y-m-d H:i:s')
  83. : '';
  84. // 状态转义
  85. $item['status_text'] = $item['status'] == 1 ? '启用' : '禁用';
  86. }
  87. }
  88. return [
  89. 'pageSize' => $pageSize,
  90. 'total' => $list['total'] ?? 0,
  91. 'list' => $list['data'] ?? []
  92. ];
  93. }
  94. /**
  95. * 分类下拉选项
  96. * @return array
  97. */
  98. public function options()
  99. {
  100. $param = request()->all();
  101. // 关键词(分类名)
  102. $keyword = getter($param, "keyword");
  103. $parentId = getter($param, "parent_id");
  104. $datas = $this->model->where(function ($query) use ($parentId) {
  105. if ($parentId) {
  106. // 只取某个父分类下的
  107. $query->where(['pid' => $parentId, 'mark' => 1]);
  108. } else {
  109. // 默认取所有启用的
  110. $query->where(['status' => 1, 'mark' => 1]);
  111. }
  112. })
  113. ->when($keyword, function ($query) use ($keyword) {
  114. $query->where('name', 'like', "%{$keyword}%");
  115. })
  116. ->select(['id', 'pid', 'name', 'status'])
  117. ->orderBy('sort', 'asc')
  118. ->get();
  119. return $datas ? $datas->toArray() : [];
  120. }
  121. /**
  122. * 删除七天之前标记软删除的数据
  123. */
  124. public function delete()
  125. {
  126. // 设置日志标题
  127. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除视频分类信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  128. ActionLogModel::record();
  129. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  130. return parent::delete();
  131. }
  132. }