<?php
/**
 *------Create thems Model------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-12 05:08:18------
 */
namespace App\Api\Model;
use \System\Model;

class SixmoneyBuy extends Model {

    protected $table = 'sixmoney_buy';

    //晋级流水统计
    function getsixstreamAmount($params=array(),$startime='',$endtime=''){
    	if(empty($params['account_identity'])){
    		return -51005;
    	}
    	$gradeinfo = lm ('UserGrade', 'Api')->select('name', 'rule', 'rule_details')->where('grade', '=', 0)->first();
        if (empty($gradeinfo)) {
            return -5002;
        }
        $gradeinfo = $gradeinfo->toArray();
        $gradeinfo['rule_details'] = explode (',', $gradeinfo['rule_details']);
        $sixbet = lm ('SixmoneyBuy', 'Api')->where ('account_identity', $params['account_identity'])->where ('status', '<', 4)->where ('game_status', '>', 0);
        $uservip = lm ('User_vip', 'Api')->select('audittime')->where('account_identity',$params['account_identity'])->first();
        if ($gradeinfo['rule_details'][0] == 'year') {
        	$startime = date ('Y-01-01', time ()) . ' 00:00:00';
            $endtime = date ('Y-12-31', time ()) . ' 23:59:59';
            $sixbet = $sixbet->where ('money_time', '>=', $startime)->where ('money_time', '<=', $endtime);
        }elseif ($gradeinfo['rule_details'][0] == 'month') {
        	$startime = date ('Y-m-01', strtotime (date ('Y', time ()) . '-' . (date ('m', time ()) - 1) . '-01')) . ' 00:00:00';
            $endtime = date ('Y-m-d', strtotime ("$startime +1 month -1 day")) . ' 23:59:59';
            $sixbet = $sixbet->where ('money_time', '>=', $startime)->where ('money_time', '<=', $endtime);
        }elseif ($gradeinfo['rule_details'][0] == 'week') {
        	$now_day = date ('W', time ()) - 1;
            $sunday_str = time () - $now_day * 60 * 60 * 24;
            $sunday = date ('Y-m-d 00:00:00', $sunday_str);
            $strday_str = time () + (6 - $now_day) * 60 * 60 * 24;
            $strday = date ('Y-m-d 23:59:59', $strday_str);
            $sixbet = $sixbet->where ('money_time', '>=', $sunday)->where ('money_time', '<=', $strday);
        }elseif ($gradeinfo['rule_details'][0] == 'day') {
        	$firstday = date ('Y-m-d 00:00:00', strtotime ("-" . $gradeinfo['rule_details'][3] . " day"));
            $sixbet = $sixbet->where ('money_time', '>=', $firstday)->where ('money_time', '<=', date ('Y-m-d 23:59:59', strtotime ('-1 day')));
        }elseif ($gradeinfo['rule_details'][0] == 'ymd') {
        	$firstday = $gradeinfo['rule_details'][3] . ' 00:00:00';
            $sixbet = $sixbet->where ('money_time', '>=', $firstday);
        }
        if(!empty($uservip)){
        	$uservip = $uservip->toArray();
        	if(!empty($uservip['audittime'])){

        		$sixbet = $sixbet->where ('money_time', '>=', $uservip['audittime']);

        	}
        }
        $sixbet=$startime?$sixbet->where ('money_time', '>=', $startime):$sixbet;
        $sixbet=$endtime?$sixbet->where ('money_time', '<', $endtime):$sixbet;
        $sixbet = $sixbet->sum ('money');

        return $sixbet;

    }
}
