同步定价
This commit is contained in:
parent
a5baec4f2e
commit
19158bfe42
@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Virtual\Commands\Sync;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Real\Card;
|
||||
use MongoDB\BSON\UTCDateTime;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Domains\Real\Repositories\PackageRepository;
|
||||
|
||||
class ActivateSync extends Command
|
||||
{
|
||||
protected $name = 'virtual:sync-activate';
|
||||
|
||||
protected $description = '同步RD激活数据';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$datetime = $this->getDateTime();
|
||||
$starttime = new UTCDateTime($datetime->copy()->startOfDay()->startOfMonth()->timestamp * 1000);
|
||||
$endtime = new UTCDateTime($datetime->copy()->endOfDay()->endOfMonth()->timestamp * 1000);
|
||||
|
||||
$this->line('查询激活数据');
|
||||
|
||||
$res = DB::connection('mongo')->table('tblCard')->select(['cNo', 'saDate', 'sPCode'])
|
||||
->where('pNo', 'No00000000768')
|
||||
->where('isDel', '<>', 1)
|
||||
->whereBetween('saDate', [$starttime, $endtime])->get()->toArray();
|
||||
|
||||
$list = [];
|
||||
$total = 0;
|
||||
|
||||
$this->line('拼装更新数据:'.count($res));
|
||||
$chucks = array_chunk($res, 10000);
|
||||
foreach ($chucks as $chuck) {
|
||||
echo '.';
|
||||
$cards = Card::select(['sim', 'package_id'])->whereIn('sim', array_pluck($chuck, 'cNo'))->get()->keyBy('sim')->toArray();
|
||||
$total += count($cards);
|
||||
|
||||
foreach ($chuck as $card) {
|
||||
$value = $cards[$card['cNo']];
|
||||
|
||||
if (!$value) {
|
||||
echo '未找到卡' . $card['cNo'] . PHP_EOL;
|
||||
continue;
|
||||
}
|
||||
|
||||
$activate_at = $card['saDate']->toDateTime()->format('Y-m-d H:i:s');
|
||||
|
||||
$list[] = [
|
||||
'sim' => $value['sim'],
|
||||
'activate_at' => $activate_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$this->line('更新卡表数据,总计更新条数:' . count($list));
|
||||
|
||||
$except = count($list) - $total;
|
||||
|
||||
$this->line("其中有{$except}张卡不在表内");
|
||||
|
||||
foreach (array_chunk($list, 3000) as $data) {
|
||||
echo '.';
|
||||
Card::updateBatch($data, 'sim');
|
||||
}
|
||||
|
||||
$this->line('更新数据成功');
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Virtual\Commands\Sync;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Real\Card;
|
||||
use App\Models\Real\Order;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Domains\Real\Repositories\BlocRepository;
|
||||
use App\Domains\Real\Repositories\CardRepository;
|
||||
use App\Domains\Real\Repositories\OrderRepository;
|
||||
use App\Domains\Real\Repositories\CompanyRepository;
|
||||
use App\Domains\Real\Repositories\PackageRepository;
|
||||
|
||||
class OrderBaseSync extends Command
|
||||
{
|
||||
protected $name = 'virtual:sync-order';
|
||||
|
||||
protected $description = '同步VD基础订单数据';
|
||||
|
||||
protected $companies;
|
||||
protected $packages;
|
||||
protected $blocs;
|
||||
|
||||
protected static $carrierOperators = [1, 0, 2];
|
||||
|
||||
protected $chunks = 5000;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
}
|
||||
}
|
@ -1,178 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Virtual\Commands\Sync;
|
||||
|
||||
use App\Dicts;
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Real\Order;
|
||||
use MongoDB\BSON\UTCDateTime;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Domains\Real\Repositories\OrderRepository;
|
||||
|
||||
class OrderCustomSync extends Command
|
||||
{
|
||||
protected $name = 'virtual:sync-custom-order';
|
||||
|
||||
protected $description = '同步VD企业订单数据';
|
||||
|
||||
protected $chunks = 5000;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->datetime = $this->getDateTime();
|
||||
|
||||
$orders = $this->getOrders();
|
||||
$orders = array_keyBy($orders, 'order_id');
|
||||
$orderItems = $this->getOrderItems(array_keys($orders));
|
||||
|
||||
$dataOrders = [];
|
||||
$dataOrderCards = [];
|
||||
|
||||
foreach ($orderItems as $key => $value) {
|
||||
$order = $orders[$value['order_id']];
|
||||
|
||||
if (!$order) {
|
||||
throw new \Exception('未找到订单数据:' . $value['order_id']);
|
||||
}
|
||||
|
||||
$dataOrders[$value['order_id']] = [
|
||||
'id' => $value['order_id'],
|
||||
'type' => $value['type'],
|
||||
'company_id' => $order['company_id'],
|
||||
'transaction_no' => $order['transaction_no'],
|
||||
'pay_channel' => $order['pay_channel'],
|
||||
'unit_price' => $order['unit_price'],
|
||||
'counts' => $order['counts'],
|
||||
'total_price' => $order['total_price'],
|
||||
'order_at' => $order['order_at'],
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
'updated_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
$dataOrderCards[$value['type']][] = [
|
||||
'sim' => $value['sim'],
|
||||
'order_id' => $value['order_id'],
|
||||
'counts' => $value['item_counts'],
|
||||
'unit_price' => $value['item_unit_price'],
|
||||
];
|
||||
|
||||
$dataPackageCards[$value['type']][] = [
|
||||
'package_id' => $value['package_id'],
|
||||
'sim' => $value['sim'],
|
||||
];
|
||||
}
|
||||
|
||||
$this->line('插入订单数据,条数:'.count($dataOrders));
|
||||
foreach (array_chunk($dataOrders, $this->chunks) as $data) {
|
||||
echo '.';
|
||||
Order::replace($data);
|
||||
}
|
||||
app(OrderRepository::class)->forgetCached();
|
||||
|
||||
unset($dataOrders);
|
||||
|
||||
$this->line('插入订单关联数据,条数:'.count(array_collapse($dataOrderCards)));
|
||||
$tables = [
|
||||
'real_order_base_cards',
|
||||
'real_order_renewal_cards',
|
||||
'real_order_renewal_package_cards',
|
||||
'real_order_flows_package_cards',
|
||||
'real_order_optional_package_cards',
|
||||
'real_order_additional_package_cards',
|
||||
];
|
||||
foreach ($dataOrderCards as $type => $orderCards) {
|
||||
foreach (array_chunk($orderCards, $this->chunks) as $data) {
|
||||
echo '.';
|
||||
DB::table($tables[$type])->replace($data);
|
||||
}
|
||||
}
|
||||
unset($dataOrderCards);
|
||||
$this->line('插入订单关联数据成功');
|
||||
|
||||
$this->line('插入套餐关联数据,条数:'.count(array_collapse($dataPackageCards)));
|
||||
$tables = [
|
||||
'',
|
||||
'real_package_renewal_cards',
|
||||
'real_package_renewal_package_cards',
|
||||
'real_package_flows_package_cards',
|
||||
'real_package_optional_package_cards',
|
||||
'real_package_additional_package_cards',
|
||||
];
|
||||
foreach ($dataPackageCards as $type => $packageCards) {
|
||||
foreach (array_chunk($packageCards, $this->chunks) as $data) {
|
||||
echo '.';
|
||||
DB::table($tables[$type])->replace($data);
|
||||
}
|
||||
}
|
||||
unset($dataPackageCards);
|
||||
$this->line('插入套餐关联数据成功');
|
||||
}
|
||||
|
||||
// 查询订单
|
||||
protected function getOrders()
|
||||
{
|
||||
$this->line('查询订单记录');
|
||||
|
||||
$starttime = $this->datetime->copy()->startOfMonth()->startOfDay();
|
||||
$endtime = $this->datetime->copy()->endOfMonth()->endOfDay();
|
||||
|
||||
$select = [
|
||||
'sn as order_id',
|
||||
'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',
|
||||
];
|
||||
|
||||
$orders = DB::connection('real')->table('jxc_custom_order')->select($select)->where('status', 3)
|
||||
->where('create_time', '>=', $starttime->timestamp)
|
||||
->where('create_time', '<=', $endtime->timestamp)
|
||||
->orderBy('create_time')->get()->toArray();
|
||||
|
||||
$pay_channel = app(Dicts::class)->get('pay_channel');
|
||||
$pay_channel = array_values($pay_channel);
|
||||
|
||||
foreach ($orders as &$item) {
|
||||
$item = (array)$item;
|
||||
$item['total_price'] = intval($item['total_price'] * 100);
|
||||
$item['counts'] = !empty($item['counts']) ? $item['counts'] : 1;
|
||||
$item['pay_channel'] = 0;
|
||||
foreach ($pay_channel as $key => $value) {
|
||||
if (in_array($item['pay_channel'], $value)) {
|
||||
$item['pay_channel'] = $key;
|
||||
}
|
||||
}
|
||||
|
||||
$item['unit_price'] = floor($total_price/$item['counts']);
|
||||
$item['order_at'] = date('Y-m-d H:i:s', $item['order_at']);
|
||||
}
|
||||
|
||||
return $orders;
|
||||
}
|
||||
|
||||
// 订单详情
|
||||
protected function getOrderItems($order_ids)
|
||||
{
|
||||
$this->line('查询订单详情');
|
||||
$select = [
|
||||
'sn as order_id',
|
||||
'sim',
|
||||
'goods_no as package_id',
|
||||
'goods_type as type',
|
||||
'unit_price as item_unit_price',
|
||||
'quantity as item_counts',
|
||||
];
|
||||
|
||||
$orderItems = DB::connection('real')->table('jxc_custom_order_item')->select($select)
|
||||
->whereIn('sn', $order_ids)->where('goods_type', '<', 6)->get()->toArray();
|
||||
|
||||
foreach ($orderItems as &$item) {
|
||||
$item = (array)$item;
|
||||
$item['item_unit_price'] = intval($item['item_unit_price'] * 100);
|
||||
}
|
||||
|
||||
return $orderItems;
|
||||
}
|
||||
}
|
55
app/Domains/Virtual/Commands/Sync/ProductSync.php
Normal file
55
app/Domains/Virtual/Commands/Sync/ProductSync.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Virtual\Commands\Sync;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use App\Models\Virtual\Product;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Domains\Virtual\Repositories\CompanyRepository;
|
||||
use App\Domains\Virtual\Repositories\PackageRepository;
|
||||
use App\Domains\Virtual\Repositories\ProductRepository;
|
||||
|
||||
class ProductSync extends Command
|
||||
{
|
||||
protected $name = 'virtual:sync-product';
|
||||
|
||||
protected $description = '同步VD定价';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$packages = app(PackageRepository::class)->where('type', 0)->get()->keyBy('sn')->toArray();
|
||||
|
||||
$fields = ['company', 'content', 'order_account'];
|
||||
|
||||
$list = DB::connection('vd_old')->table('ckb_custom_handle_log')->select($fields)
|
||||
->where('type', 13)
|
||||
->groupBy($fields)->get();
|
||||
|
||||
$products = [];
|
||||
|
||||
foreach ($list as $key => $value) {
|
||||
$value = (array)$value;
|
||||
if (!$package = $packages[$value['content']]) {
|
||||
throw new \Exception('套餐不存在');
|
||||
}
|
||||
|
||||
$base_price = floatval($value['order_account']) * 100;
|
||||
|
||||
$products[] = [
|
||||
'sn' => $package['sn'] . '_' . $value['company'] . '_' . $base_price,
|
||||
'company_id' => $value['company'],
|
||||
'package_id' => $package['id'],
|
||||
'name' => $package['name'] . ' ' . $value['order_account'],
|
||||
'base_price' => $base_price,
|
||||
'renewal_price' => 0,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
'updated_at' => date('Y-m-d H:i:s'),
|
||||
'deleted_at' => null,
|
||||
];
|
||||
}
|
||||
|
||||
Product::upsert($products, ['sn', 'deleted_at']);
|
||||
|
||||
app(ProductRepository::class)->forgetCached();
|
||||
}
|
||||
}
|
@ -24,9 +24,7 @@ class VirtualServiceProvider extends ServiceProvider
|
||||
$this->commands([
|
||||
\App\Domains\Virtual\Commands\Sync\CompanySync::class,
|
||||
\App\Domains\Virtual\Commands\Sync\PackageSync::class,
|
||||
\App\Domains\Virtual\Commands\Sync\OrderBaseSync::class,
|
||||
\App\Domains\Virtual\Commands\Sync\ActivateSync::class,
|
||||
\App\Domains\Virtual\Commands\Sync\OrderCustomSync::class,
|
||||
\App\Domains\Virtual\Commands\Sync\ProductSync::class,
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -7,4 +7,14 @@ use App\Core\Model;
|
||||
class Product extends Model
|
||||
{
|
||||
protected $table = 'virtual_products';
|
||||
|
||||
public function company()
|
||||
{
|
||||
return $this->belongsTo(Company::class, 'company_id', 'id');
|
||||
}
|
||||
|
||||
public function package()
|
||||
{
|
||||
return $this->belongsTo(Package::class, 'company_id', 'id');
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user