Attachment.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\admin\controller\routine;
  3. use Throwable;
  4. use app\common\controller\Backend;
  5. use app\common\model\Attachment as AttachmentModel;
  6. class Attachment extends Backend
  7. {
  8. /**
  9. * @var object
  10. * @phpstan-var AttachmentModel
  11. */
  12. protected object $model;
  13. protected string|array $quickSearchField = 'name';
  14. protected array $withJoinTable = ['admin', 'user'];
  15. protected string|array $defaultSortField = 'last_upload_time,desc';
  16. public function initialize(): void
  17. {
  18. parent::initialize();
  19. $this->model = new AttachmentModel();
  20. }
  21. /**
  22. * 删除
  23. * @param array $ids
  24. * @throws Throwable
  25. */
  26. public function del(array $ids = []): void
  27. {
  28. if (!$this->request->isDelete() || !$ids) {
  29. $this->error(__('Parameter error'));
  30. }
  31. $where = [];
  32. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  33. if ($dataLimitAdminIds) {
  34. $where[] = [$this->dataLimitField, 'in', $dataLimitAdminIds];
  35. }
  36. $pk = $this->model->getPk();
  37. $where[] = [$pk, 'in', $ids];
  38. $count = 0;
  39. $data = $this->model->where($where)->select();
  40. try {
  41. foreach ($data as $v) {
  42. $count += $v->delete();
  43. }
  44. } catch (Throwable $e) {
  45. $this->error(__('%d records and files have been deleted', [$count]) . $e->getMessage());
  46. }
  47. if ($count) {
  48. $this->success(__('%d records and files have been deleted', [$count]));
  49. } else {
  50. $this->error(__('No rows were deleted'));
  51. }
  52. }
  53. }