datetime = $this->getDateTime(); $this->companies = app(CompanyRepository::class)->get()->keyBy('sn'); $this->packages = app(PackageRepository::class)->get()->keyBy('sn'); $orders = $this->getOrders(); $orderItems = $this->getOrderItems($orders); foreach ($orders as &$item) { $item['type'] = $this->types[$item['sn']] ?? 255; } $dataOrderCards = []; foreach ($orderItems as $key => $value) { $dataOrderCards[$value['type']][] = [ 'type' => $value['type'], 'sim' => $value['sim'], 'company_id' => $value['company_id'], 'order_id' => $value['order_id'], 'package_id' => $value['package_id'], 'counts' => $value['counts'], 'unit_price' => $value['unit_price'], ]; } $this->line('插入订单数据,条数:'.count($orders)); foreach (array_chunk($orders, $this->chunks) as $data) { $this->getOutput()->write('.'); AddedOrder::upsert($orders, 'id'); } app(AddedOrderRepository::class)->forgetCached(); $this->line('插入订单关联数据,条数:'.count(array_collapse($dataOrderCards))); $tables = [ '', 'real_added_order_renewal_cards', 'real_added_order_renewal_package_cards', 'real_added_order_flows_package_cards', 'real_added_order_optional_package_cards', 'real_added_order_additional_package_cards', ]; foreach ($dataOrderCards as $type => $orderCards) { foreach (array_chunk($orderCards, $this->chunks) as $data) { $this->getOutput()->write('.'); DB::table($tables[$type])->upsert($data, ['sim', 'order_id']); } } unset($dataOrderCards); $this->line('插入订单关联数据成功'); } // 查询订单 protected function getOrders() { $this->line('查询订单记录'); $starttime = $this->datetime->copy()->startOfMonth()->startOfDay(); $endtime = $this->datetime->copy()->endOfMonth()->endOfDay(); $select = [ 'r_id as id', 'sn as sn', 'custom_no as company_id', 'transaction_no', 'sim_count as counts', 'amount_money as total_price', 'paytype as pay_channel', 'create_time as order_at', 'update_time as updated_at', ]; $orders = DB::connection('real')->table('jxc_custom_order')->select($select)->where('status', 3) ->whereIn('custom_no', $this->companies->keys()) ->where('create_time', '>=', $starttime->timestamp) ->where('create_time', '<=', $endtime->timestamp) ->orderBy('create_time')->get()->collect()->toArray(); foreach ($orders as &$item) { $item['company_id'] = $this->companies[$item['company_id']]['id'] ?? 0; $item['total_price'] = intval($item['total_price'] * 100) ; $item['counts'] = !empty($item['counts']) ? $item['counts'] : 1; $item['pay_channel'] = CommonService::transformerPayChannel($item['pay_channel']); $item['unit_price'] = intval($item['total_price']/$item['counts']); $item['order_at'] = date('Y-m-d H:i:s', $item['order_at']); $item['created_at'] = $item['order_at']; $item['updated_at'] = ($item['updated_at'] == '0000-00-00 00:00:00') ? $item['order_at'] : $item['updated_at']; } return $orders; } // 订单详情 protected function getOrderItems($orders) { $orders = array_keyBy($orders, 'sn'); $this->line('查询订单详情'); $select = [ 'sn as order_sn', 'sim', 'goods_no as package_id', 'goods_type as type', 'unit_price as unit_price', 'quantity as counts', ]; $orderItems = DB::connection('real')->table('jxc_custom_order_item')->select($select) ->whereIn('sn', array_keys($orders))->where('goods_type', '<', 6)->get()->collect()->toArray(); foreach ($orderItems as &$item) { $item['order_id'] = $orders[$item['order_sn']]['id'] ?? 0; $item['company_id'] = $orders[$item['order_sn']]['company_id'] ?? 0; $item['package_id'] = $this->packages[$item['package_id']]['id'] ?? 0; $item['unit_price'] = intval($item['unit_price'] * 100) ; } $this->types = array_pluck($orderItems, 'type', 'order_sn'); return $orderItems; } }