链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
8.3 KiB

  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. class MigrateStoreBalance extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'migrateData:storeBalance';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command 迁移商户余额数据到lanzu_user_balance表';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $oldAccountTableName = 'ims_cjdc_store_account';
  37. $newBalanceTableName = 'lanzu_user_balance';
  38. // 判断表是否存在
  39. if(!Schema::hasTable($oldAccountTableName)){
  40. var_dump('旧表不存在');
  41. return 0;
  42. }
  43. if(!Schema::hasTable($newBalanceTableName)){
  44. var_dump('新表不存在');
  45. return 0;
  46. }
  47. // 流水算法
  48. $newData = $this->countStore($oldAccountTableName);
  49. // 订单算法
  50. // $newData = $this->countOrder();
  51. // var_dump($newData);
  52. // var_dump(count($newData));
  53. // return 0;
  54. $bar = $this->output->createProgressBar(count($newData));
  55. $bar->start();
  56. $startTime = time();
  57. $error = [];
  58. foreach($newData as $store_id => $money){
  59. $exist = DB::table($newBalanceTableName)->where('source_id',$store_id)->where('user_type',5)->exists();
  60. if($exist){
  61. $error[] = ['store_id'=>$store_id,'money'=>$money,'msg'=>'已存在'];
  62. // $saveData = [
  63. // 'balance' => number_format($money,2,'.',''),
  64. // 'updated_at' => time()
  65. // ];
  66. // $res = DB::table($newBalanceTableName)->where('source_id',$store_id)->where('user_type',5)->increment('balance',$saveData['balance']);
  67. // if(!$res){
  68. // $error[] = ['store_id'=>$store_id,'money'=>$money];
  69. // }
  70. }else{
  71. $saveData = [
  72. 'source_id' => $store_id,
  73. 'user_type' => 5,
  74. 'balance' => number_format($money,2,'.',''),
  75. 'updated_at' => time()
  76. ];
  77. $res = DB::table($newBalanceTableName)->insert($saveData);
  78. if(!$res){
  79. $error[] = ['store_id'=>$store_id,'money'=>$money];
  80. }
  81. }
  82. $bar->advance();
  83. }
  84. $bar->finish();
  85. var_dump([time()-$startTime]);
  86. var_dump($error);
  87. return 0;
  88. }
  89. /**
  90. * 根据订单进行计算
  91. */
  92. public function countStore($oldAccountTableName)
  93. {
  94. $oldData = DB::table($oldAccountTableName)->orderBy('id','asc')->get();
  95. $newData = [];
  96. foreach ($oldData as $key => $value){
  97. $storeId = $value->store_id;
  98. if(array_key_exists($storeId,$newData)){
  99. if($value->type == 2){
  100. $newData[$storeId] -= $value->money;
  101. }else if($value->type == 1){
  102. $newData[$storeId] += $value->money;
  103. }
  104. }else{
  105. $newData[$storeId] = $value->money;
  106. }
  107. }
  108. return $newData;
  109. }
  110. /**
  111. * 根据订单进行计算
  112. */
  113. public function countOrder()
  114. {
  115. $oldStoreTableName = 'ims_cjdc_store';
  116. $oldStoreSetTableName = 'ims_cjdc_storeset';
  117. $oldStoreTypeTableName = 'ims_cjdc_storetype';
  118. $oldOrderTableName = 'ims_cjdc_order';
  119. $oldWithdrawalTableName = 'ims_cjdc_withdrawal';
  120. $newData = [];
  121. // 查询所有店铺
  122. $stores = DB::table($oldStoreTableName)->select('id','md_type','ps_poundage','award_money')->get();
  123. //店铺设置
  124. $storeSets = DB::table($oldStoreSetTableName)->select('id','store_id','is_poundage','poundage','dn_poundage','dm_poundage','yd_poundage')->get()->toArray();
  125. $storeSetArr = array_column($storeSets,null,'store_id');
  126. // 所有商家分类
  127. $storeTypes = DB::table($oldStoreTypeTableName)->get()->toArray();
  128. $storeTypeArr = array_column($storeTypes,null,'id');
  129. foreach($stores as $key => $value){
  130. $storeId = $value->id;
  131. // 获取商家手续费 % 如果商家设置有自己的手续费则用商家的
  132. if(isset($storeSetArr[$storeId]) && $storeSetArr[$storeId]->is_poundage == 1){
  133. $poundage = $storeSetArr[$storeId];
  134. }else{
  135. $poundage = $storeTypeArr[$value->md_type];
  136. }
  137. // 商家订单金额
  138. // type '1.外卖2.店内3.预定4.当面付',
  139. // pay_type '1.微信支付2.余额支付3.积分支付4.货到付款',
  140. // state '1.待付款2.待结单3.等待送达4.完成5.已评价6.取消7.拒绝8.退款中9.已退款10.退款拒绝',
  141. // 外卖
  142. $wmOrder = DB::table($oldOrderTableName)
  143. ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(ps_money) as ps_money'),DB::raw('sum(yhq_money2) as hb_money'))
  144. ->where('store_id',$storeId)
  145. ->where('type',1)
  146. ->whereIn('state',[4,5,10])
  147. ->whereIn('pay_type',[1,2])
  148. ->first();
  149. // 店内
  150. $dnOrder = DB::table($oldOrderTableName)
  151. ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(yhq_money2) as hb_money'))
  152. ->where('store_id',$storeId)
  153. ->where('type',2)
  154. ->where('dn_state',2)
  155. ->whereIn('pay_type',[1,2])
  156. ->first();
  157. // 当面
  158. $dmOrder = DB::table($oldOrderTableName)
  159. ->select(DB::raw('sum(money) as total_money'))
  160. ->where('store_id',$storeId)
  161. ->where('type',4)
  162. ->where('dm_state',2)
  163. ->whereIn('pay_type',[1,2])
  164. ->first();
  165. // 预约
  166. $yyOrder = DB::table($oldOrderTableName)
  167. ->select(DB::raw('sum(money) as total_money'))
  168. ->where('store_id',$storeId)
  169. ->where('type',3)
  170. ->where('yy_state',3)
  171. ->whereIn('pay_type',[1,2])
  172. ->first();
  173. // 提现
  174. $txTotal = DB::table($oldWithdrawalTableName)
  175. ->select(DB::raw('sum(tx_cost) as total'))
  176. ->where('store_id',$storeId)
  177. ->whereIn('state',[1,2])
  178. ->first();
  179. // 商家奖励
  180. $awardTotal = empty($value->award_money) ? 0 : $value->award_money;
  181. // 商户配送手续费
  182. $psMoney = number_format(floatval($wmOrder->ps_money)*floatval($value->ps_poundage)/100,2,'.','');
  183. // 不计算拼团和抢购的订单,目前没有这样的订单,在另外的订单表
  184. // 计算 金额 = 奖励 + 订单(外卖+店内+当面+预约) - 手续费 - 提现 - 配送费
  185. $balance = $awardTotal
  186. + ($wmOrder->total_money + $wmOrder->hb_money + $dnOrder->total_money + $dnOrder->hb_money + $dmOrder->total_money + $yyOrder->total_money )
  187. - ((
  188. ($wmOrder->total_money + $wmOrder->ps_money + $wmOrder->hb_money) * floatval($poundage->poundage)
  189. + ($dnOrder->total_money + $dnOrder->hb_money ) * floatval($poundage->dn_poundage)
  190. + $dmOrder->total_money * floatval($poundage->dm_poundage)
  191. + $yyOrder->total_money * floatval($poundage->yd_poundage)
  192. )/100)
  193. - $txTotal->total
  194. - $psMoney;
  195. $newData[$storeId] = $balance;
  196. }
  197. return $newData;
  198. }
  199. }