<?php

namespace App\Models;

use DB;

class Money_recharge extends BaseModel
{
    protected $table = "money_recharge";
    public $timestamps = false;

    //获取充值列表
    function getRechargelist($value = '', $type = 1, $page = 10, $sort = 6, $ads = 'desc', $infield = '', $invalue = '', $jointable = '', $whereIn = '', $sql = '')
    {
        $key = $this->getFeild ($type);
        $sort = is_integer ($sort) ? $this->getFeild ($sort) : $sort;
//		DB::connection()->enableQueryLog();
        if (is_array ($value)) {
            $data = $this->where ($value);
        } else {
            $data = $this->where ($key, $value);
        }

        if (!empty($whereIn)) {
            $data = $data->whereIn ('status', $whereIn);
        }
        if (!empty($sql)) {
            $data = $data->whereRaw ($sql);
        }
        if (!empty($jointable) && ($jointable == 'money_recharge_remark')) {
            $data = $data->select ('money_recharge.*', 'account_detailed.group_name', 'money_recharge_remark.name', 'money_recharge_remark.recharge_time');
            $data = $data->leftJoin ('money_recharge_remark', 'money_recharge_remark.order_id', 'money_recharge.order_id');
        } else {
            $data = $data->select ('money_recharge.*', 'account_detailed.group_name');
        }
        $data = $data->join ('account_detailed', 'account_detailed.account_identity', 'money_recharge.account_identity');

        if (!empty($infield) && !empty($invalue)) {
            $data = $data->whereIn ($infield, ['汇款', '汇款赠送']);
        } else {
            $data = $data->whereNotIn ('recharge_type', ['汇款', '汇款赠送']);
        }
        $data = $data->orderby ($sort, $ads)->paginate ($page);
//        $queries = DB::getQueryLog();
//        print_r($queries);
        if (!$data) {
            return -3020000102; //没有列表数据
        }
        return $data->toArray ();
    }

    //获取充值记录条数
    function getCount($value = '', $type = 10)
    {
        $key = $this->getFeild ($type);
        //DB::connection()->enableQueryLog();
        if (!empty($value) && is_array ($value)) {
            $data = $this->where ($value);
        } else if (!empty($value)) {
            $data = $this->where ($key, $value);
        }
        $data = $data->count ();
        //$queries = DB::getQueryLog();
        //print_r($queries);
        return $data;
    }

    //获取充值统计
    function getTotal($where = '', $type = 1, $list = 10, $sort = 6, $ads = 'desc', $page = 1, $having = '', $status = '')
    {
        $data = $this->select (DB::raw ('account_name,recharge_type,money_recharge.account_identity,count("order_id") as betnum,sum("money") as betmoney,money_recharge.status,max("name") as name,max("apply_time") as end_time,min("apply_time") as star_time'))->join ('account_detailed', 'money_recharge.account_identity', 'account_detailed.account_identity')->groupBy ('account_name')->groupBy ('money_recharge.account_identity')->groupBy ('status')->groupBy ('recharge_type');

        if (!empty($where) && is_array ($where) && !empty($having) && is_array ($having)) {
            $data = $data->where ($where);
            foreach ($having as $v) {
                $data = $data->havingRaw ($v);
            }
        } elseif (!empty($having) && is_array ($having)) {
            foreach ($having as $v) {
                $data = $data->havingRaw ($v);
            }
        } elseif (!empty($where) && is_array ($where)) {
            $data = $data->where ($where);
        }
        if (!empty($status)) {
            $nstatus = ($status == 1) ? '0' : (($status == 2) ? 1 : (($status == 3) ? 2 : '0'));
            $status = ($status == 4) ? '0' : $status;
            $c1 = array(array('recharge_type', '=', '在线充值'));
            $c2 = array(array('status', '=', $status));
            $c3 = array(array('recharge_type', '<>', '在线充值'));
            $c4 = array(array('status', '=', $nstatus));
            $data = $data->where (function ($data) use ($c1, $c2, $c3, $c4) {
                $data->where (function ($data) use ($c1, $c2) {
                    $data->where ($c1)->where ($c2);
                })->orwhere (function ($data) use ($c3, $c4) {
                    $data->where ($c3)->where ($c4);
                });
            });
        }
        $data = $data->orderBy ('star_time', 'desc')->paginate ($list);
        if (!$data) {
            return -3020000402; //没有订单统计信息
        } else {
            $data = $data->toArray ();
            $status = trans ('status.money_recharge.status');
            $ostatus = trans ('status.money_recharge.remit_status');
            foreach ($data['data'] as $k => $v) {
                if ($v['recharge_type'] == '在线充值') {
                    $data['data'][$k]['status'] = ($v['status'] == 0) ? $status[4] : $status[$v['status']];
                } else {
                    $data['data'][$k]['status'] = ($v['status'] == 3) ? $ostatus[2] : $ostatus[$v['status']];
                }
                $data['data'][$k]['statuss'] = $v['status'];
            }
            return $data;
        }
    }

