WelfareAccountCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\command;
  3. use app\model\Member;
  4. use app\model\MemberAccount;
  5. use app\model\Order;
  6. use app\model\PayDetail;
  7. use support\Db;
  8. use Symfony\Component\Console\Command\Command;
  9. use Symfony\Component\Console\Input\InputArgument;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. class WelfareAccountCommand extends Command
  13. {
  14. protected static $defaultName = 'WelfareAccountCommand';
  15. protected static $defaultDescription = 'WelfareAccountCommand';
  16. /**
  17. * @return void
  18. */
  19. protected function configure()
  20. {
  21. // $this->addArgument('name', InputArgument::OPTIONAL, '余额账户转福利账户');
  22. $this->addArgument('name', InputArgument::OPTIONAL, '福利账户余额清空');
  23. }
  24. protected function execute(InputInterface $input, OutputInterface $output): int
  25. {
  26. $accounts = MemberAccount::where('member_account_classify','WELFARE')->get()->toArray();
  27. Db::beginTransaction();
  28. try{
  29. foreach($accounts as $account){
  30. if ($account['member_account_income'] == '0.00'){
  31. echo "会员【" . $account['join_account_member_id'] . "】跳过\n";
  32. continue;
  33. }
  34. // 更新余额账户
  35. MemberAccount::where('member_account_id',$account['member_account_id'])->update([
  36. 'member_account_expend' => $account['member_account_expend'] + $account['member_account_surplus'],
  37. 'member_account_surplus' => 0
  38. ]);
  39. // 插入清零记录
  40. $this->generatePayDetail($account);
  41. echo "会员【" . $account['join_account_member_id'] . "】已处理完成\n";
  42. }
  43. Db::commit();
  44. return self::SUCCESS;
  45. }catch(\Exception $e){
  46. dump($e->getMessage());
  47. Db::rollBack();
  48. }
  49. }
  50. /**
  51. * 生成清零记录
  52. */
  53. private function generatePayDetail($account)
  54. {
  55. $payDetailData = [
  56. 'join_pay_member_id'=>$account['join_account_member_id'],
  57. 'pay_status'=>'SUCCESS',
  58. 'pay_category'=>'CLEAR',
  59. 'pay_amount'=>$account['member_account_surplus'],
  60. 'pay_paytimes'=>date('Y-m-d H:i:s'),
  61. 'pay_prepayid'=>$account['member_account_nbr'],
  62. 'pay_addtimes'=>time()
  63. ];
  64. PayDetail::insert($payDetailData);
  65. }
  66. // protected function execute(InputInterface $input, OutputInterface $output): int
  67. // {
  68. // $members = Member::select('member_id', 'member_classify')->get()->toArray();
  69. // foreach ($members as $member) {
  70. // if (!MemberAccount::where('join_account_member_id', $member['member_id'])->where('member_account_classify', 'WELFARE')->exists()) {
  71. // if (!$this->generateWelfareAccount($member['member_id'])) {
  72. // echo "会员【" . $member['member_id'] . "】跳过\n";
  73. // continue;
  74. // }
  75. // }
  76. // if ($member['member_classify'] != 'EMPLOY') {
  77. // Member::where('member_id', $member['member_id'])->update(['member_classify' => 'EMPLOY']);
  78. // }
  79. // // 改所有的充值记录
  80. // PayDetail::where('join_pay_member_id', $member['member_id'])->where('pay_category', 'RECHARGE')->where('pay_amount','1000.00')->update(['pay_prepayid' => $member['member_id'] . '-WELFARE']);
  81. // // 改所有的付款记录
  82. // PayDetail::where('join_pay_member_id', $member['member_id'])->where('pay_prepayid', $member['member_id'] . '-CASH')->update(['pay_prepayid' => $member['member_id'] . '-WELFARE']);
  83. // // 余额账户清空
  84. // MemberAccount::where('join_account_member_id', $member['member_id'])->where('member_account_classify', 'CASH')->update([
  85. // 'member_account_income' => 0,
  86. // 'member_account_expend' => 0,
  87. // 'member_account_surplus' => 0,
  88. // 'member_account_added' => 0,
  89. // ]);
  90. // echo "会员【" . $member['member_id'] . "】已处理完成\n";
  91. // }
  92. // return self::SUCCESS;
  93. // }
  94. private function generateWelfareAccount($memberId)
  95. {
  96. $cashAccount = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'CASH')->first();
  97. if (!$cashAccount) {
  98. return false;
  99. }
  100. MemberAccount::insert([
  101. 'join_account_member_id' => $memberId,
  102. 'member_account_classify' => 'WELFARE',
  103. 'member_account_status' => 'ACTIVED',
  104. 'member_account_category' => 'NORMAL',
  105. 'member_account_nbr' => $memberId . '-WELFARE',
  106. 'member_account_name' => '福利账户',
  107. 'member_account_income' => $cashAccount->member_account_income,
  108. 'member_account_expend' => $cashAccount->member_account_expend,
  109. 'member_account_surplus' => $cashAccount->member_account_surplus,
  110. 'member_account_added' => $cashAccount->member_account_added,
  111. 'member_account_addtimes' => time()
  112. ]);
  113. return true;
  114. }
  115. }