Commit 523cc838 by yink

feat(订单): 新增积分支付功能并优化订单处理逻辑

在订单创建和支付流程中引入积分支付功能,支持纯积分支付和混合支付模式。同时优化了订单分账逻辑,将直推和间推佣金改为积分形式,并增加了积分解冻和完成处理的相关功能。
parent 75115354
......@@ -144,8 +144,8 @@ protected function form()
$form->tab('基本信息', function (Form $form) {
$form->text('goods_name', '商品名称')->required();
$form->text('goods_sn', '商品编号');
$form->text('first_commission', '直推佣金比例')->help("比例如:30");
$form->text('second_commission', '间推佣金比例')->help("比例如:10");
$form->text('first_commission', '直推返积分比例')->help("比例如:30");
$form->text('second_commission', '间推返积分比例')->help("比例如:15");
$form->text('merchant_commission', '商家佣金比例')->help("比例如:10");
$form->text('sale', '销量');
$form->text('high_opinion', '好评率');
......
......@@ -21,11 +21,12 @@
use DateTime;
use DateTimeZone;
use Exception;
use App\Models\UserPointChangeRec;
class OrderController extends BaseController
{
//立即购买-确认订单
//立即购买-确认订单【好像没什么用】
public function CheckoutBuyOrder(Request $request)
{
$userObj = $request->user();
......@@ -62,7 +63,7 @@ public function CheckoutBuyOrder(Request $request)
return $this->JsonResponse($data);
}
//创建订单--立即购买
//创建订单[单商品]
public function CreateBuyOrder(Request $request)
{
$orderObj = new OrderInfoModel();
......@@ -82,6 +83,11 @@ public function CreateBuyOrder(Request $request)
$consignee = $request->consignee ?? '';
//$address_id = $request->address_id ?? 0; //地址ID
$pay_type = $request->pay_type; //支付方式 1:微信 2:积分 3:混合
if (!in_array($pay_type, [1, 2, 3])) {
return $this->JsonResponse('', '请选择支付方式', 500);
}
$goodsObj = GoodModel::find($goods_id);
if (!$goodsObj) {
return $this->JsonResponse('', '参数错误', 201);
......@@ -116,6 +122,36 @@ public function CreateBuyOrder(Request $request)
//商品总价
$total_price = ($good_price * $num);
$point_amount = $total_price;//积分抵扣金额
$order_amount=$total_price;//订单金额
$order_status = 0;
$pay_status = 0;
//判断支付方式
if ($pay_type == 1 ) {
$point_amount=0;//不用积分支付
}else if ($pay_type == 2 ) {
if ($userObj->balance < $total_price) {
return $this->JsonResponse('', '积分不足!', 500);
}
//扣除积分
if (!UserPointChangeRec::pointChangeRecord($userObj->id, $total_price, 0, 2)) {
return $this->JsonResponse('', '网络异常,积分抵扣失败!', 500);
}
$order_amount=0;
$order_status = 1;
$pay_status = 1;
}else if($pay_type == 3){
if ($userObj->balance >= $total_price) {
return $this->JsonResponse('', '积分充足,可选择积分支付!', 500);
}
$point_amount=$userObj->balance;
$order_amount=$total_price-$point_amount;
}
$tmp = [];
$tmp['goods_id'] = $goods_id;
$tmp['goods_number'] = $num;
......@@ -135,7 +171,10 @@ public function CreateBuyOrder(Request $request)
$orderObj->consignee = $consignee;
$orderObj->mobile = $phone;
$orderObj->goods_amount = $total_price;
$orderObj->order_amount = $total_price;
$orderObj->order_amount = $order_amount;
$orderObj->point_amount = $point_amount;
$orderObj->order_status = $order_status;
$orderObj->pay_status = $pay_status;
$orderObj->goods_sn = $goodObj->goods_sn;
$orderObj->delivery_type = $delivery_type;
$orderObj->merchant_id = $merchant_id;
......@@ -159,7 +198,7 @@ public function CreateBuyOrder(Request $request)
}
}
//创建订单
//创建订单[购物车]
public function CreateOrder(Request $request)
{
$orderObj = new OrderInfoModel();
......@@ -175,6 +214,12 @@ public function CreateOrder(Request $request)
$area = $request->area ?? '';
$phone = $request->phone ? $request->phone : $userObj->phone;
$consignee = $request->consignee ?? '';
$pay_type = $request->pay_type; //支付方式 1:微信 2:积分 3:混合
if (!in_array($pay_type, [1, 2, 3])) {
return $this->JsonResponse('', '请选择支付方式', 500);
}
if (!$catKey) {
return $this->JsonResponse('', '参数错误', 201);
}
......@@ -184,13 +229,6 @@ public function CreateOrder(Request $request)
return $this->JsonResponse('', '购物车无资源', 500);
}
$userAddress = '';
// if ($address_id) {
// $userAddress = UserAddress::where(['uid' => $user_id, 'id' => $address_id])->first();
// if (!$userAddress) {
// return $this->JsonResponse('', '请选择收货地址', 500);
// }
// }
$order_sn = $this->getOrderSn();
DB::beginTransaction();
try {
......@@ -239,6 +277,37 @@ public function CreateOrder(Request $request)
if ($total_price == 0) {
return $this->JsonResponse('', '参数错误', 201);
}
$point_amount = $total_price; //积分抵扣金额
$order_amount = $total_price; //订单金额
$order_status = 0;
$pay_status = 0;
//判断支付方式
if ($pay_type == 1) {
$point_amount = 0; //不用积分支付
} else if ($pay_type == 2) {
if ($userObj->balance < $total_price) {
return $this->JsonResponse('', '积分不足!', 500);
}
//扣除积分
if (!UserPointChangeRec::pointChangeRecord($userObj->id, $total_price, 0, 2)) {
return $this->JsonResponse('', '网络异常,积分抵扣失败!', 500);
}
$order_amount = 0;
$order_status = 1;
$pay_status = 1;
} else if ($pay_type == 3) {
if ($userObj->balance >= $total_price) {
return $this->JsonResponse('', '积分充足,可选择积分支付!', 500);
}
$point_amount = $userObj->balance;
$order_amount = $total_price - $point_amount;
}
$orderObj->order_sn = $order_sn;
$orderObj->user_id = $user_id;
$orderObj->address = $address;
......@@ -246,7 +315,10 @@ public function CreateOrder(Request $request)
$orderObj->consignee = $consignee;
$orderObj->mobile = $phone;
$orderObj->goods_amount = $total_price;
$orderObj->order_amount = $total_price;
$orderObj->order_amount = $order_amount;
$orderObj->point_amount = $point_amount;
$orderObj->order_status = $order_status;
$orderObj->pay_status = $pay_status;
$orderObj->delivery_type = $delivery_type;
$orderObj->merchant_id = $merchant_id;
$orderObj->store_id = $store_id;
......@@ -288,10 +360,9 @@ private function getOrderSn()
return $order_sn;
}
//订单支付
//订单支付-微信
public function pay(Request $request)
{
$order_id = $request->order_id ?? 0;
$openid = $request->user()->openid;
DB::beginTransaction();
......@@ -319,6 +390,7 @@ public function pay(Request $request)
}
}
//付款回调
public function payNotify(Request $request)
{
......@@ -531,7 +603,7 @@ public function scanCodeVerifi(Request $request)
$access_token = $mpTokenObj->getAccessToken();
$url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" . $access_token;
Log::add('发货录入url:', $url);
$data = [
"order_key" => [
......@@ -560,6 +632,9 @@ public function scanCodeVerifi(Request $request)
$orderObj->save();
}
//更新积分状态为解冻中,解冻7天
userPointChangeRec::pointUnfreeze($orderObj);
DB::commit();
} catch (Exception $e) {
DB::rollBack();
......@@ -770,7 +845,7 @@ public function orderCollect(Request $request)
//手动刷新token
public function refreshTokenLYK(Request $request)
......@@ -779,4 +854,81 @@ public function refreshTokenLYK(Request $request)
$access_token = $mpTokenObj->getAccessToken(true);
return $this->JsonResponse($access_token);
}
/**
* 轮询查询解冻结束的积分,改成已完成,积分转入余额
* @param Request $request HTTP请求对象
* @return \Illuminate\Http\JsonResponse 返回JSON响应
*
* 功能说明:
* 1. 查询所有解冻中(point_state=2)且解冻结束日期小于当前时间的积分记录
* 2. 将这些记录的状态更新为已完成(point_state=1)
* 3. 将对应的积分金额转入用户余额
*
* 使用示例:
* GET /api/order/point-unfreeze-complete
*
* 注意事项:
* 1. 需要定时调用此接口(如每分钟或每小时)
* 2. 操作在事务中进行,保证数据一致性
* 3. 记录操作日志以便追踪
*/
public function pointUnfreezeComplete(Request $request)
{
DB::beginTransaction();
try {
// 查询需要处理的积分记录
$records = DB::table('user_point_change_rec')
->where('point_state', 2) // 解冻中
->where('freeze_end_date', '<=', date('Y-m-d H:i:s')) // 解冻已结束
->get();
if ($records->isEmpty()) {
Log::add('point_unfreeze_complete', '没有需要处理的解冻积分记录');
return $this->JsonResponse([], '没有需要处理的解冻积分记录');
}
foreach ($records as $record) {
// 更新记录状态为已完成
DB::table('user_point_change_rec')
->where('id', $record->id)
->update([
'point_state' => 1, // 已完成
'updated_at' => date('Y-m-d H:i:s')
]);
// 将积分转入用户余额
$user = UserModel::find($record->user_id);
$user->balance += $record->point_amount;
$user->save();
Log::add('point_unfreeze_complete', [
'user_id' => $record->user_id,
'point_amount' => $record->point_amount,
'record_id' => $record->id
]);
}
DB::commit();
return $this->JsonResponse([
'processed_count' => count($records)
], '解冻积分处理完成');
} catch (\Exception $e) {
DB::rollBack();
Log::add('point_unfreeze_complete_error', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
return $this->JsonResponse([], '解冻积分处理失败', 500);
}
}
}
......@@ -9,6 +9,9 @@
use App\Models\GoodSku;
use Exception;
use NwVVVS\AdapayCore\AdaPay\Payment;
use App\Models\OrderGoods;
use App\Models\OrderInfo;
use App\Models\UserPointChangeRec;
class Adapay
{
......@@ -102,6 +105,10 @@ public function payNotify($params = [])
$attr_id = $item->attr_id;
$mer_id = $item->merchant_id;
$goods_number = $item->goods_number;
//创建直推分积分记录
$this->createPointRecordByOrder($orderObj->id);
//更新此商品支付状态
$item->is_pay = 1;
$item->save();
......@@ -458,8 +465,6 @@ public function queryBalance($account_params)
/**
* 处理支付确认和分账逻辑
* @param string $order_no 订单号
......@@ -517,4 +522,114 @@ public static function handlePaymentConfirmAndDivide($order_no, $order_id)
return false;
}
}
/**
* 根据订单ID生成积分记录
* @param int $order_id 订单ID
* @return bool 处理结果
* @throws Exception 处理失败时抛出异常
*
* 功能说明:
* 1. 根据订单ID获取订单信息
* 2. 获取订单关联的用户信息
* 3. 获取用户的直推和间推关系
* 4. 为相关用户创建积分记录
*
* 使用示例:
* Adapay::createPointRecordByOrder(123);
*
* 注意事项:
* 1. 需要先创建UserPointRec模型类
* 2. 积分计算逻辑需要根据业务需求调整
*/
public static function createPointRecordByOrder($order_id)
{
DB::beginTransaction();
try {
// 获取订单信息
$orderObj = OrderInfo::find($order_id);
if (!$orderObj) {
throw new Exception('订单不存在');
}
// 获取用户信息
$userObj = User::find($orderObj->user_id);
if (!$userObj) {
throw new Exception('用户不存在');
}
// 获取直推和间推用户
$spuid = $userObj->spuid; // 直推用户ID
$second_spuid = $userObj->second_spuid; // 间推用户ID
//获取订单商品积分
$goodsObjList = DB::select('
SELECT
log.id,
log.goods_id,
loi.order_amount,
log.goods_price,
log.goods_number,
lg.first_commission,
lg.second_commission,
-- 计算总的一级佣金
(log.goods_number*log.goods_price * lg.first_commission)/100 AS total_first_commission,
-- 计算总的二级佣金
(log.goods_number*log.goods_price * lg.second_commission)/100 AS total_second_commission
FROM
li_order_goods log
LEFT JOIN
li_order_info loi ON loi.id = log.order_id
LEFT JOIN
li_goods lg ON lg.id = log.goods_id
WHERE
loi.pay_type=1 and log.order_id = ?;
', [$order_id]);
if (!$goodsObjList) {
Log::add('积分记录创建失败', [
'error' => '搜索为空',
'order_id' => $order_id
]);
}
// 购物车结算会有多个商品,多笔分佣,遍历商品列表,为每个商品创建积分记录
foreach ($goodsObjList as $goodsObj) {
//判断直推是否存在
if ($spuid) {
// 计算直推用户的积分
$total_first_commission = $goodsObj->total_first_commission;
// 为直推用户创建积分记录
UserPointChangeRec::pointChangeRecord ($spuid, $total_first_commission ,1,1,3, $order_id); //
}
//判断间推是否存在
if ($second_spuid) {
// 计算间推用户的积分
$total_second_commission = $goodsObj->total_second_commission;
// 为间推用户创建积分记录
UserPointChangeRec::pointChangeRecord ($second_spuid, $total_second_commission ,1,2,3, $order_id); //
}
}
DB::commit();
return true;
} catch (\Exception $e) {
Log::add('积分记录创建失败', [
'error' => $e->getMessage(),
'order_id' => $order_id
]);
DB::rollBack();
return false;
}
}
}
......@@ -14,17 +14,17 @@ class OrderDivideRecord extends Model
protected $table = 'order_divide_record';
public const COMMISSION_TYPE = [
1 => '直推佣金',//需求调整,不分账,改成积分,支付时抵扣
2 => '间推佣金',//需求调整,不分账,改成积分,支付时抵扣
1 => '直推佣金',//需求调整,不分账,改成积分,支付时抵扣
2 => '间推佣金',//需求调整,不分账,改成积分,支付时抵扣
3 => '用户取货佣金',
4 => '手动调账分账',
];
/**
* 收益分配
* order_id 订单ID
* payconfirm_no 发起确认支付,订单流水号
* @param int $order_id 订单ID
* @param string $payconfirm_no 发起确认支付,订单流水号
* @return array 包含分账成员和确认金额的数组
*/
public static function divide($order_id, $payconfirm_no = '')
{
......@@ -32,30 +32,14 @@ public static function divide($order_id, $payconfirm_no = '')
$orderObj = OrderInfo::find($order_id);
$merchant_id = $orderObj->merchant_id; //绑定的商户
$buyer_id = $orderObj->user_id; //下单用户ID
$userObj = User::find($buyer_id);
$spuid = $userObj->spuid; //直推分享人
$second_spuid = $userObj->second_spuid; //间推分享人
$total_amount = 0; //订单商品总价
$first_proportion = $second_proportion = $merchant_proportion = ''; //佣金比例
$merchant_proportion = ''; //佣金比例
$sp_ogid = ''; //参与分佣商品ID
$commissionPreData = [
'first_member_id' => '',
'first_amount' => 0,
'second_member_id' => '',
'second_amount' => 0,
'merchant_member_id' => '',
'merchant_amount' => 0,
];
//直推是否实名
$isFirstRealName = 0;
if ($spuid) {
$isFirstRealName = HfSettleAccount::where(['member_type' => 0, 'mid' => $spuid])->count();
}
//间推是否实名
$isSecondRealName = 0;
if ($second_spuid) {
$isSecondRealName = HfSettleAccount::where(['member_type' => 0, 'mid' => $second_spuid])->count();
}
//商户是否实名
$isMerchantRealName = 0;
if ($merchant_id) {
......@@ -70,39 +54,9 @@ public static function divide($order_id, $payconfirm_no = '')
//商品信息
$goodObj = Good::find($item->goods_id);
$merchant_commission = $goodObj->merchant_commission;
$first_commission = $goodObj->first_commission;
$second_commission = $goodObj->second_commission;
$first_proportion .= $goodObj->first_commission . ",";
$second_proportion .= $goodObj->second_commission . ",";
$merchant_proportion .= $goodObj->merchant_commission . ",";
$sp_ogid = $item->id . ",";
//直推佣金
if ($spuid && $first_commission >= 1 && $first_commission < 100) {
$divide_price = number_format($goods_amount * ($first_commission / 100), 2);
$spObj = User::find($spuid);
//汇付参数
if ($spObj->member_id) {
$spObj->total_revenue += $divide_price; //总余额记录
$spObj->save();
//self::addRecord($item->id, $order_id, $goods_amount, $divide_price, $first_commission, $spuid, $user_id, 1, $isExistAccount);
$commissionPreData['first_member_id'] = $spObj->member_id;
$commissionPreData['first_amount'] += $divide_price;
}
}
//间推佣金
if ($second_spuid && $second_commission >= 1 && $second_commission < 100) {
$divide_price = number_format($goods_amount * ($second_commission / 100), 2);
$spObj = User::find($second_spuid);
//汇付参数
if ($spObj->member_id) {
$spObj->total_revenue += $divide_price;
$spObj->save();
$commissionPreData['second_member_id'] = $spObj->member_id;
$commissionPreData['second_amount'] += $divide_price;
}
}
//商户分佣记录
if ($merchant_id && $merchant_commission >= 1 && $merchant_commission < 100) {
$divide_price = number_format($goods_amount * ($merchant_commission / 100), 2);
......@@ -118,22 +72,7 @@ public static function divide($order_id, $payconfirm_no = '')
}
}
}
//组合分账参数
//直推佣金
if ($spuid && $first_commission >= 1 && $first_commission < 100 && $commissionPreData['first_amount'] > 0) {
self::addRecord($sp_ogid, $order_id, $orderObj->goods_amount, $commissionPreData['first_amount'], $first_proportion, $spuid, $buyer_id, 1, $isFirstRealName, $payconfirm_no);
if ($isFirstRealName) {
array_push($div_members, ['member_id' => $commissionPreData['first_member_id'], 'amount' => sprintf("%.2f", $commissionPreData['first_amount']), 'fee_flag' => 'N']);
}
}
//间推佣金
if ($second_spuid && $second_commission >= 1 && $second_commission < 100 && $commissionPreData['second_amount'] > 0) {
self::addRecord($sp_ogid, $order_id, $orderObj->goods_amount, $commissionPreData['second_amount'], $second_proportion, $second_spuid, $buyer_id, 2, $isSecondRealName, $payconfirm_no);
if ($isSecondRealName) {
array_push($div_members, ['member_id' => $commissionPreData['second_member_id'], 'amount' => sprintf("%.2f", $commissionPreData['second_amount']), 'fee_flag' => 'N']);
}
}
//商户佣金
if ($merchant_id && $merchant_commission >= 1 && $merchant_commission < 100 && $commissionPreData['merchant_amount'] > 0) {
self::addRecord($sp_ogid, $order_id, $orderObj->goods_amount, $commissionPreData['merchant_amount'], $merchant_proportion, $merchant_id, $buyer_id, 3, $isMerchantRealName, $payconfirm_no);
......@@ -141,25 +80,33 @@ public static function divide($order_id, $payconfirm_no = '')
array_push($div_members, ['member_id' => $commissionPreData['merchant_member_id'], 'amount' => sprintf("%.2f", $commissionPreData['merchant_amount']), 'fee_flag' => 'N']);
}
}
//平台本身
$aimeiyuePrice = $total_amount - $commissionPreData['first_amount'] - $commissionPreData['second_amount'] - $commissionPreData['merchant_amount'];
$aimeiyuePrice = $total_amount - $commissionPreData['merchant_amount'];
self::addRecord($sp_ogid, $order_id, $orderObj->goods_amount, $aimeiyuePrice, '', 0, $buyer_id, 0, 1, $payconfirm_no);
array_push($div_members, ['member_id' => 0, 'amount' => sprintf("%.2f", $aimeiyuePrice), 'fee_flag' => 'Y']);
//确认分佣金额
if (!$isFirstRealName) {
$commissionPreData['first_amount'] = 0;
}
if (!$isSecondRealName) {
$commissionPreData['second_amount'] = 0;
}
if (!$isMerchantRealName) {
$commissionPreData['merchant_amount'] = 0;
}
$confirm_amt = $aimeiyuePrice + $commissionPreData['first_amount'] + $commissionPreData['second_amount'] + $commissionPreData['merchant_amount'];
$confirm_amt = $aimeiyuePrice + $commissionPreData['merchant_amount'];
return ['div_members' => $div_members, 'confirm_amt' => sprintf("%.2f", $confirm_amt)];
}
//新增分账记录
/**
* 新增分账记录
* @param string $og_id 订单商品ID
* @param int $order_id 订单ID
* @param float $goods_amount 商品金额
* @param float $divide_price 分账金额
* @param string $commission 佣金比例
* @param int $um_id 用户或商户ID
* @param int $buyer_id 买家ID
* @param int $sh_type 分账类型
* @param int $isExistAccount 是否实名
* @param string $payconfirm_no 支付确认编号
*/
public static function addRecord($og_id, $order_id, $goods_amount, $divide_price, $commission = '', $um_id = 0, $buyer_id, $sh_type = 0, $isExistAccount, $payconfirm_no)
{
$recordObj = new self();
......@@ -177,14 +124,13 @@ public static function addRecord($og_id, $order_id, $goods_amount, $divide_price
Log::add('订单分佣记录', $recordObj->toArray());
}
public function order()
{
return $this->belongsTo(OrderInfo::class, 'order_id', 'id');
return $this->belongsTo(OrderInfo::class, 'order_id', 'id');
}
public function users()
{
return $this->belongsTo(User::class, 'user_id', 'id');
return $this->belongsTo(User::class, 'user_id', 'id');
}
}
}
\ No newline at end of file
<?php
namespace App\Models;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use App\Command\Log;
use Illuminate\Support\Facades\DB;
use Exception;
use App\Models\User as UserModel;
class UserPointChangeRec extends Model
{
use HasDateTimeFormatter;
protected $table = 'user_point_change_rec';
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
/**
* 用户积分变更记录
* @param int $user_id 用户ID
* @param int $point_amount 积分数量
* @param int $change_type 变更类型:1-增加,0-减少 (默认1)
* @param int $source 积分来源:1-直推,2-间推,3-购物,9-手动补偿 (默认1)
* @param int $point_state 积分状态:1-已完成,2-解冻中,3-冻结中 (默认1)
* @param int $order_id 关联订单ID
* @param string $remark 备注信息
* @return bool 返回操作结果
*
* 功能说明:
* 1. 记录用户积分变更情况
* 2. 仅当point_state=1(已完成)时更新用户余额
* 3. 记录变更备注信息
*
* 使用示例:
* UserPointChangeRec::pointChangeRecord(1, 100, 1, 1, 1, '订单号:123456');
* 注意事项:
* 1. 积分数量必须为正整数
* 2. 变更类型必须为0或1
*/
public static function pointChangeRecord($user_id, $point_amount, $change_type = 1, $source = 1,$point_state = 1,$order_id='', $remark = '' )
{
DB::beginTransaction();
try {
// 参数验证
if ($point_amount <= 0) {
throw new Exception('积分数量必须大于0');
}
if (!in_array($change_type, [0, 1])) {
throw new Exception('变更类型参数错误');
}
// 创建积分变更记录
DB::table('user_point_change_rec')->insert([
'user_id' => $user_id,
'order_id' => $order_id,
'point_amount' => $point_amount,
'change_type' => $change_type,
'point_state' => $point_state,
'source' => $source,
'memo' => $remark,
'created_at' => date('Y-m-d H:i:s')
]);
// 仅当状态为已完成时更新用户余额
if ($point_state == 1) {
$user = UserModel::find($user_id);
if ($change_type == 1) {
$user->balance += $point_amount; // 增加积分
} else {
if ($user->balance < $point_amount) {
throw new Exception('积分不足');
}
$user->balance -= $point_amount; // 减少积分
}
$user->save();
}
DB::commit();
Log::addByName('pay', [
'user_id' => $user_id,
'point_amount' => $point_amount,
'change_type' => $change_type,
'point_state' => $point_state
]);
return true;
} catch (\Exception $e) {
DB::rollBack();
Log::addByName('pay', [
'user_id' => $user_id,
'point_amount' => $point_amount,
'error' => $e->getMessage()
]);
return false;
}
}
/**
* 用户积分解冻
* @param object $orderObj 订单对象
* @return bool 返回操作结果
*
* 功能说明:
* 1. 根据订单ID查找对应的积分变更记录
* 2. 将冻结中的积分状态更新为解冻中
* 3. 设置7天后的解冻结束日期
*
* 使用示例:
* $order = OrderInfo::find(123);
* UserPointChangeRec::point解冻($order);
*
* 注意事项:
* 1. 仅处理状态为冻结中(3)的积分记录
* 2. 解冻后状态变为解冻中(2)
* 3. 解冻结束日期为当前时间+7天
*/
public static function pointUnfreeze($orderObj)
{
DB::beginTransaction();
try {
// 查找该订单对应的冻结中积分记录
$records = DB::table('user_point_change_rec')
->where([
'order_id' => $orderObj->id,
'point_state' => 3, // 冻结中
'change_type' => 1 // 增加积分
])
->get();
if ($records->isEmpty()) {
Log::addByName('point_unfreeze', [
'order_id' => $orderObj->id,
'msg' => '没有找到可解冻的积分记录'
]);
}
// 设置7天后的解冻结束日期
$freezeEndDate = date('Y-m-d H:i:s', strtotime('+7 days'));
// 更新积分记录状态为解冻中
DB::table('user_point_change_rec')
->where('id', $records[0]->id)
->update([
'point_state' => 2, // 解冻中
'freeze_end_date' => $freezeEndDate,
'updated_at' => date('Y-m-d H:i:s')
]);
DB::commit();
Log::addByName('point_unfreeze', [
'order_id' => $orderObj->id,
'point_amount' => $records[0]->point_amount,
'freeze_end_date' => $freezeEndDate
]);
return true;
} catch (\Exception $e) {
DB::rollBack();
Log::addByName('point_unfreeze_error', [
'order_id' => $orderObj->id,
'error' => $e->getMessage()
]);
return false;
}
}
}
a:5:{s:6:"_token";s:40:"lDy1gvxe9uvq34XAskYEPOP3pXUIsMNLmOm9YdYG";s:52:"login_admin_59ba36addc2b2f9401580f014c7f58ea4e30989d";i:1;s:5:"admin";a:1:{s:4:"prev";a:0:{}}s:6:"_flash";a:2:{s:3:"new";a:0:{}s:3:"old";a:0:{}}s:9:"_previous";a:1:{s:3:"url";s:27:"http://amy-test.yyinhong.cn";}}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment