同步VD卡数据

This commit is contained in:
邓皓元 2018-12-25 15:08:11 +08:00
parent eb2eae3d3b
commit 324b39b7c6
19 changed files with 555 additions and 442 deletions

View File

@ -27,6 +27,7 @@ class Kernel extends ConsoleKernel
{
$logPath = storage_path('logs/artisan.log');
$schedule->command('real:sync-mongo')->cron('* * * * *')->withoutOverlapping()->appendOutputTo($logPath);
$schedule->command('virtual:sync-card')->cron('* * * * *')->withoutOverlapping()->appendOutputTo($logPath);
}
/**

View File

@ -0,0 +1,391 @@
<?php
namespace App\Domains\Virtual\Commands\Sync;
use Carbon\Carbon;
use App\Models\Card\Card;
use App\Models\Virtual\Order;
use App\Models\Virtual\OrderCard;
use Illuminate\Support\Facades\DB;
use App\Models\Virtual\OrderRenewalCard;
use App\Domains\Virtual\Services\CommonService;
use App\Domains\Virtual\Services\ProductService;
use App\Domains\Card\Repositories\BlocRepository;
use App\Domains\Card\Repositories\CardRepository;
use App\Domains\Virtual\Repositories\CompanyRepository;
use App\Domains\Virtual\Repositories\PackageRepository;
use App\Domains\Virtual\Repositories\ProductRepository;
class CardSync extends Command
{
protected $name = 'virtual:sync-card';
protected $description = '同步VD卡信息数据';
protected static $carrierOperators = [10 => 0, 11 => 1, 12 => 2];
protected static $payChannels = [10 => 'wx', 11 => 'alipay', 12 => 'bank'];
protected $blocs;
protected $packages;
protected $products;
protected $limit = 1;
const FILENAME = 'app/command/sync-card.json';
const INIT_ID = 0;
public function handle()
{
$contents = $this->getFile();
$maxId = $contents['maxId'];
$nextId = $contents['maxId'];
$this->saveFile(1, $maxId);
$query = DB::connection('vd_old')->table('ckb_custom')
->select(['id', 'custom_no', 'imsi', 'carrieroperator', 'iccid', 'card_number', 'card_from', 'iccid', 'company', 'custom_state', 'create_time' ,'update_time'])
->where('id', '>', $maxId);
$logQuery = DB::connection('vd_old')->table('ckb_custom_handle_log')
->select(['type', 'company', 'pay_type', 'content', 'valid_start_time', 'valid_end_time', 'sale_account', 'order_account', 'create_time']);
$total = $query->count();
$this->line('待同步条数:' . $total);
if ($total) {
$this->blocs = app(BlocRepository::class)->get()->pluck('id', 'shorthand')->toArray();
$this->packages = app(PackageRepository::class)->get()->keyBy('sn');
$this->products = app(ProductRepository::class)->get()->keyBy('sn');
}
$page = 1;
while ($total) {
echo $page . PHP_EOL;
$value = $query->offset(($page - 1) * $this->limit)->limit($this->limit)->get()->first();
if (!$value) {
break;
}
$value = (array)$value;
$logs = $logQuery->where('custom_no', $value['custom_no'])->get()->collect();
$existed = Card::where('sim', $value['card_number'])->first();
DB::beginTransaction();
try {
$card = $this->handleCard($value, $logs, $existed);
$this->handleOrder($card, $logs, $existed);
$this->handleRenewals($card, $logs, $existed);
$this->handleRenewalPackages($card, $logs, $existed);
$this->handleFlows($card, $logs, $existed);
DB::commit();
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
$nextId = $value['id'];
if ($page * $this->limit >= $total) {
break;
}
$page++;
}
app(CardRepository::class)->forgetCached();
$this->saveFile(0, $nextId);
}
// 卡数据转换
protected function handleCard($value, $logs, $existed)
{
// 判断卡类型
$type = ($value['card_number'][3] >= 5) ? 1 : ($existed ? 0 : 2);
// 激活记录
$activateLog = $logs->where('type', 10)->first();
$activated_at = $activateLog ? date('Y-m-d H:i:s', $activateLog['valid_start_time']) : null;
$cardData = [
'sim' => $value['card_number'],
'imsi' => $value['imsi'],
'iccid' => $value['iccid'],
'bloc_id' => $this->blocs[$value['card_from']] ?? 0,
'carrier_operator' => self::$carrierOperators[$value['carrieroperator']],
'activated_at' => $existed['activated_at'] ?? $activated_at,
'virtual_activated_at' => $activated_at,
'type' => $type,
'cancelled_at' => ($value['custom_state'] === 13) ? date('Y-m-d H:i:s', $value['update_time']) : null,
'created_at' => date('Y-m-d H:i:s', $value['create_time']),
'updated_at' => date('Y-m-d H:i:s', $value['update_time']),
];
if ($existed) {
$card = Card::where('sim', $card['sim'])->update($cardData);
} else {
$card = Card::create($cardData);
}
return $card;
}
// 销售记录
protected function handleOrder($card, $logs, $existed)
{
if (!$res = $logs->where('type', 13)->first()) {
return null;
}
$package = $this->getPackage($res['content']);
$unit_price = floatval($res['sale_account']) * 100;
$custom_price = floatval($res['order_account']) * 100;
$product = $this->getProduct($package, $res['company'], $unit_price);
// 按规则生成订单编号 (月+公司+产品)
$sn = date('Ym', $res['create_time']) . sprintf('%08d', $custom_price) . sprintf('%04d', $product['id']);
$orderData = [
'sn' => $sn,
'source' => 1,
'type' => 0,
'company_id' => $res['company'],
'package_id' => $package['id'],
'product_id' => $product['id'],
'pay_channel' => self::$payChannels[$res['pay_type']],
'unit_price' => $unit_price,
'counts' => 1,
'total_price' => $unit_price,
'custom_price' => $custom_price,
'order_at' => date('Y-m-d H:i:s', $res['create_time']),
'order_status' => 4,
'transaction_status' => 1,
'created_at' => date('Y-m-d H:i:s', $res['create_time']),
];
if ($order = Order::where('sn', $orderData['sn'])->first()) {
$order->counts = $order->counts + 1;
$order->total_price = $order->total_price + $unit_price;
$order->custom_price = $order->custom_price + $custom_price;
$order->save();
} else {
$order = Order::create($orderData);
}
OrderCard::upsert([
'sim' => $card['sim'],
'order_id' => $order['id'],
'company_id' => $order['company_id'],
'package_id' => $order['package_id'],
], ['sim', 'order_id']);
}
// 续费记录
protected function handleRenewals($card, $logs, $existed)
{
$res = $logs->where('type', 11)->values()->all();
if (empty($res)) {
return ;
}
$array = [];
foreach ($res as $item) {
$package = $this->getPackage($item['content']);
$unit_price = floatval($item['sale_account']) * 100;
$custom_price = floatval($item['order_account']) * 100;
$sn = date('Ym', $item['create_time']) . sprintf('%04d', $item['company']) . sprintf('%04d', $package['id']) . sprintf('%04d', $custom_price);
if (!$array[$sn]) {
$array[$sn] = [
'sn' => $sn,
'type' => 1,
'company_id' => $item['company'],
'package_id' => $package->id,
'unit_price' => $unit_price,
'counts' => 1,
'total_price' => $unit_price,
'custom_price' => $custom_price,
'order_at' => date('Y-m-d H:i:s', $item['create_time']),
];
} else {
$array[$sn] = [];
}
if ($order = Order::where('sn', $orderData['sn'])->first()) {
$order->counts = $order->counts + 1;
$order->total_price = $order->total_price + $unit_price;
$order->custom_price = $order->custom_price + $custom_price;
$order->save();
} else {
$order = Order::create($orderData);
}
if ($orderRenewalCard = OrderRenewalCard::where('sim', $card['sim'])->where('order_id', $order->id)->first()) {
$orderRenewalCard->count = $orderRenewalCard->count + 1;
$orderRenewalCard->save();
} else {
OrderRenewalCard::upsert([
'sim' => $card->sim,
'order_id' => $order->id,
'company_id' => $order->company_id,
'package_id' => $order->package_id,
'counts' => 1,
'unit_price' => $order->unit_price
], ['sim', 'order_id']);
}
}
}
// 续费包记录
protected function handleRenewalPackages($card, $logs, $existed)
{
$res = $logs->where('type', 14)->values()->all();
if (empty($res)) {
return ;
}
foreach ($res as $item) {
$package = $this->getPackage($item['content']);
$unit_price = floatval($item['sale_account']) * 100;
$custom_price = floatval($item['order_account']) * 100;
$sn = date('Ym', $item['create_time']) . sprintf('%04d', $item['company']) . sprintf('%04d', $package['id']) . sprintf('%04d', $custom_price);
$array[] = [
'sn' => $sn,
'type' => 2,
'company_id' => $item['company'],
'unit_price' => $unit_price,
'counts' => DB::raw('counts + 1'),
'total_price' => DB::raw("total_price + {$unit_price}"),
'custom_price' => DB::raw("custom_price + {$custom_price}"),
'order_at' => date('Y-m-d H:i:s', $item['create_time']),
];
}
}
// 加油包记录
protected function handleFlows($card, $logs, $existed)
{
$res = $logs->where('type', 15)->values()->all();
if (empty($res)) {
return ;
}
foreach ($res as $item) {
$package = $this->getPackage($item['content']);
$unit_price = floatval($item['sale_account']) * 100;
$custom_price = floatval($item['order_account']) * 100;
$sn = date('Ym', $item['create_time']) . sprintf('%04d', $item['company']) . sprintf('%04d', $package['id']) . sprintf('%04d', $custom_price);
$array[] = [
'sn' => $sn,
'type' => 3,
'company_id' => $item['company'],
'unit_price' => $unit_price,
'counts' => DB::raw('counts + 1'),
'total_price' => DB::raw("total_price + {$unit_price}"),
'custom_price' => DB::raw("custom_price + {$custom_price}"),
'order_at' => date('Y-m-d H:i:s', $item['create_time']),
];
}
}
/**
* 获取套餐
*
* @param string $sn
* @return void
*/
protected function getPackage($sn)
{
if (!$package = $this->packages[$sn]) {
throw new \Exception('套餐不存在');
}
return $package;
}
/**
* 获取定价
*
* @param string $sn
* @return void
*/
protected function getProduct($package, $companyId, $price)
{
$sn = strtoupper($package['sn'] . '_' . $companyId . '_' . $price);
if (!$product = $this->products[$sn]) {
$product = app(ProductService::class)->store([
'name' => $package['name'] . '' . $price,
'company_id' => $companyId,
'package_id' => $package['id'],
'base_price' => $price,
'renewal_price' => $price,
]);
$this->products[$sn] = $product;
}
return $product;
}
/**
* 获取文件内容
*
* @return void
*/
protected function getFile()
{
$file = storage_path(self::FILENAME);
if (!file_exists($file)) {
$dir = dirname($file);
if (null !== $dir && !is_dir($dir)) {
mkdir($dir, 0777, true);
}
$this->saveFile(0, self::INIT_ID);
}
$contents = file_get_contents($file);
return json_decode($contents, 256);
}
/**
* 写入文件
*
* @param integer $status 状态 1运行中 0运行结束
* @param integer $maxId 最后查询的ID
* @return void
*/
protected function saveFile(int $status, int $maxId)
{
$file = storage_path(self::FILENAME);
$contents = json_encode([
'status' => $status,
'maxId' => $maxId,
]);
file_put_contents($file, $contents);
}
}

