DeleteDb.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services\Interface\Database\Delete;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Services\Interface\Database\CommonDb;
  6. class DeleteDb extends CommonDb
  7. {
  8. public function __construct(array $interfaceInfo, array $assembleInfo, array $assembleColumns, array $requestData, array $columnIdToCodes)
  9. {
  10. parent::__construct($interfaceInfo, $assembleInfo, $assembleColumns, $requestData, $columnIdToCodes);
  11. }
  12. public function exec()
  13. {
  14. $delete_type = $this->interfaceSettingInfo['delete_type'] ?? 0;
  15. if (2 === $delete_type) { // 真实删除单条数据
  16. $result = $this->deleteTrueOne();
  17. } elseif (3 === $delete_type) { // 批量伪删除
  18. $result = $this->deleteFakeMulti();
  19. } elseif (1 === $delete_type) { // 伪删除单条数据
  20. $result = $this->deleteFakeOne();
  21. } else {
  22. $result = $this->deleteFakeOne();
  23. }
  24. return $result;
  25. }
  26. /**
  27. * 真实删除.
  28. *
  29. * @return bool
  30. */
  31. public function deleteTrueOne()
  32. {
  33. $entityId = $this->requestData['id'] ?? '';
  34. $entityMid = $this->requestData['mid'] ?? '';
  35. if (empty($entityId) && empty($entityMid)) {
  36. abort(508, '参数错误');
  37. }
  38. $find = DB::connection($this->getDbConnection())->table($this->table);
  39. if ($entityId) {
  40. $find->where('id', $entityId);
  41. }
  42. if ($entityMid) {
  43. $find->where('mid', $entityMid);
  44. }
  45. return $find->delete();
  46. }
  47. /**
  48. * 伪删除.
  49. *
  50. * @return array|callable|false
  51. */
  52. public function deleteFakeOne()
  53. {
  54. $entityId = $this->requestData['id'] ?? '';
  55. $entityMid = $this->requestData['mid'] ?? '';
  56. if (empty($entityId) && empty($entityMid)) {
  57. abort(508, '参数错误');
  58. }
  59. $find = DB::connection($this->getDbConnection())->table($this->table);
  60. if ($entityId) {
  61. $find->where('id', $entityId);
  62. }
  63. if ($entityMid) {
  64. $find->where('mid', $entityMid);
  65. }
  66. $existInfo = $find->first('id');
  67. if (empty($existInfo)) {
  68. abort(508, '参数错误2');
  69. }
  70. return DB::table($this->table)->where('id', $existInfo->id)->update(['is_delete' => 1]);
  71. }
  72. /**
  73. * 批量伪删除.
  74. *
  75. * @return int
  76. */
  77. public function deleteFakeMulti()
  78. {
  79. $idArr = $this->requestData['id'] ?? [];
  80. $midArr = $this->requestData['mid'] ?? [];
  81. if (empty($idArr) && empty($midArr)) {
  82. abort(508, '参数错误');
  83. }
  84. // return DB::table($this->table)->where('id', $id)->orWhere('mid', $mid)->update(['is_delete' => 1]);
  85. }
  86. }