<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function _initialize()
    {
        $this->recordModel= new \app\admin\model\greenroom\Record;
        $this->adminModel= new \app\admin\model\Admin;
        $this->userlinkModel= new \app\admin\model\greenroom\Userlink;
    }
    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 跑链接收益
     *
     */
    public function runrecord()
    {

        $userlink = db('userlink')
            ->join(['fa_link'], 'fa_userlink.link_id = fa_link.id')
            ->field(
                "fa_link.link as link,
                    fa_link.id as link_id,
                    fa_link.minincome as minincome,
                    fa_link.maxincome as maxincome
            ")->group("link_id")->select();

        $record=[];
        $nowtime =date("Y-m-d h:i:s");
        $unixtime=time();
        foreach ($userlink as $key=>$val){
            $income = mt_rand ($val['minincome']*10000,$val['maxincome']*10000)/10000;
            $record[$val['link_id']]=[
                'link'=>$val['link'],
                'time'=>$nowtime,
                'income'=>$income
            ];
        }
        if($record){
            $this->recordModel->saveAll($record,true);
        }
        $where=['incomeendtime'=>['gt',$unixtime]];
        $usedata = db('admin')->field("money,id")->where($where)->select();
        $buylink = db('userlink')->field("id,uid,link_id,income,dayincome,oldincome")->select();
        $addincome=$linkincome=[];
        foreach ($buylink as $key=>$val){
            $addincome[$val['uid']]=isset($addincome[$val['uid']])?$addincome[$val['uid']]+$record[$val['link_id']]['income']:$record[$val['link_id']]['income'];

            $linkincome[$val['uid']][$val['id']]['income']=$val['income']+$record[$val['link_id']]['income'];
            $linkincome[$val['uid']][$val['id']]['dayincome']=$val['dayincome']+$record[$val['link_id']]['income'];

        }
        $upincome=[];
        $upuserlink=[];
        foreach ($usedata as $k=>$vl){
            if(isset($addincome[$vl['id']])){
                $upincome[]=['id'=>$vl['id'],
                    'money'=>$vl['money']+$addincome[$val['uid']]
                ];
            }
            foreach ($linkincome[$val['uid']] as $key=>$val){
                $upuserlink[$key]=$val;
                $upuserlink[$key]['id']=$key;
                $upuserlink[$key]['endtime']=$unixtime;

            }
            if($upincome){
                $this->adminModel->saveAll($upincome);
                $this->userlinkModel->saveAll($upuserlink);
            }

        }
    }


    public function oldrecord()
    {
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $userlink = db('userlink')->select();
        $uplink=[];
        foreach ($userlink as $key=>$val){
            if($val['dayincome']>0){
                $uplinktemp=[
                    'id'=>$val['id'],
                    'dayincome'=>0,
                    'oldincome'=>$val['dayincome'],
                ];
                if($val['endtime']<$beginYesterday){
                    $uplinktemp['oldincome']=0;
                }
                $uplink[]= $uplinktemp;
            }
        }
        $this->userlinkModel->saveAll($uplink);
    }

}
