UserActionBar.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. class UserActionBar extends Model
  7. {
  8. protected $table = "user_action_bars";
  9. public $timestamps = false;
  10. public function AddUserActionBar($owner_type, $owner_id, $barId)
  11. {
  12. if ($owner_type == "") {
  13. return "empty user type";
  14. }
  15. if ($owner_id == "") {
  16. return "empty user id";
  17. }
  18. if ($barId == "") {
  19. return "empty action bar id";
  20. }
  21. //INSERT INTO demo_in(a,b,c) SELECT 123, 2, 4 FROM DUAL WHERE NOT EXISTS(SELECT c FROM demo_in WHERE c = 4);
  22. $sql = sprintf("insert into user_action_bars(owner_type, owner_id, bar_id)
  23. select ?, ?, ? from temp_uab
  24. where not exists(
  25. select id from user_action_bars
  26. where owner_type = ? and owner_id = ? and bar_id = ? and is_del = false);");
  27. $result = DB::insert($sql, [$owner_type, $owner_id, $barId, $owner_type, $owner_id, $barId]);
  28. Log::debug("AddUserActionBar " . $result);
  29. return "success";
  30. }
  31. public function RemoveUserActionBar($owner_type, $owner_id, $barId)
  32. {
  33. if ($owner_type == "") {
  34. return "empty user type";
  35. }
  36. if ($owner_id == "") {
  37. return "empty user id";
  38. }
  39. if ($barId == "") {
  40. return "empty action bar id";
  41. }
  42. $data = $this->where("owner_type", $owner_type)
  43. ->where("owner_id", $owner_id)
  44. ->where("bar_id", $barId)
  45. ->where("is_del", false)
  46. ->first();
  47. if (!$data) {
  48. return "nothing to remove";
  49. }
  50. $this->is_del = true;
  51. $this->where("owner_type", $owner_type)
  52. ->where("owner_id", $owner_id)
  53. ->where("bar_id", $barId)
  54. ->where("is_del", false)
  55. ->update(["is_del" => true]);
  56. return "success";
  57. }
  58. /**
  59. * @param $uid
  60. * @return array
  61. */
  62. public function ListActionBarIds($uid)
  63. {
  64. $bars = $this->select("bar_id")
  65. ->where("user_id", $uid)
  66. ->where("status", "normal")
  67. ->where("is_del", false)
  68. ->get();
  69. if (count($bars) == 0) {
  70. return [];
  71. }
  72. $bar_ids = [];
  73. foreach ($bar_ids as $bar_id) {
  74. array_push($bar_ids, $bar_id->bar_id);
  75. }
  76. return $bar_ids;
  77. }
  78. }