<?php

namespace app\command;

use app\model\Member;
use app\model\MemberAccount;
use app\model\Order;
use app\model\PayDetail;
use support\Db;
use support\Log;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class WelfareAccountCommand extends Command
{

    protected static $defaultName = 'WelfareAccountCommand';
    protected static $defaultDescription = 'WelfareAccountCommand';

    /**
     * @return void
     */
    protected function configure()
    {
        // $this->addArgument('name', InputArgument::OPTIONAL, '余额账户转福利账户');
        $this->addArgument('name', InputArgument::OPTIONAL, '福利账户余额清空');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $accounts = MemberAccount::where('member_account_classify','WELFARE')->get()->toArray();
        Db::beginTransaction();
        try{
            foreach($accounts as $account){
                if ($account['member_account_income'] == '0.00' || $account['member_account_surplus'] == '0.00'){
                    echo "会员【" . $account['join_account_member_id'] . "】跳过\n";
                    continue;
                }
                // 更新余额账户
                MemberAccount::where('member_account_id',$account['member_account_id'])->update([
                    'member_account_expend' => $account['member_account_expend'] + $account['member_account_surplus'],
                    'member_account_surplus' => 0
                ]);

                // 插入清零记录
                $this->generatePayDetail($account);
                // 记录日志
                Log::info("会员【" . $account['join_account_member_id'] . "】清除福利已完成",$account);

                echo "会员【" . $account['join_account_member_id'] . "】已处理完成\n";
            }
            Db::commit();

            return self::SUCCESS;
        }catch(\Exception $e){
            dump($e->getMessage());

            Db::rollBack();
            return self::SUCCESS;
        }
    }

    /**
     * 生成清零记录
     */
    private function generatePayDetail($account)
    {
        $payDetailData = [
            'join_pay_member_id'=>$account['join_account_member_id'],
            'pay_status'=>'SUCCESS',
            'pay_category'=>'CLEAR',
            'pay_amount'=>$account['member_account_surplus'],
            'pay_paytimes'=>date('Y-m-d H:i:s'),
            'pay_prepayid'=>$account['member_account_nbr'],
            'pay_addtimes'=>time()
        ];
        
        PayDetail::insert($payDetailData);
    }

    // protected function execute(InputInterface $input, OutputInterface $output): int
    // {
    //     $members = Member::select('member_id', 'member_classify')->get()->toArray();
    //     foreach ($members as $member) {
    //         if (!MemberAccount::where('join_account_member_id', $member['member_id'])->where('member_account_classify', 'WELFARE')->exists()) {
    //             if (!$this->generateWelfareAccount($member['member_id'])) {
    //                 echo "会员【" . $member['member_id'] . "】跳过\n";
    //                 continue;
    //             }
    //         }
    //         if ($member['member_classify'] != 'EMPLOY') {
    //             Member::where('member_id', $member['member_id'])->update(['member_classify' => 'EMPLOY']);
    //         }
    //         // 改所有的充值记录
    //         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']);

    //         // 改所有的付款记录
    //         PayDetail::where('join_pay_member_id', $member['member_id'])->where('pay_prepayid', $member['member_id'] . '-CASH')->update(['pay_prepayid' => $member['member_id'] . '-WELFARE']);

    //         // 余额账户清空
    //        MemberAccount::where('join_account_member_id', $member['member_id'])->where('member_account_classify', 'CASH')->update([
    //            'member_account_income' => 0,
    //            'member_account_expend' => 0,
    //            'member_account_surplus' => 0,
    //            'member_account_added' => 0,

    //        ]);


    //         echo "会员【" . $member['member_id'] . "】已处理完成\n";
    //     }

    //     return self::SUCCESS;
    // }

    private function generateWelfareAccount($memberId)
    {
        $cashAccount = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'CASH')->first();
        if (!$cashAccount) {
            return false;
        }

        MemberAccount::insert([
            'join_account_member_id' => $memberId,
            'member_account_classify' => 'WELFARE',
            'member_account_status' => 'ACTIVED',
            'member_account_category' => 'NORMAL',
            'member_account_nbr' => $memberId . '-WELFARE',
            'member_account_name' => '福利账户',
            'member_account_income' => $cashAccount->member_account_income,
            'member_account_expend' => $cashAccount->member_account_expend,
            'member_account_surplus' => $cashAccount->member_account_surplus,
            'member_account_added' => $cashAccount->member_account_added,
            'member_account_addtimes' => time()
        ]);

        return true;
    }
}