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 // // 定时任务执行完成 $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) ->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; } }