<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/18
 * Time: 15:16
 */

namespace Biz\Game;

use  App\Commons\Model\Money_transfer;
use  App\Commons\Model\Wagent_log as Wagent_logModel ;
use  App\Commons\Model\Money_details as Money_detailsModel ;


//转账逻辑详细实现
class TranferMoneyLogic
{
    private $accountModel = null ;
    private $wagentModel = null ;
    private $transferModel = null ;
    private $accountDetailModel = null ;
    private $paras = null ;
    private $type = null ;
    private $money = 0 ;


    public  function  __construct( $accountModel,$parasArray,$wagetnModel )
    {
        $this->accountModel = $accountModel ;
        $this->wagentModel = $wagetnModel ;
        $this->paras = $parasArray ;
    }

    public   function   dorun(){
        $ret0 = $this->parentCheck() ;
        if ( $ret0 !== true){
            return $ret0 ;
        }


        try {
            _beginTransaction();

            $ret1 =  $this->wagentLogRecord();
            if (!$ret1){  _rollBack();  return $this->makeret(0,'false_1',0);   }

            $ret2 = $this->wagentRecord();
            if (!$ret2){  _rollBack();  return $this->makeret(0,'false_2',0);   }

            $ret3 = $this->accountDetailUpdate();
            if (!$ret3){  _rollBack();  return $this->makeret(0,'false_3',0);   }

            $ret4 = $this->moneyTranferRecord();
            if (!$ret4){  _rollBack();  return $this->makeret(0,'false_4',0);   }

            $ret5 = $this->moneyDetailInsrt();
            if (!$ret5){  _rollBack();  return $this->makeret(0,'false_5',0);   }

            _commit();
            return $this->makeret(1,'success',1);


        }catch (\Exception $e){
            _rollBack();
            return $this->makeret(0,'false_0'.$e->getMessage(), 0 ) ;
        }

    }

    //返回数据
    private function  makeret($status=1,$msg='success',$datas=[]){
        return [
            'status'=>$status,
            'msg'=>$msg,
            'datas'=>$datas
        ];
    }

    //参数检测
    private function  parentCheck(){

        //IN代表代理账户转入到用户账号       OUT代表用户账号转入到代理账号上
        $type = isset($this->paras['type']) ?  strtoupper(trim($this->paras['type'])) : '';
        if (!in_array( $type , ['IN','OUT'])){ return $this->makeret(0,'type error!','type error');  }
        $this->type = $type ;

        //转账金额不能小于等于0
        $credit  = isset($this->paras['credit']) ?  floatval(trim($this->paras['credit'])) : 0;
        if ( $credit <=0 ){   return $this->makeret(0,'credit error','credit error'); }
        $this->money = $credit ;

        //代理上传的订单号不能为空
        $billno = isset($this->paras['billno']) ?  trim($this->paras['billno']) : '';
        if (empty($billno)){ return $this->makeret(0,'billno empty','billno empty');  }
        //代理上传的订单号不能重复
        $transferModel = new Money_transfer();
        $hasexit = $transferModel->findOneByAttr('billno',$billno);
        if ( $hasexit ){   return $this->makeret(0,'billno exists','billno exists'); }
        $this->transferModel = $transferModel ;

        //用户详情数据，内有用户余额信息
        $parentinfo = lm('account_detailed', 'commons')->where('account_identity', $this->accountModel->identity)->first();
        if (empty($parentinfo)) {  return $this->makeret(1,'account_detailed not exists','0');     }
        $cash = $parentinfo->cash ;
        if ( $type == 'OUT'  &&  $cash<$credit ){
            return $this->makeret(0,'no_enough_credit_user','no_enough_credit');
        }
        if (  $type == 'IN' &&  $this->wagentModel->money <$credit ){
            return $this->makeret(0,'no_enough_credit_agent','no_enough_credit');
        }
        $this->accountDetailModel = $parentinfo ;

        return true;
    }


