manghe/app/common/model/WxpayLog.php
2025-04-27 15:19:03 +08:00

89 lines
3.1 KiB
PHP
Executable File

<?php
namespace app\common\model;
class WxpayLog extends Base
{
// 设置当前模型对应的完整数据表名称
protected $table = 'wxpay_log';
/**
* 获取列表
*/
public static function getList($where = [],$field='*',$order='',$pageSize = "15")
{
$list = self::where($where)
->field($field)
->order($order)
->paginate(['list_rows'=>$pageSize,'query' => request()->param()]);
$page = $list->render();
$data['list'] = $list->toArray()['data'];
$data['count']=$list->total();
$data['last_page']=$list->toArray()['last_page'];
$data['page']=$page;
return $data;
}
/**
* 迁移旧数据到ProfitPay表
* 将WxpayLog表中的旧数据迁移到profit_pay表
* @param int $limit 每次处理的数据量
* @param int $offset 起始偏移量
* @return array 处理结果
*/
public static function migrateToNewTable($limit = 100, $offset = 0)
{
try {
// 获取旧表数据
$oldRecords = self::limit($limit)->offset($offset)->order('id asc')->select();
$total = count($oldRecords);
$success = 0;
$failed = 0;
$detail = [];
// 处理每条记录
foreach ($oldRecords as $record) {
try {
// 解析content字段中的json数据
$content = json_decode($record['content'], true);
$change_money = isset($content['total_fee']) ? ($content['total_fee'] / 100) : 0;
// 检查记录是否已经存在
$exists = ProfitPay::where('order_num', $record['order_no'])->find();
if ($exists) {
$detail[] = "订单 {$record['order_no']} 已存在于新表中,跳过";
continue;
}
// 插入到新表
ProfitPay::insert([
'user_id' => $record['user_id'],
'order_num' => $record['order_no'],
'change_money' => $change_money,
'content' => isset($content['attach']) ? $content['attach'] : '微信支付',
'pay_type' => $record['channel'] == 2 ? 2 : 1, // 1微信 2支付宝
'addtime' => $record['addtime'],
]);
$success++;
} catch (\Exception $e) {
$failed++;
$detail[] = "处理订单 {$record['order_no']} 失败: " . $e->getMessage();
}
}
return [
'status' => 1,
'message' => "迁移完成:共处理 {$total} 条记录,成功 {$success} 条,失败 {$failed}",
'detail' => $detail
];
} catch (\Exception $e) {
return [
'status' => 0,
'message' => "迁移过程发生错误: " . $e->getMessage()
];
}
}
}