OrdersJobs.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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\Promoter\Users as UsersModel;
  14. use App\Models\Manager\OrdersProduct;
  15. use App\Models\Manager\Promoter\Customers as CustomersModel;
  16. use App\Models\Manager\Orders as OrdersModel;
  17. use App\Models\Api\Promoter\Configuration as ConfigurationModel;
  18. use App\Models\Manager\Promoter\Product as ProductModel;
  19. use App\Models\Manager\Promoter\CommonProduct as CommonProductModel;
  20. use App\Models\Manager\Promoter\EarningsRecord as EarningsRecordModel;
  21. use App\Models\Manager\CustomExternal as CustomExternalModel;
  22. use App\Models\Manager\Promoter\AuthCorp as AuthCorpModel;
  23. use App\Models\Manager\Promoter\ExternalFollow as ExternalFollowModel;
  24. use App\Models\Manager\Promoter\OpenworkGroupMember as OpenworkGroupMemberModel;
  25. use App\Jobs\Api\Promoter\LevelTwoOrdersJobs;
  26. /**
  27. * 推广员一级订单收益
  28. * @author 唐远望
  29. * @version 2.0
  30. * @date 2025-10-10
  31. */
  32. class OrdersJobs implements ShouldQueue
  33. {
  34. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  35. protected $message_data;
  36. protected $Records;
  37. /**
  38. * Create a new job instance.
  39. *
  40. * @return void
  41. */
  42. public function __construct(array $message_data)
  43. {
  44. $this->message_data = $message_data;
  45. }
  46. /**
  47. * 订单下定-推广员推广订单处理
  48. * @author 唐远望
  49. * @version 1.0
  50. * @date 2025-10-10
  51. * @return void
  52. */
  53. public function handle()
  54. {
  55. try {
  56. $company_id = $this->message_data['company_id'];
  57. (new DbService())->getConnectionNameByCompanyId($company_id);
  58. DB::connection('company')->beginTransaction();
  59. // 创建任务记录
  60. $this->Records = Records::create([
  61. 'job_id' => 'SASS_' . $company_id . '_OrdersJobs',
  62. 'name' => static::class,
  63. 'payload' => json_encode($this->message_data),
  64. 'status' => 'processing',
  65. 'started_at' => now()
  66. ]);
  67. $uid = $this->message_data['uid'];
  68. $order_id = $this->message_data['order_id'];
  69. $share_uid = $this->message_data['share_uid'];
  70. $before_revenue_amount = 0;
  71. $UsersModel = new UsersModel();
  72. $OrdersProductModel = new OrdersProduct();
  73. $CustomersModel = new CustomersModel();
  74. $orderModel = new OrdersModel();
  75. // $ProductModel = new ProductModel();
  76. $CommonProductModel = new CommonProductModel();
  77. $EarningsRecordModel = new EarningsRecordModel();
  78. $CustomExternal = new CustomExternalModel();
  79. $AuthCorpModel = new AuthCorpModel();
  80. $ExternalFollowModel = new ExternalFollowModel();
  81. $OpenworkGroupMemberModel = new OpenworkGroupMemberModel();
  82. if ($share_uid == '') {
  83. //如果没有推广员分享用户ID时
  84. $promoter_customers_data = $CustomersModel->where('custom_uid', $uid)->first();
  85. if (!$promoter_customers_data) {
  86. $this->Records->delete();
  87. return true; //不存在推广员,不赠送奖励
  88. }
  89. $share_uid = $promoter_customers_data->promoter_userid;
  90. //查询推广员状态
  91. $promoter_info = $UsersModel->where('custom_uid', $share_uid)->first();
  92. if (!$promoter_info || $promoter_info['status'] == 0) {
  93. $this->Records->delete();
  94. return true; //不存在推广员或不是推广员状态,不赠送奖励
  95. }
  96. } else {
  97. //查询推广员状态
  98. $promoter_info = $UsersModel->where('custom_uid', $share_uid)->first();
  99. if (!$promoter_info || $promoter_info['status'] == 0) {
  100. $this->Records->delete();
  101. return true; //不存在推广员或不是推广员状态,不赠送奖励
  102. }
  103. }
  104. $before_revenue_amount = $promoter_info->total_commission_amount;
  105. //查询推广员配置信息
  106. $ConfigurationModel = new ConfigurationModel();
  107. //获取应用配置开关状态
  108. $application_switch_status_data = $ConfigurationModel->where(['code_name' => 'application_switch_status', 'company_id' => $company_id])->first();
  109. if (!$application_switch_status_data) {
  110. $this->Records->delete();
  111. return true; //推广员开关信息未配置
  112. }
  113. if ($application_switch_status_data->content == 0) {
  114. $this->Records->delete();
  115. return true; //推广员功能未开启
  116. }
  117. //查询客户关系配置
  118. $customer_relationship_rules_data = $ConfigurationModel->where(['code_name' => 'customer_relationship_rules', 'company_id' => $company_id])->first();
  119. if (!$customer_relationship_rules_data) {
  120. $this->Records->delete();
  121. return true; //客户关系配置信息未配置
  122. }
  123. //国内用户定义配置
  124. $application_domestic_user_data = $ConfigurationModel->where(['code_name' => 'application_domestic_user', 'company_id' => $company_id])->first();
  125. if (!$application_domestic_user_data) {
  126. $this->Records->delete();
  127. return true; //国内用户定义配置
  128. }
  129. //执行企微客户查询更新操作
  130. $CustomExternal->customToExternal($company_id, $uid);
  131. //查询绑定SCRM关系
  132. $corpId = $AuthCorpModel->where('company_id', $company_id)->value('corpid');
  133. $application_domestic_user_content = $application_domestic_user_data->content;
  134. $add_wecom_customer_service_status = $application_domestic_user_content['effective_customer_definition']['add_wecom_customer_service_status'];//添加企微客服 状态0=关闭 1=开启
  135. $customer_churn_status = $application_domestic_user_content['effective_customer_definition']['customer_churn_status'];//企微客户状态为正常未流失状态 0=关闭 1=开启
  136. $exit_wechat_group_status = $application_domestic_user_content['effective_customer_definition']['exit_wechat_group_status'];//客户已入企微群且未退群 状态0=关闭 1=开启
  137. if ($add_wecom_customer_service_status == 1) {
  138. if (!empty($corpId)) {
  139. $externalUserId = $CustomExternal->where('custom_uid', $uid)->where('corpid', $corpId)->value('external_userid'); //Scrm平台外部联系人id
  140. if (!$externalUserId) {
  141. $this->Records->delete();
  142. return true; //客户未绑定Scrm平台
  143. }
  144. }
  145. }
  146. if ($customer_churn_status == 1) {
  147. if (!empty($corpId)) {
  148. $externalUserId = $CustomExternal->where('custom_uid', $uid)->where('corpid', $corpId)->value('external_userid'); //Scrm平台外部联系人id
  149. if ($externalUserId) {
  150. //查询客户状态
  151. $external_follow_info = $ExternalFollowModel->where(['external_userid' => $externalUserId, 'corpid' => $corpId, 'status' => 1])->first(); //状态1=流失 0=正常
  152. if ($external_follow_info) {
  153. $this->Records->delete();
  154. return true; //客户已流失
  155. }
  156. }
  157. }
  158. }
  159. if ($exit_wechat_group_status == 1) {
  160. if (!empty($corpId)) {
  161. $externalUserId = $CustomExternal->where('custom_uid', $uid)->where('corpid', $corpId)->value('external_userid'); //Scrm平台外部联系人id
  162. if ($externalUserId) {
  163. //查询客户状态
  164. $external_group_chat_info = $OpenworkGroupMemberModel->where(['external_userid' => $externalUserId, 'corpid' => $corpId, 'status' => 1])->first(); //状态,0在群,1离群
  165. if ($external_group_chat_info) {
  166. $this->Records->delete();
  167. return true; //客户已入企微群,且已退群
  168. }
  169. }
  170. }
  171. }
  172. $promoter_userid = 0;
  173. $content_config = $customer_relationship_rules_data->content;
  174. if ($content_config['customer_binding_model_type'] == 1) { //客户关系绑定模型 1=简单模式 2=平衡模式 3=竞争模式 4=自定义
  175. //查询用户绑定的
  176. $promoter_customers_data = $CustomersModel->where('custom_uid', $uid)->first();
  177. if ($promoter_customers_data) {
  178. $promoter_userid = $promoter_customers_data->promoter_userid;
  179. } else {
  180. //新增用户绑定
  181. $CustomersModel->insert(['custom_uid' => $uid, 'promoter_userid' => $share_uid, 'insert_time' => time(), 'binding_scene' => '推广下单', 'status' => '1','company_id'=>$company_id]);
  182. $promoter_userid = $share_uid;
  183. }
  184. } else if ($content_config['customer_binding_model_type'] == 2) { //客户关系绑定模型 1=简单模式 2=平衡模式 3=竞争模式 4=自定义
  185. //查询用户绑定的
  186. $promoter_customers_data = $CustomersModel->where('custom_uid', $uid)->first();
  187. if ($promoter_customers_data) {
  188. $end_time = $promoter_customers_data->insert_time + 86400 * 15; //15天之内奖励为原先推广员
  189. if ($end_time >= time()) {
  190. $promoter_userid = $promoter_customers_data->promoter_userid;
  191. } else {
  192. $promoter_userid = $share_uid; //15天之后奖励为新推广员
  193. $promoter_customers_data->promoter_userid = $share_uid;
  194. $promoter_customers_data->save();
  195. }
  196. } else {
  197. //新增用户绑定
  198. $CustomersModel->insert(['custom_uid' => $uid, 'promoter_userid' => $share_uid, 'insert_time' => time(), 'binding_scene' => '推广下单', 'status' => '1','company_id'=>$company_id]);
  199. $promoter_userid = $share_uid;
  200. }
  201. } else if ($content_config['customer_binding_model_type'] == 3) { //客户关系绑定模型 1=简单模式 2=平衡模式 3=竞争模式 4=自定义
  202. //查询用户绑定的
  203. $promoter_customers_data = $CustomersModel->where('custom_uid', $uid)->first();
  204. if ($promoter_customers_data) {
  205. if ($promoter_customers_data->promoter_userid != $share_uid) {
  206. $promoter_userid = $share_uid; //15天之后奖励为新推广员
  207. $promoter_customers_data->promoter_userid = $share_uid;
  208. $promoter_customers_data->save();
  209. }
  210. } else {
  211. //新增用户绑定
  212. $CustomersModel->insert(['custom_uid' => $uid, 'promoter_userid' => $share_uid, 'insert_time' => time(), 'binding_scene' => '推广下单', 'status' => '1','company_id'=>$company_id]);
  213. $promoter_userid = $share_uid;
  214. }
  215. }
  216. //查询订单信息
  217. $order_info = $orderModel->where('custom_uid', $uid)->where('id', $order_id)->first();
  218. if (!$order_info) {
  219. $this->Records->delete();
  220. return true; //订单不存在
  221. }
  222. $product_data = $OrdersProductModel->where('order_id', $order_id)->get(['product_id', 'product_name', 'shop_id', 'terminal_type', 'pay_total', 'buy_num'])->toArray();
  223. if (empty($product_data)) {
  224. $this->Records->delete();
  225. return true; //订单商品不存在
  226. }
  227. $is_first_order = 0;
  228. $first_order_reward_rules_data = '';
  229. //查询用户是否下单记录
  230. $other_order_info = $orderModel->where('custom_uid', $uid)->whereNotIN('id', [$order_id])->first();
  231. if (!$other_order_info) {
  232. //校验下单时间是否在配置时间范围内
  233. $first_order_reward_rules = $ConfigurationModel->where(['code_name' => 'first_order_reward_rules', 'company_id' => $company_id])->first();
  234. if (!$first_order_reward_rules) {
  235. $is_first_order = 0; //首单不奖励,没有配置信息
  236. }
  237. $first_order_reward_rules_data = $first_order_reward_rules->content;
  238. if ($first_order_reward_rules_data['status'] != 1) {
  239. $is_first_order = 0; //首单不奖励,没有开启奖励
  240. }
  241. if ($first_order_reward_rules_data['reward_amount_expiration_date'] != 1) {
  242. $is_first_order = 0; //首单不奖励,没有开启奖励
  243. }
  244. //首单奖励截止时间
  245. $first_order_end_time = $order_info['insert_time'] + 86400 * $first_order_reward_rules_data['reward_amount_expiration_date'];
  246. if ($first_order_end_time < time()) {
  247. $is_first_order = 0; //首单不奖励,奖励时间已过
  248. }
  249. $is_first_order = 1; //首单
  250. }
  251. //获取佣金配置信息
  252. $promotion_product_config = $ConfigurationModel->where(['code_name' => 'promotion_product_config', 'company_id' => $company_id])->first();
  253. if (!$promotion_product_config) {
  254. $this->Records->delete();
  255. return true; //佣金配置信息未配置
  256. }
  257. $promotion_product_config_data = $promotion_product_config->content;
  258. $insert_data = [];
  259. //如果是首单,则查询首单配置信息,给予奖励
  260. if ($is_first_order == 1) {
  261. //首单奖励佣金(下单范围&奖励条件类型)
  262. $is_give_first_order_reward = 1; //是否给予首单奖励1=是 0=否
  263. //商品件数
  264. $buy_num = '0';
  265. $first_commission_rate = 0;
  266. $first_reward_amount = 0;
  267. foreach ($product_data as $first_key => $first_value) {
  268. $product_id = $first_value['product_id'];
  269. $reward_conditions_product_ids = $first_order_reward_rules_data['reward_conditions_product_ids']; //下单产品范围
  270. $reward_conditions_product_ids = $reward_conditions_product_ids ? explode(',', $reward_conditions_product_ids) : [];
  271. if (!empty($reward_conditions_product_ids) && !in_array($product_id, $reward_conditions_product_ids)) {
  272. $is_give_first_order_reward = 0; //不符合奖励条件
  273. }
  274. $buy_num += $first_value['buy_num'];
  275. }
  276. $reward_conditions_amount = array_key_exists('reward_conditions_amount',$first_order_reward_rules_data) ? $first_order_reward_rules_data['reward_conditions_amount']:'0'; //下单金额范围
  277. $reward_conditions_type =array_key_exists('reward_conditions_type',$first_order_reward_rules_data) ? $first_order_reward_rules_data['reward_conditions_type']:'0'; //奖励条件类型
  278. if ($reward_conditions_type == 1) { //首单奖励规则奖励条件类型 1=下单金额 2=下单商品数
  279. if ($order_info['pay_total'] < $reward_conditions_amount) {
  280. $is_give_first_order_reward = 0; //不符合奖励条件
  281. }
  282. }
  283. if ($reward_conditions_type == 2) { //首单奖励规则奖励条件类型 1=下单金额 2=下单商品数
  284. $reward_conditions_boxes_number = array_key_exists('reward_conditions_boxes_number',$first_order_reward_rules_data) ? $first_order_reward_rules_data['reward_conditions_boxes_number']:'0'; //下单商品数范围
  285. if ($buy_num < $reward_conditions_boxes_number) {
  286. $is_give_first_order_reward = 0; //不符合奖励条件
  287. }
  288. }
  289. $reward_amount_number = $first_order_reward_rules_data['reward_amount_number']; //首单额外奖励金额
  290. if ($is_give_first_order_reward == 1 && $reward_amount_number > 0) {
  291. $first_commission_rate = 100; //固定金额奖励,佣金比为100%
  292. $first_reward_amount = $reward_amount_number;
  293. }
  294. if ($first_commission_rate > 0.01 && $first_reward_amount > 0.01) {
  295. $after_revenue_amount = round($before_revenue_amount + $first_reward_amount, 2);
  296. $insert_data[] = [
  297. 'company_id' => $company_id,
  298. 'order_id' => $order_id,
  299. 'product_id' => '0',
  300. 'is_first_order' => $is_first_order,
  301. 'product_name' => '',
  302. 'shop_id' => $order_info->shop_id,
  303. 'pay_total' => $order_info->pay_total,
  304. 'custom_uid' => $uid,
  305. 'promoter_userid' => $promoter_userid,
  306. 'commission_rate' => $first_commission_rate,
  307. 'reward_amount' => $first_reward_amount,
  308. 'before_revenue_amount' => $before_revenue_amount,
  309. 'after_revenue_amount' => $after_revenue_amount,
  310. 'revenue_type' => 1, //收益类型1=产品收益2=注册收益
  311. 'insert_time' => time(),
  312. 'status' => 1, //状态1=预计收益2=结算中3=审核中4=已转到余额
  313. ];
  314. }
  315. //推广奖励佣金
  316. foreach ($product_data as $key => $value) {
  317. $product_id = $value['product_id'];
  318. $commission_rate = 0;
  319. $reward_amount = 0;
  320. //------旧规则,每个推广员商品配置独立的佣金规则------
  321. // $product_info = $ProductModel->where(['company_id' => $company_id, 'status' => 1, 'promoter_userid' => $promoter_userid, 'product_id' => $product_id])->first();
  322. // if (!$product_info) {
  323. // continue;
  324. // }
  325. // if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  326. // $commission_rate = $product_info->mini_commission_rate;
  327. // $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  328. // }
  329. // if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  330. // $commission_rate = $product_info->live_commission_rate;
  331. // $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  332. // }
  333. // if ($commission_rate < 0.01 || $reward_amount < 0.01) {
  334. // continue;
  335. // }
  336. // ------新规则,公告商品池佣金规则------
  337. if ($promotion_product_config_data['store_type'] == 1) { //推广店铺类型1=全部店铺2=指定店铺
  338. if ($promotion_product_config_data['product_type'] == 1) { //推广商品类型1=全部商品2=指定商品
  339. if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  340. $commission_rate = $promotion_product_config_data['mini_commission_rate'];
  341. $reward_amount = $commission_rate > 0 ? round($value['pay_total'] * $commission_rate / 100, 2) : 0;
  342. }
  343. if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  344. $commission_rate = $promotion_product_config_data['live_commission_rate'];
  345. $reward_amount = $commission_rate > 0 ? round($value['pay_total'] * $commission_rate / 100, 2) : 0;
  346. }
  347. } else {
  348. $product_info = $CommonProductModel->where(['company_id' => $company_id, 'status' => 1, 'product_id' => $product_id])->first();
  349. if (!$product_info) {
  350. continue;
  351. }
  352. if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  353. $commission_rate = $product_info->mini_commission_rate;
  354. $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  355. }
  356. if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  357. $commission_rate = $product_info->live_commission_rate;
  358. $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  359. }
  360. if ($commission_rate < 0.01 || $reward_amount < 0.01) {
  361. continue;
  362. }
  363. }
  364. }
  365. if ($promotion_product_config_data['store_type'] == 2) { //推广店铺类型1=全部店铺2=指定店铺
  366. //校验店铺是否在配置范围内
  367. $shopid_list = explode(',', $promotion_product_config_data['shopid_list']);
  368. if (!in_array($value['shop_id'], $shopid_list)) {
  369. continue;
  370. }
  371. if ($promotion_product_config_data['product_type'] == 1) { //推广商品类型1=全部商品2=指定商品
  372. if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  373. $commission_rate = $promotion_product_config_data['mini_commission_rate'];
  374. $reward_amount = $commission_rate > 0 ? round($value['pay_total'] * $commission_rate / 100, 2) : 0;
  375. }
  376. if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  377. $commission_rate = $promotion_product_config_data['live_commission_rate'];
  378. $reward_amount = $commission_rate > 0 ? round($value['pay_total'] * $commission_rate / 100, 2) : 0;
  379. }
  380. } else {
  381. $product_info = $CommonProductModel->where(['company_id' => $company_id, 'status' => 1, 'product_id' => $product_id])->first();
  382. if (!$product_info) {
  383. continue;
  384. }
  385. if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  386. $commission_rate = $product_info->mini_commission_rate;
  387. $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  388. }
  389. if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  390. $commission_rate = $product_info->live_commission_rate;
  391. $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  392. }
  393. if ($commission_rate < 0.01 || $reward_amount < 0.01) {
  394. continue;
  395. }
  396. }
  397. }
  398. if ($commission_rate < 0.01 || $reward_amount < 0.01) {
  399. continue;
  400. }
  401. $after_revenue_amount = round($before_revenue_amount + $reward_amount, 2);
  402. $insert_data[] = [
  403. 'company_id' => $company_id,
  404. 'order_id' => $order_id,
  405. 'product_id' => $product_id,
  406. 'is_first_order' => '0',
  407. 'product_name' => $value['product_name'],
  408. 'shop_id' => $value['shop_id'],
  409. 'pay_total' => $value['pay_total'],
  410. 'custom_uid' => $uid,
  411. 'promoter_userid' => $promoter_userid,
  412. 'commission_rate' => $commission_rate,
  413. 'reward_amount' => $reward_amount,
  414. 'before_revenue_amount' => $before_revenue_amount,
  415. 'after_revenue_amount' => $after_revenue_amount,
  416. 'revenue_type' => 1, //收益类型1=产品收益2=注册收益
  417. 'insert_time' => time(),
  418. 'status' => 1, //状态1=预计收益2=结算中3=审核中4=已转到余额
  419. ];
  420. }
  421. $EarningsRecordModel->insert($insert_data);
  422. //统计首单奖励金额
  423. $UsersModel->where('custom_uid', $promoter_userid)->update([
  424. 'first_order_reward_amount' => DB::raw('first_order_reward_amount+' . $reward_amount),
  425. 'product_commission_amount' => DB::raw('product_commission_amount+' . $reward_amount)
  426. ]);
  427. } else {
  428. foreach ($product_data as $key => $value) {
  429. $product_id = $value['product_id'];
  430. $commission_rate = 0;
  431. $reward_amount = 0;
  432. //------旧规则,每个推广员商品配置独立的佣金规则------
  433. // $product_info = $ProductModel->where(['company_id' => $company_id, 'status' => 1, 'promoter_userid' => $promoter_userid, 'product_id' => $product_id])->first();
  434. // if (!$product_info) {
  435. // continue;
  436. // }
  437. // if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  438. // $commission_rate = $product_info->mini_commission_rate;
  439. // $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  440. // }
  441. // if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  442. // $commission_rate = $product_info->live_commission_rate;
  443. // $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  444. // }
  445. // if ($commission_rate < 0.01 || $reward_amount < 0.01) {
  446. // continue;
  447. // }
  448. //------新规则,公告商品池佣金规则------
  449. if ($promotion_product_config_data['store_type'] == 1) { //推广店铺类型1=全部店铺2=指定店铺
  450. if ($promotion_product_config_data['product_type'] == 1) { //推广商品类型1=全部商品2=指定商品
  451. if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  452. $commission_rate = $promotion_product_config_data['mini_commission_rate'];
  453. $reward_amount = $commission_rate > 0 ? round($value['pay_total'] * $commission_rate / 100, 2) : 0;
  454. }
  455. if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  456. $commission_rate = $promotion_product_config_data['live_commission_rate'];
  457. $reward_amount = $commission_rate > 0 ? round($value['pay_total'] * $commission_rate / 100, 2) : 0;
  458. }
  459. } else {
  460. $product_info = $CommonProductModel->where(['company_id' => $company_id, 'status' => 1, 'product_id' => $product_id])->first();
  461. if (!$product_info) {
  462. continue;
  463. }
  464. if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  465. $commission_rate = $product_info->mini_commission_rate;
  466. $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  467. }
  468. if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  469. $commission_rate = $product_info->live_commission_rate;
  470. $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  471. }
  472. if ($commission_rate < 0.01 || $reward_amount < 0.01) {
  473. continue;
  474. }
  475. }
  476. }
  477. if ($promotion_product_config_data['store_type'] == 2) { //推广店铺类型1=全部店铺2=指定店铺
  478. //校验店铺是否在配置范围内
  479. $shopid_list = explode(',', $promotion_product_config_data['shopid_list']);
  480. if (!in_array($value['shop_id'], $shopid_list)) {
  481. continue;
  482. }
  483. if ($promotion_product_config_data['product_type'] == 1) { //推广商品类型1=全部商品2=指定商品
  484. if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  485. $commission_rate = $promotion_product_config_data['mini_commission_rate'];
  486. $reward_amount = $commission_rate > 0 ? round($value['pay_total'] * $commission_rate / 100, 2) : 0;
  487. }
  488. if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  489. $commission_rate = $promotion_product_config_data['live_commission_rate'];
  490. $reward_amount = $commission_rate > 0 ? round($value['pay_total'] * $commission_rate / 100, 2) : 0;
  491. }
  492. } else {
  493. $product_info = $CommonProductModel->where(['company_id' => $company_id, 'status' => 1, 'product_id' => $product_id])->first();
  494. if (!$product_info) {
  495. continue;
  496. }
  497. if ($value['terminal_type'] == 1) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  498. $commission_rate = $product_info->mini_commission_rate;
  499. $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  500. }
  501. if ($value['terminal_type'] == 3) { //终端类型1=小程序 2=H5 3=直播间 4=PC
  502. $commission_rate = $product_info->live_commission_rate;
  503. $reward_amount = round($value['pay_total'] * $commission_rate / 100, 2);
  504. }
  505. if ($commission_rate < 0.01 || $reward_amount < 0.01) {
  506. continue;
  507. }
  508. }
  509. }
  510. if ($commission_rate < 0.01 || $reward_amount < 0.01) {
  511. continue;
  512. }
  513. $after_revenue_amount = round($before_revenue_amount + $reward_amount, 2);
  514. $insert_data[] = [
  515. 'company_id' => $company_id,
  516. 'order_id' => $order_id,
  517. 'product_id' => $product_id,
  518. 'is_first_order' => $is_first_order,
  519. 'product_name' => $value['product_name'],
  520. 'shop_id' => $value['shop_id'],
  521. 'pay_total' => $value['pay_total'],
  522. 'custom_uid' => $uid,
  523. 'promoter_userid' => $promoter_userid,
  524. 'commission_rate' => $commission_rate,
  525. 'reward_amount' => $reward_amount,
  526. 'before_revenue_amount' => $before_revenue_amount,
  527. 'after_revenue_amount' => $after_revenue_amount,
  528. 'revenue_type' => 1, //收益类型1=产品收益2=注册收益
  529. 'commission_level' => '1', //推广等级1=一级推广员2=二级推广员3=三级推广员
  530. 'insert_time' => time(),
  531. 'status' => 1, //状态1=预计收益2=结算中3=审核中4=已转到余额
  532. ];
  533. }
  534. $EarningsRecordModel->insert($insert_data);
  535. //统计非首单奖励金额
  536. $UsersModel->where('custom_uid', $promoter_userid)->update([
  537. 'product_commission_amount' => DB::raw('product_commission_amount+' . $reward_amount)
  538. ]);
  539. }
  540. DB::connection('company')->commit();
  541. // 开启二级推广员订单处理队列
  542. // LevelTwoOrdersJobs::dispatch(['uid'=>$uid,'custom_uid'=>$promoter_userid,'company_id'=>$company_id,'order_id'=>$order_id]);
  543. //删除任务记录
  544. $this->Records->delete();
  545. // 成功处理...
  546. } catch (\Exception $e) {
  547. DB::rollBack();
  548. // 失败处理...
  549. if ($this->Records) {
  550. $this->Records->delete();
  551. }
  552. Log::info('job_error', '推广员订单收益队列运行失败', ['data'=>$this->message_data,'error' => $e->getMessage()]);
  553. }
  554. }
  555. public function failed(\Throwable $exception)
  556. {
  557. Log::info('job_error', '推广员订单收益队列彻底失败', ['data'=>$this->message_data, 'error' => $exception]);
  558. // 失败处理...
  559. if ($this->Records) {
  560. $this->Records->delete();
  561. }
  562. }
  563. }