setName('gzcAddByBill') ->setDescription('从bill表中查询未提交到公证处的养老金记录,并提交公证处'); } protected function execute(Input $input, Output $output) { $output->writeln('定时任务执行中...'); $this->billGzcAdd(); /** 修复 历史数据脚本 */ // $gzcLogList = Db::name("gzc_logs") // ->where('link_id', '=', 0) // ->select() // ->toArray(); // $orderSnList = array_column($gzcLogList, 'out_trade_no'); // $userBillList = Db::name('user_bill') // ->where('status', 1) // ->where('commission_type', 5) // ->whereIn('order_sn', $orderSnList) // ->select() // ->toArray(); // $userBillMap = []; // foreach ($userBillList as $key => $value) { // $userBillMap[$value['order_sn'] . '_' . $value['uid'] . '_' . $value['number']] = [ // 'bill_id' => $value['bill_id'], // 'type_shop' => $value['type_shop'] // ]; // } // unset($userBillList); // $str = ''; // foreach ($gzcLogList as $value) { // if (isset($userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']])) { // $update = [ // 'link_id' => $userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']]['bill_id'], // 'order_type' => $userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']]['type_shop'], // ]; // if (empty($value['platform_no'])) { // $update['platform_no'] = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999); // } // Db::name("gzc_logs") // ->where('id', '=', $value['id']) // ->update($update); // $str .= $value['id'] . '、'; // } // } // echo $str; // // 定时任务执行中... // // 181、274、275、277、279、280、281、282、283、284、285、286、287、288、289、290、291、292、293、294、295、296、297、298、299、300、301、302、303、304、305、306、307、308、309、310、311、312、313、314、315、316、317、318、319、320、321、322、323、324、325、326、327、328、329、330、331、332、333、334、335、336352、353、354、355、356、357、358、359、360、361、362、363、364、365、366、367、368、369、370、371、372、373、374、375、376、377、378、379、380、381、382、383、384、385、386、387、388、389、390、391、392、393、394、395、396、398、400、401、402、403、404、405、406、407、408、409、410、411、412、413、414、415431、432、433、434、435、436、437、438、439、440、441、442、443、444、445、446、447、448、449、450、451、452、453、454、455、456、457、458、459、460、461、462、463、464、465、466、467、468、469、470、471、472、473、474、475、476、477、478、479、480、481、482、483、484、485、486、487、488、489、490、491、492508、509、510、511、512、513、514、515、516、517、518、519、520、521、522、523、524、525、526、527、528、529、530、1591、1607、1608、1632、1633、1634、1649、1650、1651、1652、2108、2109、2110、2111、3687、3688、3689、3690、3691、3692、3693、3694、3695、3696、3697、3698、3699、3700、3701、3702、3703、3704、3705、3706、3707、3708、 // // 定时任务执行完成 /** 删除重复数据脚本 */ // $gzcLogList = Db::query('SELECT link_id FROM rrx_gzc_logs WHERE link_id != 0 GROUP BY link_id HAVING COUNT(*) > 1'); // $linkIdList = array_column($gzcLogList, 'link_id'); // $gzcLogList = Db::name("gzc_logs") // ->whereIn('link_id', $linkIdList) // ->field('id, gzc, success, link_id') // ->order('id ASC') // ->select() // ->toArray(); // $gzcLogMap = []; // foreach ($gzcLogList as $item) { // $gzcLogMap[$item['link_id']][] = $item; // } // // $delAllIdList = []; // foreach ($gzcLogMap as $gzcLogArr) { // $gzcIdList = []; // $allIds = []; // // 单次遍历同时收集所有ID和需要保留的ID // foreach ($gzcLogArr as $gzcLog) { // $allIds[] = $gzcLog['id']; // if ($gzcLog['gzc'] != 0) { // $gzcIdList[] = $gzcLog['id']; // } // } // // 确定需要删除的ID列表 // if (!empty($gzcIdList)) { // // 保留有gzc标志的记录,删除其他 // $delIdList = array_diff($allIds, $gzcIdList); // } else { // // 保留第一条记录,删除其他 // $delIdList = array_slice($allIds, 1); // } // $delAllIdList = array_merge($delAllIdList, $delIdList); // } // // // 如果后续需要索引连续 // $delAllIdList = array_values($delAllIdList); // $res = Db::name("gzc_logs") // ->whereIn('id', $delAllIdList) // ->delete(); // echo implode('、', $delAllIdList); // echo 'deleteNum' . $res; // // 定时任务执行中... // // 2327、2362、2375、2413、2414、2487、2536、2550、2564、320、2576、2578、2579、2580、2581、2588、2589、2599、2601、2602、2603、2604、2605、2606、2607、2608、2614、2638、2666、2680、2706、2707、2721、2729、2736、2739、2744、2748、2811、2859、2860、2861、2897、2928、2933、3264、3277、3279、3571、3572、3573、357、1590、3673、3675、1631、3678、3679、3680、3681、2106、3683、2107、3684、2112、3685、2113、3686 // // deleteNum78 // // 定时任务执行完成 /** 将之前多跑出来的佣金和 */ // $userBillLinkIdMap = Db::query('SELECT link_id FROM `rrx_user_bill` WHERE link_id != 0 GROUP BY link_id, type_shop, uid, number, commission_type HAVING COUNT(*) > 1'); // $linkIdList = array_column($userBillLinkIdMap, 'link_id'); // // $userBillList = Db::name("user_bill") // ->whereIn('link_id', $linkIdList) // ->field('bill_id, gzc, link_id, type_shop, uid, number, commission_type, order_sn, status') // ->order('bill_id ASC') // ->select() // ->toArray(); // // $userBillMap = []; // $userBillIdList = []; // foreach ($userBillList as $item) { // $userBillIdList[] = $item['bill_id']; // $userBillMap[$item['link_id'] . '_' . $item['type_shop'] . '_' . $item['uid'] . '_' . $item['commission_type'] . '_' . $item['number']][] = $item; // } // // $gzcLogList = Db::name("gzc_logs") // ->whereIn('link_id', $userBillIdList) // ->field('id, gzc, success, link_id') // ->order('id ASC') // ->select() // ->toArray(); // // 提前查询过了,按正常来说 就是一对一,上边数据修复过后,没有重复数据 // $gzcLogMap = array_column($gzcLogList, null, 'link_id'); // // $delAllIdList = []; // $delYongJin = []; // $yjNum = 0.00; // $yongjinKouchu = []; // $yongjinKouchuDelBillIdList = []; // foreach ($userBillMap as $userBillArr) { // $allIds = []; // $baoLiuIds = []; // $yjDataList = []; // // 单次遍历同时收集所有ID和需要保留的ID // foreach ($userBillArr as $userBill) { // $allIds[] = $userBill['bill_id']; // // 如果该条记录是养老金记录 则根据 养老金公证处记录表 内的条件 来决定是否删除该条记录 // if ($userBill['commission_type'] == 5) { // if (!empty($gzcLogMap[$userBill['bill_id']] ?? []) && $gzcLogMap[$userBill['bill_id']]['success'] == 1) { // // 如果该条 养老金记录 推送到了公证处明细记录表 // $baoLiuIds[] = $userBill['bill_id']; // } // } // // if ($userBill['commission_type'] == 3) { // if ($userBill['status'] == 1) { // $yjDataList[] = $userBill; // } // } // } // // 确定需要删除的ID列表 // if (!empty($billIdList)) { // // 保留有gzc标志的记录,删除其他 // $delIdList = array_diff($allIds, $billIdList); // } else if (!empty($yjDataList)) { // // 保留第一条 已生效 的佣金,其余的,需要从用户身上扣减 并删除 // $yjDataOne = array_shift($yjDataList); // if (!empty($yjDataList)) { // // 如果移除掉第一个 还有已经发放的 佣金 需要从用户身上扣除 // foreach ($yjDataList as $billData) { // $delYongJin[] = $billData['bill_id']; // $yjNum += $billData['number']; // if (!isset($yongjinKouchu[$billData['uid']])) { // $yongjinKouchu[$billData['uid']] = 0.00; // } // $yongjinKouchu[$billData['uid']] += $billData['number']; // $yongjinKouchuDelBillIdList[$billData['uid']][] = $billData['bill_id']; // } // } // $delIdList = array_values(array_diff($allIds, [$yjDataOne['bill_id']])); // } else { // // 保留第一条记录,删除其他 // $delIdList = array_slice($allIds, 1); // } // $delAllIdList = array_merge($delAllIdList, $delIdList); // } // // echo '应删除的Bill记录' . implode(',', $delAllIdList) . "\n"; // echo '应删除的佣金记录' . implode(',', $delYongJin) . "\n"; // echo '应删除的佣金金额' . $yjNum . "\n"; // // $successDecYongjin = 0.00; // $fallDelBillIdList = []; // if (!empty($yongjinKouchu)) { // foreach ($yongjinKouchu as $uid => $number) { // $user = Db::name('user')->where('uid', '=', $uid)->find(); // if ($user) { // if ($user['brokerage_price'] >= $number) { // $updateNum = Db::name('user')->where('uid', '=', $uid)->dec('brokerage_price', $number)->update(); // if ($updateNum) { // $successDecYongjin += $number; // echo "用户佣金扣除成功(uid:$uid ,账号:{$user['account']} ,用户佣金余额:{$user['brokerage_price']},应扣除佣金:$number )\n"; // continue; // } else { // echo "用户佣金余额不足(uid:$uid ,账号:{$user['account']} ,用户佣金余额:{$user['brokerage_price']},应扣除佣金:$number )\n"; // } // } else { // echo "用户佣金余额不足(uid:$uid ,账号:{$user['account']} ,用户佣金余额:{$user['brokerage_price']},应扣除佣金:$number )\n"; // } // } else { // echo "未查询到用户信息(uid:$uid ,应扣除佣金:$number )\n"; // } // $fallDelBillIdList = array_values(array_merge($fallDelBillIdList, $yongjinKouchuDelBillIdList[$uid])); // } // } // echo '因佣金扣减失败而不能删除的佣金记录:' . implode(',', $fallDelBillIdList) . "\n"; // echo '成功扣减佣金总额:' . $successDecYongjin . "\n"; // $delAllIdList = array_values(array_diff($delAllIdList, $fallDelBillIdList)); // echo '实际删除的Bill记录' . implode(',', $delAllIdList) . "\n"; // Db::name("user_bill") // ->whereIn('bill_id', $delAllIdList) // ->delete(); // Db::name("gzc_logs") // ->whereIn('link_id', $delAllIdList) // ->delete(); // $delYongJin = array_values(array_diff($delYongJin, $fallDelBillIdList)); // echo '实际应删除的佣金记录' . implode(',', $delYongJin) . "\n"; $output->writeln('定时任务执行完成'); } public function billGzcAdd() { $testUserIdList = Db::name('user') ->where('test_user', 1) ->column('uid'); // 1. 使用chunk分批处理 Db::name('user_bill') ->where('status', 1) ->where('gzc', 0) ->where('commission_type', 5) ->whereNotIn('uid', $testUserIdList) ->chunk(100, function ($bills) { $this->processBills($bills); }); } protected function processBills($bills) { // 2. 预加载用户实名认证信息 $userIds = array_unique(array_column($bills->toArray(), 'uid')); $certifications = Db::name('user_certification') ->whereIn('uid', $userIds) ->where('status', '=', 1) ->column('user_name,mobile,user_card', 'uid'); // 3. 订单类型映射配置 $orderMap = [ 0 => ['table' => 'rrx_store_order', 'condition' => [['paid', '=', 1]]], 1 => ['table' => 'rrx_order_pdd'], 2 => ['table' => 'rrx_order_vip'], 3 => ['table' => 'rrx_order_su'], 5 => ['table' => 'rrx_order_tb'], 6 => ['table' => 'rrx_order_jd'], 9 => ['table' => 'rrx_order_huafei'], 10 => [ 'table' => 'rrx_order_merchant', 'condition' => [['paid', '=', 1], ['status', '=', 1]], 'field_map' => ['order_sn' => 'out_trade_no'] ], ]; foreach ($bills as $bill) { $typeShop = $bill['type_shop'] ?? 0; // 4. 跳过无效类型 if (in_array($typeShop, [4, 7, 8])) continue; // 5. 使用映射配置查询订单 $online = $this->getOrderData($orderMap, $typeShop, $bill); if (!$online) { Log::info("用户{$bill['uid']}订单{$bill['order_sn']}不存在"); continue; } // 6. 检查实名认证 if (!isset($certifications[$bill['uid']])) { Log::info("用户{$bill['uid']}未实名认证"); continue; } // 7. 验证养老金金额 $pension = $bill['number'] ?? 0; if ($pension <= 0) { Log::info("订单{$online['order_sn']}养老金金额无效: {$pension}"); continue; } // 8. 事务处理 Db::transaction(function () use ($bill, $online, $certifications, $pension, $typeShop) { // 检查是否已存在记录 $exists = Db::name("gzc_logs") ->where("link_id", $bill['bill_id']) ->where("order_type", $typeShop) ->count(); if ($exists) return; // 创建唯一订单ID $orderId = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999); Db::name("gzc_logs")->insert([ 'uid' => $bill['uid'], 'platform_no' => $orderId, 'account_type' => 1, 'user_name' => $certifications[$bill['uid']]['user_name'], 'mobile' => $certifications[$bill['uid']]['mobile'], 'user_card' => $certifications[$bill['uid']]['user_card'], 'pension' => $pension, 'order_type' => $typeShop, 'out_trade_no' => $online['order_sn'], 'link_id' => $bill['bill_id'], 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s') ]); Db::name('user_bill')->where('bill_id', $bill['bill_id'])->update([ 'is_gzc' => 1, 'gzc' => 1 ]); }); } } protected function getOrderData($orderMap, $typeShop, $bill) { $config = $orderMap[$typeShop] ?? $orderMap[0]; $query = Db::table($config['table']) ->where($config['field_map']['order_sn'] ?? 'order_sn', $bill['order_sn']); if (!empty($config['condition'])) { $query->where($config['condition']); } $result = $query->find(); // 字段映射处理 if ($result && isset($config['field_map'])) { foreach ($config['field_map'] as $src => $dest) { $result[$src] = $result[$dest] ?? null; } } return $result; } }