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. ;
  105. if (!$allocation) {
  106. $this->error('分配记录不存在');
  107. }
  108. if ($allocation['card_type'] >= 3) {
  109. // 日期卡:使用原有的取消分配逻辑
  110. $result = $this->companyPackageService->cancelAllocation(
  111. $allocationId,
  112. $userId,
  113. $remark
  114. );
  115. } else {
  116. // 次卡/积分卡:使用新的权限取消逻辑
  117. $result = $this->companyPackageService->cancelPackagePermission(
  118. $allocationId,
  119. $userId,
  120. $remark
  121. );
  122. }
  123. } catch (\Throwable $e) {
  124. $this->error($e->getMessage());
  125. }
  126. if ($result) {
  127. $this->success('取消分配成功');
  128. } else {
  129. $this->error('取消分配失败');
  130. }
  131. }
  132. /**
  133. * 获取企业成员已分配的套餐
  134. */
  135. public function myAssignedPackages()
  136. {
  137. $userId = $this->auth->id;
  138. $allocations = Db::name('ba_company_package_allocation')
  139. ->alias('cpa')
  140. ->leftJoin('ba_product p', 'p.id = cpa.product_id')
  141. ->leftJoin('ba_package_price pp', 'pp.id = cpa.package_price_id')
  142. ->leftJoin('ba_user u', 'u.id = cpa.assigned_by_user_id')
  143. ->where('cpa.assigned_to_user_id', $userId)
  144. ->where('cpa.allocation_status', 1) // 已分配
  145. ->field('cpa.*,
  146. p.name as product_name, p.type as product_type, p.link,
  147. pp.spec_name, pp.type as package_type, pp.card_type,
  148. u.nickname as assigned_by_name')
  149. ->select()
  150. ->toArray();
  151. $result = [];
  152. foreach ($allocations as $allocation) {
  153. $result[] = [
  154. 'allocation_id' => $allocation['id'],
  155. 'product_name' => $allocation['product_name'],
  156. 'product_type' => $allocation['product_type'],
  157. 'product_link' => $allocation['link'],
  158. 'spec_name' => $allocation['spec_name'],
  159. 'package_type' => $allocation['package_type'],
  160. 'card_type' => $allocation['card_type'],
  161. 'card_type_text' => $this->companyPackageService->getCardTypeText($allocation['card_type']),
  162. 'end_time' => $allocation['end_time'],
  163. 'remaining_times' => $allocation['remaining_times'],
  164. 'remaining_points' => $allocation['remaining_points'],
  165. 'assigned_by' => $allocation['assigned_by_name'],
  166. 'allocation_time' => $allocation['allocation_time']
  167. ];
  168. }
  169. $this->success('', $result);
  170. }
  171. /**
  172. * 使用企业次卡/积分卡
  173. */
  174. public function useSharedPackage()
  175. {
  176. if (!$this->request->isPost()) {
  177. $this->error('非法请求');
  178. }
  179. $userId = $this->auth->id;
  180. $packagePriceId = $this->request->post('package_price_id', 0);
  181. $quantity = $this->request->post('quantity', 1);
  182. $remark = $this->request->post('remark', '');
  183. if ($packagePriceId <= 0 || $quantity <= 0) {
  184. $this->error('参数错误');
  185. }
  186. $result = $this->companyPackageService->useSharedCompanyPackage(
  187. $userId,
  188. $packagePriceId,
  189. $quantity,
  190. null,
  191. $remark
  192. );
  193. if ($result['success']) {
  194. $this->success($result['message'], [
  195. 'remaining_times' => $result['remaining_times'],
  196. 'remaining_points' => $result['remaining_points']
  197. ]);
  198. } else {
  199. $this->error('使用失败');
  200. }
  201. }
  202. }