    //代理钱变化日志记录  wageng_log增加
    private function   wagentLogRecord(){
        $beforeMoney = intval(trim($this->wagentModel->money)*100);
        $afterMoney =  ( $this->type == 'IN') ? (($beforeMoney - $this->money*100)/100) : (($beforeMoney + $this->money*100)/100) ;

        $model = new  Wagent_logModel();
        $model->agent_name = $this->wagentModel->agent_name ;
        $model->account_identity = $this->accountModel->identity;
        $model->ordernumber = $this->paras['billno'];
        $model->credit = $this->money;
        $model->agent_money_before = $beforeMoney/100;
        $model->agent_money_after = sprintf("%.2f",$afterMoney) ;

        $ret = $model->save();
        return    $ret;

    }

    //代理钱更新  wagent表更行
    private  function  wagentRecord(){
        $beforeMoney = intval(trim($this->wagentModel->money)*100);
        $afterMoney =  ( $this->type == 'IN') ? (($beforeMoney - $this->money*100)/100) : (($beforeMoney + $this->money*100)/100) ;

        $this->wagentModel->money = sprintf("%.2f",$afterMoney)   ;
        $ret =  $this->wagentModel->save();
        return $ret;
    }

    //用户余额更新
    private function accountDetailUpdate(){

        $beforeMoney = intval(trim($this->accountDetailModel->cash)*100 ) ;
        $afterMoney =  ( $this->type == 'IN') ? (($beforeMoney + $this->money*100)/100) : (($beforeMoney - $this->money*100)/100) ;
        $this->accountDetailModel->cash = sprintf("%.2f",$afterMoney)   ;

        $beforeMoney = intval(trim($this->accountDetailModel->available_cash)*100 ) ;
        $afterMoney =  ( $this->type == 'IN') ? (($beforeMoney + $this->money*100)/100) : (($beforeMoney - $this->money*100)/100) ;
        $this->accountDetailModel->available_cash = sprintf("%.2f",$afterMoney)   ;

        $ret = $this->accountDetailModel->save();

        return $ret  ;

    }

    //代理转账记录  money_tranfer表增加
    private function  moneyTranferRecord(){

        $this->transferModel->account_indent = $this->accountModel->identity;
        $this->transferModel->ordernumber = date("YmdHis").uniqid();
        $this->transferModel->billno = $this->paras['billno'];
        $this->transferModel->tradeamount = sprintf("%.2f", $this->money )    ;
        $this->transferModel->blance = $this->accountDetailModel->cash ;
        $this->transferModel->type = $this->type ;
        $this->transferModel->tradtype = ($this->type == 'IN') ? 8 : 9 ;
        $this->transferModel->tfrom = 0 ;
        $this->transferModel->tto = 0 ;
        $this->transferModel->addtime = date("Y-m-d H:i:s") ;
        $this->transferModel->agent_name = $this->wagentModel->agent_name ;

        $ret = $this->transferModel->save();

        return    $ret ;

    }

    //用户流水记录
    private function  moneyDetailInsrt(){

        $model = new Money_detailsModel();
        $model->info_identity = UUID();
        $model->trade_id = $this->paras['billno'] ;
        $model->account_name = $this->accountModel->account ;
        $model->account_identity = $this->accountModel->identity ;
        $model->money = sprintf("%.2f", $this->money )    ; ;
        $model->money_time = date("Y-m-d H:i:s") ;
        $model->money_type = ($this->type == 'IN') ?  1 : 2 ;
        $model->money_cash =  $this->accountDetailModel->cash ;
        $model->trade_type = ($this->type == 'IN') ?  23 : 24 ;
        $model->trade_desc = ($this->type == 'IN') ?  '从代理转入': '转出到代理';
        $model->reason = '代理接口操作';
        $model->sysetem_user  = 1 ;
        $model->status = 1 ;
        $model->out_order_id = $this->transferModel->ordernumber  ;
        $model->remark = '代理转入转出记录';
        $ret = $model->save();
        return $ret ;

    }


}