GroupUser.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 GroupUser extends Model
  7. {
  8. protected $table = "group_users";
  9. public $timestamps = false;
  10. protected $fillable = ["group_id", "user_id"];
  11. /**
  12. * todo
  13. * 这里需要分页,也可能需要根据群组的状态排序,后面再处理
  14. * 列出用户相关的群组
  15. *
  16. * @param $uid
  17. * @return array
  18. */
  19. public function ListGroupIds($uid)
  20. {
  21. $groups = $this->select("group_id")->where("user_id", $uid)->where("is_del", false)->all();
  22. $group_ids = [];
  23. foreach ($groups as $group) {
  24. array_push($group_ids, $group->group_id);
  25. }
  26. return $group_ids;
  27. }
  28. public function GroupAddUser($gid, $uid)
  29. {
  30. if ($gid == "") {
  31. return ["code" => EMPTY_GROUP_ID];
  32. }
  33. if ($uid == "") {
  34. return ["code" => EMPTY_USER_ID];
  35. }
  36. $result = $this->firstOrCreate(["group_id" => $gid, "user_id" => $uid, "is_del" => false]);
  37. unset($result["is_del"]);
  38. unset($result["created_user_id"]);
  39. unset($result["updated_user_id"]);
  40. return ["code" => SUCCESS, "data" => $result];
  41. }
  42. public function GroupRemoveUser($id)
  43. {
  44. if ($id < 1) {
  45. return INVALID_GROUP_USER_ID;
  46. }
  47. $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
  48. if ($row == 0) {
  49. return INVALID_GROUP_USER_ID;
  50. }
  51. return SUCCESS;
  52. }
  53. }