UserActionBar.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // 重新添加ownerType 和 ownerId的数据
  32. if (count($barIds) < 1) {
  33. return ["code" => SUCCESS, "data" => []];
  34. }
  35. $data = [];
  36. foreach ($barIds as $barId) {
  37. array_push($data, array("owner_type" => $ownerType, "owner_id" => $ownerId, "bar_id" => $barId, "is_del" => false));
  38. }
  39. $this->insert($data);
  40. // 查出批量插入的数据,并返回给前端
  41. $new_data = $this->select("id", "owner_type", "owner_id", "bar_id", "status")
  42. ->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)->get();
  43. return ["code" => SUCCESS, "data" => $new_data];
  44. }
  45. private function checkActionBar($barId)
  46. {
  47. return SUCCESS;
  48. }
  49. private function checkOwner($ownerType, $ownerId)
  50. {
  51. return SUCCESS;
  52. }
  53. public function RemoveUserActionBar($id)
  54. {
  55. if ($id < 1) {
  56. return INVALID_U_A_ID;
  57. }
  58. // check the data if exist in system.
  59. // $item = $this->where("id", $id)->where("is_del", false)->first();
  60. // if (!$item) {
  61. // return INVALID_U_A_ID;
  62. // }
  63. $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  64. if ($row == 0) {
  65. return INVALID_GROUP_ID;
  66. }
  67. return SUCCESS;
  68. }
  69. /**
  70. * @param $ownerType
  71. * @param $ownerId
  72. * @return array
  73. */
  74. public function ListActionBarIds($ownerType, $ownerId)
  75. {
  76. $opera = $this->select("bar_id");
  77. if ($ownerType != "") {
  78. $opera = $opera->where("owner_type", $ownerType);
  79. }
  80. $bars = $opera->where("owner_id", $ownerId)->where("status", "normal")->where("is_del", false)->get();
  81. if (count($bars) == 0) {
  82. return [];
  83. }
  84. $bar_ids = [];
  85. foreach ($bars as $bar) {
  86. array_push($bar_ids, $bar["bar_id"]);
  87. }
  88. Log::debug("========" . json_encode($bar_ids));
  89. return $bar_ids;
  90. }
  91. }