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

382 lines
15 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. public function getBalanceById($id)
  30. {
  31. $oldStoreTableName = 'ims_cjdc_store';
  32. $oldStoreSetTableName = 'ims_cjdc_storeset';
  33. $oldStoreTypeTableName = 'ims_cjdc_storetype';
  34. $oldOrderTableName = 'ims_cjdc_order';
  35. $oldWithdrawalTableName = 'ims_cjdc_withdrawal';
  36. // 查询店铺
  37. $stores = DB::table($oldStoreTableName)->select('id','md_type','ps_poundage','award_money')->where('id',$id)->get();
  38. if(count($stores) <= 0){
  39. var_dump('店铺不存在');
  40. return ;
  41. }
  42. //店铺设置
  43. $storeSets = DB::table($oldStoreSetTableName)
  44. ->select('id','store_id','is_poundage','poundage','dn_poundage','dm_poundage','yd_poundage')
  45. ->where('store_id',$id)->first();
  46. // 所有分类
  47. $storeTypes = DB::table($oldStoreTypeTableName)->get()->toArray();
  48. $storeTypeArr = array_column($storeTypes,null,'id');
  49. foreach($stores as $key => $value){
  50. $storeId = $value->id;
  51. // 获取商家手续费 % 如果商家设置有自己的手续费则用商家的
  52. if(!empty($storeSets) && $storeSets->is_poundage == 1){
  53. $poundage = $storeSets;
  54. }else{
  55. $poundage = $storeTypeArr[$value->md_type];
  56. }
  57. // 商家订单金额
  58. // type '1.外卖2.店内3.预定4.当面付',
  59. // pay_type '1.微信支付2.余额支付3.积分支付4.货到付款',
  60. // state '1.待付款2.待结单3.等待送达4.完成5.已评价6.取消7.拒绝8.退款中9.已退款10.退款拒绝',
  61. // 外卖
  62. $wmOrder = DB::table($oldOrderTableName)
  63. ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(ps_money) as ps_money'),DB::raw('sum(yhq_money2) as hb_money'))
  64. ->where('store_id',$storeId)
  65. ->where('type',1)
  66. ->whereIn('state',[4,5,10])
  67. ->whereIn('pay_type',[1,2])
  68. ->first();
  69. // 店内
  70. $dnOrder = DB::table($oldOrderTableName)
  71. ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(yhq_money2) as hb_money'))
  72. ->where('store_id',$storeId)
  73. ->where('type',2)
  74. ->where('dn_state',2)
  75. ->whereIn('pay_type',[1,2])
  76. ->first();
  77. // 当面
  78. $dmOrder = DB::table($oldOrderTableName)
  79. ->select(DB::raw('sum(money) as total_money'))
  80. ->where('store_id',$storeId)
  81. ->where('type',4)
  82. ->where('dm_state',2)
  83. ->whereIn('pay_type',[1,2])
  84. ->first();
  85. // 预约
  86. $yyOrder = DB::table($oldOrderTableName)
  87. ->select(DB::raw('sum(money) as total_money'))
  88. ->where('store_id',$storeId)
  89. ->where('type',3)
  90. ->where('yy_state',3)
  91. ->whereIn('pay_type',[1,2])
  92. ->first();
  93. // 提现
  94. $txTotal = DB::table($oldWithdrawalTableName)
  95. ->select(DB::raw('sum(tx_cost) as total'))
  96. ->where('store_id',$storeId)
  97. ->whereIn('state',[1,2])
  98. ->first();
  99. // 商家奖励
  100. $awardTotal = empty($value->award_money) ? 0 : $value->award_money;
  101. // 商户配送手续费
  102. $psMoney = number_format(floatval($wmOrder->ps_money)*floatval($value->ps_poundage)/100,2,'.','');
  103. // 不计算拼团和抢购的订单,目前没有这样的订单,在另外的订单表
  104. // 计算 金额 = 奖励 + 订单(外卖+店内+当面+预约) - 手续费 - 提现 - 配送费
  105. $balance = $awardTotal
  106. + ($wmOrder->total_money + $wmOrder->hb_money + $dnOrder->total_money + $dnOrder->hb_money + $dmOrder->total_money + $yyOrder->total_money )
  107. - ((
  108. ($wmOrder->total_money + $wmOrder->ps_money + $wmOrder->hb_money) * floatval($poundage->poundage)
  109. + ($dnOrder->total_money + $dnOrder->hb_money ) * floatval($poundage->dn_poundage)
  110. + $dmOrder->total_money * floatval($poundage->dm_poundage)
  111. + $yyOrder->total_money * floatval($poundage->yd_poundage)
  112. )/100)
  113. - $txTotal->total
  114. - $psMoney;
  115. // $newData[$storeId] = number_format($balance,2,'.','');
  116. }
  117. return number_format($balance,2,'.','');
  118. }
  119. /**
  120. * Execute the console command.
  121. * 单个店铺处理
  122. * @return int
  123. */
  124. public function handle()
  125. {
  126. $oldStoreTableName = 'ims_cjdc_store';
  127. $newBalanceTableName = 'lanzu_user_balance';
  128. $store_id = 2 ; // 店铺id
  129. $balance = $this->getBalanceById($store_id);
  130. // var_dump(number_format($balance,2,'.',''));
  131. // return ;
  132. $res = 1;
  133. $userId = 0;// 店铺的提现用户id
  134. $storeInfo = DB::table($oldStoreTableName)->select('user_id')->find($store_id);
  135. if(empty($storeInfo) || empty($storeInfo->user_id)){
  136. $res = ['store_id'=>$store_id,'balance'=>$balance,'msg'=>'店铺的用户不存在'];
  137. var_dump($res);
  138. return ;
  139. }
  140. $userId = $storeInfo->user_id;
  141. $exist = DB::table($newBalanceTableName)->where('source_id',$userId)->where('user_type',5)->exists();
  142. if($exist){
  143. $res = ['store_id'=>$store_id,'balance'=>$balance,'msg'=>'已存在'];
  144. }else{
  145. $saveData = [
  146. 'source_id' => $userId,
  147. 'user_type' => 5,
  148. 'balance' => $balance,
  149. 'created_at' => time(),
  150. 'updated_at' => time()
  151. ];
  152. // var_dump($saveData);
  153. $res = DB::table($newBalanceTableName)->insert($saveData);
  154. if(!$res){
  155. $res = ['store_id'=>$store_id,'user_id'=>$userId,'balance'=>$balance,'msg'=>'添加失败'];
  156. }
  157. };
  158. var_dump($res);
  159. return ;
  160. }
  161. /**
  162. * 根据订单进行计算
  163. */
  164. public function countStore($oldAccountTableName)
  165. {
  166. $oldData = DB::table($oldAccountTableName)->orderBy('id','asc')->get();
  167. $newData = [];
  168. foreach ($oldData as $key => $value){
  169. $storeId = $value->store_id;
  170. if(array_key_exists($storeId,$newData)){
  171. if($value->type == 2){
  172. $newData[$storeId] -= $value->money;
  173. }else if($value->type == 1){
  174. $newData[$storeId] += $value->money;
  175. }
  176. }else{
  177. $newData[$storeId] = $value->money;
  178. }
  179. }
  180. return $newData;
  181. }
  182. /**
  183. * 根据订单进行计算
  184. */
  185. public function countOrder()
  186. {
  187. $oldStoreTableName = 'ims_cjdc_store';
  188. $oldStoreSetTableName = 'ims_cjdc_storeset';
  189. $oldStoreTypeTableName = 'ims_cjdc_storetype';
  190. $oldOrderTableName = 'ims_cjdc_order';
  191. $oldWithdrawalTableName = 'ims_cjdc_withdrawal';
  192. $newData = [];
  193. // 查询所有店铺
  194. $stores = DB::table($oldStoreTableName)->select('id','md_type','ps_poundage','award_money')->where('id',)->get();
  195. //店铺设置
  196. $storeSets = DB::table($oldStoreSetTableName)->select('id','store_id','is_poundage','poundage','dn_poundage','dm_poundage','yd_poundage')->get()->toArray();
  197. $storeSetArr = array_column($storeSets,null,'store_id');
  198. // 所有商家分类
  199. $storeTypes = DB::table($oldStoreTypeTableName)->get()->toArray();
  200. $storeTypeArr = array_column($storeTypes,null,'id');
  201. foreach($stores as $key => $value){
  202. $storeId = $value->id;
  203. // 获取商家手续费 % 如果商家设置有自己的手续费则用商家的
  204. if(isset($storeSetArr[$storeId]) && $storeSetArr[$storeId]->is_poundage == 1){
  205. $poundage = $storeSetArr[$storeId];
  206. }else{
  207. $poundage = $storeTypeArr[$value->md_type];
  208. }
  209. // 商家订单金额
  210. // type '1.外卖2.店内3.预定4.当面付',
  211. // pay_type '1.微信支付2.余额支付3.积分支付4.货到付款',
  212. // state '1.待付款2.待结单3.等待送达4.完成5.已评价6.取消7.拒绝8.退款中9.已退款10.退款拒绝',
  213. // 外卖
  214. $wmOrder = DB::table($oldOrderTableName)
  215. ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(ps_money) as ps_money'),DB::raw('sum(yhq_money2) as hb_money'))
  216. ->where('store_id',$storeId)
  217. ->where('type',1)
  218. ->whereIn('state',[4,5,10])
  219. ->whereIn('pay_type',[1,2])
  220. ->first();
  221. // 店内
  222. $dnOrder = DB::table($oldOrderTableName)
  223. ->select(DB::raw('sum(money) as total_money'),DB::raw('sum(yhq_money2) as hb_money'))
  224. ->where('store_id',$storeId)
  225. ->where('type',2)
  226. ->where('dn_state',2)
  227. ->whereIn('pay_type',[1,2])
  228. ->first();
  229. // 当面
  230. $dmOrder = DB::table($oldOrderTableName)
  231. ->select(DB::raw('sum(money) as total_money'))
  232. ->where('store_id',$storeId)
  233. ->where('type',4)
  234. ->where('dm_state',2)
  235. ->whereIn('pay_type',[1,2])
  236. ->first();
  237. // 预约
  238. $yyOrder = DB::table($oldOrderTableName)
  239. ->select(DB::raw('sum(money) as total_money'))
  240. ->where('store_id',$storeId)
  241. ->where('type',3)
  242. ->where('yy_state',3)
  243. ->whereIn('pay_type',[1,2])
  244. ->first();
  245. // 提现
  246. $txTotal = DB::table($oldWithdrawalTableName)
  247. ->select(DB::raw('sum(tx_cost) as total'))
  248. ->where('store_id',$storeId)
  249. ->whereIn('state',[1,2])
  250. ->first();
  251. // 商家奖励
  252. $awardTotal = empty($value->award_money) ? 0 : $value->award_money;
  253. // 商户配送手续费
  254. $psMoney = number_format(floatval($wmOrder->ps_money)*floatval($value->ps_poundage)/100,2,'.','');
  255. // 不计算拼团和抢购的订单,目前没有这样的订单,在另外的订单表
  256. // 计算 金额 = 奖励 + 订单(外卖+店内+当面+预约) - 手续费 - 提现 - 配送费
  257. $balance = $awardTotal
  258. + ($wmOrder->total_money + $wmOrder->hb_money + $dnOrder->total_money + $dnOrder->hb_money + $dmOrder->total_money + $yyOrder->total_money )
  259. - ((
  260. ($wmOrder->total_money + $wmOrder->ps_money + $wmOrder->hb_money) * floatval($poundage->poundage)
  261. + ($dnOrder->total_money + $dnOrder->hb_money ) * floatval($poundage->dn_poundage)
  262. + $dmOrder->total_money * floatval($poundage->dm_poundage)
  263. + $yyOrder->total_money * floatval($poundage->yd_poundage)
  264. )/100)
  265. - $txTotal->total
  266. - $psMoney;
  267. $newData[$storeId] = $balance;
  268. }
  269. return $newData;
  270. }
  271. /**
  272. * 旧的处理方式
  273. */
  274. public function old()
  275. {
  276. $oldAccountTableName = 'ims_cjdc_store_account';
  277. $newBalanceTableName = 'lanzu_user_balance';
  278. // 判断表是否存在
  279. if(!Schema::hasTable($oldAccountTableName)){
  280. var_dump('旧表不存在');
  281. return 0;
  282. }
  283. if(!Schema::hasTable($newBalanceTableName)){
  284. var_dump('新表不存在');
  285. return 0;
  286. }
  287. // 流水算法
  288. $newData = $this->countStore($oldAccountTableName);
  289. // 订单算法
  290. // $newData = $this->countOrder();
  291. var_dump($newData);
  292. var_dump(count($newData));
  293. return 0;
  294. $storeList = DB::table($newBalanceTableName)->pluck('user_id','id');
  295. $bar = $this->output->createProgressBar(count($newData));
  296. $bar->start();
  297. $startTime = time();
  298. $error = [];
  299. foreach($newData as $store_id => $money){
  300. $userId = 0;
  301. if(!isset($storeList[$store_id])){
  302. $error[] = ['store_id'=>$store_id,'money'=>$money,'msg'=>'店铺的用户不存在'];
  303. }
  304. $exist = DB::table($newBalanceTableName)->where('source_id',$userId)->where('user_type',5)->exists();
  305. if($exist){
  306. $error[] = ['store_id'=>$store_id,'money'=>$money,'msg'=>'已存在'];
  307. // $saveData = [
  308. // 'balance' => number_format($money,2,'.',''),
  309. // 'updated_at' => time()
  310. // ];
  311. // $res = DB::table($newBalanceTableName)->where('source_id',$store_id)->where('user_type',5)->increment('balance',$saveData['balance']);
  312. // if(!$res){
  313. // $error[] = ['store_id'=>$store_id,'money'=>$money];
  314. // }
  315. }else{
  316. $saveData = [
  317. 'source_id' => $userId,
  318. 'user_type' => 5,
  319. 'balance' => number_format($money,2,'.',''),
  320. 'updated_at' => time()
  321. ];
  322. // $res = DB::table($newBalanceTableName)->insert($saveData);
  323. // if(!$res){
  324. // $error[] = ['store_id'=>$store_id,'money'=>$money];
  325. // }
  326. }
  327. $bar->advance();
  328. }
  329. $bar->finish();
  330. var_dump([time()-$startTime]);
  331. var_dump($error);
  332. return 0;
  333. }
  334. }