2025-10-18 01:16:25 +08:00

207 lines
4.9 KiB
PHP

<?php
namespace addons\shopro\library\ccblife\model;
use think\Model;
/**
* 建行支付日志模型
*
* 用于记录建行支付相关的日志信息
* 表名: fa_ccb_payment_log
*/
class CcbPaymentLog extends Model
{
/**
* 数据表名称(不含前缀)
* @var string
*/
protected $name = 'ccb_payment_log';
/**
* 自动写入时间戳字段
* @var bool
*/
protected $autoWriteTimestamp = false;
/**
* 字段类型转换
* @var array
*/
protected $type = [
'order_id' => 'integer',
'user_id' => 'integer',
'amount' => 'float',
'status' => 'integer',
'pay_time' => 'integer',
'create_time' => 'integer',
];
/**
* 支付状态常量
*/
const STATUS_PENDING = 0; // 待支付
const STATUS_SUCCESS = 1; // 支付成功
const STATUS_FAILED = 2; // 支付失败
const STATUS_CANCELLED = 3; // 已取消
/**
* 支付状态映射
* @var array
*/
public static $statusMap = [
self::STATUS_PENDING => '待支付',
self::STATUS_SUCCESS => '支付成功',
self::STATUS_FAILED => '支付失败',
self::STATUS_CANCELLED => '已取消',
];
/**
* 获取状态文本
*
* @param int $status 状态值
* @return string
*/
public static function getStatusText($status)
{
return self::$statusMap[$status] ?? '未知状态';
}
/**
* 根据订单ID查找支付日志
*
* @param int $orderId 订单ID
* @return CcbPaymentLog|null
*/
public static function findByOrderId($orderId)
{
return self::where('order_id', $orderId)
->order('create_time', 'desc')
->find();
}
/**
* 根据支付流水号查找
*
* @param string $payFlowId 支付流水号
* @return CcbPaymentLog|null
*/
public static function findByPayFlowId($payFlowId)
{
return self::where('pay_flow_id', $payFlowId)->find();
}
/**
* 根据订单号查找
*
* @param string $orderSn 订单号
* @return CcbPaymentLog|null
*/
public static function findByOrderSn($orderSn)
{
return self::where('order_sn', $orderSn)
->order('create_time', 'desc')
->find();
}
/**
* 创建支付日志
*
* @param array $data 日志数据
* @return CcbPaymentLog|false
*/
public static function createLog($data)
{
$defaultData = [
'status' => self::STATUS_PENDING,
'create_time' => time(),
];
$logData = array_merge($defaultData, $data);
return self::create($logData);
}
/**
* 更新支付状态
*
* @param string $payFlowId 支付流水号
* @param int $status 状态
* @param array $extraData 额外数据
* @return bool
*/
public static function updateStatus($payFlowId, $status, $extraData = [])
{
$updateData = array_merge([
'status' => $status,
], $extraData);
return self::where('pay_flow_id', $payFlowId)->update($updateData) !== false;
}
/**
* 记录支付成功
*
* @param string $payFlowId 支付流水号
* @param string $transId 交易ID
* @param array $callbackData 回调数据
* @return bool
*/
public static function recordSuccess($payFlowId, $transId, $callbackData = [])
{
return self::updateStatus($payFlowId, self::STATUS_SUCCESS, [
'trans_id' => $transId,
'pay_time' => time(),
'callback_data' => is_array($callbackData) ? json_encode($callbackData, JSON_UNESCAPED_UNICODE) : $callbackData,
]);
}
/**
* 记录支付失败
*
* @param string $payFlowId 支付流水号
* @param string $errorMsg 错误信息
* @return bool
*/
public static function recordFailure($payFlowId, $errorMsg)
{
return self::updateStatus($payFlowId, self::STATUS_FAILED, [
'error_msg' => $errorMsg,
]);
}
/**
* 获取待支付的日志列表
*
* @param int $limit 限制数量
* @return array
*/
public static function getPendingList($limit = 100)
{
return self::where('status', self::STATUS_PENDING)
->where('create_time', '>', time() - 3600 * 24) // 24小时内
->limit($limit)
->select();
}
/**
* 关联订单模型(如果需要)
*
* @return \think\model\relation\BelongsTo
*/
public function order()
{
return $this->belongsTo('app\admin\model\shopro\order\Order', 'order_id', 'id');
}
/**
* 关联用户模型(如果需要)
*
* @return \think\model\relation\BelongsTo
*/
public function user()
{
return $this->belongsTo('app\common\model\User', 'user_id', 'id');
}
}