298 lines
8.3 KiB
PHP
298 lines
8.3 KiB
PHP
<?php
|
|
|
|
class Account_entity{
|
|
|
|
const TYPE_USER = 'user';
|
|
const TYPE_BIZ = 'biz';
|
|
const AC_TYPE_USER = 1;
|
|
const AC_TYPE_BIZ = 2;
|
|
|
|
const MONEY_FLOW_IN = 'in'; //资金流入
|
|
const MONEY_FLOW_OUT = 'out'; //资金流出
|
|
|
|
const TRADE_TYPE_CONSUME = 1; //入账
|
|
const TRADE_TYPE_CASH = 2; //提现
|
|
const TRADE_TYPE_CHARGE = 3; //冲正
|
|
const TRADE_TYPE_TRANSFER = 4; //转账
|
|
|
|
private $appid;
|
|
private $ci;
|
|
|
|
/**
|
|
* 记账户流水暂存的余额
|
|
* @var array
|
|
*/
|
|
private $arr_tr_mleft = array();
|
|
|
|
public function __construct($appid,$params=[]){
|
|
$this->ci = & get_instance();
|
|
$this->ci->load->model('app/user_account_model','account_model');
|
|
$this->ci->load->model('app/user_accountlog_model','accountlog_model');
|
|
$this->account_model = $this->ci->account_model;
|
|
$this->accountlog_model = $this->ci->accountlog_model;
|
|
$this->app_id = $appid;
|
|
$params['order_id'] && $this->order_id = $params['order_id'];
|
|
$params['jsondata'] && $this->jsondata = $params['jsondata'];
|
|
$params['ck'] && $this->ck = $params['ck'];
|
|
}
|
|
|
|
/**
|
|
* 充值
|
|
* @param $account_id (账户ID)
|
|
* @param $money (金额)
|
|
* @param string $descrip (描述)
|
|
* @param array $svr (account_id, rate)
|
|
* @return array (sql数组)
|
|
*/
|
|
public function charge($account_id, $money, $descrip = ''){
|
|
|
|
$trade_type = self::TRADE_TYPE_CONSUME;
|
|
!$descrip && $descrip = '入账';
|
|
|
|
$ids = array($account_id);
|
|
|
|
//日志余额
|
|
$mleft = $this->get_logmleft($ids);
|
|
|
|
/*交易开始*/
|
|
$this->trade_start();
|
|
|
|
$this->arr_tr_mleft = $mleft;
|
|
$this->arr_tr_param = array('trade_type' => $trade_type, 'descrip' => $descrip);
|
|
|
|
//入账
|
|
$this->tr_money($account_id, $money, self::MONEY_FLOW_IN);
|
|
|
|
$ret = $this->trade_complete();
|
|
/*交易结束*/
|
|
|
|
return $ret;
|
|
}
|
|
|
|
/**
|
|
* 提现
|
|
* @param $account_id
|
|
* @param $money
|
|
* @param string $descrip
|
|
*/
|
|
public function cash($account_id, $money, $descrip = '提现'){
|
|
$trade_type = self::TRADE_TYPE_CASH;
|
|
|
|
//日志余额
|
|
$mleft = $this->get_logmleft([$account_id]);
|
|
|
|
/*交易开始*/
|
|
$this->trade_start();
|
|
|
|
$this->arr_tr_mleft = $mleft;
|
|
$this->arr_tr_param = array('trade_type' => $trade_type, 'descrip' => $descrip);
|
|
|
|
//入账
|
|
$this->tr_money($account_id, $money, self::MONEY_FLOW_OUT);
|
|
|
|
$ret = $this->trade_complete();
|
|
/*交易结束*/
|
|
|
|
return $ret;
|
|
}
|
|
|
|
/**
|
|
* 获取存交易流水的余额
|
|
* @param $account_ids
|
|
* @return array
|
|
*/
|
|
public function get_logmleft($account_ids){
|
|
|
|
$mleft = array();
|
|
foreach($account_ids as $v){
|
|
$log = $this->get_accountlog('last', $v);
|
|
$money_left = floatval($log['money_left']);
|
|
|
|
if($money_left <= 0){
|
|
$account = $this->get_account($v);
|
|
$money_left = floatval($account['money_left']);
|
|
}
|
|
|
|
$mleft[$v] = $money_left;
|
|
}
|
|
|
|
return $mleft;
|
|
}
|
|
|
|
/**
|
|
* 获取一条账户日志
|
|
* @param string $type 类型(last 最后一条)
|
|
* @param $account_id
|
|
* @param $logid
|
|
* @return mixed
|
|
*/
|
|
public function get_accountlog($type = 'last', $account_id = 0, $logid = 0){
|
|
$where = array();
|
|
$account_id && $where['account_id'] = $account_id;
|
|
$logid && $where['id'] = $logid;
|
|
switch($type){
|
|
default:
|
|
$order = 'id DESC';
|
|
}
|
|
$list = $this->accountlog_model->select($where, $order, 1, 1);
|
|
|
|
return $list ? $list[0] : array();
|
|
}
|
|
/**
|
|
* 获取账户
|
|
* @param $id (根据type组合)
|
|
* @param $type (user|biz|sys)
|
|
* @return mixed
|
|
*/
|
|
public function get_account($id, $type = ''){
|
|
$where = array('app_id' => $this->app_id);
|
|
switch($type){
|
|
case 'user':
|
|
$where['type'] = self::AC_TYPE_USER;
|
|
$where['app_uid'] = $id;
|
|
break;
|
|
case 'biz':
|
|
$where['type'] = self::AC_TYPE_BIZ;
|
|
$where['biz_id'] = $id;
|
|
break;
|
|
default:
|
|
$where['id'] = $id;
|
|
}
|
|
|
|
$account = $this->account_model->get($where);
|
|
|
|
return $account;
|
|
}
|
|
/**
|
|
*
|
|
* 开户
|
|
* @param $id (根据type 可以是uid、biz_id)
|
|
* @param string $type (user|biz)
|
|
* @return int
|
|
*/
|
|
public function new_account($id, $type = ''){
|
|
$data = array('app_id' => $this->app_id, 'c_time' => time());
|
|
switch($type){
|
|
case self::TYPE_USER:
|
|
$data['type'] = self::AC_TYPE_USER;
|
|
$data['app_uid'] = $id;
|
|
break;
|
|
case self::TYPE_BIZ:
|
|
$data['type'] = self::AC_TYPE_BIZ;
|
|
$data['biz_id'] = $id;
|
|
break;
|
|
default:
|
|
return 0;
|
|
}
|
|
$id = $this->account_model->add($data);
|
|
|
|
return $id;
|
|
}
|
|
|
|
/**
|
|
* 交易开始
|
|
*/
|
|
public function trade_start(){
|
|
$this->is_tr = true;
|
|
$this->arr_tr_mleft = array();
|
|
$this->arr_tr_param = array();
|
|
$this->sql_array = array();
|
|
//生成交易ID
|
|
$this->sid = date('Ymd') . time() . str_pad(rand(0, 999), 3, '0', STR_PAD_LEFT);
|
|
}
|
|
/**
|
|
* 交易完成
|
|
* @return array
|
|
*/
|
|
public function trade_complete(){
|
|
$sql_arr = $this->sql_array;
|
|
|
|
$status = true;
|
|
$msg = '';
|
|
|
|
if(!$this->is_tr){
|
|
$status = false;
|
|
$msg = '交易未初始化';
|
|
goto end;
|
|
}
|
|
|
|
if(!$sql_arr){
|
|
$status = false;
|
|
$msg = '无交易数据';
|
|
goto end;
|
|
}
|
|
|
|
end:
|
|
//设置交易结束
|
|
$this->is_tr = false;
|
|
$this->arr_tr_mleft = array();
|
|
$this->arr_tr_param = array();
|
|
$this->sql_array = array();
|
|
|
|
if(!$status){
|
|
$sql_arr = array();
|
|
}
|
|
|
|
return $sql_arr;
|
|
}
|
|
/**
|
|
* 交易金额
|
|
* @param $account_id
|
|
* @param $money (金额)
|
|
* @param $type (资金类型)
|
|
*/
|
|
protected function tr_money($account_id, $money, $type = 'in'){
|
|
|
|
$money = floatval($money);
|
|
|
|
if('out' == $type){
|
|
$logmoney = -$money;
|
|
$sql = "UPDATE `lc_app_user_account` SET money_left = money_left-{$money} WHERE id = {$account_id} AND money_left >= {$money}";
|
|
} else {
|
|
$logmoney = $money;
|
|
$sql = "UPDATE `lc_app_user_account` SET money_total=money_total+{$money}, money_left = money_left+{$money} WHERE id = {$account_id}";
|
|
}
|
|
|
|
$this->sql_array[] = $sql;
|
|
|
|
$this->tr_accountlog($account_id, $logmoney);
|
|
}
|
|
/**
|
|
* 记录money log
|
|
* @param $account_id (账户ID
|
|
* @param $money (金额,负数表示出账)
|
|
*/
|
|
private function tr_accountlog($account_id, $money){
|
|
$descrip = $this->arr_tr_param['descrip'];
|
|
|
|
$trade_type = $this->arr_tr_param['trade_type'];
|
|
|
|
$money_left = $this->arr_tr_mleft[$account_id];
|
|
$money_left += $money;//计算这次日志余额
|
|
$this->arr_tr_mleft[$account_id] = $money_left;
|
|
|
|
$c_time = $this->arr_tr_param['c_time'];
|
|
!$c_time && $c_time = time();
|
|
|
|
$data = array(
|
|
'account_id' => $this->ci->db->escape($account_id),
|
|
'trade_type' => $trade_type,
|
|
'money_type' => 0,
|
|
'money_in' => $money>0?$this->ci->db->escape($money):0,
|
|
'money_out' => $money<0?$this->ci->db->escape(abs($money)):0,
|
|
'money_left' => $money_left,
|
|
'descrip' => $this->ci->db->escape($descrip),
|
|
'c_time' => $this->ci->db->escape($c_time),
|
|
);
|
|
$this->ck && $data['ck'] = $this->ci->db->escape($this->ck);
|
|
$this->order_id && $data['sid'] = $this->order_id;
|
|
if($this->jsondata && is_array($this->jsondata)){
|
|
$data['jsondata'] = "'".json_encode($this->jsondata,JSON_UNESCAPED_UNICODE)."'";
|
|
}
|
|
|
|
$str_key = '`' . implode('`,`', array_keys($data)) .'`';
|
|
$str_val = "" . implode(",", array_values($data));
|
|
$this->sql_array[] = "INSERT INTO `lc_app_user_accountlog` ({$str_key}) VALUES ($str_val)";
|
|
}
|
|
}
|