链街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.

223 lines
7.7 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. $oldStoreTableName = 'ims_cjdc_store';
  37. $oldOrderTableName = 'ims_cjdc_order';
  38. $oldWithdrawalTableName = 'ims_cjdc_withdrawal';
  39. $oldAccountTableName = 'ims_cjdc_store_account';
  40. $newBalanceTableName = 'lanzu_user_balance';
  41. // 判断表是否存在
  42. if(!Schema::hasTable($oldAccountTableName)){
  43. var_dump('旧表不存在');
  44. return 0;
  45. }
  46. if(!Schema::hasTable($newBalanceTableName)){
  47. var_dump('新表不存在');
  48. return 0;
  49. }
  50. // 流水算法
  51. // $newData = $this->countStore($oldAccountTableName);
  52. // 订单算法
  53. $newData = $this->countOrder($oldStoreTableName,$oldOrderTableName,$oldWithdrawalTableName);
  54. var_dump($newData);
  55. // var_dump(number_format($newData[12],2));
  56. return 0;
  57. $bar = $this->output->createProgressBar(count($newData));
  58. $bar->start();
  59. $startTime = time();
  60. $error = [];
  61. foreach($newData as $store_id => $money){
  62. $exist = DB::table($newBalanceTableName)->where('source_id',$store_id)->where('user_type',5)->exists();
  63. if($exist){
  64. $saveData = [
  65. 'balance' => number_format($money,2),
  66. 'updated_at' => time()
  67. ];
  68. return 0;
  69. $res = DB::table($newBalanceTableName)->update($saveData);
  70. if(!$res){
  71. $error[] = ['store_id'=>$store_id,'money'=>$money];
  72. }
  73. }else{
  74. $saveData = [
  75. 'source_id' => $store_id,
  76. 'user_type' => 5,
  77. 'balance' => number_format($money),
  78. 'updated_at' => time()
  79. ];
  80. return 0;
  81. $res = DB::table($newBalanceTableName)->insert($saveData);
  82. if(!$res){
  83. $error[] = ['store_id'=>$store_id,'money'=>$money];
  84. }
  85. }
  86. $bar->advance();
  87. }
  88. $bar->finish();
  89. var_dump([time()-$startTime]);
  90. var_dump($error);
  91. return 0;
  92. }
  93. /**
  94. * 根据订单进行计算
  95. */
  96. public function countStore($oldAccountTableName)
  97. {
  98. $oldData = DB::table($oldAccountTableName)->orderBy('id','asc')->get();
  99. $newData = [];
  100. foreach ($oldData as $key => $value){
  101. $storeId = $value->store_id;
  102. if(array_key_exists($storeId,$newData)){
  103. if($value->type == 2){
  104. $newData[$storeId] -= $value->money;
  105. }else if($value->type == 1){
  106. $newData[$storeId] += $value->money;
  107. }
  108. }else{
  109. $newData[$storeId] = $value->money;
  110. }
  111. }
  112. return $newData;
  113. }
  114. /**
  115. * 根据订单进行计算
  116. */
  117. public function countOrder( $oldStoreTableName, $oldOrderTableName, $oldWithdrawalTableName)
  118. {
  119. $newData = [];
  120. // 查询所有店铺
  121. $stores = DB::table($oldStoreTableName)->select('id','md_type','award_money')->get();
  122. //店铺设置
  123. $storeSets = DB::table('ims_cjdc_storeset')->get()->toArray();
  124. $storeSetArr = array_column($storeSets,null,'store_id');
  125. // 所有商家分类
  126. $storeTypes = DB::table('ims_cjdc_storetype')->get()->toArray();
  127. $storeTypeArr = array_column($storeTypes,null,'id');
  128. return $storeTypeArr;
  129. foreach($stores as $key => $value){
  130. $storeId = $value->id;
  131. // 获取商家手续费 % 如果商家设置有自己的手续费则用商家的
  132. if($storeSets[$storeId]['is_poundage'] == 1){
  133. $poundage = $storeSetArr[$storeId];
  134. }else{
  135. $poundage = $storeTypeArr[$storeId];
  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('ims_cjdc_order')
  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. ->get();
  149. // 店内
  150. $dnOrder = DB::table('ims_cjdc_order')
  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. ->get();
  157. // 当面
  158. $dmOrder = DB::table('ims_cjdc_order')
  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. ->get();
  165. // 预约
  166. $yyOrder = DB::table('ims_cjdc_order')
  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. ->get();
  173. // 提现
  174. $txTotal = DB::table('ims_cjdc_withdrawal')
  175. ->select(DB::raw('sum(tx_cost) as total'))
  176. ->where('store_id',$storeId)
  177. ->whereIn('state',[1,2])
  178. ->get();
  179. // 商家奖励
  180. $awardTotal = empty($value->award_money) ? 0 : $value->award_money;
  181. // 计算 金额 = 奖励 + 订单() - 手续费 - 提现
  182. $balance = $awardTotal
  183. + ($wmOrder->total_money + $wmOrder->hb_money + $dnOrder->total_money + $dnOrder->hb_money + $dmOrder->total_money + $yyOrder->total_money )
  184. - ((
  185. ($wmOrder->total_money + $wmOrder->ps_money + $wmOrder->hb_money) * $poundage->poundage
  186. + ($dnOrder->total_money + $dnOrder->hb_money ) * $poundage->dn_poundage
  187. + $dmOrder->total_money * $poundage->dm_poundage
  188. + $yyOrder->total_money * $poundage->yd_poundage
  189. )/100)
  190. - ($txTotal->total)
  191. ;
  192. $newData[$storeId] = number_format($balance,2);
  193. }
  194. return $newData;
  195. }
  196. }