<?php
/**
 * Created by PhpStorm.
 * User: ikeke
 * Date: 2018/12/4
 * Time: 18:24
 */

namespace App\Models;


class Kygame_transfer_record extends BaseModel
{
    protected $table = "kygame_transfer_record";
    public $timestamps = false;

    /**
     * 获取所有游戏记录
     * @param string $where
     * @param int $limit
     * @return mixed
     */
    public function recordlist($where='',$limit=15)
    {
        $data = $this->orderBy('id','desc');

        if(is_array($where)&&count($where)>0){
            $data = $data->where($where);
        }

        $data=$data->paginate($limit);

        return $data->toArray();
    }

    /**
     * 更新转账状态
     */
    public function setTransferCredit($where='',$d)
    {

        $res = $this->where($where)->update($d);

        if(!$res)
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }

    /**
     * 转账记录统计
     * @param $where
     * @return array
     */
    public function getSum($where)
    {
        $arr = array();
        if(is_array($where)&&count($where)>0)
        {
            //查询总投注金额
            $sum = $this->where($where)->sum('transfer_money');
            //查询转账数
            $count = $this->where($where)->count();
            //查询转账人数
            $person = $this->where($where)->select('local_user')->distinct()->get()->toArray();
            //查询余额转入到游戏总金额
            $in = $this->where($where)->where('transfer_type', 1)->sum('transfer_money');
            //查询游戏转出到余额总金额
            $out = $this->where($where)->where('transfer_type', 2)->sum('transfer_money');
        }

        $arr['total'] = $sum;
        $arr['count'] = $count;
        $arr['person'] = count($person);
        $arr['in'] = $in;
        $arr['out'] = $out;

        return $arr;
    }
}