UserActionBar.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Log;
  5. class UserActionBar extends Model
  6. {
  7. protected $table = "user_action_bars";
  8. protected $fillable = ["owner_type", "owner_id", "bar_id"];
  9. public $timestamps = false;
  10. public function AddUserActionBar($ownerType, $ownerId, $barId)
  11. {
  12. if ($ownerType == "") {
  13. return ["code" => EMPTY_OWNER_TYPE];
  14. }
  15. if ($ownerId == "") {
  16. return ["code" => EMPTY_OWNER_ID];
  17. }
  18. if ($barId == "") {
  19. return ["code" => EMPTY_BAR_ID];
  20. }
  21. // check owner and barId if exist in system.
  22. $code = $this->checkOwner($ownerType, $ownerId);
  23. if ($code != 0) {
  24. return ["code" => $code];
  25. }
  26. $code = $this->checkActionBar($barId);
  27. if ($code != 0) {
  28. return ["code" => $code];
  29. }
  30. $result = $this->firstOrCreate(["owner_type" => $ownerType, "owner_id" => $ownerId, "bar_id" => $barId, "is_del" => false]);
  31. unset($result["is_del"]);
  32. unset($result["created_user_id"]);
  33. unset($result["updated_user_id"]);
  34. return ["code" => SUCCESS, "data" => $result];
  35. }
  36. private function checkActionBar($barId)
  37. {
  38. return SUCCESS;
  39. }
  40. private function checkOwner($ownerType, $ownerId)
  41. {
  42. return SUCCESS;
  43. }
  44. public function RemoveUserActionBar($id)
  45. {
  46. if ($id < 1) {
  47. return INVALID_U_A_ID;
  48. }
  49. // check the data if exist in system.
  50. // $item = $this->where("id", $id)->where("is_del", false)->first();
  51. // if (!$item) {
  52. // return INVALID_U_A_ID;
  53. // }
  54. $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  55. if ($row == 0) {
  56. return INVALID_GROUP_ID;
  57. }
  58. return SUCCESS;
  59. }
  60. /**
  61. * @param $ownerType
  62. * @param $ownerId
  63. * @return array
  64. */
  65. public function ListActionBarIds($ownerType, $ownerId)
  66. {
  67. $opera = $this->select("bar_id");
  68. if ($ownerType != "") {
  69. $opera = $opera->where("owner_type", $ownerType);
  70. }
  71. $bars = $opera->where("owner_id", $ownerId)->where("status", "normal")->where("is_del", false)->get();
  72. if (count($bars) == 0) {
  73. return [];
  74. }
  75. $bar_ids = [];
  76. foreach ($bars as $bar) {
  77. array_push($bar_ids, $bar["bar_id"]);
  78. }
  79. Log::debug("========" . json_encode($bar_ids));
  80. return $bar_ids;
  81. }
  82. }