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

231 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. $saveData = [
  62. 'balance' => number_format($money,2,'.',''),
  63. 'updated_at' => time()
  64. ];
  65. // var_dump($saveData);
  66. // return 0;
  67. $res = DB::table($newBalanceTableName)->where('source_id',$store_id)->where('user_type',5)->update($saveData);
  68. if(!$res){
  69. $error[] = ['store_id'=>$store_id,'money'=>$money];
  70. }
  71. }else{
  72. $saveData = [
  73. 'source_id' => $store_id,
  74. 'user_type' => 5,
  75. 'balance' => number_format($money,2,'.',''),
  76. 'updated_at' => time()
  77. ];
  78. // var_dump($saveData);
  79. // return 0;
  80. $res = DB::table($newBalanceTableName)->insert($saveData);
  81. if(!$res){
  82. $error[] = ['store_id'=>$store_id,'money'=>$money];
  83. }
  84. }
  85. $bar->advance();
  86. }
  87. $bar->finish();
  88. var_dump([time()-$startTime]);
  89. var_dump($error);
  90. return 0;
  91. }
  92. /**
  93. * 根据订单进行计算
  94. */
  95. public function countStore($oldAccountTableName)
  96. {
  97. $oldData = DB::table($oldAccountTableName)->orderBy('id','asc')->get();
  98. $newData = [];
  99. foreach ($oldData as $key => $value){
  100. $storeId = $value->store_id;
  101. if(array_key_exists($storeId,$newData)){
  102. if($value->type == 2){
  103. $newData[$storeId] -= $value->money;
  104. }else if($value->type == 1){
  105. $newData[$storeId] += $value->money;
  106. }
  107. }else{
  108. $newData[$storeId] = $value->money;
  109. }
  110. }
  111. return $newData;
  112. }
  113. /**
  114. * 根据订单进行计算
  115. */
  116. public function countOrder()
  117. {
  118. $oldStoreTableName = 'ims_cjdc_store';
  119. $oldStoreSetTableName = 'ims_cjdc_storeset';
  120. $oldStoreTypeTableName = 'ims_cjdc_storetype';
  121. $oldOrderTableName = 'ims_cjdc_order';
  122. $oldWithdrawalTableName = 'ims_cjdc_withdrawal';
  123. $newData = [];
  124. // 查询所有店铺
  125. $stores = DB::table($oldStoreTableName)->select('id','md_type','ps_poundage','award_money')->get();
  126. //店铺设置
  127. $storeSets = DB::table($oldStoreSetTableName)->select('id','store_id','is_poundage','poundage','dn_poundage','dm_poundage','yd_poundage')->get()->toArray();
  128. $storeSetArr = array_column($storeSets,null,'store_id');
  129. // 所有商家分类
  130. $storeTypes = DB::table($oldStoreTypeTableName)->get()->toArray();
  131. $storeTypeArr = array_column($storeTypes,null,'id');
  132. foreach($stores as $key => $value){
  133. $storeId = $value->id;
  134. // 获取商家手续费 % 如果商家设置有自己的手续费则用商家的
  135. if(isset($storeSetArr[$storeId]) && $storeSetArr[$storeId]->is_poundage == 1){
  136. $poundage = $storeSetArr[$storeId];
  137. }else{
  138. $poundage = $storeTypeArr[$value->md_type];
  139. }
  140. // 商家订单金额
  141. // type '1.外卖2.店内3.预定4.当面付',
  142. // pay_type '1.微信支付2.余额支付3.积分支付4.货到付款',
  143. // state '1.待付款2.待结单3.等待送达4.完成5.已评价6.取消7.拒绝8.退款中9.已退款10.退款拒绝',
  144. // 外卖
  145. $wmOrder = DB::table($oldOrderTableName)
  146. ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(ps_money) as ps_money'),DB::raw('sum(yhq_money2) as hb_money'))
  147. ->where('store_id',$storeId)
  148. ->where('type',1)
  149. ->whereIn('state',[4,5,10])
  150. ->whereIn('pay_type',[1,2])
  151. ->first();
  152. // 店内
  153. $dnOrder = DB::table($oldOrderTableName)
  154. ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(yhq_money2) as hb_money'))
  155. ->where('store_id',$storeId)
  156. ->where('type',2)
  157. ->where('dn_state',2)
  158. ->whereIn('pay_type',[1,2])
  159. ->first();
  160. // 当面
  161. $dmOrder = DB::table($oldOrderTableName)
  162. ->select(DB::raw('sum(money) as total_money'))
  163. ->where('store_id',$storeId)
  164. ->where('type',4)
  165. ->where('dm_state',2)
  166. ->whereIn('pay_type',[1,2])
  167. ->first();
  168. // 预约
  169. $yyOrder = DB::table($oldOrderTableName)
  170. ->select(DB::raw('sum(money) as total_money'))
  171. ->where('store_id',$storeId)
  172. ->where('type',3)
  173. ->where('yy_state',3)
  174. ->whereIn('pay_type',[1,2])
  175. ->first();
  176. // 提现
  177. $txTotal = DB::table($oldWithdrawalTableName)
  178. ->select(DB::raw('sum(tx_cost) as total'))
  179. ->where('store_id',$storeId)
  180. ->whereIn('state',[1,2])
  181. ->first();
  182. // 商家奖励
  183. $awardTotal = empty($value->award_money) ? 0 : $value->award_money;
  184. // 商户配送手续费
  185. $psMoney = number_format(floatval($wmOrder->ps_money)*floatval($value->ps_poundage)/100,2,'.','');
  186. // 不计算拼团和抢购的订单,目前没有这样的订单,在另外的订单表
  187. // 计算 金额 = 奖励 + 订单(外卖+店内+当面+预约) - 手续费 - 提现 - 配送费
  188. $balance = $awardTotal
  189. + ($wmOrder->total_money + $wmOrder->hb_money + $dnOrder->total_money + $dnOrder->hb_money + $dmOrder->total_money + $yyOrder->total_money )
  190. - ((
  191. ($wmOrder->total_money + $wmOrder->ps_money + $wmOrder->hb_money) * floatval($poundage->poundage)
  192. + ($dnOrder->total_money + $dnOrder->hb_money ) * floatval($poundage->dn_poundage)
  193. + $dmOrder->total_money * floatval($poundage->dm_poundage)
  194. + $yyOrder->total_money * floatval($poundage->yd_poundage)
  195. )/100)
  196. - $txTotal->total
  197. - $psMoney;
  198. $newData[$storeId] = $balance;
  199. }
  200. return $newData;
  201. }
  202. }