Group.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Log;
  5. use PhpParser\Node\Expr\AssignOp\Mod;
  6. class Group extends Model
  7. {
  8. protected $table = "groups";
  9. public $timestamps = false;
  10. /**
  11. * 创建一个用户组,组名必填
  12. *
  13. * @param array $params
  14. * @return array|int
  15. */
  16. public function CreateGroup(array $params)
  17. {
  18. $this->name = $params["name"];
  19. $this->description = $params["description"];
  20. $this->icon = $params["icon"];
  21. $this->status = "normal";
  22. if ($this->name == "") {
  23. return ["code" => EMPTY_GROUP_NAME];
  24. }
  25. // check and isn't allowed same name of group
  26. $group = $this->where("name", $this->name)->where("is_del", false)->first();
  27. if ($group) {
  28. return ["code" => REPEAT_GROUP_NAME];
  29. }
  30. $this->save();
  31. $params["id"] = $this->getQueueableId();
  32. return ["code" => SUCCESS, "data" => $params];
  33. }
  34. public function ModifyGroup(array $params)
  35. {
  36. $update = [];
  37. $gid = $params["id"];
  38. if ($gid == "") {
  39. return EMPTY_GROUP_ID;
  40. }
  41. if ($params["name"] != "") {
  42. $update["name"] = $params["name"];
  43. }
  44. if ($params["description"] != "") {
  45. $update["description"] = $params["description"];
  46. }
  47. if ($params["icon"] != "") {
  48. $update["icon"] = $params["icon"];
  49. }
  50. if (count($update) == 0) {
  51. return NOTHING_UPDATE;
  52. }
  53. $group = $this->where("id", $gid)->where("is_del", false)->first();
  54. if (!$group) {
  55. return INVALID_GROUP_ID;
  56. }
  57. $this->where("id", $gid)->where("is_del", false)->update($update);
  58. return SUCCESS;
  59. }
  60. public function DeleteGroup(array $params)
  61. {
  62. $gid = $params["id"];
  63. if ($gid == "") {
  64. return EMPTY_GROUP_ID;
  65. }
  66. $row = $this->where("id", $gid)->where("is_del", false)->update(["is_del" => true]);
  67. if ($row == 0) {
  68. return INVALID_GROUP_ID;
  69. }
  70. return SUCCESS;
  71. }
  72. // public function LoadGroup(array $params){
  73. // $gid = $params["gid"];
  74. // if ($gid == ""){
  75. // return"empty group id";
  76. // }
  77. // }
  78. public function ListGroup(array $params)
  79. {
  80. $uid = $params["uid"];
  81. $groupUser = new GroupUser();
  82. $groupIds = $groupUser->ListGroupIds($uid);
  83. if (count($groupIds) == 0) {
  84. return [];
  85. }
  86. //
  87. $result = $this->select("id", "name", "description", "icon", "status")
  88. ->where("id", $groupIds)
  89. ->where("is_del", false)
  90. ->all();
  91. return $result;
  92. }
  93. }