View File

@ -4,16 +4,10 @@ namespace App\Domains\Virtual\Commands\Sync;
use Carbon\Carbon;
use App\Models\Card\Card;
use App\Models\Virtual\Order;
use App\Models\Virtual\OrderCard;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use App\Domains\Virtual\Services\CommonService;
use App\Domains\Virtual\Services\ProductService;
use App\Domains\Card\Repositories\BlocRepository;
use App\Domains\Card\Repositories\CardRepository;
use App\Domains\Virtual\Repositories\CompanyRepository;
use App\Domains\Virtual\Repositories\PackageRepository;
use App\Domains\Virtual\Repositories\ProductRepository;
class CardSync extends Command
{
@ -22,13 +16,10 @@ class CardSync extends Command
protected $description = '同步VD卡信息数据';
protected static $carrierOperators = [10 => 0, 11 => 1, 12 => 2];
protected static $payChannels = [10 => 'wx', 11 => 'alipay', 12 => 'bank'];
protected $blocs;
protected $packages;
protected $products;
protected $limit = 1;
protected $limit = 1000;
const FILENAME = 'app/command/sync-card.json';
const INIT_ID = 0;
@ -42,67 +33,61 @@ class CardSync extends Command
$this->saveFile(1, $maxId);
$query = DB::connection('vd_old')->table('ckb_custom')
->select(['id', 'custom_no', 'imsi', 'carrieroperator', 'iccid', 'card_number', 'card_from', 'iccid', 'company', 'custom_state', 'create_time' ,'update_time'])
->select(['id', 'custom_no', 'imsi', 'carrieroperator', 'iccid', 'card_number', 'card_from', 'iccid', 'company', 'custom_state', 'create_time' ,'update_time', 'card_cycle_start'])
->where('id', '>', $maxId);
$logQuery = DB::connection('vd_old')->table('ckb_custom_handle_log')
->select(['type', 'company', 'pay_type', 'content', 'valid_start_time', 'valid_end_time', 'sale_account', 'order_account', 'create_time']);
$total = $query->count();
$this->line('待同步条数:' . $total);
if ($total) {
$this->blocs = app(BlocRepository::class)->get()->pluck('id', 'shorthand')->toArray();
$this->packages = app(PackageRepository::class)->get()->keyBy('sn');
$this->products = app(ProductRepository::class)->get()->keyBy('sn');
}
$page = 1;
while ($total) {
echo $page . PHP_EOL;
$value = $query->offset(($page - 1) * $this->limit)->limit($this->limit)->get()->first();
echo 'nextId #: ' . $nextId . PHP_EOL;
$res = $query->offset(($page - 1) * $this->limit)->limit($this->limit)->get();
if (!$value) {
if (empty($res)) {
break;
}
$array = [];
foreach ($res as $key => $value) {
$value = (array)$value;
$logs = $logQuery->where('custom_no', $value['custom_no'])->get()->collect();
$existed = Card::where('sim', $value['card_number'])->first();
$card = $this->transformerCard($value, $logs, $existed);
$order = $this->transformerOrder($value, $logs, $existed);
$renewals = $this->transformerRenewals($value, $logs, $existed);
$renewalPackages = $this->transformerRenewalPackages($value, $logs, $existed);
$flows = $this->transformerFlows($value, $logs, $existed);
DB::beginTransaction();
try {
// 写入卡数据
Card::upsert($card, 'sim');
// 写入订单数据
Order::upsert($order, 'sn');
// 写入订单卡关系数据
OrderCard::upsert([
'sim' => $card['sim'],
'order_id' => $order['id'],
'company_id' => $order['company_id'],
'package_id' => $order['package_id'],
]);
// 增值包订单
AddedOrder::upsert();
} catch (\Exception $e) {
}
$array[] = [
'sim' => $value['card_number'],
'imsi' => $value['imsi'],
'iccid' => $value['iccid'],
'bloc_id' => $this->blocs[$value['card_from']] ?? 0,
'carrier_operator' => self::$carrierOperators[$value['carrieroperator']],
'type' => ($value['card_number'][3] >= 5) ? 1 : 0,
'activated_at' => date('Y-m-d H:i:s', $value['card_cycle_start']),
'virtual_activated_at' => date('Y-m-d H:i:s', $value['card_cycle_start']),
'cancelled_at' => ($value['custom_state'] === 13) ? date('Y-m-d H:i:s', $value['update_time']) : null,
'created_at' => date('Y-m-d H:i:s', $value['create_time']),
'updated_at' => date('Y-m-d H:i:s', $value['update_time']),
];
$nextId = $value['id'];
}
$builder = Card::query()->toBase();
$sql = $builder->getGrammar()->compileInsert($builder, $array);
$sql .= 'on conflict (sim) do update set
activated_at=COALESCE(cards.activated_at, excluded.virtual_activated_at),
virtual_activated_at=excluded.virtual_activated_at,
cancelled_at=excluded.cancelled_at';
$builder->connection->insert($sql, Arr::flatten($array, 1));
$this->saveFile(0, $nextId);
if ($page * $this->limit >= $total) {
break;
@ -112,207 +97,6 @@ class CardSync extends Command
}
app(CardRepository::class)->forgetCached();
$this->saveFile(0, $nextId);
}
// 卡数据转换
protected function transformerCard($value, $logs, $existed)
{
// 判断卡类型
$type = ($value['card_number'][3] >= 5) ? 1 : ($existed ? 0 : 2);
// 激活记录
$activateLog = $logs->where('type', 10)->first();
$activated_at = $activateLog ? date('Y-m-d H:i:s', $activateLog['valid_start_time']) : null;
return [
'sim' => $value['card_number'],
'imsi' => $value['imsi'],
'iccid' => $value['iccid'],
'bloc_id' => $this->blocs[$value['card_from']] ?? 0,
'carrier_operator' => self::$carrierOperators[$value['carrieroperator']],
'activated_at' => $existed['activated_at'] ?? $activated_at,
'virtual_activated_at' => $activated_at,
'type' => $type,
'cancelled_at' => ($value['custom_state'] === 13) ? date('Y-m-d H:i:s', $value['update_time']) : null,
'created_at' => date('Y-m-d H:i:s', $value['create_time']),
'updated_at' => date('Y-m-d H:i:s', $value['update_time']),
];
}
// 销售记录
protected function transformerOrder($value, $logs, $existed)
{
if (!$res = $logs->where('type', 13)->first()) {
return null;
}
$package = $this->getPackage($res['content']);
$unit_price = floatval($res['sale_account']) * 100;
$custom_price = floatval($res['order_account']) * 100;
$product = $this->getProduct($package, $res['company'], $unit_price);
// 按规则生成订单编号 (月+公司+产品)
$sn = date('Ym', $res['create_time']) . sprintf('%08d', $custom_price) . sprintf('%04d', $product['id']);
$order = [
'sn' => $sn,
'type' => 1,
'company_id' => $res['company'],
'package_id' => $package['id'],
'product_id' => $product['id'],
'pay_channel' => self::$payChannels[$res['pay_type']],
'unit_price' => $unit_price,
'counts' => DB::raw('counts + 1'),
'total_price' => DB::raw("total_price + {$unit_price}"),
'custom_price' => DB::raw("custom_price + {$custom_price}"),
'order_at' => date('Y-m-d H:i:s', $res['create_time']),
'order_status' => 4,
'transaction_status' => 1,
];
return $order;
}
// 续费记录
protected function transformerRenewals($value, $logs, $existed)
{
$res = $logs->where('type', 11)->values()->all();
if (empty($res)) {
return [];
}
$array = [];
foreach ($res as $item) {
$package = $this->getPackage($item['content']);
$unit_price = floatval($item['sale_account']) * 100;
$custom_price = floatval($item['order_account']) * 100;
$sn = date('Ym', $item['create_time']) . sprintf('%04d', $item['company']) . sprintf('%04d', $package['id']) . sprintf('%04d', $custom_price);
$array[] = [
'sn' => $sn,
'type' => 1,
'company_id' => $item['company'],
'unit_price' => $unit_price,
'counts' => DB::raw('counts + 1'),
'total_price' => DB::raw("total_price + {$unit_price}"),
'custom_price' => DB::raw("custom_price + {$custom_price}"),
'order_at' => date('Y-m-d H:i:s', $item['create_time']),
];
}
return $array;
}
// 续费包记录
protected function transformerRenewalPackages($value, $logs, $existed)
{
$res = $logs->where('type', 14)->values()->all();
if (empty($res)) {
return [];
}
$array = [];
foreach ($res as $item) {
$package = $this->getPackage($item['content']);
$unit_price = floatval($item['sale_account']) * 100;
$custom_price = floatval($item['order_account']) * 100;
$sn = date('Ym', $item['create_time']) . sprintf('%04d', $item['company']) . sprintf('%04d', $package['id']) . sprintf('%04d', $custom_price);
$array[] = [
'sn' => $sn,
'type' => 2,
'company_id' => $item['company'],
'unit_price' => $unit_price,
'counts' => DB::raw('counts + 1'),
'total_price' => DB::raw("total_price + {$unit_price}"),
'custom_price' => DB::raw("custom_price + {$custom_price}"),
'order_at' => date('Y-m-d H:i:s', $item['create_time']),
];
}
return $array;
}
// 加油包记录
protected function transformerFlows($value, $logs, $existed)
{
$res = $logs->where('type', 15)->values()->all();
if (empty($res)) {
return [];
}
$array = [];
foreach ($res as $item) {
$package = $this->getPackage($item['content']);
$unit_price = floatval($item['sale_account']) * 100;
$custom_price = floatval($item['order_account']) * 100;
$sn = date('Ym', $item['create_time']) . sprintf('%04d', $item['company']) . sprintf('%04d', $package['id']) . sprintf('%04d', $custom_price);
$array[] = [
'sn' => $sn,
'type' => 3,
'company_id' => $item['company'],
'unit_price' => $unit_price,
'counts' => DB::raw('counts + 1'),
'total_price' => DB::raw("total_price + {$unit_price}"),
'custom_price' => DB::raw("custom_price + {$custom_price}"),
'order_at' => date('Y-m-d H:i:s', $item['create_time']),
];
}
return $array;
}
/**
* 获取套餐
*
* @param string $sn
* @return void
*/
protected function getPackage($sn)
{
if (!$package = $this->packages[$sn]) {
throw new \Exception('套餐不存在');
}
return $package;
}
/**
* 获取定价
*
* @param string $sn
* @return void
*/
protected function getProduct($package, $companyId, $price)
{
$sn = strtoupper($package['sn'] . '_' . $companyId . '_' . $price);
if (!$product = $this->products[$sn]) {
$product = app(ProductService::class)->store([
'name' => $package['name'] . '' . $price,
'company_id' => $companyId,
'package_id' => $package['id'],
'base_price' => $price,
'renewal_price' => $price,
]);
$this->products[$sn] = $product;
}
return $product;
}
/**

View File

@ -64,7 +64,7 @@ class PackageService extends Service
$rule = [
'sn' => ['required', 'between:2,32', Rule::unique($this->packageRepository->getTable(), 'sn')->ignore($attributes['id'])],
'name' => ['required', 'between:2,32', Rule::unique($this->packageRepository->getTable(), 'name')->ignore($attributes['id'])],
'type' => ['required', 'in:0,2,3'],
'type' => ['required', 'in:0,1,2'],
'carrier_operator' => ['required', 'in:0,1,2,3'],
'cost_price' => ['numeric', 'min:0'],
'guide_price' => ['numeric', 'min:0'],

View File

@ -12,5 +12,5 @@ class Card extends Model
protected $primaryKey = 'sim';
protected $dates = ['activate_at', 'virtual_activate_at'];
protected $dates = ['activated_at', 'virtual_activated_at', 'cancelled_at'];
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Models;
use Exception;
use Illuminate\Database\Eloquent\Builder;
trait HasCompositePrimaryKey
{
/**
* Get the value indicating whether the IDs are incrementing.
*
* @return bool
*/
public function getIncrementing()
{
return false;
}
/**
* Set the keys for a save update query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSaveQuery(Builder $query)
{
foreach ($this->getKeyName() as $key) {
if ($this->$key) {
$query->where($key, '=', $this->$key);
} else {
throw new Exception(__METHOD__ . 'Missing part of the primary key: ' . $key);
}
}
return $query;
}
}

View File

@ -1,35 +0,0 @@
<?php
namespace App\Models\Virtual;
use App\Core\Model;
class AddedOrder extends Model
{
protected $table = 'virtual_added_orders';
public function renewalCards()
{
return $this->belongsToMany(Card::class, 'virtual_added_order_renewal_cards', 'order_id', 'sim');
}
public function renewalPackageCards()
{
return $this->belongsToMany(Card::class, 'virtual_added_order_renewal_package_cards', 'order_id', 'sim');
}
public function flowPackageCards()
{
return $this->belongsToMany(Card::class, 'virtual_added_order_flows_package_cards', 'order_id', 'sim');
}
public function optionalPackageCards()
{
return $this->belongsToMany(Card::class, 'virtual_added_order_optional_package_cards', 'order_id', 'sim');
}
public function additionalPackageCards()
{
return $this->belongsToMany(Card::class, 'virtual_added_order_additional_package_cards', 'order_id', 'sim');
}
}

View File

@ -3,6 +3,7 @@
namespace App\Models\Virtual;
use App\Core\Model;
use App\Models\Card\Card;
use App\Models\CompanyBase;
class Company extends CompanyBase

View File

@ -4,27 +4,18 @@ namespace App\Models\Virtual;
use App\Core\Model;
use App\Models\Real\OrderCard;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Virtual\Relations\OrderRelations;
class Order extends Model
{
use SoftDeletes, OrderRelations;
protected $table = 'virtual_orders';
protected $casts = [
'extends' => 'array',
];
public function cards()
{
return $this->hasMany(OrderCard::class, 'order_id', 'id');
}
public function company()
{
return $this->belongsTO(Company::class, 'company_id', 'id');
}
public function package()
{
return $this->belongsTo(Package::class, 'package_id', 'id');
}
protected $dates = ['order_at'];
}

View File

@ -3,86 +3,13 @@
namespace App\Models\Virtual;
use App\Core\Model;
use App\Models\HasCompositePrimaryKey;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Virtual\Relations\OrderRelations;
class OrderCard extends Model
{
use SoftDeletes;
use SoftDeletes, OrderRelations, HasCompositePrimaryKey;
protected $table = 'virtual_order_cards';
public function company()
{
return $this->belongsTo(Company::class, 'company_id', 'sim');
}
public function package()
{
return $this->belongsTo(Package::class, 'package_id', 'sim');
}
public function order()
{
return $this->belongsTo(Order::class, 'order_id', 'sim');
}
// 续费套餐
public function renewalPackages()
{
return $this->belongsToMany(Package::class, 'virtual_added_order_renewal_cards', 'sim', 'package_id');
}
// 续费包套餐
public function renewalPackagePackages()
{
return $this->belongsToMany(Package::class, 'virtual_added_order_renewal_package_cards', 'sim', 'package_id');
}
// 加油包套餐
public function flowPackagePackages()
{
return $this->belongsToMany(Package::class, 'virtual_added_order_flows_package_cards', 'sim', 'package_id');
}
// 可选包套餐
public function optionalPackagePackages()
{
return $this->belongsToMany(Package::class, 'virtual_added_order_optional_package_cards', 'sim', 'package_id');
}
// 附加包套餐
public function additionalPackagePackages()
{
return $this->belongsToMany(Package::class, 'virtual_added_order_additional_package_cards', 'sim', 'package_id');
}
// 续费订单
public function renewalOrders()
{
return $this->belongsToMany(Order::class, 'virtual_added_order_renewal_cards', 'sim', 'order_id');
}
// 续费订单
public function renewalPackageOrders()
{
return $this->belongsToMany(Order::class, 'virtual_added_order_renewal_package_cards', 'sim', 'order_id');
}
// 加油包订单
public function flowPackageOrders()
{
return $this->belongsToMany(Order::class, 'virtual_added_order_flows_package_cards', 'sim', 'order_id');
}
// 可选包订单
public function optionalPackageOrders()
{
return $this->belongsToMany(Order::class, 'virtual_added_order_optional_package_cards', 'sim', 'order_id');
}
// 附加包订单
public function additionalPackageOrders()
{
return $this->belongsToMany(Order::class, 'virtual_added_order_additional_package_cards', 'sim', 'order_id');
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Models\Virtual;
use App\Core\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Virtual\Relations\OrderRelations;
class OrderRenewalPackageCard extends Model
{
use SoftDeletes, OrderRelations;
protected $table = 'virtual_order_flows_package_cards';
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Models\Virtual;
use App\Core\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Virtual\Relations\OrderRelations;
class OrderRenewalCard extends Model
{
use SoftDeletes, OrderRelations;
protected $table = 'virtual_order_renewal_package_cards';
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Models\Virtual;
use App\Core\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Virtual\Relations\OrderRelations;
class OrderRenewalPackageCard extends Model
{
use SoftDeletes, OrderRelations;
protected $table = 'virtual_order_renewal_cards';
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Models\Virtual\Relations;
use App\Models\Card\Card;
use App\Models\Virtual\Order;
use App\Models\Virtual\Company;
use App\Models\Virtual\Package;
trait OrderRelations
{
public function company()
{
return $this->belongsTo(Company::class, 'company_id', 'sim');
}
public function package()
{
return $this->belongsTo(Package::class, 'package_id', 'sim');
}
public function order()
{
return $this->belongsTo(Order::class, 'order_id', 'sim');
}
public function cards()
{
return $this->belongsTo(Card::class, 'sim', 'sim');
}
}

View File

@ -22,7 +22,7 @@ class CreateVirtualPackagesTable extends Migration
$table->integer('parent_id')->unsigned()->default(0)->comment('父级ID');
$table->string('sn', 20)->comment('套餐编号');
$table->string('name', 32)->comment('套餐名称');
$table->tinyInteger('type')->unsigned()->default(255)->comment('套餐类型0:基础套餐 1续费包 2:加油包 3:可选包 4:附加包');
$table->tinyInteger('type')->unsigned()->default(255)->comment('套餐类型0:基础套餐 1续费包 2:加油包');
$table->tinyInteger('carrier_operator')->unsigned()->default(255)->comment('运营商(0:联通 1:移动 2:电信)');
$table->integer('cost_price')->unsigned()->default(0)->comment('成本价格');
$table->integer('guide_price')->unsigned()->default(0)->comment('指导价格');

View File

@ -1,54 +0,0 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateVirtualAddedOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasTable('virtual_added_orders')) {
return;
}
Schema::create('virtual_added_orders', function (Blueprint $table) {
$table->increments('id')->comment('订单ID');
$table->string('sn', 32)->comment('订单编号');
$table->tinyInteger('type')->unsigned()->default(0)->comment('订单类型1:套餐续费 2续费包 3:加油包 4:可选包 5:附加包)');
$table->integer('company_id')->unsigned()->default(0)->comment('企业ID');
$table->string('transaction_no', 64)->comment('交易流水号');
$table->string('pay_channel', 20)->default('')->comment('支付频道');
$table->integer('unit_price')->unsigned()->default(0)->comment('单价');
$table->integer('counts')->unsigned()->default(0)->comment('数量');
$table->integer('total_price')->unsigned()->default(0)->comment('总价');
$table->integer('custom_price')->unsigned()->default(0)->comment('自定义总价');
$table->timestamp('order_at')->nullable()->comment('下单时间');
$table->text('remark')->nullable()->comment('订单备注');
$table->timestamps();
$table->softDeletes();
$table->unique(['sn', 'deleted_at']);
$table->index('type');
$table->index('company_id');
$table->index('order_at');
$table->comment("VD增值包订单");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('virtual_added_orders');
}
}

View File

@ -4,14 +4,12 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateVirtualAddedOrderCardsTables extends Migration
class CreateVirtualOrderCardsTables extends Migration
{
protected $tables = [
'virtual_added_order_renewal_cards' => 'VD卡关联基础订单',
'virtual_added_order_renewal_package_cards' => 'VD卡关联续费包订单',
'virtual_added_order_flows_package_cards' => 'VD卡关联加油包订单',
'virtual_added_order_optional_package_cards' => 'VD卡关联可选包订单',
'virtual_added_order_additional_package_cards' => 'VD卡关联附加包订单',
'virtual_order_renewal_cards' => 'VD卡关联基础订单',
'virtual_order_renewal_package_cards' => 'VD卡关联续费包订单',
'virtual_order_flows_package_cards' => 'VD卡关联加油包订单',
];
/**

View File

@ -19,12 +19,11 @@ return array(
'CreateRealOrderCardsTable' => $baseDir . '/database/migrations/2018_12_24_164434_create_real_order_cards_table.php',
'CreateRealOrdersTable' => $baseDir . '/database/migrations/2018_12_24_164430_create_real_orders_table.php',
'CreateRealPackagesTable' => $baseDir . '/database/migrations/2018_12_24_164423_create_real_packages_table.php',
'CreateVirtualAddedOrderCardsTables' => $baseDir . '/database/migrations/2018_12_24_170946_create_virtual_added_order_cards_tables.php',
'CreateVirtualAddedOrdersTable' => $baseDir . '/database/migrations/2018_12_24_170936_create_virtual_added_orders_table.php',
'CreateVirtualCompaniesTable' => $baseDir . '/database/migrations/2018_12_24_164716_create_virtual_companies_table.php',
'CreateVirtualCompanyAccountsTable' => $baseDir . '/database/migrations/2018_12_24_164728_create_virtual_company_accounts_table.php',
'CreateVirtualCompanyAddressesTable' => $baseDir . '/database/migrations/2018_12_24_164735_create_virtual_company_addresses_table.php',
'CreateVirtualOrderCardsTable' => $baseDir . '/database/migrations/2018_12_24_165555_create_virtual_order_cards_table.php',
'CreateVirtualOrderCardsTables' => $baseDir . '/database/migrations/2018_12_24_170946_create_virtual_order_cards_tables.php',
'CreateVirtualOrdersTable' => $baseDir . '/database/migrations/2018_12_24_164779_create_virtual_orders_table.php',
'CreateVirtualPackagesTable' => $baseDir . '/database/migrations/2018_12_24_164722_create_virtual_packages_table.php',
'CreateVirtualProductsTable' => $baseDir . '/database/migrations/2018_12_24_164742_create_virtual_products_table.php',

View File

@ -722,12 +722,11 @@ class ComposerStaticInite79258a3e34ad3e251999111d9f334d9
'CreateRealOrderCardsTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164434_create_real_order_cards_table.php',
'CreateRealOrdersTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164430_create_real_orders_table.php',
'CreateRealPackagesTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164423_create_real_packages_table.php',
'CreateVirtualAddedOrderCardsTables' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_170946_create_virtual_added_order_cards_tables.php',
'CreateVirtualAddedOrdersTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_170936_create_virtual_added_orders_table.php',
'CreateVirtualCompaniesTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164716_create_virtual_companies_table.php',
'CreateVirtualCompanyAccountsTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164728_create_virtual_company_accounts_table.php',
'CreateVirtualCompanyAddressesTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164735_create_virtual_company_addresses_table.php',
'CreateVirtualOrderCardsTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_165555_create_virtual_order_cards_table.php',
'CreateVirtualOrderCardsTables' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_170946_create_virtual_order_cards_tables.php',
'CreateVirtualOrdersTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164779_create_virtual_orders_table.php',
'CreateVirtualPackagesTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164722_create_virtual_packages_table.php',
'CreateVirtualProductsTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164742_create_virtual_products_table.php',