123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Log;
- class UserActionBar extends Model
- {
- protected $table = "user_action_bars";
- protected $fillable = ["owner_type", "owner_id", "bar_id"];
- public $timestamps = false;
- /**
- * 配置角色/用户/群组有权限的action bars,当前的做法是,
- * 每次更新都先把ownerId和ownerType匹配的数据全部标记删除,
- * 然后然批量插入新的配置的action bars数据行。
- *
- * @param $ownerType 拥有者用户类型
- * @param $ownerId 拥有者用户id
- * @param $barId 分配的action bars
- * @return array
- */
- public function AssignUserActionBar($ownerType, $ownerId, $barIds)
- {
- if ($ownerType == "") {
- return ["code" => EMPTY_OWNER_TYPE];
- }
- if ($ownerId < 1) {
- return ["code" => EMPTY_OWNER_ID];
- }
- // 删除所有ownerType 和 ownerId
- $this->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)
- ->update(["is_del" => true]);
- Log::debug(gettype($barIds));
- Log::debug(count($barIds));
- Log::debug($barIds);
- // 重新添加ownerType 和 ownerId的数据
- if (count($barIds) < 1) {
- return ["code" => SUCCESS, "data" => []];
- }
- $data = [];
- foreach ($barIds as $barId) {
- array_push($data, array("owner_type" => $ownerType, "owner_id" => $ownerId, "bar_id" => $barId, "is_del" => false));
- }
- $this->insert($data);
- // 插入批量插入的数据,并返回给前端
- $new_data = $this->select("id", "owner_type", "owner_id", "bar_id", "status")
- ->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)->get();
- return ["code" => SUCCESS, "data" => $new_data];
- }
- private function checkActionBar($barId)
- {
- return SUCCESS;
- }
- private function checkOwner($ownerType, $ownerId)
- {
- return SUCCESS;
- }
- public function RemoveUserActionBar($id)
- {
- if ($id < 1) {
- return INVALID_U_A_ID;
- }
- // check the data if exist in system.
- // $item = $this->where("id", $id)->where("is_del", false)->first();
- // if (!$item) {
- // return INVALID_U_A_ID;
- // }
- $row = $this->where("id", $id)->where("is_del", false)->update(["is_del" => true]);
- if ($row == 0) {
- return INVALID_GROUP_ID;
- }
- return SUCCESS;
- }
- /**
- * @param $ownerType
- * @param $ownerId
- * @return array
- */
- public function ListActionBarIds($ownerType, $ownerId)
- {
- $opera = $this->select("bar_id");
- if ($ownerType != "") {
- $opera = $opera->where("owner_type", $ownerType);
- }
- $bars = $opera->where("owner_id", $ownerId)->where("status", "normal")->where("is_del", false)->get();
- if (count($bars) == 0) {
- return [];
- }
- $bar_ids = [];
- foreach ($bars as $bar) {
- array_push($bar_ids, $bar["bar_id"]);
- }
- Log::debug("========" . json_encode($bar_ids));
- return $bar_ids;
- }
- }
|