    //获取充值订单详情
    function getOrderDetails($value, $type = 1, $jointable = '', $columnn = '', $columnw = '')
    {
        $key = $this->getFeild ($type);
        if (is_array ($value) && !empty($value)) {
            $data = $this->where ($value);
        } else {
            $data = $this->where ($key, $value);
        }
        if ($jointable == 'account_bank') {
            $data = $data->select ('money_recharge.account_name as account', 'order_id as trade_id', 'bank_name', 'account_bank.account_name', 'bank_number', 'bank_address', 'apply_time as money_time', 'money', 'money_recharge.status', 'recharge_type as trade_type', 'reason', 'remark')->join ($jointable, $this->table . '.' . $columnn, $jointable . '.' . $columnw);
        } elseif (!empty($jointable)) {
            $data = $data->join ($jointable, $this->table . '.' . $columnn, $jointable . '.' . $columnw);
        }
        $data = $data->first ();
        if (!$data) {
            return -3020000302; //没有订单信息
        }
        return $data->toArray ();
    }

    //获取充值统计详情
    function getTotalDetails($value, $type = 1, $list = 10, $page = 1, $sort = 6, $ads = 'desc')
    {
        $key = $this->getFeild ($type);
        $sort = is_integer ($sort) ? $this->getFeild ($sort) : $sort;
        if (!is_array ($value)) {
            $data = $this->where ($key, $value)->paginate ($list);
        } else {
            //提现统计详情，条件会员、状态，关联用户详情表
            $data = $this->select ('money_recharge.account_name as account', 'order_id as trade_id', 'bank_name', 'account_bank.account_name', 'bank_number', 'bank_address', 'apply_time as money_time', 'complete_time', 'money', 'money_recharge.status', 'recharge_type as trade_type', 'reason', 'remark')->join ('account_bank', 'money_recharge.account_identity', 'account_bank.account_identity')->where ($value)->orderby ($sort, $ads)->paginate ($list);
        }
        if (!$data) {
            return -3020000502; //没有订单信息
        }
        return $data->toArray ();
    }

    //充值金额汇总统计
    function rechargeMoneyCount($where = '')
    {
        $data = array();
        $data['all_money'] = $this->where ('recharge_type', '<>', 1)->where ('status', '=', 2)->where ('recharge_type', '<>', '汇款')->where ('recharge_type', '<>', '汇款赠送')->sum ('money');
        $data['online_success'] = $this->whereIn ('recharge_type', ['在线充值'])->where ('status', 1)->sum ('money');
        $data['admin_success'] = $this->where ('recharge_type', '后台充值')->where ('status', 1)->sum ('money');
        $data['service_money'] = $this->whereIn ('recharge_type', ['后台充值赠送', '活动充值赠送'])->where ('status', 1)->sum ('money');
        // $data['online_fail'] = $this->where('recharge_type', 1)->where('status', 3)->sum('money');
        $data['admin_fail'] = $this->where ('recharge_type', '活动充值')->where ('status', 1)->sum ('money');
        $data['handing'] = $this->where ('recharge_type', '<>', '汇款')->where ('recharge_type', '<>', '汇款赠送')->where ('status', 1)->sum ('money');
        if (!empty($where) && is_array ($where)) {
            $data['all_money'] = $this->where ($where)->where ('recharge_type', '<>', 1)->where ('status', '<>', 3)->where ('recharge_type', '<>', '汇款')->where ('recharge_type', '<>', '汇款赠送')->sum ('money');
            $data['online_success'] = $this->whereIn ('recharge_type', ['在线充值'])->where ($where)->where ('status', 1)->sum ('money');
            $data['admin_success'] = $this->where ('recharge_type', '后台充值')->where ($where)->where ('status', 1)->sum ('money');
            $data['service_money'] = $this->whereIn ('recharge_type', ['后台充值赠送', '活动充值赠送'])->where ($where)->where ('status', 1)->sum ('money');
            // $data['online_fail'] = $this->where('recharge_type', 1)->where($where)->where('status', 3)->sum('money');
            $data['admin_fail'] = $this->where ('recharge_type', '活动充值')->where ($where)->where ('status', 1)->sum ('money');
            $data['handing'] = $this->where ($where)->where ('recharge_type', '<>', '汇款')->where ('recharge_type', '<>', '汇款赠送')->where ('status', 1)->sum ('money');
        }
        return $data;
    }

