|
|
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Schema;
class MigrateStoreBalance extends Command{ /** * The name and signature of the console command. * * @var string */ protected $signature = 'migrateData:storeBalance';
/** * The console command description. * * @var string */ protected $description = 'Command 迁移商户余额数据到lanzu_user_balance表';
/** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); }
/** * Execute the console command. * * @return int */ public function handle() { $oldAccountTableName = 'ims_cjdc_store_account';
$newBalanceTableName = 'lanzu_user_balance';
// 判断表是否存在
if(!Schema::hasTable($oldAccountTableName)){ var_dump('旧表不存在'); return 0; } if(!Schema::hasTable($newBalanceTableName)){ var_dump('新表不存在'); return 0; }
// 流水算法
$newData = $this->countStore($oldAccountTableName); // 订单算法
// $newData = $this->countOrder();
// var_dump($newData);
// var_dump(count($newData));
// return 0;
$bar = $this->output->createProgressBar(count($newData)); $bar->start(); $startTime = time(); $error = [];
foreach($newData as $store_id => $money){ $exist = DB::table($newBalanceTableName)->where('source_id',$store_id)->where('user_type',5)->exists(); if($exist){ $saveData = [ 'balance' => number_format($money,2,'.',''), 'updated_at' => time() ]; // var_dump($saveData);
// return 0;
$res = DB::table($newBalanceTableName)->where('source_id',$store_id)->where('user_type',5)->update($saveData); if(!$res){ $error[] = ['store_id'=>$store_id,'money'=>$money]; } }else{ $saveData = [ 'source_id' => $store_id, 'user_type' => 5, 'balance' => number_format($money,2,'.',''), 'updated_at' => time() ]; // var_dump($saveData);
// return 0;
$res = DB::table($newBalanceTableName)->insert($saveData); if(!$res){ $error[] = ['store_id'=>$store_id,'money'=>$money]; } } $bar->advance(); } $bar->finish(); var_dump([time()-$startTime]); var_dump($error); return 0; }
/** * 根据订单进行计算 */ public function countStore($oldAccountTableName) { $oldData = DB::table($oldAccountTableName)->orderBy('id','asc')->get(); $newData = []; foreach ($oldData as $key => $value){ $storeId = $value->store_id; if(array_key_exists($storeId,$newData)){ if($value->type == 2){ $newData[$storeId] -= $value->money; }else if($value->type == 1){ $newData[$storeId] += $value->money; } }else{ $newData[$storeId] = $value->money; } } return $newData; }
/** * 根据订单进行计算 */ public function countOrder() { $oldStoreTableName = 'ims_cjdc_store'; $oldStoreSetTableName = 'ims_cjdc_storeset'; $oldStoreTypeTableName = 'ims_cjdc_storetype'; $oldOrderTableName = 'ims_cjdc_order'; $oldWithdrawalTableName = 'ims_cjdc_withdrawal';
$newData = []; // 查询所有店铺
$stores = DB::table($oldStoreTableName)->select('id','md_type','ps_poundage','award_money')->get(); //店铺设置
$storeSets = DB::table($oldStoreSetTableName)->select('id','store_id','is_poundage','poundage','dn_poundage','dm_poundage','yd_poundage')->get()->toArray(); $storeSetArr = array_column($storeSets,null,'store_id');
// 所有商家分类
$storeTypes = DB::table($oldStoreTypeTableName)->get()->toArray(); $storeTypeArr = array_column($storeTypes,null,'id');
foreach($stores as $key => $value){ $storeId = $value->id;
// 获取商家手续费 % 如果商家设置有自己的手续费则用商家的
if(isset($storeSetArr[$storeId]) && $storeSetArr[$storeId]->is_poundage == 1){ $poundage = $storeSetArr[$storeId]; }else{ $poundage = $storeTypeArr[$value->md_type]; }
// 商家订单金额
// type '1.外卖2.店内3.预定4.当面付',
// pay_type '1.微信支付2.余额支付3.积分支付4.货到付款',
// state '1.待付款2.待结单3.等待送达4.完成5.已评价6.取消7.拒绝8.退款中9.已退款10.退款拒绝',
// 外卖
$wmOrder = DB::table($oldOrderTableName) ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(ps_money) as ps_money'),DB::raw('sum(yhq_money2) as hb_money')) ->where('store_id',$storeId) ->where('type',1) ->whereIn('state',[4,5,10]) ->whereIn('pay_type',[1,2]) ->first(); // 店内
$dnOrder = DB::table($oldOrderTableName) ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(yhq_money2) as hb_money')) ->where('store_id',$storeId) ->where('type',2) ->where('dn_state',2) ->whereIn('pay_type',[1,2]) ->first(); // 当面
$dmOrder = DB::table($oldOrderTableName) ->select(DB::raw('sum(money) as total_money')) ->where('store_id',$storeId) ->where('type',4) ->where('dm_state',2) ->whereIn('pay_type',[1,2]) ->first(); // 预约
$yyOrder = DB::table($oldOrderTableName) ->select(DB::raw('sum(money) as total_money')) ->where('store_id',$storeId) ->where('type',3) ->where('yy_state',3) ->whereIn('pay_type',[1,2]) ->first(); // 提现
$txTotal = DB::table($oldWithdrawalTableName) ->select(DB::raw('sum(tx_cost) as total')) ->where('store_id',$storeId) ->whereIn('state',[1,2]) ->first();
// 商家奖励
$awardTotal = empty($value->award_money) ? 0 : $value->award_money; // 商户配送手续费
$psMoney = number_format(floatval($wmOrder->ps_money)*floatval($value->ps_poundage)/100,2,'.','');
// 不计算拼团和抢购的订单,目前没有这样的订单,在另外的订单表
// 计算 金额 = 奖励 + 订单(外卖+店内+当面+预约) - 手续费 - 提现 - 配送费
$balance = $awardTotal + ($wmOrder->total_money + $wmOrder->hb_money + $dnOrder->total_money + $dnOrder->hb_money + $dmOrder->total_money + $yyOrder->total_money ) - (( ($wmOrder->total_money + $wmOrder->ps_money + $wmOrder->hb_money) * floatval($poundage->poundage) + ($dnOrder->total_money + $dnOrder->hb_money ) * floatval($poundage->dn_poundage) + $dmOrder->total_money * floatval($poundage->dm_poundage) + $yyOrder->total_money * floatval($poundage->yd_poundage) )/100) - $txTotal->total - $psMoney;
$newData[$storeId] = $balance; } return $newData; }}
|