Files
2025-07-27 12:22:34 +08:00

291 lines
7.7 KiB
PHP

<?php
class BizAccount
{
const MONEY_FLOW_IN = 'in'; //资金流入
const MONEY_FLOW_OUT = 'out'; //资金流出
const TRADE_TYPE_CONSUME = 1; //充值
const TRADE_TYPE_USE = 2; //使用
const MONEY_TYPE_CLUES = 1;// 线索分佣
const MONEY_TYPE_ORDER = 1;// 订单分佣
private $ci;
/**
* 记账户流水暂存的余额
* @var array
*/
private $arr_tr_mleft = array();
public function __construct($params = [])
{
$this->ci = &get_instance();
$this->ci->load->model('biz/biz_account_model', 'account_model');
$this->ci->load->model('biz/biz_accountLog_model', 'accountlog_model');
$this->account_model = $this->ci->account_model;
$this->accountlog_model = $this->ci->accountlog_model;
$params['ck'] && $this->ck = $params['ck'];
$params['jsondata'] && $this->jsondata = $params['jsondata'];
}
/**
* 充值
* @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 $descrip
* @return array
*/
public function useMoney($account_id, $money, $descrip = '')
{
$trade_type = self::TRADE_TYPE_USE;
!$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_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->getAccountById($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 $bizId
* @param $add 是否创建账户
* @return mixed
*/
public function getAccountBizId($bizId, $add = false)
{
$where = ['biz_id' => $bizId];
$account = $this->account_model->get($where);
if (!$account && $add) {
$accountId = $this->new_account($bizId);
$account = $this->getAccountById($accountId);
}
return $account;
}
public function getAccountById($id)
{
$account = $this->account_model->get(['id' => $id]);
return $account;
}
/**
*
* 开户
* @param $id biz_id
* @return int
*/
public function new_account($id)
{
$data = array('biz_id' => $id, 'c_time' => time());
$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_biz_account` SET money_left = money_left-{$money} WHERE id = {$account_id} AND money_left >= {$money}";
} else {
$logmoney = $money;
$sql = "UPDATE `lc_biz_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_biz_accountlog` ({$str_key}) VALUES ($str_val)";
}
}