UserActionBar.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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" => SUCCESS, "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. $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  54. if ($row == 0) {
  55. return INVALID_GROUP_ID;
  56. }
  57. return SUCCESS;
  58. }
  59. /**
  60. * @param $uid
  61. * @return array
  62. */
  63. public function ListActionBarIds($uid)
  64. {
  65. $bars = $this->select("bar_id")
  66. ->where("user_id", $uid)
  67. ->where("status", "normal")
  68. ->where("is_del", false)
  69. ->get();
  70. if (count($bars) == 0) {
  71. return [];
  72. }
  73. $bar_ids = [];
  74. foreach ($bar_ids as $bar_id) {
  75. array_push($bar_ids, $bar_id->bar_id);
  76. }
  77. return $bar_ids;
  78. }
  79. }