| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace app\api\controller\invitation;
- use app\common\controller\Frontend;
- use app\common\service\CompanyPackageService;
- use think\facade\Db;
- class Allocation extends Frontend
- {
- protected array $noNeedPermission = ['allocationList', 'assign', 'cancel'];
- protected $companyPackageService;
- public function initialize(): void
- {
- parent::initialize();
- $this->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('使用失败');
- }
- }
- }
|