<?php

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class InterestTreasure extends Command
{
    protected $outputInfo;
    protected function configure()
    {
        // 指令配置
        $this->setName('InterestTreasure');
        // 设置参数
        
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令执行
        $this->outputInfo = $output;
        $this->closeAccount();
        
    }

    public function closeAccount()
    {
        $this->outputInfo->writeln('开始结算');
        try {
            //逻辑代码
            $this->yebeveryday();
        } catch (\Exception $exception) {
            //Log::info($exception->getMessage());
            var_dump($exception->getTrace());
            return ;
        }
    }

    public function yebeveryday()
    {
        $nowtime = time();
        $keepnum = 0;
        $closenum = 0;
        $getdoings = Db::table('lc_yuebao_lists')->where('status=1 and profit_time<=' . (time() - 86400))->select();
        foreach ($getdoings as $getdoing) {
            try {
                $new_profit_time = $nowtime;
                if ($new_profit_time >= $getdoing['end_time']) {
                    $new_profit_time = $getdoing['end_time'];
                }
                $seconds = floor(($new_profit_time - $getdoing['profit_time']) / 86400);
                $nowprift = ($getdoing['money'] * $getdoing['lily'] / 100) * $seconds;
                $profit = $getdoing['profit'] + $seconds;
                $id = $getdoing['id'];
                var_dump($seconds);
                var_dump($getdoing['profit_time'] + $seconds * 86400);
                if ($nowprift > 0) {
                    Db::table('lc_yuebao_lists')->where('id='. $getdoing['id'])
                        ->update([
                            'profit_time' => $getdoing['profit_time'] + $seconds * 86400,
                            'nowprofit' => $getdoing['nowprofit'] + $nowprift,
                            'profit' => $getdoing['profit'] + $seconds
                        ]);
                    $keepnum = $keepnum + 1;
                    //获取用户余额；
                    addFinance($getdoing['uid'], $nowprift, 1, "余额宝利息返利");
                    $getuserinfo = Db::table('lc_user')->where('id=' . $getdoing['uid'])->find();
                    //记录日志！
                    unset($getdoing['id']);
                    unset($getdoing['profit_time']);
                    unset($getdoing['profit']);
                    $getdoing['status'] = 2;
                    $getdoing['nowprofit'] = $nowprift;
                    $getdoing['balance'] = $getuserinfo['money'];
                    $getdoing['closetime'] = time();
                    $getdoing['remarks'] = "自动结算";
                    Db::table('lc_yuebao_log')->insert($getdoing);
                    //更新用户余额
                    $newbalance = $getuserinfo['money'] + $nowprift;
                    Db::table('lc_user')->where('id=' . $getdoing['uid'])->update(['money' => $newbalance]);
                }
                //第二步，已到期待结算
                if ($profit >= $getdoing['days']) {
                    //更新参保状态。
                    Db::table('lc_yuebao_lists')->where('id=' . $id)
                        ->update(['status' => 2]);
                    //获取用户余额；
                    addFinance($getdoing['uid'], $getdoing['money'], 1, "余额宝返还本金");
                    $getuserinfo = Db::table('lc_user')->where('id=' . $getdoing['uid'])->find();
                    //更新用户余额
                    $newbalance = $getuserinfo['money'] + $getdoing['money'];
                    Db::table('lc_user')->where('id=' . $getdoing['uid'])->update(['money' => $newbalance]);
                    $getuc = Db::table('lc_yuebao_uc')->where('uid=' . $getdoing['uid'])->find();
                    Db::table('lc_yuebao_uc')->where('uid=' . $getdoing['uid'])->update(['balance' => $getuc['balance'] - $getdoing['money']]);
                    $saveuclog = array(
                        'uid' => $getdoing['uid'],
                        'balance' => $getuc['balance'],
                        'money' => "-" . $getdoing['money'],
                        'addtime' => time(),
                        'remarks' => $getdoing['yebtitle'] . "到期结算"
                    );
                    Db::table('lc_yuebao_uclog')->insert($saveuclog);
                    $closenum = $closenum + 1;
                }
            }catch (\Exception $e){
                var_dump($e->getTraceAsString());
                $this->outputInfo->error("结算失败，ID: " . $getdoing['id'] . " 错误信息: " . $e->getMessage());
            }
        }
        $this->outputInfo->writeln('结算成功：' . $keepnum . '条，到期：' . $closenum . '条');
    }
}
