优化同步

This commit is contained in:
邓皓元 2018-12-10 13:54:49 +08:00
parent b415ef49a0
commit b551c5bad8
17 changed files with 184 additions and 288 deletions

View File

@ -1,76 +0,0 @@
<?php
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\Card;
use MongoDB\BSON\UTCDateTime;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\CardRepository;
use App\Domains\Real\Repositories\PackageRepository;
class ActivateSync extends Command
{
protected $name = 'real:sync-activate';
protected $description = '同步RD激活数据';
protected $chunks = 1000;
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, $this->chunks);
foreach ($chucks as $chuck) {
echo '.';
$cards = Card::select(['sim', 'package_id'])->whereIn('sim', array_pluck($chuck, 'cNo'))->get()->keyBy('sim')->toArray();
$total += count($chuck);
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' => intval($value['sim']),
'activate_at' => $activate_at,
];
}
}
$this->line('更新卡表数据,总计更新条数:' . count($list));
$except = $total - count($list);
$this->line("其中有{$except}张卡不在表内");
foreach (array_chunk($list, $this->chunks) as $data) {
echo '.';
app(CardRepository::class)->where('sim')->updateActivateAt($data);
}
app(CardRepository::class)->forgetCached();
$this->line('更新数据成功');
}
}

View File

@ -4,15 +4,17 @@ namespace App\Domains\Real\Commands\Sync;
use App\Dicts;
use Carbon\Carbon;
use App\Models\Real\Order;
use MongoDB\BSON\UTCDateTime;
use App\Models\Real\AddedOrder;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Services\CommonService;
use App\Domains\Real\Repositories\OrderRepository;
use App\Domains\Real\Repositories\CompanyRepository;
use App\Domains\Real\Repositories\PackageRepository;
use App\Domains\Real\Repositories\AddedOrderRepository;
class OrderCustomSync extends Command
class AddedOrderSync extends Command
{
protected $name = 'real:sync-custom-order';
protected $name = 'real:sync-added-order';
protected $description = '同步RD企业订单数据';
@ -21,70 +23,50 @@ class OrderCustomSync extends Command
public function handle()
{
$this->datetime = $this->getDateTime();
$this->companies = app(CompanyRepository::class)->get()->keyBy('sn');
$this->packages = app(PackageRepository::class)->get()->keyBy('sn');
$orders = $this->getOrders();
$orders = array_keyBy($orders, 'order_id');
$orderItems = $this->getOrderItems(array_keys($orders));
$orderItems = $this->getOrderItems($orders);
$dataOrders = [];
$dataOrderCards = [];
$dataPackageCards = [];
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' => CommonService::parseCompanyId($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']][$value['sim']] = [
$dataOrderCards[$value['type']][] = [
'sim' => $value['sim'],
'order_id' => $value['order_id'],
'counts' => $value['item_counts'],
'unit_price' => $value['item_unit_price'],
];
$dataPackageCards[$value['type']][$value['sim']] = [
$dataPackageCards[$value['type']][] = [
'package_id' => $value['package_id'],
'sim' => $value['sim'],
];
}
$this->line('插入订单数据,条数:'.count($dataOrders));
foreach (array_chunk($dataOrders, $this->chunks) as $data) {
$this->line('插入订单数据,条数:'.count($orders));
foreach (array_chunk($orders, $this->chunks) as $data) {
echo '.';
Order::upsert($data, 'id');
AddedOrder::upsert($orders, 'id');
}
app(OrderRepository::class)->forgetCached();
unset($dataOrders);
app(AddedOrderRepository::class)->forgetCached();
$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',
'',
'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) {
echo '.';
DB::table($tables[$type])->upsert($data, ['sim','order_id']);
DB::table($tables[$type])->upsert($data, ['sim', 'order_id']);
}
}
unset($dataOrderCards);
@ -102,7 +84,7 @@ class OrderCustomSync extends Command
foreach ($dataPackageCards as $type => $packageCards) {
foreach (array_chunk($packageCards, $this->chunks) as $data) {
echo '.';
DB::table($tables[$type])->upsert($data, ['sim','package_id']);
DB::table($tables[$type])->upsert($data, ['sim', 'package_id']);
}
}
unset($dataPackageCards);
@ -118,14 +100,15 @@ class OrderCustomSync extends Command
$endtime = $this->datetime->copy()->endOfMonth()->endOfDay();
$select = [
'id as order_id',
'sn as order_sn',
'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)
@ -138,28 +121,35 @@ class OrderCustomSync extends Command
foreach ($orders as &$item) {
$item = (array)$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'] = 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['unit_price'] = intval($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($order_ids)
protected function getOrderItems($orders)
{
$orders = array_keyBy($orders, 'sn');
$this->line('查询订单详情');
$select = [
'sn as order_id',
'sn as order_sn',
'sim',
'goods_no as package_id',
'goods_type as type',
@ -168,31 +158,15 @@ class OrderCustomSync extends Command
];
$orderItems = DB::connection('real')->table('jxc_custom_order_item')->select($select)
->whereIn('sn', $order_ids)->where('goods_type', '<', 6)->get()->toArray();
->whereIn('sn', array_keys($orders))->where('goods_type', '<', 6)->get()->toArray();
foreach ($orderItems as &$item) {
$item = (array)$item;
$item['item_unit_price'] = intval($item['item_unit_price'] * 100);
$item['order_id'] = $orders[$item['order_sn']]['id'] ?? 0;
$item['package_id'] = $this->packages[$item['package_id']]['id'] ?? 0;
$item['unit_price'] = intval($item['item_unit_price'] * 100);
}
return $orderItems;
}
// 从MongoDB上获取卡数据
protected function getCards()
{
$starttime = new UTCDateTime($this->datetime->copy()->startOfDay()->startOfMonth()->timestamp * 1000);
$endtime = new UTCDateTime($this->datetime->copy()->endOfDay()->endOfMonth()->timestamp * 1000);
$select = ['cNo', 'bNo', 'sPCode', 'iccid', 'imsi', 'jBatchNo', 'exPCodes.cDate', 'exPCodes.pEffDate', 'exPCodes.oNo', 'exPCodes.pType', 'comId', 'oType'];
return DB::connection('mongo')->table('tblCard')->select($select)->where('pNo', 'No00000000768')->where('isDel', '<>', 1)->where('bNo', 'exists', true)
->where(function ($query) use ($starttime, $endtime) {
$query->where(function ($q) use ($starttime, $endtime) {
$q->where('exPCodes.cDate', '>=', $starttime)->where('exPCodes.cDate', '<=', $endtime)->where('exPCodes.oDate', 'exists', false);
})->orWhere(function ($q) use ($starttime, $endtime) {
$q->where('exPCodes.oDate', '>=', $starttime)->where('exPCodes.oDate', '<=', $endtime);
});
})->get()->toArray();
}
}

View File

@ -3,11 +3,12 @@
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\Card;
use App\Models\Card\Card;
use MongoDB\BSON\UTCDateTime;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\BlocRepository;
use App\Domains\Real\Repositories\CardRepository;
use Illuminate\Support\Facades\Artisan;
use App\Domains\Card\Repositories\BlocRepository;
use App\Domains\Card\Repositories\CardRepository;
class MongoSync extends Command
{
@ -32,13 +33,13 @@ class MongoSync extends Command
$utcDateTime = new UTCDateTime($microtime);
$blocs = app(BlocRepository::class)->get()->pluck('id', 'sn');
Artisan::call('real:sync-bloc');
$blocs = app(BlocRepository::class)->get()->pluck('id', 'sn')->toArray();
$query = DB::connection('mongo')->table('tblCard')
->select(['cNo', 'iccid', 'imsi', 'comId', 'oType', 'saDate', 'sDate'])
->where('isDel', '<>', 1)
->where('sDate', '>', $utcDateTime)
->whereIn('comId', array_values($blocs));
->where('sDate', '>', $utcDateTime);
$total = $query->count();
@ -47,20 +48,22 @@ class MongoSync extends Command
$page = 1;
while ($total) {
echo $page . PHP_EOL;
$res = $query->offset(($page - 1) * $this->limit)->limit($this->limit)->get();
$values = [];
foreach ($res as $key => $value) {
echo '.';
$activate_at = $value['saDate'] ? $value['saDate']->toDateTime()->format('Y-m-d H:i:s') : null;
$values[] = [
'sim' => $value['cNo'],
'imsi' => $value['imsi'],
'iccid' => $value['iccid'],
'bloc_id' => $blocs[$card_detail['comId']] ?? 0,
'carrier_operator' => self::$carrierOperators[$card_detail['oType']] ?? 255,
'activate_at' => $card['saDate']->toDateTime()->format('Y-m-d H:i:s'),
'created_at' => $card['sDate']->toDateTime()->format('Y-m-d H:i:s'),
'imsi' => $value['imsi'] ?? '',
'iccid' => $value['iccid'] ?? '',
'bloc_id' => $blocs[$value['comId']] ?? 0,
'carrier_operator' => self::$carrierOperators[$value['oType']] ?? 255,
'activate_at' => $activate_at,
'created_at' => $value['sDate']->toDateTime()->format('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
}
@ -70,6 +73,8 @@ class MongoSync extends Command
if ($page * $this->limit >= $total) {
break;
}
$page++;
}
app(CardRepository::class)->forgetCached();

View File

@ -3,15 +3,15 @@
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\Card;
use App\Models\Real\Order;
use App\Models\Real\OrderCard;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Services\CommonService;
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;
use App\Domains\Real\Repositories\OrderCardRepository;
class OrderBaseSync extends Command
{
@ -25,13 +25,12 @@ class OrderBaseSync extends Command
protected static $carrierOperators = [1, 0, 2];
protected $chunks = 2000;
protected $chunks = 1000;
public function handle()
{
$this->companies = app(CompanyRepository::class)->get()->keyBy('sn');
$this->packages = app(PackageRepository::class)->get()->keyBy('sn');
$this->blocs = app(BlocRepository::class)->get()->keyBy('sn');
$orders = $this->getOrders();
$cards = $this->getCards($orders);
@ -48,39 +47,12 @@ class OrderBaseSync extends Command
app(OrderRepository::class)->forgetCached();
$this->line('插入订单数据成功');
$this->line('插入卡数据,条数:'.count($cards));
foreach (array_chunk($cards, $this->chunks) as $data) {
echo '.';
foreach ($data as &$item) {
unset($item['order_id']);
unset($item['order_at']);
}
Card::upsert($data, 'sim,deleted_at');
}
app(CardRepository::class)->forgetCached();
$this->line('插入卡数据成功');
$this->line('插入订单关联数据,条数:'.count($cards));
foreach (array_chunk($cards, $this->chunks) as $data) {
echo '.';
$card_ids = app(CardRepository::class)->select(['id', 'sim'])->whereIn('sim', array_pluck($data, 'sim'))->get()->pluck('id', 'sim')->toArray();
$array = [];
foreach ($data as $item) {
$array[] = [
'card_id' => $card_ids[$item['sim']],
'order_id' => $item['order_id'],
'counts' => 1,
'unit_price' => $item['price'],
];
}
DB::table('real_order_base_cards')->whereIn('card_id', array_pluck($array, 'card_id'))->delete();
DB::table('real_order_base_cards')->insert($array);
OrderCard::upsert($data, ['sim', 'order_id']);
}
app(OrderCardRepository::class)->forgetCached();
$this->line('插入订单关联数据成功');
} catch (\Exception $e) {
$this->error($e->getMessage());
@ -128,7 +100,7 @@ class OrderBaseSync extends Command
foreach ($orders as $item) {
$item = (array)$item;
$array[] = [
'id' => $item['o_id'],
'sn' => $item['o_number'],
@ -171,16 +143,18 @@ class OrderBaseSync extends Command
foreach ($item['s_section_number'] as $value) {
$sim = explode('-', $value['section_no'])[0];
$order = $orders[$item['o_number']];
$orderRow = $orderRows[$item['s_number']];
for ($i=0; $i < $value['counts']; $i++) {
$cards[] = [
'sim' => intval($sim),
'order_id' => $order['id'],
'company_id' => $this->companies[$order['o_customer_number']]['id'] ?? 0,
'company_id' => $order['company_id'],
'package_id' => $order['package_id'],
'order_at' => $order['order_at'],
'price' => $order['unit_price'],
'created_at' => $order['created_at'],
'updated_at' => $order['updated_at'],
'deleted_at' => $order['deleted_at'],
];
$sim++;
@ -201,55 +175,8 @@ class OrderBaseSync extends Command
$this->line('排重后卡总数: ' . count($cards));
if (!count($cards)) {
throw new \Exception('销售数据为空');
}
$card_details = $this->getCardDetails($cards);
foreach ($cards as &$value) {
$card_detail = $card_details[$value['sim']];
if (!$card_detail) {
$this->error('Mongo上未找到卡数据:' . $value['sim']);
}
$value['imsi'] = intval($card_detail['imsi']) ?? 0;
$value['iccid'] = intval($card_detail['iccid']) ?? 0;
$value['bloc_id'] = $this->blocs[$card_detail['comId']]['id'] ?? 0;
$value['carrier_operator'] = self::$carrierOperators[$card_detail['oType']] ?? 255;
$value['created_at'] = date('Y-m-d H:i:s');
$value['updated_at'] = date('Y-m-d H:i:s');
$value['deleted_at'] = null;
}
return array_values($cards);
}
// 获取卡详细数据
protected function getCardDetails($cards)
{
$this->line('从MongoDB中取卡详细数据');
$cardChunks = array_chunk($cards, $this->chunks);
$card_details = [];
foreach ($cardChunks as $cardChunk) {
$res = DB::connection('mongo')->table('tblCard')->select(['cNo', 'iccid', 'imsi', 'comId', 'oType'])
->where('isDel', '<>', 1)
->whereIn('cNo', array_map('strval', array_pluck($cardChunk, 'sim')))->get()->toArray();
$card_details = array_merge($card_details, $res);
echo '.';
}
unset($cardChunks);
$card_details = array_keyBy($card_details, 'cNo');
$this->line('获取成功,卡详情总数:' . count($card_details));
return $card_details;
}
// 查询排单记录
protected function getOrderRows($orders)

View File

@ -26,8 +26,7 @@ class RealServiceProvider extends ServiceProvider
\App\Domains\Real\Commands\Sync\BlocSync::class,
\App\Domains\Real\Commands\Sync\PackageSync::class,
\App\Domains\Real\Commands\Sync\OrderBaseSync::class,
\App\Domains\Real\Commands\Sync\ActivateSync::class,
\App\Domains\Real\Commands\Sync\OrderCustomSync::class,
\App\Domains\Real\Commands\Sync\AddedOrderSync::class,
]);
}

View File

@ -0,0 +1,62 @@
<?php
namespace App\Domains\Real\Repositories;
use App\Core\Repository;
use App\Models\Real\AddedOrder as Model;
class AddedOrderRepository extends Repository
{
/**
* 是否关闭缓存
*
* @var boolean
*/
protected $cacheSkip = false;
/**
* 是否开启数据转化
*
* @var bool
*/
protected $needTransform = false;
/**
* @var array
*/
protected $fieldSearchable = [
'id' => '=',
'created_at' => 'like',
];
public function model() {
return Model::class;
}
/**
* 数据格式化
*
* @param mixed $result
*
* @return mixed
*/
public function transform($model)
{
return $model->toArray();
}
/**
* 查询条件
*
* @return void
*/
public function withConditions(array $conditions = [])
{
if (isset($conditions['id'])) {
$conditions['id'] = array_wrap($conditions['id']);
$this->model = $this->model->whereIn('id', $conditions['id']);
}
return $this;
}
}

View File

@ -3,10 +3,10 @@
namespace App\Domains\Real\Repositories;
use App\Core\Repository;
use App\Models\Real\Card as Model;
use App\Models\Real\OrderCard as Model;
use Illuminate\Support\Facades\DB;
class CardRepository extends Repository
class OrderCardRepository extends Repository
{
/**
* 是否关闭缓存

View File

@ -3,9 +3,9 @@
namespace App\Domains\Virtual\Repositories;
use App\Core\Repository;
use App\Models\Virtual\Card as Model;
use App\Models\Virtual\OrderCard as Model;
class CardRepository extends Repository
class OrderCardRepository extends Repository
{
/**
* 是否关闭缓存

View File

@ -3,32 +3,33 @@
namespace App\Models\Real;
use App\Core\Model;
use App\Models\OrderBase;
class AddedOrder extends Model
{
protected $table = 'real_added_orders';
public function renewalCards()
{
return $this->belongsToMany(Card::class, 'real_order_renewal_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'real_added_order_renewal_cards', 'order_id', 'sim');
}
public function renewalPackageCards()
{
return $this->belongsToMany(Card::class, 'real_order_renewal_package_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'real_added_order_renewal_package_cards', 'order_id', 'sim');
}
public function flowPackageCards()
{
return $this->belongsToMany(Card::class, 'real_order_flows_package_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'real_added_order_flows_package_cards', 'order_id', 'sim');
}
public function optionalPackageCards()
{
return $this->belongsToMany(Card::class, 'real_order_optional_package_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'real_added_order_optional_package_cards', 'order_id', 'sim');
}
public function additionalPackageCards()
{
return $this->belongsToMany(Card::class, 'real_order_additional_package_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'real_added_order_additional_package_cards', 'order_id', 'sim');
}
}

View File

@ -3,10 +3,11 @@
namespace App\Models\Real;
use App\Core\Model;
use App\Models\OrderBase;
class Order extends OrderBase
class Order extends Model
{
protected $table = 'real_orders';
public function cards()
{
return $this->hasMany(Card::class, 'order_id', 'id');

View File

@ -5,11 +5,11 @@ namespace App\Models\Real;
use App\Core\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Card extends Model
class OrderCard extends Model
{
use SoftDeletes;
protected $table = 'real_cards';
protected $table = 'real_order_cards';
public function company()
{
@ -59,30 +59,30 @@ class Card extends Model
// 续费订单
public function renewalOrders()
{
return $this->belongsToMany(Order::class, 'real_order_renewal_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'real_added_order_renewal_cards', 'sim', 'order_id');
}
// 续费订单
public function renewalPackageOrders()
{
return $this->belongsToMany(Order::class, 'real_order_renewal_package_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'real_added_order_renewal_package_cards', 'sim', 'order_id');
}
// 加油包订单
public function flowPackageOrders()
{
return $this->belongsToMany(Order::class, 'real_order_flows_package_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'real_added_order_flows_package_cards', 'sim', 'order_id');
}
// 可选包订单
public function optionalPackageOrders()
{
return $this->belongsToMany(Order::class, 'real_order_optional_package_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'real_added_order_optional_package_cards', 'sim', 'order_id');
}
// 附加包订单
public function additionalPackageOrders()
{
return $this->belongsToMany(Order::class, 'real_order_additional_package_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'real_added_order_additional_package_cards', 'sim', 'order_id');
}
}

View File

@ -6,28 +6,30 @@ use App\Core\Model;
class AddedOrder extends Model
{
protected $table = 'virtual_added_orders';
public function renewalCards()
{
return $this->belongsToMany(Card::class, 'virtual_order_renewal_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'virtual_added_order_renewal_cards', 'order_id', 'sim');
}
public function renewalPackageCards()
{
return $this->belongsToMany(Card::class, 'virtual_order_renewal_package_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'virtual_added_order_renewal_package_cards', 'order_id', 'sim');
}
public function flowPackageCards()
{
return $this->belongsToMany(Card::class, 'virtual_order_flows_package_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'virtual_added_order_flows_package_cards', 'order_id', 'sim');
}
public function optionalPackageCards()
{
return $this->belongsToMany(Card::class, 'virtual_order_optional_package_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'virtual_added_order_optional_package_cards', 'order_id', 'sim');
}
public function additionalPackageCards()
{
return $this->belongsToMany(Card::class, 'virtual_order_additional_package_cards', 'order_id', 'sim');
return $this->belongsToMany(Card::class, 'virtual_added_order_additional_package_cards', 'order_id', 'sim');
}
}

View File

@ -3,10 +3,11 @@
namespace App\Models\Virtual;
use App\Core\Model;
use App\Models\OrderBase;
class Order extends OrderBase
class Order extends Model
{
protected $table = 'virtual_orders';
public function cards()
{
return $this->hasMany(Card::class, 'order_id', 'id');

View File

@ -5,11 +5,11 @@ namespace App\Models\Virtual;
use App\Core\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Card extends Model
class OrderCard extends Model
{
use SoftDeletes;
protected $table = 'virtual_cards';
protected $table = 'virtual_order_cards';
public function company()
{
@ -59,30 +59,30 @@ class Card extends Model
// 续费订单
public function renewalOrders()
{
return $this->belongsToMany(Order::class, 'virtual_order_renewal_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'virtual_added_order_renewal_cards', 'sim', 'order_id');
}
// 续费订单
public function renewalPackageOrders()
{
return $this->belongsToMany(Order::class, 'virtual_order_renewal_package_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'virtual_added_order_renewal_package_cards', 'sim', 'order_id');
}
// 加油包订单
public function flowPackageOrders()
{
return $this->belongsToMany(Order::class, 'virtual_order_flows_package_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'virtual_added_order_flows_package_cards', 'sim', 'order_id');
}
// 可选包订单
public function optionalPackageOrders()
{
return $this->belongsToMany(Order::class, 'virtual_order_optional_package_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'virtual_added_order_optional_package_cards', 'sim', 'order_id');
}
// 附加包订单
public function additionalPackageOrders()
{
return $this->belongsToMany(Order::class, 'virtual_order_additional_package_cards', 'sim', 'order_id');
return $this->belongsToMany(Order::class, 'virtual_added_order_additional_package_cards', 'sim', 'order_id');
}
}

View File

@ -62,7 +62,7 @@ class CreateBaseTables extends Migration
db_alter("{$prefix}_packages", "{$type}套餐");
Schema::create("{$prefix}_cards", function (Blueprint $table) {
Schema::create("{$prefix}_order_cards", function (Blueprint $table) {
$table->increments('id')->comment('关联表ID');
$table->bigInteger('sim')->unsigned()->default(0)->comment('sim号');
$table->integer('order_id')->unsigned()->default(0)->comment('订单ID');
@ -146,8 +146,8 @@ class CreateBaseTables extends Migration
Schema::create("cards", function (Blueprint $table) {
$table->bigInteger('sim')->unsigned()->default(0)->comment('sim号');
$table->bigInteger('imsi')->unsigned()->default(0)->comment('imsi号');
$table->bigInteger('iccid')->unsigned()->default(0)->comment('iccid号');
$table->string('imsi', 32)->default('')->comment('imsi号');
$table->string('iccid', 32)->default('')->comment('iccid号');
$table->integer('bloc_id')->unsigned()->default(0)->comment('来源集团ID');
$table->tinyInteger('carrier_operator')->unsigned()->default(255)->comment('运营商(0:联通 1:移动 2:电信)');
$table->timestamp('activate_at')->nullable()->comment('激活时间');
@ -211,7 +211,7 @@ class CreateBaseTables extends Migration
foreach (app(Dicts::class)->get('tables') as $prefix => $type) {
Schema::dropIfExists("{$prefix}_added_orders");
Schema::dropIfExists("{$prefix}_orders");
Schema::dropIfExists("{$prefix}_cards");
Schema::dropIfExists("{$prefix}_order_cards");
Schema::dropIfExists("{$prefix}_packages");
Schema::dropIfExists("{$prefix}_companies");
}

View File

@ -8,11 +8,11 @@ use Illuminate\Database\Migrations\Migration;
class CreateOrderTables extends Migration
{
protected $tables = [
'custom_order_renewal_cards' => '卡关联可选包订单',
'custom_order_renewal_package_cards' => '卡关联加油包订单',
'custom_order_flows_package_cards' => '卡关联续费包订单',
'custom_order_optional_package_cards' => '卡关联续费订单',
'custom_order_additional_package_cards' => '卡关联基础订单',
'added_order_renewal_cards' => '卡关联可选包订单',
'added_order_renewal_package_cards' => '卡关联加油包订单',
'added_order_flows_package_cards' => '卡关联续费包订单',
'added_order_optional_package_cards' => '卡关联续费订单',
'added_order_additional_package_cards' => '卡关联基础订单',
];
/**
@ -28,7 +28,7 @@ class CreateOrderTables extends Migration
$table_comment = $type . $table_comment;
Schema::create($table_name, function (Blueprint $table) use ($prefix) {
$table->integer('order_id')->unsigned()->default(0)->comment('订单ID');
$table->integer('sim')->unsigned()->default(0)->comment('SIM卡号');
$table->bigInteger('sim')->unsigned()->default(0)->comment('SIM卡号');
if ($prefix === 'virtual') {
$table->integer('company_id')->unsigned()->default(0)->comment("企业ID");

View File

@ -25,7 +25,7 @@ class CreatePackageTables extends Migration
foreach ($this->tables as $table_name => $table_comment) {
Schema::create($table_name, function (Blueprint $table) {
$table->integer('package_id')->unsigned()->default(0)->comment('套餐ID');
$table->integer('sim')->unsigned()->default(0)->comment('SIM卡号');
$table->bigInteger('sim')->unsigned()->default(0)->comment('SIM卡号');
$table->primary(['package_id', 'sim']);
});