    //汇款金额汇总统计
    function sendMoneyCount($where = '')
    {
        $data = array();
        $data['all_money'] = $this->where ('status', '<>', 2)->whereIn ('recharge_type', ['汇款', '汇款赠送'])->sum ('money');
        $data['success_money'] = $this->whereIn ('recharge_type', ['汇款', '汇款赠送'])->where ('status', 1)->sum ('money');
        $data['give_money'] = $this->whereIn ('recharge_type', ['汇款赠送'])->where ('status', 1)->sum ('money');
        $data['fail_money'] = $this->whereIn ('recharge_type', ['汇款'])->where ('status', 0)->sum ('money');
        if (!empty($where) && is_array ($where)) {
            $data['all_money'] = $this->where ($where)->where ('status', '<>', 2)->whereIn ('recharge_type', ['汇款', '汇款赠送'])->sum ('money');
            $data['success_money'] = $this->where ($where)->whereIn ('recharge_type', ['汇款', '汇款赠送'])->where ('status', 1)->sum ('money');
            $data['give_money'] = $this->where ($where)->whereIn ('recharge_type', ['汇款赠送'])->where ('status', 1)->sum ('money');
            $data['fail_money'] = $this->where ($where)->whereIn ('recharge_type', ['汇款'])->where ('status', 0)->sum ('money');
        }

        return $data;
    }

    //字段对应值
    private function getFeild($num)
    {
        $data = array(
            '1' => 'id',
            '2' => 'info_identity',
            '3' => 'order_id',
            '4' => 'account_name',
            '5' => 'account_identity',
            '6' => 'apply_time',
            '7' => 'completetime',
            '8' => 'recharge_type',
            '9' => 'sysetem_user',
            '10' => 'status',
            '11' => 'name',
            '12' => 'recharge_time',
            '13' => 'remark',
        );
        return $data[$num];
    }

    //最后一个存款时间
    function getLast($account_id)
    {
        $time = $this->select ('apply_time', 'money', 'recharge_type', 'account_name')->where ('account_identity', $account_id)->orderBy ('apply_time', 'desc')->first ();
        if (!$time) {
            return array();
        }
        return $time->toArray ();
    }

    /**
     * 添加充值数据
     * @param $data
     * @return int
     */
    function addMoney($data)
    {
        if (!$this->checkOrderID ($data['order_id'])) {
            return -3010126022; //订单号已存在
        }
        $this->account_name = $data['account_name'];
        $this->account_identity = $data['account_identity'];
        $this->money = $data['money'];
        $this->reason = $data['reason'];
        $this->sysetem_user = $data['sysetem_user'];
        $this->money_cash = $data['money_cash'];
        $this->recharge_type = $data['recharge_type'];
        $this->status = $data['status'];
        $this->info_identity = UUID ();
        $this->order_id = $data['order_id'];
        $this->apply_time = $data['apply_time'];
        $this->complete_time = $data['complete_time'];
        $this->remark = $data['remark'] ?? '后台充值';
        $this->rate = $data['rate'] ?? 1;
        $res = $this->save ();
        if (!$res) {
            return -3020011322; //加款失败
        }
        return $this->order_id;

    }

    //修改信息
    function updateInfo($data, $field, $value)
    {
        $res = $this->where ($field, $value)->update ($data);
        if ($res < 0) {
            return -3020100102; //操作失败
        }
        return 1;
    }

    //检测订单号
    function checkOrderID($order_id)
    {
        $res = $this->where ('order_id', $order_id)->first ();
        if (!$res) {
            return true;
        }
        return false;
    }

    //充值统计
    function totalRecharge($whereTime = '', $type)
    {
        if (is_array ($whereTime) && count ($whereTime) > 0) {
            $this->where ($whereTime)->where ('recharge_type', $type)->sum ('momney');
        }
    }

    //更新状态
    function updateStatus($order_id, $status)
    {
        $data = [
            'status' => $status,
            'complete_time' => date ('Y-m-d H:i:s'),
            'sysetem_user' => session ('adminInfo.admin_name'),
        ];
        $res = $this->where ('order_id', $order_id)->update ($data);
        if (!$res) {
            return -5010050022; //状态更新失败
        }
        return 1;
    }

    function text()
    {
        $data = $this->select (DB::raw ('recharge_type'))->groupBy ('recharge_type')->get ();
        return $data->toArray ();

    }

    //获取待处理汇款详情
    function getRimitDetails($order_id)
    {
        $data = $this->where ('order_id', $order_id)->where ('status', 0)->first ();
        if (!$data) {
            return -3020035022; //汇款待审订单不存在或已处理
        }
        return $data->toArray ();
    }

    //汇款
    function countRimitUser($timearea)
    {
        $data = $this->select ('account_identity')->whereBetween ('apply_time', $timearea)->where ('recharge_type', '汇款')->groupBy ('account_identity')->get ();
        if (!$data) {
            return 0;
        }
        $data = $data->toArray ();
        return count ($data);
    }

    //加款检测
    function checkAdd($account_id, $money, $time)
    {
        $res = $this->where ('account_identity', $account_id)->where ('money', $money)
            ->where ('apply_time', '>', $time)->first ();
        if (!$res) {
            return 1;
        }
        return -5023809122;
    }

}