Files
code-bang/backend/application/common/command/DemandDeal.php
T

150 lines
7.4 KiB
PHP
Raw Normal View History

2026-09-17 16:25:31 +08:00
<?php
namespace app\common\command;
use app\common\service\AccountLogService;
use app\common\service\AccountService;
use app\common\service\EasyWeChatService;
use app\common\service\OrderService;
2026-09-30 14:56:56 +08:00
use app\common\service\OrderShipService;
2026-10-14 14:44:01 +08:00
use app\common\service\PromotionService;
2026-09-17 16:25:31 +08:00
use app\common\service\SubscribeMessageService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\common\service\DemandService;
use think\Log;
use think\Db;
class DemandDeal extends Command
{
protected function configure()
{
$this->setName('demand:deal')->setDescription('bidding deal command!');
}
protected function execute(Input $input, Output $output)
{
$output->writeln("bidding:deal:");
$this->confirmReceiptTimeoutDeal($input, $output);
$this->demandProfitUnfreezeDeal($input, $output);
2026-10-14 14:44:01 +08:00
//兜底:结算交易完成但未结算的推广佣金(如后台直接改为已完成的订单)
PromotionService::settlePendingOrders();
2026-09-17 16:25:31 +08:00
}
/**
* 确认收货超时处理
* @param Input $input
* @param Output $output
* @return void
*/
protected function confirmReceiptTimeoutDeal(Input $input, Output $output)
{
$confirmReceiptTimeoutDay = intval(config('site.confirm_receipt_timeout_day') ?? 7);
$where = [
'status' => DemandService::STATUS_CONFIRM_RECEIPT,
'accept_status' => DemandService::$Common::SUCCESS,
];
DemandService::where($where)->chunk(100, function ($demandList) use ($confirmReceiptTimeoutDay) {
foreach ($demandList as $item) {
try {
Db::transaction(function () use ($item, $confirmReceiptTimeoutDay) {
2026-09-30 14:56:56 +08:00
//未发货的需求不自动确认收货,等待开发者发货交付
2026-09-30 16:57:26 +08:00
$ship = OrderShipService::getDemandLastShip($item->id);
2026-09-30 14:56:56 +08:00
if (!$ship) {
return;
}
//发货后N天未确认收货,则自动确认收货
if (time() > strtotime("+$confirmReceiptTimeoutDay day", intval($ship->createtime))) {
2026-09-17 16:25:31 +08:00
$item->status = DemandService::STATUS_FINISHED;
$item->settle_status = DemandService::$Common::ONGOING;
$saveRes = $item->save();
if (!$saveRes) {
exception('更新结算状态失败');
}
//订单状态改为已完成
$upRes = OrderService::where(['parent_order_no' => $item->parent_order_no])->update(['order_status' => OrderService::ORDER_STATUS_FINISHED]);
if (!$upRes) {
exception('更新订单状态失败');
}
2026-10-14 14:44:01 +08:00
//交易完成:结算推广佣金
PromotionService::settleOrders(['parent_order_no' => $item->parent_order_no]);
2026-09-17 16:25:31 +08:00
}
});
} catch (\Exception $e) {
Log::info('确认收货超时处理异常:' . json_encode(['file' => $e->getFile(), 'line' => $e->getLine(), 'error' => $e->getMessage(), 'item' => $item], JSON_UNESCAPED_UNICODE));
}
}
});
}
/**
* 订单收益解冻处理
* @param Input $input
* @param Output $output
* @return void
*/
protected function demandProfitUnfreezeDeal(Input $input, Output $output)
{
$where = [
'status' => DemandService::STATUS_FINISHED,
'settle_status' => DemandService::$Common::ONGOING,
];
DemandService::where($where)->with(['orders', 'winBidding'])->chunk(100, function ($demandList) {
foreach ($demandList as $item) {
try {
Db::transaction(function () use ($item) {
//根据订单列表查找冻结金额日志,根据冻结日志解冻冻结金额到余额
$userId = $item->winBidding->user_id;
2026-10-28 16:59:16 +08:00
$settleTotalAmount = '0';
2026-09-17 16:25:31 +08:00
foreach ($item->orders as $order) {
$logWhere = [
'user_id' => $userId,
'account_type' => AccountLogService::ACCOUNT_TYPE_FREEZE,
'type' => AccountLogService::TYPE_FREEZE,
'inc_or_exp' => AccountLogService::INC_EXP_INCREASE,
'refer_id' => $order->id,
'status' => AccountLogService::$Common::STATUS_OFF,
];
$log = AccountLogService::where($logWhere)->find();
if ($log) {
$amount = $log->amount;
2026-10-28 16:59:16 +08:00
$settleTotalAmount = bcadd($settleTotalAmount, (string)$amount, 2);
2026-09-17 16:25:31 +08:00
//写入余额日志
$title = OrderService::ORDER_TYPE_MAP[$order->order_type] ?? '需求收益';
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '解冻-冻结金额扣除', AccountLogService::TYPE_UNFREEZE, 0, AccountLogService::INC_EXP_EXPEND, $order->id, $order->total_amount);
$log_res2 = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, $userId, $amount, $title . '解冻-余额增加', AccountLogService::TYPE_PROFIT, 0, AccountLogService::INC_EXP_INCREASE, $order->id, $order->total_amount);
//减去冻结,加到余额里面
AccountService::updateData($userId, $amount, -$amount, 0, 0, $log_res->id);
//更新日志状态
$log->status = AccountLogService::$Common::STATUS_ON;
$saveRes = $log->save();
if (!$saveRes) {
exception('更新余额日志状态失败');
}
}
}
//更新结算状态
$item->settle_status = DemandService::$Common::SUCCESS;
$saveRes2 = $item->save();
if (!$saveRes2) {
exception('更新需求结算状态失败');
}
//发送结算完成通知
$msgData = [
'values' => [
$item->parent_order_no,
2026-10-28 16:59:16 +08:00
$settleTotalAmount,
2026-09-17 16:25:31 +08:00
SubscribeMessageService::MSG_MAP[SubscribeMessageService::MSG_TEMP_DEMAND_SETTLE]['remark'],
]
];
(new EasyWeChatService)->sendMsg($userId, SubscribeMessageService::MSG_TEMP_DEMAND_SETTLE, $msgData);
});
} catch (\Exception $e) {
Log::info('订单收益解冻处理异常:' . json_encode(['file' => $e->getFile(), 'line' => $e->getLine(), 'error' => $e->getMessage(), 'item' => $item], JSON_UNESCAPED_UNICODE));
}
}
});
}
}