diff --git a/app/Domains/Real/Commands/Sync/AddedOrderSync.php b/app/Domains/Real/Commands/Sync/AddedOrderSync.php index 97fe06cf..13a2b307 100644 --- a/app/Domains/Real/Commands/Sync/AddedOrderSync.php +++ b/app/Domains/Real/Commands/Sync/AddedOrderSync.php @@ -31,6 +31,7 @@ class AddedOrderSync extends Command 'real_order_flows_package_cards', 'real_order_optional_package_cards', 'real_order_additional_package_cards', + 'real_order_group_package_cards', ]; public function handle() @@ -88,25 +89,31 @@ class AddedOrderSync extends Command $virtualTable = str_replace('real_', 'virtual_', $table); - if (!in_array($virtualTable, ['virtual_order_optional_package_cards', 'virtual_order_additional_package_cards'])) { - $starttime = $this->datetime->copy()->startOfMonth()->startOfDay(); - $endtime = $this->datetime->copy()->endOfMonth()->endOfDay(); + if (in_array($virtualTable, ['virtual_order_optional_package_cards', 'virtual_order_additional_package_cards'])) { + $virtualTable = 'virtual_order_flows_package_cards'; + } - $orders = DB::table($virtualTable)->selectRaw('sim, order_id') - ->where('created_at', '>=', $starttime->format('Y-m-d H:i:s')) - ->where('created_at', '<=', $endtime->format('Y-m-d H:i:s')) - ->whereIn('sim', array_pluck($data, 'sim')) - ->whereNull('deleted_at') - ->orderBy('created_at') - ->get() - ->collect() - ->groupBy('sim')->toArray(); + if (in_array($virtualTable, ['virtual_order_group_package_cards'])) { + $virtualTable = 'virtual_order_renewal_cards'; + } - foreach ($data as &$value) { - $i = array_count_values($simArray)[$value['sim']] ?? 0; - array_push($simArray, $value['sim']); - $value['virtual_order_id'] = $orders[$value['sim']][$i]['order_id'] ?? 0; - } + $starttime = $this->datetime->copy()->startOfMonth()->startOfDay(); + $endtime = $this->datetime->copy()->endOfMonth()->endOfDay(); + + $orders = DB::table($virtualTable)->selectRaw('sim, order_id') + ->where('created_at', '>=', $starttime->format('Y-m-d H:i:s')) + ->where('created_at', '<=', $endtime->format('Y-m-d H:i:s')) + ->whereIn('sim', array_pluck($data, 'sim')) + ->whereNull('deleted_at') + ->orderBy('created_at') + ->get() + ->collect() + ->groupBy('sim')->toArray(); + + foreach ($data as &$value) { + $i = array_count_values($simArray)[$value['sim']] ?? 0; + array_push($simArray, $value['sim']); + $value['virtual_order_id'] = $orders[$value['sim']][$i]['order_id'] ?? 0; } MongoCardJob::dispatch(array_pluck($data, 'sim')); @@ -184,7 +191,7 @@ class AddedOrderSync extends Command ]; $cards = DB::connection('real')->table('jxc_custom_order_item')->select($select) - ->whereIn('sn', array_keys($orders))->where('goods_type', '<', 6)->get()->collect()->toArray(); + ->whereIn('sn', array_keys($orders))->where('goods_type', '<=', 6)->get()->collect()->toArray(); $array = []; diff --git a/app/Domains/Real/Commands/Sync/PackageSync.php b/app/Domains/Real/Commands/Sync/PackageSync.php index d8c1af53..1fc10739 100644 --- a/app/Domains/Real/Commands/Sync/PackageSync.php +++ b/app/Domains/Real/Commands/Sync/PackageSync.php @@ -39,7 +39,7 @@ class PackageSync extends Command $flowPackages = $this->getFlowPackages(); $optionalPackages = $this->getOptionalPackages(); $additionalPackages = $this->getAdditionalPackages(); - $groupPackages = $this->getGroupPackages(); + $groupPackages = $this->getGroupPackages($basePackages); $packages = array_merge($renewalPackages, $flowPackages, $optionalPackages, $additionalPackages, $groupPackages); @@ -206,28 +206,45 @@ class PackageSync extends Command } // 组合包 - protected function getGroupPackages() + protected function getGroupPackages($basePackages) { + $basePackages = array_keyBy($basePackages, 'sn'); + $select = [ DB::raw('6 as type'), - 'jxc_package_addoptional.bag_number as sn', - 'jxc_package_addoptional.name as name', - DB::raw('"qw" as carrier_operator'), - DB::raw('0 as cost_price'), - 'jxc_package_addoptional.guide_price as guide_price', - DB::raw('0 as flows'), - 'jxc_package_addoptional.content_count as messages', - 'jxc_package_addoptional.reset_cycle as reset_months', - 'jxc_package_addoptional.service_cycle as service_months', - 'jxc_package_addoptional.create_time as created_at', - 'jxc_package_addoptional.del', + 'jxc_package_group.sn as sn', + 'jxc_package_group.name as name', + 'jxc_package_group_item.package_sn as parent_sn', + 'jxc_package_group.price as cost_price', + 'jxc_package_group.price as guide_price', + 'jxc_package_group.createtime as created_at', + 'jxc_package_group.del', ]; - $packages = DB::connection('real')->table('jxc_package_addoptional')->join('jxc_package_value_add_relation', function ($join) { - $join->on('jxc_package_value_add_relation.value_add_number', '=', 'jxc_package_addoptional.bag_number') - ->whereIn('jxc_package_value_add_relation.custom_no', $this->companies) - ->where('jxc_package_value_add_relation.type', 4); - })->select($select)->where('jxc_package_addoptional.tid', 1)->get(); + $packages = DB::connection('real')->table('jxc_package_group')->join('jxc_package_group_item', function ($join) { + $join->on('jxc_package_group_item.sn', '=', 'jxc_package_group.sn') + ->where('jxc_package_group_item.type', 1); + })->select($select)->get(); + + $packages->map(function ($item, $index) use ($basePackages, &$packages) { + $basePackage = $basePackages[$item->parent_sn]; + + if (!$basePackage) { + unset($packages[$index]); + return; + throw new InvalidArgumentException('组合包未找到对应基础套餐 #:'.$item->sn); + } + + $item->parent_id = $basePackage['id']; + $item->carrier_operator = array_flip(self::$carrier_operator)[$basePackage['carrier_operator']] ?? 'qw'; + $item->flows = $basePackage['flows']; + $item->voices = $basePackage['voices']; + $item->messages = $basePackage['messages']; + $item->has_messages = $basePackage['has_messages']; + $item->has_lbs = $basePackage['has_lbs']; + $item->reset_months = $basePackage['reset_months']; + $item->service_months = $basePackage['service_months']; + }); return $this->transform($packages); } diff --git a/app/Domains/Real/Http/Controllers/OrderController.php b/app/Domains/Real/Http/Controllers/OrderController.php index 6dae1cb1..705d2c3c 100644 --- a/app/Domains/Real/Http/Controllers/OrderController.php +++ b/app/Domains/Real/Http/Controllers/OrderController.php @@ -39,6 +39,7 @@ class OrderController extends Controller $orders->transform(function ($item) { return $item->only([ 'id', + 'type', 'sn', 'business_type', 'business_type_name', diff --git a/app/Domains/Virtual/Services/OrderService.php b/app/Domains/Virtual/Services/OrderService.php index 198b86a8..1f1d83f3 100644 --- a/app/Domains/Virtual/Services/OrderService.php +++ b/app/Domains/Virtual/Services/OrderService.php @@ -2,6 +2,7 @@ namespace App\Domains\Virtual\Services; use App\Dicts; +use Carbon\Carbon; use App\Core\Service; use App\Models\Card\Card; use App\Models\Virtual\Order; @@ -183,45 +184,6 @@ class OrderService extends Service } try { - // 改企业的卡新增一批虚拟卡,并替换原有订单里的卡 - if (isset($attributes['sign']) && $attributes['sign'] == 2) { - $simArray = implode(',', array_pluck($attributes['selected'], 'sim')); - try { - DB::statement("select change_cards('{{$simArray}}'::INT8[]);"); - } catch (\Throwable $th) { - throw $th; - } - } - - // 转销售和改企业的都立即激活卡 - if (isset($attributes['sign']) && in_array($attributes['sign'], [1, 2])) { - $array = array_map(function ($item) use ($attributes) { - return ['sim' => $item['sim'], 'virtual_activated_at' => $attributes['order_at']]; - }, $attributes['selected']); - - Card::upsert($array, 'sim', ['virtual_activated_at']); - - $attributes['type'] = 0; - $originType = $attributes['type']; - - // 销售订单,如果单卡使用多次,仅第一次为销售,其他改续费。 - $extras = []; - $selected = $attributes['selected']; - $attributes['counts'] = count($selected); - - foreach ($selected as &$card) { - if ($card['counts'] > 1) { - array_push($extras, [ - 'sim' => $card['sim'], - 'counts' => $card['counts'] - 1, - ]); - $card['counts'] = 1; - } - } - - $attributes['selected'] = $selected; - } - if ($attributes['id']) { if (!$node = $this->orderRepository->find($attributes['id'])) { throw new NotExistException('订单不存在或已删除'); @@ -265,36 +227,148 @@ class OrderService extends Service ]); } } else { - $maxId = Order::withTrashed()->max('id'); - $attributes['id'] = ++$maxId; - if ($product->company_id != $attributes['company_id']) { throw new NotAllowedException('非法操作'); } + $maxId = Order::withTrashed()->max('id'); - if (isset($attributes['selected']) && count($attributes['selected']) === $attributes['counts']) { - $attributes['order_status'] = 5; - $attributes['transaction_status'] = 1; + if ($attributes['type'] !== 0) { + $simArray = array_unique(array_pluck($attributes['selected'], 'sim')); + $simCompany = $this->orderCardPartitionRepository->select(['sim', 'company_id']) + ->where('type', 0)->whereIn('sim', $simArray) + ->get()->pluck('company_id', 'sim')->toArray(); + + $news = []; + $extras = []; + + foreach ($simArray as $value) { + if (!in_array($value, array_keys($simCompany))) { + array_push($news, $value); + continue; + } + + if ($simCompany[$value] !== $attributes['company_id']) { + array_push($extras, $value); + continue; + } + } + + if (count($news)) { + // 按订单时间激活 + $array = array_map(function ($item) use ($newOrderData) { + return ['sim' => $item, 'virtual_activated_at' => $newOrderData['order_at']]; + }, $news); + + Card::upsert($array, 'sim', ['virtual_activated_at']); + + // 创建订单 + $newOrderData = array_except($attributes, ['selected', 'sign']); + $newOrderData['id'] = ++$maxId; + $newOrderData['sn'] = $this->generateSn(); + $newOrderData['type'] = 0; + $newOrderData['order_status'] = 5; + $newOrderData['transaction_status'] = 1; + $newOrderData['counts'] = count($news); + $newOrderData['total_price'] = $attributes['unit_price'] * count($news); + $newOrderData['custom_price'] = $attributes['unit_price'] * count($news); + $newOrderData['order_at'] = $attributes['order_at'] ?? date('Y-m-d H:i:s'); + $newOrderData['package_id'] = $attributes['package_id'] ?? $product->package_id; + + $newOrder = $this->orderRepository->create($newOrderData); + + // 创建订单卡关联 + $this->upsertOrderCards(array_map(function ($item) { + return ['sim' => $item, 'counts' => 1]; + }, $news), $newOrder); + + // 剩下需要续费的卡 + $selected = $attributes['selected']; + $selectedNews = []; + + foreach ($selected as $key => $item) { + if (isset($selectedNews[$item['sim']])) { + $selectedNews[$item['sim']]['counts'] += $item['counts']; + continue; + } + + if (in_array($item['sim'], $news)) { + $counts = $item['counts'] - 1; + $selectedNews[$item['sim']] = [ + 'sim' => $item['sim'], + 'counts' => $counts + ]; + continue; + } + + $selectedNews[$item['sim']] = [ + 'sim' => $item['sim'], + 'counts' => $item['counts'], + ]; + } + + $selectedNews = array_filter($selectedNews, function ($item) { + return $item['counts'] > 0; + }); + + $attributes['counts'] = array_sum(array_pluck($selectedNews, 'counts')); + $attributes['selected'] = array_values($selectedNews); + } + + if (count($extras)) { + $attributes['package_id'] = $attributes['package_id'] ?? $product->package_id; + + $having = "MAX(service_end_at) < '%s'"; + $having = sprintf($having, Carbon::now()->subMonths(6)->endOfMonth()); + + $replacement = $this->orderCardPartitionRepository->select(['sim']) + ->where('company_id', $attributes['company_id']) + ->where('package_id', $attributes['package_id']) + ->whereIn('type', [0, 1]) + ->groupBy('sim') + ->havingRaw($having) + ->get()->pluck('sim')->toArray(); + + if (count($replacement) !== count($extras)) { + throw new NotAllowedException('可替换的卡量不足,不能进行转销售操作'); + } + + $updates = []; + foreach ($extras as $key => $value) { + $updates[] = [ + 'sim' => $value, + 'original_sim' => $replacement[$key], + ]; + } + + $this->orderCardPartitionRepository->updateBatch($updates, 'sim'); + } } - $attributes['total_price'] = $attributes['unit_price'] * $attributes['counts']; - $attributes['custom_price'] = $attributes['unit_price'] * $attributes['counts']; - $attributes['order_at'] = $attributes['order_at'] ?? date('Y-m-d H:i:s'); - $attributes['package_id'] = $attributes['package_id'] ?? $product->package_id; + if ($attributes['counts'] !== 0) { + $attributes['id'] = ++$maxId; - $node = $this->orderRepository->create($attributes); + if (isset($attributes['selected']) && count($attributes['selected']) === $attributes['counts']) { + $attributes['order_status'] = 5; + $attributes['transaction_status'] = 1; + } + + $attributes['total_price'] = $attributes['unit_price'] * $attributes['counts']; + $attributes['custom_price'] = $attributes['unit_price'] * $attributes['counts']; + $attributes['order_at'] = $attributes['order_at'] ?? date('Y-m-d H:i:s'); + $attributes['package_id'] = $attributes['package_id'] ?? $product->package_id; + + $node = $this->orderRepository->create($attributes); + } } - if ($attributes['selected']) { + if (isset($attributes['selected']) && is_array($attributes['selected']) && count($attributes['selected'])) { if ($attributes['type'] === 0) { $exists = $this->orderCardPartitionRepository->withConditions([ 'type' => 0, 'sim' => array_pluck($attributes['selected'], 'sim') ])->count(); - - if ($exists) { DB::rollBack(); $simArray = implode(',', array_pluck($attributes['selected'], 'sim')); @@ -304,24 +378,11 @@ class OrderService extends Service try { $this->upsertOrderCards($attributes['selected'], $node); - $this->orderRepository->forgetCached(); - $this->orderCardPartitionRepository->forgetCached(); - app(RealOrderCardPartitionRepository::class)->forgetCached(); } catch (\Exception $e) { DB::rollBack(); throw $e; } } - - if (!empty($extras)) { - $extraOrderData = $attributes; - $extraOrderData['type'] = $originType; - $extraOrderData['counts'] = array_sum(array_pluck($extras, 'counts')); - $extraOrderData['total_price'] = $attributes['unit_price'] * $extraOrderData['counts']; - $extraOrderData['custom_price'] = $attributes['unit_price'] * $extraOrderData['counts']; - $extraOrder = $this->orderRepository->create($attributes); - $this->upsertOrderCards($extras, $extraOrder); - } } catch (\Exception $e) { DB::rollBack(); throw $e; @@ -329,6 +390,10 @@ class OrderService extends Service DB::commit(); + $this->orderRepository->forgetCached(); + $this->orderCardPartitionRepository->forgetCached(); + app(RealOrderCardPartitionRepository::class)->forgetCached(); + return $node; } @@ -585,7 +650,7 @@ class OrderService extends Service if ($node->type === 0) { $sql = 'SELECT COUNT(*) as counts FROM virtual_order_cards_partition WHERE type != 0 AND sim In ( SELECT sim FROM virtual_order_cards_partition WHERE order_id = ? - )'; + ) AND deleted_at IS NULL'; $counts = DB::select($sql, [$id])[0]->counts; diff --git a/database/migrations/2018_12_24_164434_create_real_order_cards_table.php b/database/migrations/2018_12_24_164434_create_real_order_cards_table.php index d7bc65dd..4b10b05b 100644 --- a/database/migrations/2018_12_24_164434_create_real_order_cards_table.php +++ b/database/migrations/2018_12_24_164434_create_real_order_cards_table.php @@ -40,7 +40,6 @@ class CreateRealOrderCardsTable extends Migration $table->addPartition('real_order_flows_package_cards', 'list', [3]); $table->addPartition('real_order_optional_package_cards', 'list', [4]); $table->addPartition('real_order_additional_package_cards', 'list', [5]); - $table->addPartition('real_order_group_package_cards', 'list', [6]); }); Schema::table('real_order_cards', function (Blueprint $table) { @@ -63,14 +62,6 @@ class CreateRealOrderCardsTable extends Migration Schema::table('real_order_optional_package_cards', function (Blueprint $table) { $table->unique(['sim', 'order_id', 'deleted_at']); }); - - Schema::table('real_order_additional_package_cards', function (Blueprint $table) { - $table->unique(['sim', 'order_id', 'deleted_at']); - }); - - Schema::table('real_order_group_package_cards', function (Blueprint $table) { - $table->unique(['sim', 'order_id', 'deleted_at']); - }); } /** diff --git a/database/migrations/2019_04_17_161638_add_real_order_group_package_cards_partition.php b/database/migrations/2019_04_17_161638_add_real_order_group_package_cards_partition.php new file mode 100644 index 00000000..b1c48d89 --- /dev/null +++ b/database/migrations/2019_04_17_161638_add_real_order_group_package_cards_partition.php @@ -0,0 +1,34 @@ +addPartition('real_order_group_package_cards', 'list', [6]); + }); + + Schema::table('real_order_group_package_cards', function (Blueprint $table) { + $table->unique(['sim', 'order_id', 'deleted_at']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('real_order_group_package_cards'); + } +} diff --git a/frontend/src/components/table/table.vue b/frontend/src/components/table/table.vue index f3532248..2bba645b 100644 --- a/frontend/src/components/table/table.vue +++ b/frontend/src/components/table/table.vue @@ -721,7 +721,7 @@ export default { this.cloneColumns.forEach(col => (col._filterVisible = false)); }, handleBodyScroll(event) { - if (this.showHeader) + if (this.showHeader && this.$refs.header) this.$refs.header.scrollLeft = event.target.scrollLeft; if (this.isLeftFixed) this.$refs.fixedBody.scrollTop = event.target.scrollTop; diff --git a/frontend/src/views/virtual/orders/cards.vue b/frontend/src/views/virtual/orders/cards.vue index 8691090f..9eeb425a 100644 --- a/frontend/src/views/virtual/orders/cards.vue +++ b/frontend/src/views/virtual/orders/cards.vue @@ -67,6 +67,18 @@ +