companyPackageService = new CompanyPackageService(); } /** * 获取可分配的企业套餐列表(区分类型) */ public function allocationList() { $userId = $this->auth->id; // 获取用户所在企业 $companyService = new CompanyPackageService(); $companyInfo = $companyService->getUserCompany($userId); if (!$companyInfo) { $this->error('您不属于任何企业'); } $result = $this->companyPackageService->getAvailableCompanyPackages( $companyInfo['id'], $userId ); $this->success('', $result); } /** * 分配套餐给企业成员(统一处理所有卡类型) */ public function assign() { if (!$this->request->isPost()) { $this->error('非法请求'); } $userId = $this->auth->id; $seatsId = $this->request->post('seats_id', 0); $assignedToUserId = $this->request->post('assigned_to_user_id', 0); $remark = $this->request->post('remark', ''); if ($seatsId <= 0 || $assignedToUserId <= 0) { $this->error('参数错误'); } try { // 获取席位信息以确定卡类型 $seats = Db::table('ba_company_package_seats') ->where('id', $seatsId) ->find(); if (!$seats) { $this->error('企业套餐不存在'); } if ($seats['card_type'] >= 3) { // 日期卡:使用原有的分配逻辑 $result = $this->companyPackageService->allocatePackage( $seatsId, $assignedToUserId, $userId, $remark ); } else { // 次卡/积分卡:使用新的权限分配逻辑 $result = $this->companyPackageService->assignPackagePermission( $seatsId, $assignedToUserId, $userId, $remark ); } } catch (\Throwable $e) { $this->error($e->getMessage()); } if ($result['success'] ?? $result) { $this->success('分配成功'); } else { $this->error('分配失败'); } } /** * 取消分配 */ public function cancel() { if (!$this->request->isPost()) { $this->error('非法请求'); } $userId = $this->auth->id; $allocationId = $this->request->post('allocation_id', 0); $remark = $this->request->post('remark', ''); if ($allocationId <= 0) { $this->error('参数错误'); } // try { // 获取分配记录以确定卡类型 $allocation = Db::table('ba_company_package_allocation') ->alias('cpa') ->leftJoin('ba_package_price pp', 'pp.id = cpa.package_price_id') ->where('cpa.id', $allocationId) ->field('cpa.*, pp.card_type') ->find(); if (!$allocation) { $this->error('分配记录不存在'); } if ($allocation['card_type'] >= 3) { // 日期卡:使用原有的取消分配逻辑 $result = $this->companyPackageService->cancelAllocation( $allocationId, $userId, $remark ); } else { // 次卡/积分卡:使用新的权限取消逻辑 $result = $this->companyPackageService->cancelPackagePermission( $allocationId, $userId, $remark ); } // } catch (\Throwable $e) { // $this->error($e->getMessage()); // } if ($result) { $this->success('取消分配成功'); } else { $this->error('取消分配失败'); } } /** * 获取企业成员已分配的套餐 */ public function myAssignedPackages() { $userId = $this->auth->id; $allocations = Db::name('ba_company_package_allocation') ->alias('cpa') ->leftJoin('ba_product p', 'p.id = cpa.product_id') ->leftJoin('ba_package_price pp', 'pp.id = cpa.package_price_id') ->leftJoin('ba_user u', 'u.id = cpa.assigned_by_user_id') ->where('cpa.assigned_to_user_id', $userId) ->where('cpa.allocation_status', 1) // 已分配 ->field('cpa.*, p.name as product_name, p.type as product_type, p.link, pp.spec_name, pp.type as package_type, pp.card_type, u.nickname as assigned_by_name') ->select() ->toArray(); $result = []; foreach ($allocations as $allocation) { $result[] = [ 'allocation_id' => $allocation['id'], 'product_name' => $allocation['product_name'], 'product_type' => $allocation['product_type'], 'product_link' => $allocation['link'], 'spec_name' => $allocation['spec_name'], 'package_type' => $allocation['package_type'], 'card_type' => $allocation['card_type'], 'card_type_text' => $this->companyPackageService->getCardTypeText($allocation['card_type']), 'end_time' => $allocation['end_time'], 'remaining_times' => $allocation['remaining_times'], 'remaining_points' => $allocation['remaining_points'], 'assigned_by' => $allocation['assigned_by_name'], 'allocation_time' => $allocation['allocation_time'] ]; } $this->success('', $result); } /** * 使用企业次卡/积分卡 */ public function useSharedPackage() { if (!$this->request->isPost()) { $this->error('非法请求'); } $userId = $this->auth->id; $packagePriceId = $this->request->post('package_price_id', 0); $quantity = $this->request->post('quantity', 1); $remark = $this->request->post('remark', ''); if ($packagePriceId <= 0 || $quantity <= 0) { $this->error('参数错误'); } $result = $this->companyPackageService->useSharedCompanyPackage( $userId, $packagePriceId, $quantity, null, $remark ); if ($result['success']) { $this->success($result['message'], [ 'remaining_times' => $result['remaining_times'], 'remaining_points' => $result['remaining_points'] ]); } else { $this->error('使用失败'); } } }