diff --git a/app/Admin/Controllers/v3/GoodsNewController.php b/app/Admin/Controllers/v3/GoodsNewController.php index 9da7d90..07b9fbe 100644 --- a/app/Admin/Controllers/v3/GoodsNewController.php +++ b/app/Admin/Controllers/v3/GoodsNewController.php @@ -40,6 +40,7 @@ class GoodsNewController extends AdminController $grid->cover_img_url->image('',50); $grid->name->editable(); $grid->price->editable(); + $grid->column('goods_unit','单位')->editable(); $grid->column('market_id')->display(function($marketId){ $item = MarketModel::getMarketInfo($marketId,'name'); return empty($item) ? '' : $item->name; diff --git a/app/Console/Commands/MigrateStoreBalance.php b/app/Console/Commands/MigrateStoreBalance.php index 487b85d..dee1070 100644 --- a/app/Console/Commands/MigrateStoreBalance.php +++ b/app/Console/Commands/MigrateStoreBalance.php @@ -39,42 +39,185 @@ class MigrateStoreBalance extends Command */ public function handle() { + $oldStoreTableName = 'ims_cjdc_store'; $oldOrderTableName = 'ims_cjdc_order'; $oldWithdrawalTableName = 'ims_cjdc_withdrawal'; $oldAccountTableName = 'ims_cjdc_store_account'; - $newTableName = 'lanzu_user_balance'; + $newBalanceTableName = 'lanzu_user_balance'; // 判断表是否存在 if(!Schema::hasTable($oldAccountTableName)){ var_dump('旧表不存在'); return 0; } - if(!Schema::hasTable($newTableName)){ + if(!Schema::hasTable($newBalanceTableName)){ var_dump('新表不存在'); return 0; } - $oldData = DB::table($oldAccountTableName)->orderBy('id','asc')->get(); - $bar = $this->output->createProgressBar(count($oldData)); + // 流水算法 + // $newData = $this->countStore($oldAccountTableName); + // 订单算法 + $newData = $this->countOrder($oldStoreTableName,$oldOrderTableName,$oldWithdrawalTableName); + +var_dump($newData); +// var_dump(number_format($newData[12],2)); + +return 0; + $bar = $this->output->createProgressBar(count($newData)); $bar->start(); $startTime = time(); $error = []; - $newData = []; - foreach ($oldData as $key => $value){ - $storeId = $value->store_id; - - - + + 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() + ]; + return 0; + $res = DB::table($newBalanceTableName)->update($saveData); + if(!$res){ + $error[] = ['store_id'=>$store_id,'money'=>$money]; + } + }else{ + $saveData = [ + 'source_id' => $store_id, + 'user_type' => 5, + 'balance' => number_format($money), + 'updated_at' => time() + ]; + return 0; + $res = DB::table($newBalanceTableName)->insert($saveData); + if(!$res){ + $error[] = ['store_id'=>$store_id,'money'=>$money]; + } + } $bar->advance(); } - - DB::table($newTableName)->insert($newData); $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, $oldOrderTableName, $oldWithdrawalTableName) + { + $newData = []; + // 查询所有店铺 + $stores = DB::table($oldStoreTableName)->select('id','md_type','award_money')->get(); + + //店铺设置 + $storeSets = DB::table('ims_cjdc_storeset')->get()->toArray(); + $storeSetArr = array_column($storeSets,null,'store_id'); + + // 所有商家分类 + $storeTypes = DB::table('ims_cjdc_storetype')->get()->toArray(); + $storeTypeArr = array_column($storeTypes,null,'id'); + + return $storeTypeArr; + + foreach($stores as $key => $value){ + $storeId = $value->id; + + // 获取商家手续费 % 如果商家设置有自己的手续费则用商家的 + if($storeSets[$storeId]['is_poundage'] == 1){ + $poundage = $storeSetArr[$storeId]; + }else{ + $poundage = $storeTypeArr[$storeId]; + } + + // 商家订单金额 + // 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('ims_cjdc_order') + ->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]) + ->get(); + // 店内 + $dnOrder = DB::table('ims_cjdc_order') + ->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]) + ->get(); + // 当面 + $dmOrder = DB::table('ims_cjdc_order') + ->select(DB::raw('sum(money) as total_money')) + ->where('store_id',$storeId) + ->where('type',4) + ->where('dm_state',2) + ->whereIn('pay_type',[1,2]) + ->get(); + // 预约 + $yyOrder = DB::table('ims_cjdc_order') + ->select(DB::raw('sum(money) as total_money')) + ->where('store_id',$storeId) + ->where('type',3) + ->where('yy_state',3) + ->whereIn('pay_type',[1,2]) + ->get(); + // 提现 + $txTotal = DB::table('ims_cjdc_withdrawal') + ->select(DB::raw('sum(tx_cost) as total')) + ->where('store_id',$storeId) + ->whereIn('state',[1,2]) + ->get(); + + // 商家奖励 + $awardTotal = empty($value->award_money) ? 0 : $value->award_money; + + // 计算 金额 = 奖励 + 订单() - 手续费 - 提现 + $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) * $poundage->poundage + + ($dnOrder->total_money + $dnOrder->hb_money ) * $poundage->dn_poundage + + $dmOrder->total_money * $poundage->dm_poundage + + $yyOrder->total_money * $poundage->yd_poundage + )/100) + - ($txTotal->total) + ; + + $newData[$storeId] = number_format($balance,2); + } + + return $newData; + } }