<?php
namespace App\Models;
use DB;

class LuckyMoney extends BaseModel {

	protected $table = 'lucky_money';
	public $timestamps = false;
    //获取列表
    function getList($list=10,$where){
        $data=$this->join ('account', 'account.identity', '=', 'lucky_money.account_identity')
            ->select(DB::raw('account,account_identity,take_money,lm_order_id,room_id,money,take_money,created_at,num,take_num,lm_return_money,type,take_num,num'))->where($where)->orderBy('created_at','desc')->paginate($list);
        if(!$data){
            return -5012345332;
        }
        return $data->toArray();
    }

    //退还金额
    function getReturn($list,$where){
        $data=$this->join ('account', 'account.identity', '=', 'lucky_money.account_identity')
            ->select(DB::raw('account,account_identity,take_money,lm_order_id,room_id,money,take_money,created_at,num,take_num,lm_return_money'))
            ->whereRaw('money<>take_money')->where('lm_return_money','>',0)->where($where)->orderBy('lucky_money.id','desc')->paginate($list);
        if(!$data){
            return -5012345332;
        }
        return $data->toArray();
    }

    function getRefund($list,$where){
        $data=$this->join ('account', 'account.identity', '=', 'lucky_money.account_identity')
            ->select(DB::raw('account,account_identity,take_money,lm_order_id,room_id,money,take_money,created_at,num,take_num,lm_return_money'))
            ->whereRaw('money<>take_money')->where('lm_return_money','=',0)->where($where)->orderBy('created_at','asc')->paginate($list);
        if(!$data){
            return -5012345332;
        }
        return $data->toArray();
    }
    //领取金额
    function getReceive($list,$where){
        $data=$this->join ('account', 'account.identity', '=', 'lucky_money.account_identity')
            ->select(DB::raw('account,lucky_money.account_identity,take_money,lm_order_id,room_id,money,take_money,created_at,num,take_num,lm_return_money'))
            ->where('take_money','>',0)->where($where)->orderBy('lucky_money.id','desc')->paginate($list);
        if(!$data){
            return -5012345332;
        }
        return $data->toArray();
    }


    function getRefundArr($id_array){
        $data=$this->whereIn('lm_order_id',$id_array)->get();
        if(!$data){
            return -5040012522;
        }
        $data=$data->toArray();
        $account_cash_array=array();
        foreach ($data as $k=>$v){
            $account_cash_array[$v['lm_order_id']]=$v['take_money'];
        }
        return $account_cash_array;
    }

    function getRefundA($id_array){
        $data=$this->whereIn('lm_order_id',$id_array)->get();
        if(!$data){
            return -5040012522;
        }
        $data=$data->toArray();
        $account_cash=array();
        foreach ($data as $k=>$v){
            $account_cash[$v['lm_order_id']]=$v['money'];
        }
        return $account_cash;
    }

    //退还红包
    function refundData($lm_order_id,$data) {
        // dd($data);
        // $res1=$this->where('lm_order_id',$lm_order_id)->update($data);
        // if(!$res1){
        //     return -5040022022;//增加约失败
        // }

        $res2=$this->where('lm_order_id',$lm_order_id)->increment('lm_return_money',$data);
        if(!$res2){
            return -5040022022;//
        }
        return 1;
    }
}
