WelfareAccountCommand.php 5.4 KB

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