RecruitmentBySpreadJobs.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Jobs\Api\Promoter;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Servers\DB\DbService;
  10. use App\Models\OpenWork\job\Records;
  11. use App\Facades\Servers\Logs\Log;
  12. use Illuminate\Support\Facades\DB;
  13. use App\Models\Manager\CustomExternal as CustomExternalModel;
  14. use App\Models\Manager\Promoter\RecruitCustomers as RecruitCustomersModel;
  15. use App\Models\Manager\Promoter\Users as UsersModel;
  16. use App\Models\Api\Promoter\Configuration as ConfigurationModel;
  17. use App\Models\Manager\Promoter\EarningsRecord as EarningsRecordModel;
  18. use App\Models\Manager\Promoter\Customers as CustomersModel;
  19. use App\Models\Manager\Promoter\AuthCorp as AuthCorpModel;
  20. use App\Models\Manager\Promoter\ExternalFollow as ExternalFollowModel;
  21. use App\Models\Manager\Promoter\OpenworkGroupMember as OpenworkGroupMemberModel;
  22. /**
  23. * 推广员拉新收益队列
  24. * @author 唐远望
  25. * @version 2.0
  26. * @date 2025-10-14
  27. */
  28. class RecruitmentBySpreadJobs implements ShouldQueue
  29. {
  30. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  31. protected $message_data;
  32. protected $Records;
  33. /**
  34. * Create a new job instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct(array $message_data)
  39. {
  40. $this->message_data = $message_data;
  41. }
  42. /**
  43. * 推广员拉新注册赠送奖励
  44. * @author 唐远望
  45. * @version 1.0
  46. * @date 2025-10-14
  47. * @return void
  48. */
  49. public function handle()
  50. {
  51. try {
  52. $company_id = $this->message_data['company_id'];
  53. (new DbService())->getConnectionNameByCompanyId($company_id);
  54. DB::connection('company')->beginTransaction();
  55. // 创建任务记录
  56. $this->Records = Records::create([
  57. 'job_id' => 'SASS_' . $company_id . '_RecruitmentBySpreadJobs',
  58. 'name' => static::class,
  59. 'payload' => json_encode($this->message_data),
  60. 'status' => 'processing',
  61. 'started_at' => now()
  62. ]);
  63. $uid = $this->message_data['uid'];
  64. $shareUid = $this->message_data['share_uid'];
  65. $company_id = $this->message_data['company_id'];
  66. $binding_scene = $this->message_data['binding_scene'];
  67. $before_revenue_amount = 0;
  68. $customers_atstus = 1;
  69. //查询推广员状态
  70. $UsersModel = new UsersModel();
  71. $CustomersModel = new CustomersModel();
  72. $CustomExternal = new CustomExternalModel();
  73. $AuthCorpModel = new AuthCorpModel();
  74. $ExternalFollowModel = new ExternalFollowModel();
  75. $OpenworkGroupMemberModel = new OpenworkGroupMemberModel();
  76. $EarningsRecordModel = new EarningsRecordModel();
  77. $RecruitCustomersModel = new RecruitCustomersModel();
  78. $promoter_info = $UsersModel->where('custom_uid', $shareUid)->first();
  79. if (!$promoter_info || $promoter_info['status'] == 0) {
  80. return true; //不存在推广员或不是推广员状态,不赠送奖励
  81. $this->Records->delete();
  82. }
  83. $before_revenue_amount = $promoter_info->total_commission_amount;
  84. //查询推广员配置信息
  85. $ConfigurationModel = new ConfigurationModel();
  86. //获取应用配置开关状态
  87. $application_switch_status_data = $ConfigurationModel->where(['code_name' => 'application_switch_status', 'company_id' => $company_id])->first();
  88. if (!$application_switch_status_data) {
  89. return true; //推广员开关信息未配置
  90. $this->Records->delete();
  91. }
  92. if ($application_switch_status_data->content == 0) {
  93. return true; //推广员功能未开启
  94. $this->Records->delete();
  95. }
  96. //国内用户定义配置
  97. $application_domestic_user_data = $ConfigurationModel->where(['code_name' => 'application_domestic_user', 'company_id' => $company_id])->first();
  98. if (!$application_domestic_user_data) {
  99. return true; //国内用户定义未配置
  100. $this->Records->delete();
  101. }
  102. //查询绑定SCRM关系
  103. $corpId = $AuthCorpModel->where('company_id', $company_id)->value('corpid');
  104. $application_domestic_user_content = $application_domestic_user_data->content;
  105. $successful_type_array = $application_domestic_user_content['new_user_definition']['successful_type'] ? explode(',', $application_domestic_user_content['new_user_definition']['successful_type']) : [];
  106. $other_conditions_type_array = $application_domestic_user_content['new_user_definition']['other_conditions_type'] ? explode(',', $application_domestic_user_content['new_user_definition']['other_conditions_type']) : [];
  107. //当开启了添加企微客服或者企微客户状态为正常未流失或者客户已入企微群且未退群 验证时才去查询更新
  108. if (in_array('2', $successful_type_array) || in_array('1', $other_conditions_type_array) || in_array('2', $other_conditions_type_array)) {
  109. //执行企微客户查询更新操作
  110. $CustomExternal->customToExternal($company_id, $uid);
  111. }
  112. if (in_array('2', $successful_type_array)) { //拉新成功定义 1=注册登录小程序 2=添加企微客服
  113. if (!empty($corpId)) {
  114. $externalUserId = $CustomExternal->where('custom_uid', $uid)->where('corpid', $corpId)->value('external_userid'); //Scrm平台外部联系人id
  115. if (!$externalUserId) {
  116. return true; //客户未绑定Scrm平台
  117. $this->Records->delete();
  118. }
  119. }
  120. }
  121. if (in_array('1', $other_conditions_type_array)) { //新用户定义其他条件 1=企微客户状态为正常未流失 2=客户已入企微群且未退群
  122. if (!empty($corpId)) {
  123. $externalUserId = $CustomExternal->where('custom_uid', $uid)->where('corpid', $corpId)->value('external_userid'); //Scrm平台外部联系人id
  124. if ($externalUserId) {
  125. //查询客户状态
  126. $external_follow_info = $ExternalFollowModel->where(['external_userid' => $externalUserId, 'corpid' => $corpId, 'status' => 1])->first(); //状态1=流失 0=正常
  127. if ($external_follow_info) {
  128. return true; //客户已流失
  129. $this->Records->delete();
  130. }
  131. }
  132. }
  133. }
  134. if (in_array('2', $other_conditions_type_array)) { //新用户定义其他条件 1=企微客户状态为正常未流失 2=客户已入企微群且未退群
  135. if (!empty($corpId)) {
  136. $externalUserId = $CustomExternal->where('custom_uid', $uid)->where('corpid', $corpId)->value('external_userid'); //Scrm平台外部联系人id
  137. if ($externalUserId) {
  138. //查询客户状态
  139. $external_group_chat_info = $OpenworkGroupMemberModel->where(['external_userid' => $externalUserId, 'corpid' => $corpId, 'status' => 1])->first(); //状态,0在群,1离群
  140. if ($external_group_chat_info) {
  141. return true; //客户已入企微群,且已退群
  142. $this->Records->delete();
  143. }
  144. }
  145. }
  146. }
  147. //获取客户关系配置规则
  148. $customer_relationship_rules_data = $ConfigurationModel->where(['code_name' => 'customer_relationship_rules', 'company_id' => $company_id])->first();
  149. //获取新客注册奖励规则
  150. $user_registration_reward_rules_data = $ConfigurationModel->where(['code_name' => 'user_registration_reward_rules', 'company_id' => $company_id])->first();
  151. $is_open_user_registration_reward = 0; //未开启
  152. $user_registration_reward_rules_content = '';
  153. if ($user_registration_reward_rules_data) {
  154. $user_registration_reward_rules_content = $user_registration_reward_rules_data->content;
  155. $is_open_user_registration_reward = $user_registration_reward_rules_content['status'];
  156. }
  157. $regist_binding_id = 0;
  158. if ($customer_relationship_rules_data) { //customer_relationship_rules.customer_binding_model_type 客户关系绑定模型 1=简单模式 2=平衡模式 3=竞争模式 4=自定义
  159. //添加用户推广关系绑定
  160. $insert_data = [
  161. 'company_id' => $company_id,
  162. 'promoter_userid' => $shareUid,
  163. 'custom_uid' => $uid,
  164. 'binding_scene' => '注册绑定',
  165. 'status' => '1',
  166. 'insert_time' => time(),
  167. ];
  168. $regist_binding_id = $RecruitCustomersModel->insertGetId($insert_data);
  169. $CustomersModel->insert(['custom_uid' => $uid, 'promoter_userid' => $shareUid, 'insert_time' => time(), 'binding_scene' => '注册绑定', 'status' => $customers_atstus,'company_id' => $company_id]);
  170. }
  171. if ($is_open_user_registration_reward == 1) {
  172. $issuance_review_type = $user_registration_reward_rules_content['issuance_review_type']; //用户注册奖励规则发放审核类型 1=需要审核 2=不需要审核
  173. $reward_amount = $user_registration_reward_rules_content['reward_amount'];
  174. $after_revenue_amount = round($before_revenue_amount + $reward_amount, 2);
  175. $earnings_record_status = $issuance_review_type == 1 ? 2 : 4; //状态1=预计收益2=结算中3=审核中4=已转到余额
  176. //赠送新客注册奖励
  177. $insert_data = [
  178. 'company_id' => $company_id,
  179. 'custom_uid' => $uid,
  180. 'promoter_userid' => $shareUid,
  181. 'reward_amount' => $reward_amount,
  182. 'before_revenue_amount' => $before_revenue_amount,
  183. 'after_revenue_amount' => $after_revenue_amount,
  184. 'commission_rate' => '100', //固定奖励金额佣金比为100%
  185. 'regist_binding_id' => $regist_binding_id,
  186. 'revenue_type' => 2, //收益类型1=产品收益2=注册收益
  187. 'insert_time' => time(),
  188. 'status' => $earnings_record_status
  189. ];
  190. $EarningsRecordModel->insertGetId($insert_data);
  191. //无需审核时,直接发放奖励到余额
  192. if ($issuance_review_type == 2) {
  193. // 增加余额
  194. $promoter_info->total_commission_amount += $reward_amount;
  195. $promoter_info->save();
  196. }
  197. }
  198. DB::connection('company')->commit();
  199. //删除任务记录
  200. $this->Records->delete();
  201. // 成功处理...
  202. } catch (\Exception $e) {
  203. DB::rollBack();
  204. // 失败处理...
  205. if ($this->Records) {
  206. $this->Records->delete();
  207. }
  208. Log::info('job_error', '推广员拉新收益队列运行失败', ['data'=>$this->message_data, 'error' => $e->getMessage()]);
  209. }
  210. }
  211. public function failed(\Throwable $exception)
  212. {
  213. Log::info('job_error', '推广员拉新收益队列彻底失败', ['data'=>$this->message_data, 'error' => $exception]);
  214. // 失败处理...
  215. if ($this->Records) {
  216. $this->Records->delete();
  217. }
  218. }
  219. }