UserActionBar.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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($uid, $barId)
  11. {
  12. if ($uid == "") {
  13. return "empty user id";
  14. }
  15. if ($barId == "") {
  16. return "empty action bar id";
  17. }
  18. //INSERT INTO demo_in(a,b,c) SELECT 123, 2, 4 FROM DUAL WHERE NOT EXISTS(SELECT c FROM demo_in WHERE c = 4);
  19. $sql = sprintf("insert into user_action_bars(user_id, bar_id)
  20. select ?, ? from temp_uab
  21. where not exists(
  22. select id from user_action_bars
  23. where user_id = ? and bar_id = ? and is_del = false);");
  24. $result = DB::insert($sql, [$uid, $barId, $uid, $barId]);
  25. Log::debug("AddUserActionBar " . $result);
  26. return "success";
  27. }
  28. public function RemoveUserActionBar($uid, $barId)
  29. {
  30. if ($uid == "") {
  31. return "empty user id";
  32. }
  33. if ($barId == "") {
  34. return "empty action bar id";
  35. }
  36. $data = $this->where("user_id", $uid)
  37. ->where("bar_id", $barId)
  38. ->where("is_del", false)
  39. ->first();
  40. if (!$data) {
  41. return "nothing to remove";
  42. }
  43. $this->is_del = true;
  44. $this->where("user_id", $uid)
  45. ->where("bar_id", $barId)
  46. ->where("is_del", false)
  47. ->update(["is_del" => true]);
  48. return "success";
  49. }
  50. /**
  51. * @param $uid
  52. * @return array
  53. */
  54. public function ListActionBarIds($uid)
  55. {
  56. $bars = $this->select("bar_id")
  57. ->where("user_id", $uid)
  58. ->where("status", "normal")
  59. ->where("is_del", false)
  60. ->get();
  61. if (count($bars) == 0) {
  62. return [];
  63. }
  64. $bar_ids = [];
  65. foreach ($bar_ids as $bar_id) {
  66. array_push($bar_ids, $bar_id->bar_id);
  67. }
  68. return $bar_ids;
  69. }
  70. }