Allocation.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace app\api\controller\invitation;
  3. use app\common\controller\Frontend;
  4. use app\common\service\CompanyPackageService;
  5. use think\facade\Db;
  6. class Allocation extends Frontend
  7. {
  8. protected array $noNeedPermission = ['allocationList', 'assign', 'cancel'];
  9. protected $companyPackageService;
  10. public function initialize(): void
  11. {
  12. parent::initialize();
  13. $this->companyPackageService = new CompanyPackageService();
  14. }
  15. /**
  16. * 获取可分配的企业套餐列表(区分类型)
  17. */
  18. public function allocationList()
  19. {
  20. $userId = $this->auth->id;
  21. // 获取用户所在企业
  22. $companyService = new CompanyPackageService();
  23. $companyInfo = $companyService->getUserCompany($userId);
  24. if (!$companyInfo) {
  25. $this->error('您不属于任何企业');
  26. }
  27. $result = $this->companyPackageService->getAvailableCompanyPackages(
  28. $companyInfo['id'],
  29. $userId
  30. );
  31. $this->success('', $result);
  32. }
  33. /**
  34. * 分配套餐给企业成员(统一处理所有卡类型)
  35. */
  36. public function assign()
  37. {
  38. if (!$this->request->isPost()) {
  39. $this->error('非法请求');
  40. }
  41. $userId = $this->auth->id;
  42. $seatsId = $this->request->post('seats_id', 0);
  43. $assignedToUserId = $this->request->post('assigned_to_user_id', 0);
  44. $remark = $this->request->post('remark', '');
  45. if ($seatsId <= 0 || $assignedToUserId <= 0) {
  46. $this->error('参数错误');
  47. }
  48. try {
  49. // 获取席位信息以确定卡类型
  50. $seats = Db::table('ba_company_package_seats')
  51. ->where('id', $seatsId)
  52. ->find();
  53. if (!$seats) {
  54. $this->error('企业套餐不存在');
  55. }
  56. if ($seats['card_type'] >= 3) {
  57. // 日期卡:使用原有的分配逻辑
  58. $result = $this->companyPackageService->allocatePackage(
  59. $seatsId,
  60. $assignedToUserId,
  61. $userId,
  62. $remark
  63. );
  64. } else {
  65. // 次卡/积分卡:使用新的权限分配逻辑
  66. $result = $this->companyPackageService->assignPackagePermission(
  67. $seatsId,
  68. $assignedToUserId,
  69. $userId,
  70. $remark
  71. );
  72. }
  73. } catch (\Throwable $e) {
  74. $this->error($e->getMessage());
  75. }
  76. if ($result['success'] ?? $result) {
  77. $this->success('分配成功');
  78. } else {
  79. $this->error('分配失败');
  80. }
  81. }
  82. /**
  83. * 取消分配
  84. */
  85. public function cancel()
  86. {
  87. if (!$this->request->isPost()) {
  88. $this->error('非法请求');
  89. }
  90. $userId = $this->auth->id;
  91. $allocationId = $this->request->post('allocation_id', 0);
  92. $remark = $this->request->post('remark', '');
  93. if ($allocationId <= 0) {
  94. $this->error('参数错误');
  95. }
  96. // try {
  97. // 获取分配记录以确定卡类型
  98. $allocation = Db::table('ba_company_package_allocation')
  99. ->alias('cpa')
  100. ->leftJoin('ba_package_price pp', 'pp.id = cpa.package_price_id')
  101. ->where('cpa.id', $allocationId)
  102. ->field('cpa.*, pp.card_type')
  103. ->find();
  104. if (!$allocation) {
  105. $this->error('分配记录不存在');
  106. }
  107. if ($allocation['card_type'] >= 3) {
  108. // 日期卡:使用原有的取消分配逻辑
  109. $result = $this->companyPackageService->cancelAllocation(
  110. $allocationId,
  111. $userId,
  112. $remark
  113. );
  114. } else {
  115. // 次卡/积分卡:使用新的权限取消逻辑
  116. $result = $this->companyPackageService->cancelPackagePermission(
  117. $allocationId,
  118. $userId,
  119. $remark
  120. );
  121. }
  122. // } catch (\Throwable $e) {
  123. // $this->error($e->getMessage());
  124. // }
  125. if ($result) {
  126. $this->success('取消分配成功');
  127. } else {
  128. $this->error('取消分配失败');
  129. }
  130. }
  131. /**
  132. * 获取企业成员已分配的套餐
  133. */
  134. public function myAssignedPackages()
  135. {
  136. $userId = $this->auth->id;
  137. $allocations = Db::name('ba_company_package_allocation')
  138. ->alias('cpa')
  139. ->leftJoin('ba_product p', 'p.id = cpa.product_id')
  140. ->leftJoin('ba_package_price pp', 'pp.id = cpa.package_price_id')
  141. ->leftJoin('ba_user u', 'u.id = cpa.assigned_by_user_id')
  142. ->where('cpa.assigned_to_user_id', $userId)
  143. ->where('cpa.allocation_status', 1) // 已分配
  144. ->field('cpa.*,
  145. p.name as product_name, p.type as product_type, p.link,
  146. pp.spec_name, pp.type as package_type, pp.card_type,
  147. u.nickname as assigned_by_name')
  148. ->select()
  149. ->toArray();
  150. $result = [];
  151. foreach ($allocations as $allocation) {
  152. $result[] = [
  153. 'allocation_id' => $allocation['id'],
  154. 'product_name' => $allocation['product_name'],
  155. 'product_type' => $allocation['product_type'],
  156. 'product_link' => $allocation['link'],
  157. 'spec_name' => $allocation['spec_name'],
  158. 'package_type' => $allocation['package_type'],
  159. 'card_type' => $allocation['card_type'],
  160. 'card_type_text' => $this->companyPackageService->getCardTypeText($allocation['card_type']),
  161. 'end_time' => $allocation['end_time'],
  162. 'remaining_times' => $allocation['remaining_times'],
  163. 'remaining_points' => $allocation['remaining_points'],
  164. 'assigned_by' => $allocation['assigned_by_name'],
  165. 'allocation_time' => $allocation['allocation_time']
  166. ];
  167. }
  168. $this->success('', $result);
  169. }
  170. /**
  171. * 使用企业次卡/积分卡
  172. */
  173. public function useSharedPackage()
  174. {
  175. if (!$this->request->isPost()) {
  176. $this->error('非法请求');
  177. }
  178. $userId = $this->auth->id;
  179. $packagePriceId = $this->request->post('package_price_id', 0);
  180. $quantity = $this->request->post('quantity', 1);
  181. $remark = $this->request->post('remark', '');
  182. if ($packagePriceId <= 0 || $quantity <= 0) {
  183. $this->error('参数错误');
  184. }
  185. $result = $this->companyPackageService->useSharedCompanyPackage(
  186. $userId,
  187. $packagePriceId,
  188. $quantity,
  189. null,
  190. $remark
  191. );
  192. if ($result['success']) {
  193. $this->success($result['message'], [
  194. 'remaining_times' => $result['remaining_times'],
  195. 'remaining_points' => $result['remaining_points']
  196. ]);
  197. } else {
  198. $this->error('使用失败');
  199. }
  200. }
  201. }