UserActionBar.php 2.2 KB

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