UserActionBar.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /**
  11. * 配置角色/用户/群组有权限的action bars,当前的做法是,
  12. * 每次更新都先把ownerId和ownerType匹配的数据全部标记删除,
  13. * 然后然批量插入新的配置的action bars数据行。
  14. *
  15. * @param $ownerType 拥有者用户类型
  16. * @param $ownerId 拥有者用户id
  17. * @param $barId 分配的action bars
  18. * @return array
  19. */
  20. public function AssignUserActionBar($ownerType, $ownerId, $barIds)
  21. {
  22. if ($ownerType == "") {
  23. return ["code" => EMPTY_OWNER_TYPE];
  24. }
  25. if ($ownerId < 1) {
  26. return ["code" => EMPTY_OWNER_ID];
  27. }
  28. // 删除所有ownerType 和 ownerId
  29. $this->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)
  30. ->update(["is_del" => true]);
  31. Log::debug(gettype($barIds));
  32. Log::debug(count($barIds));
  33. Log::debug($barIds);
  34. // 重新添加ownerType 和 ownerId的数据
  35. if (count($barIds) < 1) {
  36. return ["code" => SUCCESS, "data" => []];
  37. }
  38. $data = [];
  39. foreach ($barIds as $barId) {
  40. array_push($data, array("owner_type" => $ownerType, "owner_id" => $ownerId, "bar_id" => $barId, "is_del" => false));
  41. }
  42. $this->insert($data);
  43. // 插入批量插入的数据,并返回给前端
  44. $new_data = $this->select("id", "owner_type", "owner_id", "bar_id", "status")
  45. ->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)->get();
  46. return ["code" => SUCCESS, "data" => $new_data];
  47. }
  48. private function checkActionBar($barId)
  49. {
  50. return SUCCESS;
  51. }
  52. private function checkOwner($ownerType, $ownerId)
  53. {
  54. return SUCCESS;
  55. }
  56. public function RemoveUserActionBar($id)
  57. {
  58. if ($id < 1) {
  59. return INVALID_U_A_ID;
  60. }
  61. // check the data if exist in system.
  62. // $item = $this->where("id", $id)->where("is_del", false)->first();
  63. // if (!$item) {
  64. // return INVALID_U_A_ID;
  65. // }
  66. $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  67. if ($row == 0) {
  68. return INVALID_GROUP_ID;
  69. }
  70. return SUCCESS;
  71. }
  72. /**
  73. * @param $ownerType
  74. * @param $ownerId
  75. * @return array
  76. */
  77. public function ListActionBarIds($ownerType, $ownerId)
  78. {
  79. $opera = $this->select("bar_id");
  80. if ($ownerType != "") {
  81. $opera = $opera->where("owner_type", $ownerType);
  82. }
  83. $bars = $opera->where("owner_id", $ownerId)->where("status", "normal")->where("is_del", false)->get();
  84. if (count($bars) == 0) {
  85. return [];
  86. }
  87. $bar_ids = [];
  88. foreach ($bars as $bar) {
  89. array_push($bar_ids, $bar["bar_id"]);
  90. }
  91. Log::debug("========" . json_encode($bar_ids));
  92. return $bar_ids;
  93. }
  94. }