setName('gzcAddByBill') ->setDescription('从bill表中查询未提交到公证处的养老金记录,并提交公证处'); } protected function execute(Input $input, Output $output) { $output->writeln('定时任务执行中...'); $this->billGzcAdd(); $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('is_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; } }