id ?? 0; if ($relation->founder == 2) { util::fail("不能删老板"); } if ($id == $thisShopAdminId) { util::fail("不能删自己"); } self::valid($relation, $mainId); $adminId = $relation->adminId ?? 0; $admin = AdminClass::getById($adminId, true); if (empty($admin)) { util::fail('没有找到员工信息'); } //员工离职,多处使用,关键词 staff_lz StaffClass::executeStaffResignation($admin, $relation); } public static function valid($relation, $mainId) { if (!isset($relation->mainId) || $relation->mainId != $mainId) { util::fail('没有权限'); } } //创建员工 ssh 2021.5.9 public static function appGenerateStaff($data, $admin) { $mobile = $data['mobile'] ?? ''; $shopId = $data['shopId'] ?? 0; $name = $data['name'] ?? '未命名'; $remark = isset($data['remark']) ? $data['remark'] : ''; $status = 1; $super = $data['super'] ?? 0; $remind = isset($data['remind']) ? $data['remind'] : 0; $roleId = $data['roleId'] ?? 0; $role = AdminRoleClass::getById($roleId); if (empty($role)) { util::fail('没有找到角色!'); } $shop = ShopClass::getById($shopId); if (empty($shop)) { util::fail('没有找到门店57'); } $avatar = $admin['avatar'] ?? ''; $adminId = $admin['id'] ?? 0; $mainId = $shop['mainId'] ?? 0; $shopAdmin = self::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true); if (!empty($shopAdmin)) { //已删除员工补回 if (isset($shopAdmin->delStatus) && $shopAdmin->delStatus == 1) { $shopAdmin->delStatus = 0; $shopAdmin->roleId = $roleId; $shopAdmin->remind = $remind; $shopAdmin->status = $status; $shopAdmin->remark = $remark; $shopAdmin->name = $name; $shopAdmin->avatar = $avatar; $shopAdmin->super = $super; $shopAdmin->save(); AdminRoleClass::counters(['num' => 1], ['id' => $roleId]); } } else { $addData = [ 'adminId' => $adminId, 'roleId' => $roleId, 'remind' => $remind, 'status' => $status, 'name' => $name, 'mobile' => $mobile, 'avatar' => $avatar, 'super' => $super, 'mainId' => $mainId ]; $shopAdmin = self::add($addData, true); AdminRoleClass::counters(['num' => 1], ['id' => $roleId]); } $update = ['currentShopId' => $shopId]; $shop = ShopClass::getById($shopId, true); $pfShopId = $shop->pfShopId ?? 0; $update['currentGhsShopId'] = $pfShopId; AdminClass::updateById($adminId, $update); return $shopAdmin; } /** * 执行员工离职操作 * @param Admin $admin 管理员模型 * @param Staff $staff 员工模型 */ public static function executeStaffResignation($admin, $staff) { // 标记员工为已删除 $staff->delStatus = 1; $staff->save(); $mobile = $admin->mobile; $hasShop = 0; // 检查管理员是否有自己的门店 $shopList = ShopClass::getAllByCondition(['mobile' => $mobile], null, 'id,shopName,mainId,pfShopId,lsShopId,adminId,ptStyle', null, true); if (empty($shopList)) { // 情况1:自己没有店,在别人那边当员工,直接退出 $admin->currentShopId = 0; $admin->currentGhsShopId = 0; $admin->save(); } else { // 情况2:自己有店,自己可能开批零店,也可能只开零售店,也在其他店当员工 $hdMainId = 0; $ghsMainId = 0; if (count($shopList) == 1) { foreach ($shopList as $shop) { if ($shop->ptStyle == 1) { $admin->currentShopId = $shop->id; $admin->currentGhsShopId = 0; $admin->save(); } else { util::fail('账号异常,请联系管理员,编号266389'); } } } else { if (count($shopList) != 2) { util::fail('账号异常,请联系管理员,编号873380'); } foreach ($shopList as $shop) { if ($shop->ptStyle == 2) { $ghsMainId = $shop->mainId; $admin->currentGhsShopId = $shop->id; $admin->save(); } if ($shop->ptStyle == 1) { $hdMainId = $shop->mainId; $admin->currentShopId = $shop->id; $admin->save(); } } if ($ghsMainId != $hdMainId) { util::fail('账号异常,请联系管理,编号0068'); } if ($ghsMainId == 0) { util::fail('账号异常,请联系管理,编号006835'); } } $adminId = $admin->id; $staffList = StaffClass::getAllByCondition(['mainId' => $hdMainId, 'adminId' => $adminId], null, '*', null, true); if (empty($staffList)) { $role = AdminRoleClass::getByCondition(['mainId' => $hdMainId], true); $roleId = $role->id; $avatar = $admin->avatar; $name = $admin->name; $staffData = [ 'adminId' => $adminId, 'mainId' => $hdMainId, 'remind' => 0, 'roleId' => $roleId, 'avatar' => $avatar, 'name' => $name, 'super' => 1, 'finance' => 0, 'switchShop' => 0, 'founder' => 1, 'mobile' => $mobile, ]; self::add($staffData); } else { foreach ($staffList as $myStaff) { $myStaff->delStatus = 0; $myStaff->save(); } } if (count($staffList) > 1) { util::fail('账号异常,请联系管理员,异常编号56988'); } $hasShop = 1; } //把 xhHdDevice 与 xhGhsDevice 表下的数据更新为退出(删除) $ghsDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['adminId'=>$admin->id, 'mainId'=>$staff->mainId], null, 'id,status,clientId', null, true); foreach ($ghsDevices as $ghsDevice) { $ghsDevice->status = 0; $ghsDevice->clientId = $ghsDevice->clientId . '_delete_' . date('Y-m-d H:i:s'); $ghsDevice->save(); } $hdDevices = \bizHd\device\classes\HdDeviceClass::getAllByCondition(['adminId'=>$admin->id, 'mainId'=>$staff->mainId], null, 'id,status,clientId',null, true); foreach ($hdDevices as $hdDevice) { $hdDevice->status = 0; $hdDevice->clientId = $hdDevice->clientId . '_delete_' . date('Y-m-d H:i:s'); $hdDevice->save(); } return ['hasShop' => $hasShop]; } }