<?php

namespace App\Models;

/**
 * 资金流水
 */
class Amount 
{

    /*
     * 充值记录
     */
    public function recharge($data){

    	$moneyrecharge = new \App\Models\Money_recharge;

    	if(empty($data['info_identity'])){
    		$data['info_identity'] = UUID ();
    	}
    	if(empty($data['order_id'])){
    		$data['order_id'] = OrderID ();
    	}
    	if(empty($data['apply_time'])){
    		$data['apply_time'] = date ('Y-m-d H:i:s');
    	}
    	if(empty($data['complete_time'])){
    		$data['complete_time'] = date ('Y-m-d H:i:s');
    	}
    	if(empty($data['money'])){
    		return -50002;
    	}
    	if(empty($data['account_identity'])){
    		return -50003;
    	}
    	if(empty($data['account_name'])){
    		return -50004;
    	}
    	if(empty($data['status'])){
    		return -50005;
    	}
    	if(empty($data['recharge_type'])){
    		return -50006;
    	}
    	if(empty($data['sysetem_user'])){
    		return -50007;
    	}
    	if(empty($data['reason'])){
    		return -50008;
    	}
    	if(empty($data['money_cash'])){
    		return -50009;
    	}
    	if(!empty($data['info_identity'])){
    		$recharge = $moneyrecharge->where('info_identity',$data['info_identity'])->first();
    	}

    	if(empty($recharge)){
    		$res =$moneyrecharge->insert($data);
    	}else{
    		$res =$moneyrecharge->where('info_identity',$data['info_identity'])->update($data);
    	}
    	
    	return $res;
    }


    /*
     * 资金明细
     */
    public function detailed($data){

    	$moneydetails = new \App\Models\Money_details;

    	if(empty($data['info_identity'])){
    		$data['info_identity'] = UUID ();
    	}
    	if(empty($data['trade_id'])){
    		$data['trade_id'] = OrderID ();
    	}
    	if(empty($data['money_time'])){
    		$data['money_time'] = date ('Y-m-d H:i:s');
    	}
    	if(empty($data['money'])){
    		return -50002;
    	}
    	if(empty($data['account_identity'])){
    		return -50003;
    	}
    	if(empty($data['account_name'])){
    		return -50004;
    	}
    	if(empty($data['money_type'])){
    		return -50010;
    	}
    	if(empty($data['money_cash'])){
    		return -50009;
    	}
    	if(empty($data['trade_type'])){
    		return -50011;
    	}
    	if(empty($data['trade_desc'])){
    		return -50012;
    	}
    	if(empty($data['sysetem_user'])){
    		return -50007;
    	}

    	if(!empty($data['info_identity'])){
    		$detailed = $moneydetails->where('info_identity',$data['info_identity'])->first();
    	}
    	if(empty($detailed)){
    		$res =$moneydetails->insert($data);
    	}else{
    		$res =$moneydetails->where('info_identity',$data['info_identity'])->update($data);
    	}
    	
    	return $res;

    }

    /*
     * 充值备注表
     */
    public function rechargeremark($data){

    	$MoneyRechargeRemark = new \App\Models\MoneyRechargeRemark;

    	if(empty($data['order_id'])){
    		$data['order_id'] = OrderID ();
    	}
    	if(empty($data['recharge_time'])){
    		$data['recharge_time'] = date ('Y-m-d H:i:s');
    	}
    	if(empty($data['money'])){
    		return -50002;
    	}
    	if(empty($data['name'])){
    		return -50004;
    	}

    	if(!empty($data['order_id'])){
    		$rechremark = $MoneyRechargeRemark->where('order_id',$data['order_id'])->first();
    	}
    	if(empty($rechremark)){
    		$res =$MoneyRechargeRemark->insert($data);
    	}else{
    		$res =$MoneyRechargeRemark->where('order_id',$data['order_id'])->update($data);
    	}
    	
    	return $res;
    	
    }

    /*
     * 投注记录表
     */
    public function withdrawrule($data){

    	$Withdrawrule = new \App\Models\Withdraw_rule;

    	$data['updated_at'] = date ('Y-m-d H:i:s');
    	if(empty($data['created_at'])){
    		$data['created_at'] = date ('Y-m-d H:i:s');
    	}
    	if(empty($data['bettingMoney'])){
    		return -50014;
    	}
    	if(empty($data['needBettingMoney'])){
    		return -50015;
    	}
    	if(empty($data['start_time'])){
    		return -50016;
    	}
    	if(empty($data['end_time'])){
    		return -50017;
    	}
    	if(!empty($data['id'])){
    		$drawrule = $Withdrawrule->where('id',$data['id'])->first();
    	}
    	if(empty($drawrule)){
    		$res =$Withdrawrule->insert($data);
    	}else{
    		$res =$Withdrawrule->where('id',$data['id'])->update($data);
    	}

    	return $res;
    }

    /**
     * 更新用户金额
     * @param  [type] $money 操作金额
     * @param  [type] $nowCash  做类型
     * @param  [type]  $last_money 操作后金额
     * @param  [type] $Cash  当前余额
     * @return [type]           [description]
     */
    public function updateAccountMoney($account_identity, $money, $type, $last_money, $cash = '')
    {
        $res=S('DB')->select("select update_money('".$account_identity."',".$money.",".$type.")");
        return $res;
    }
    /**
     * 更新用户金额第二种
     * @param  [type] $money 操作金额
     * @param  [type] $nowCash  做类型
     * @param  [type]  $last_money 操作后金额
     * @param  [type] $Cash  当前余额
     * @return [type]           [description]
     */
    public function updateAccountMoneytwo($account_identity, $money, $type, $datatype, $remark = '')
    {
        $res=S('DB')->select("select update_money('".$account_identity."',".$money.",".$type.",".$datatype.",".$remark.")");
        return $res;
    }

    /*
     * 更新用户余额
     */
    public function updateUser($data){

        $Account_detailed = new \App\Models\Account_detailed;

        if(empty($data['account_identity'])){
            return -50003;
        }
        $res = $Account_detailed->where('account_identity', $data['account_identity'])->update($data);

        return $res;
    }







}





?>