<?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;
use Biz\Account\AccountManager;

class MoneyRecharge extends Model {

    protected $table = 'money_recharge';

    //晋级充值统计

    /**
     * @param array $params
     * @param $time 上传统计时间
     * @return int
     */
    function getPromotionAmount($params=array(),$time=''){
    	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']);
        $save = lm ('MoneyRecharge', 'Api')->where ('account_identity', $params['account_identity'])->whereIn ('recharge_type', ['汇款','后台充值','在线充值'])->where ('status', 1);
        $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';
            $save = $save->where ('complete_time', '>=', $startime)->where ('complete_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';
            $save = $save->where ('complete_time', '>=', $startime)->where ('complete_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);
            $save = $save->where ('complete_time', '>=', $sunday)->where ('complete_time', '<=', $strday);
        }elseif ($gradeinfo['rule_details'][0] == 'day') {
        	$firstday = date ('Y-m-d 00:00:00', strtotime ("-" . $gradeinfo['rule_details'][3] . " day"));
            $save = $save->where ('complete_time', '>=', $firstday)->where ('complete_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';
            $save = $save->where ('complete_time', '>=', $firstday);
        }

        if(!empty($uservip)){
            $uservip = $uservip->toArray();
            if(!empty($uservip['audittime'])){
                $save = $save->where ('complete_time', '>=', $uservip['audittime']);
            }
        }
        $save=$time?$save->where ('complete_time', '>', $time):$save;
        $save = $save->sum ('money');

        return $save;
    }

    /**
     * 充值记录
     *
     * @access public
     * @return String
     */
    public function rechargeRecord()
    {
        // 获取用户信息
        $accountManagerClass = new AccountManager;
        $userInfo = $accountManagerClass -> getCurrentUser();
        if (empty($userInfo['identity'])) {
            Render([], '2001', lang('Common','Api') -> get('user does login'));
        }
        $rechargeRecordWhere = array();
        // 用户ID
        $rechargeRecordWhere['account_identity'] = $userInfo['identity'];
        $rechargeRecordWhere['status'] = 1;
        // 时间区间
        $startTime = isset($_POST['startTime'])? $_POST['startTime'] : '1900-01-01 00:00:00';
        $endTime = isset($_POST['endTime'])? $_POST['endTime'] : '3000-12-12 00:00:00';
        $rechargeRecordBetween = [$startTime, $endTime];
        // 当前页
        $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
        // 分页大小
        $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
        // 起始查询位置
        $begin = ($currentPage - 1) * $pageSize;
        $rechargeRecord = lm('money_recharge', "commons");
        $rechargeRecordSelect = ['id', 'info_identity', 'order_id', 'apply_time', 'money', 'recharge_type', 'money_cash'];
        // 获取盈亏记录
        $result['list'] = $rechargeRecord -> rechargeRecord($rechargeRecordSelect, $rechargeRecordWhere, $rechargeRecordBetween, $begin, $pageSize);
        // 获取记录总数
        $result['total'] = $rechargeRecord -> rechargeRecordTotal($rechargeRecordWhere, $rechargeRecordBetween);
        // 总页数计算
        $result['countPage'] = ceil($result['total'] / $pageSize);
        $result['currentPage'] = $currentPage;

        return $result;
    }

    /**
     * 取款记录
     *
     * @access public
     * @return String
     */
    public function moneyOutRecord()
    {
        // 获取用户信息
        $accountManagerClass = new AccountManager;
        $userInfo = $accountManagerClass -> getCurrentUser();
        if (empty($userInfo['identity'])) {
            Render([], '2001', lang('Common','Api') -> get('user does login'));
        }
        $moneyTakeWhere = array();
        // 用户ID
        $moneyTakeWhere['account_identity'] = $userInfo['identity'];
        // 时间区间
        $startTime = isset($_POST['startTime'])? $_POST['startTime'] : '1900-01-01 00:00:00';
        $endTime = isset($_POST['endTime'])? $_POST['endTime'] : '3000-12-12 00:00:00';
        $moneyTakeBetween = ['apply_time' => [$startTime, $endTime]];
        // 当前页
        $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
        // 分页大小
        $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
        // 起始查询位置
        $begin = ($currentPage - 1) * $pageSize;
        $moneyTake = lm('Money_take', "commons");
        $moneyTakeSelect = ['id', 'account_name', 'money', 'apply_time', 'pass_time', 'give_time', 'bank_no', 'bank_info', 'status', 'money_cash'];
        // 获取提款记录
        $result['list'] = $moneyTake -> moneyTake($moneyTakeSelect, $moneyTakeWhere, $moneyTakeBetween, $begin, $pageSize);
        // 获取记录总数
        $result['total'] = $moneyTake -> moneyTakeTotal($moneyTakeWhere, $moneyTakeBetween);
        // 总页数计算
        $result['countPage'] = ceil($result['total'] / $pageSize);
        $result['currentPage'] = $currentPage;

        return $result;
    }

}
