文件
Henglianshutong/addons/shopro/console/PartnerAccept.php

243 行
10 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace addons\shopro\console;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use addons\shopro\service\partner\PartnerRewardService;
use addons\shopro\service\partner\DailySettleService;
use addons\shopro\service\commission\Agent as AgentService;
use fast\Random;
/**
* 联调验收剧本 §14 步骤 3–9
* 链:E(县) ← D(V6馆) ← A(品酒师) ← B(V5) ← C(V5) ← X(买家)
*/
class PartnerAccept extends Command
{
protected function configure()
{
$this->setName('shopro:partner:accept')
->setDescription('跑通合伙股东验收剧本:V5极差/平级、馆县拆分、30日暂停、退款、日结');
}
protected function execute(Input $input, Output $output)
{
$goodsId = Db::name('shopro_commission_goods')->where('status', 1)->value('goods_id');
if (!$goodsId) {
$output->writeln('no commission goods');
return 1;
}
$goods = Db::name('shopro_goods')->where('id', $goodsId)->find();
$service = new PartnerRewardService();
$yesterday = date('Y-m-d', strtotime('-1 day'));
$yTime = strtotime($yesterday . ' 12:00:00');
$users = Db::transaction(function () use ($goods, $service, $yTime) {
$e = $this->ensureUser('13800000005', '验收区县E');
$d = $this->ensureUser('13800000004', '验收酒道馆D', $e['id']);
$a = $this->ensureUser('13800000001', '测试品酒师A', $d['id']);
$b = $this->ensureUser('13800000002', '测试买家B', $a['id']);
$c = $this->ensureUser('13800000006', '验收创客C', $b['id']);
$x = $this->ensureUser('13800000007', '验收买家X', $c['id']);
$this->ensureAgent($e['id'], 1, ['is_county' => 1, 'county_code' => '110101']);
$this->ensureAgent($d['id'], 7, ['is_venue' => 1]); // V6
$this->ensureAgent($a['id'], 1);
$this->ensureAgent($b['id'], 6); // V5
$this->ensureAgent($c['id'], 6); // V5
$this->ensureAgent($x['id'], 1);
// 活跃:各受益人先自购一笔(金额 1)
foreach ([$e, $d, $a, $b, $c] as $u) {
$o = $this->createPaidOrder($u['id'], $goods, 1);
$service->touchSelfOrder($u['id'], $o['paid_time']);
}
// 步骤4:X 下 1398 → C 极差100,B 平级10,D 馆50,E 县50,C 推三398
$orderX = $this->createPaidOrder($x['id'], $goods, 1398);
$service->touchSelfOrder($x['id'], $orderX['paid_time']);
$service->settleOrder($orderX['id']);
// 步骤6:A 自购时间改到 31 天前,Y 挂在 A 下下一单 → A push3 skip
$aInactiveAt = time() - 31 * 86400;
Db::name('shopro_commission_agent')->where('user_id', $a['id'])->update([
'last_self_order_time' => $aInactiveAt,
]);
$y = $this->ensureUser('13800000008', '验收暂停测Y', $a['id']);
$this->ensureAgent($y['id'], 1);
$orderY = $this->createPaidOrder($y['id'], $goods, 1398);
$service->touchSelfOrder($y['id'], $orderY['paid_time']);
$service->settleOrder($orderY['id']);
// 步骤7:退 X 单
$service->refund($orderX['id']);
// 步骤8:补一笔「昨天」合格单供日结(挂在活跃的 B 下,买家 Z)
$z = $this->ensureUser('13800000009', '验收日结Z', $b['id']);
$this->ensureAgent($z['id'], 1);
// 恢复 B/D/E 活跃
foreach ([$b, $d, $e] as $u) {
$service->touchSelfOrder($u['id'], $yTime);
}
$orderZ = $this->createPaidOrder($z['id'], $goods, 1398, $yTime);
$service->touchSelfOrder($z['id'], $yTime);
$service->settleOrder($orderZ['id']);
$yd = date('Y-m-d', $yTime);
Db::name('shopro_partner_voucher_plan')->where('order_id', $orderZ['id'])->update([
'start_date' => $yd,
'released_days' => 0,
'released_amount' => 0,
'status' => 1,
]);
foreach ([$e, $d, $a, $b, $c, $x, $y, $z] as $u) {
(new AgentService($u['id']))->runAgentUpgradePlan(false);
}
return compact('a', 'b', 'c', 'd', 'e', 'x', 'y', 'z', 'orderX', 'orderY', 'orderZ') + ['settle_date' => $yd];
});
$settleDate = $users['settle_date'] ?? $yesterday;
$report1 = (new DailySettleService())->run($settleDate, true);
$report2 = (new DailySettleService())->run($settleDate, true);
$output->writeln('=== 验收账号 ===');
foreach (['e' => '区县E', 'd' => '馆D-V6', 'a' => '品酒师A', 'b' => '创客B-V5', 'c' => '创客C-V5', 'x' => '买家X'] as $k => $label) {
$u = $users[$k];
$output->writeln("{$label} id={$u['id']} mobile={$u['mobile']}");
}
$this->dumpRewards($output, '步骤4 X单(已退)', $users['orderX']['id']);
$this->dumpRewards($output, '步骤6 Y单(A暂停)', $users['orderY']['id']);
$this->dumpRewards($output, '步骤8 昨日Z单', $users['orderZ']['id']);
$output->writeln('=== 日结 ===');
$output->writeln('settle1 gmv=' . ($report1['gmv'] ?? $report1->gmv ?? '') . ' madow=' . ($report1['madow_pool'] ?? $report1->madow_pool ?? ''));
$output->writeln('settle2(重跑) gmv=' . ($report2['gmv'] ?? $report2->gmv ?? '') . ' madow=' . ($report2['madow_pool'] ?? $report2->madow_pool ?? ''));
$madowCount = Db::name('shopro_partner_reward')->where('settle_date', $settleDate)->where('reward_type', 'madow_share')->where('status', 1)->count();
$venueShare = Db::name('shopro_partner_reward')->where('settle_date', $settleDate)->where('reward_type', 'venue_share')->where('status', 1)->sum('amount');
$countyShare = Db::name('shopro_partner_reward')->where('settle_date', $settleDate)->where('reward_type', 'county_share')->where('status', 1)->sum('amount');
$voucherRel = Db::name('shopro_partner_reward')->where('settle_date', $settleDate)->where('reward_type', 'voucher_release')->where('status', 1)->sum('amount');
$output->writeln("日结发放 madow笔={$madowCount} venue_share={$venueShare} county_share={$countyShare} voucher_release={$voucherRel}");
$output->writeln('shopro:partner:accept done');
return 0;
}
protected function dumpRewards(Output $output, $title, $orderId)
{
$output->writeln("=== {$title} order={$orderId} ===");
$rows = Db::name('shopro_partner_reward')->where('order_id', $orderId)->select();
foreach ($rows as $row) {
$ext = json_decode($row['ext'] ?: '{}', true) ?: [];
$output->writeln(sprintf(
' %s user=%s amount=%s status=%s %s',
$row['reward_type'],
$row['user_id'],
$row['amount'],
$row['status'],
$ext['formula'] ?? ($ext['skip_reason'] ?? '')
));
}
}
protected function ensureUser($mobile, $nickname, $parentId = 0)
{
$user = Db::name('user')->where('mobile', $mobile)->find();
if ($user) {
$update = ['nickname' => $nickname];
if ($parentId) {
$update['parent_user_id'] = $parentId;
}
Db::name('user')->where('id', $user['id'])->update($update);
return Db::name('user')->where('id', $user['id'])->find();
}
$salt = Random::alnum();
$now = time();
$id = Db::name('user')->insertGetId([
'group_id' => 1,
'username' => $mobile,
'nickname' => $nickname,
'mobile' => $mobile,
'password' => md5(md5('Demo123456') . $salt),
'salt' => $salt,
'status' => 'normal',
'parent_user_id' => $parentId,
'jointime' => $now,
'createtime' => $now,
'updatetime' => $now,
]);
return Db::name('user')->where('id', $id)->find();
}
protected function ensureAgent($userId, $level, $extra = [])
{
$now = time();
$data = array_merge([
'level' => $level,
'status' => 'normal',
'is_venue' => 0,
'is_county' => 0,
'county_code' => '',
'updatetime' => $now,
], $extra);
$agent = Db::name('shopro_commission_agent')->where('user_id', $userId)->find();
if ($agent) {
Db::name('shopro_commission_agent')->where('user_id', $userId)->update($data);
return;
}
$data['user_id'] = $userId;
$data['apply_num'] = 1;
$data['become_time'] = $now;
$data['createtime'] = $now;
Db::name('shopro_commission_agent')->insert($data);
}
protected function createPaidOrder($userId, $goods, $payFee, $paidTime = null)
{
$now = $paidTime ?: time();
$orderId = Db::name('shopro_order')->insertGetId([
'type' => 'goods',
'order_sn' => 'PA' . date('YmdHis', $now) . Random::numeric(4),
'user_id' => $userId,
'goods_original_amount' => $payFee,
'goods_amount' => $payFee,
'order_amount' => $payFee,
'pay_fee' => $payFee,
'remain_pay_fee' => 0,
'status' => 'paid',
'paid_time' => $now,
'pay_mode' => 'online',
'platform' => 'H5',
'ext' => json_encode(['partner_accept' => 1], JSON_UNESCAPED_UNICODE),
'createtime' => $now,
'updatetime' => $now,
]);
Db::name('shopro_order_item')->insert([
'order_id' => $orderId,
'user_id' => $userId,
'goods_id' => $goods['id'],
'goods_type' => 'normal',
'goods_title' => $goods['title'] ?? 'demo',
'goods_image' => $goods['image'] ?? '',
'goods_original_price' => $payFee,
'goods_price' => $payFee,
'goods_num' => 1,
'pay_fee' => $payFee,
'ext' => json_encode(['is_commission' => 1, 'partner_accept' => 1], JSON_UNESCAPED_UNICODE),
'createtime' => $now,
'updatetime' => $now,
]);
return [
'id' => $orderId,
'user_id' => $userId,
'pay_fee' => $payFee,
'paid_time' => $now,
'status' => 'paid',
];
}
}