@@ -0,0 +1,242 @@
|
||||
<?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',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\console;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\console\input\Option;
|
||||
use think\Db;
|
||||
use addons\shopro\service\partner\PartnerRewardService;
|
||||
use addons\shopro\service\commission\Agent as AgentService;
|
||||
use app\admin\model\shopro\commission\Log as LogModel;
|
||||
use fast\Random;
|
||||
|
||||
/**
|
||||
* 空库验收:A 推 B 下一单,核对推三返一
|
||||
*/
|
||||
class PartnerDemo extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('shopro:partner:demo')
|
||||
->addOption('amount', 'a', Option::VALUE_OPTIONAL, '买家订单实付', '1398')
|
||||
->addOption('refund', 'r', Option::VALUE_NONE, '结算后再整单退回')
|
||||
->addOption('venue', null, Option::VALUE_NONE, '为买家B开通酒道馆')
|
||||
->addOption('fix-team', null, Option::VALUE_NONE, '只重算团队人数并为B开通酒道馆,不新建订单')
|
||||
->setDescription('创建测试品酒师A/买家B并结算一单,验收推三返一');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
if ($input->getOption('fix-team')) {
|
||||
return $this->fixTeam($output);
|
||||
}
|
||||
|
||||
$amount = round((float) $input->getOption('amount'), 2);
|
||||
if ($amount <= 0) {
|
||||
$output->writeln('amount must > 0');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$goodsId = Db::name('shopro_commission_goods')->where('status', 1)->value('goods_id');
|
||||
if (!$goodsId) {
|
||||
$output->writeln('no commission goods, run shopro:partner:install first');
|
||||
return 1;
|
||||
}
|
||||
$goods = Db::name('shopro_goods')->where('id', $goodsId)->find();
|
||||
|
||||
$result = Db::transaction(function () use ($amount, $goods, $input) {
|
||||
$a = $this->ensureUser('13800000001', '测试品酒师A');
|
||||
$b = $this->ensureUser('13800000002', '测试买家B', $a['id']);
|
||||
$this->ensureAgent($a['id'], 1);
|
||||
$this->ensureAgent($b['id'], 1);
|
||||
if ($input->getOption('venue')) {
|
||||
$this->openVenue($b['id']);
|
||||
}
|
||||
|
||||
$service = new PartnerRewardService();
|
||||
$selfOrder = $this->createPaidOrder($a['id'], $goods, 1);
|
||||
$service->touchSelfOrder($a['id'], $selfOrder['paid_time']);
|
||||
$service->settleOrder($selfOrder['id']);
|
||||
|
||||
$buyOrder = $this->createPaidOrder($b['id'], $goods, $amount);
|
||||
$service->touchSelfOrder($b['id'], $buyOrder['paid_time']);
|
||||
$service->settleOrder($buyOrder['id']);
|
||||
|
||||
if ($input->getOption('refund')) {
|
||||
$service->refund($buyOrder['id']);
|
||||
}
|
||||
|
||||
return [
|
||||
'a' => $a,
|
||||
'b' => $b,
|
||||
'self_order_id' => $selfOrder['id'],
|
||||
'buy_order_id' => $buyOrder['id'],
|
||||
'refunded' => (bool) $input->getOption('refund'),
|
||||
];
|
||||
});
|
||||
|
||||
$rewards = Db::name('shopro_partner_reward')
|
||||
->where('order_id', $result['buy_order_id'])
|
||||
->select();
|
||||
$aWallet = Db::name('user')->where('id', $result['a']['id'])->value('commission');
|
||||
$plan = Db::name('shopro_partner_voucher_plan')->where('order_id', $result['buy_order_id'])->find();
|
||||
$seq = Db::name('shopro_commission_agent')->where('user_id', $result['a']['id'])->value('direct_order_seq');
|
||||
|
||||
$output->writeln('A=' . $result['a']['id'] . ' ' . $result['a']['nickname'] . ' mobile=13800000001');
|
||||
$output->writeln('B=' . $result['b']['id'] . ' ' . $result['b']['nickname'] . ' mobile=13800000002 parent=' . $result['a']['id']);
|
||||
$output->writeln('A自购单=' . $result['self_order_id'] . ' B下单=' . $result['buy_order_id']);
|
||||
$output->writeln('A直推序号=' . $seq . ' A佣金钱包=' . $aWallet);
|
||||
$output->writeln('B券计划=' . json_encode($plan ?: [], JSON_UNESCAPED_UNICODE));
|
||||
foreach ($rewards as $row) {
|
||||
$output->writeln(sprintf(
|
||||
'reward id=%s type=%s user=%s amount=%s status=%s formula=%s',
|
||||
$row['id'],
|
||||
$row['reward_type'],
|
||||
$row['user_id'],
|
||||
$row['amount'],
|
||||
$row['status'],
|
||||
$this->formulaOf($row)
|
||||
));
|
||||
}
|
||||
$this->recountTeam($result['b']['id']);
|
||||
$this->recountTeam($result['a']['id']);
|
||||
$output->writeln($result['refunded'] ? 'shopro:partner:demo refunded' : 'shopro:partner:demo done');
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function fixTeam(Output $output)
|
||||
{
|
||||
$a = $this->ensureUser('13800000001', '测试品酒师A');
|
||||
$b = $this->ensureUser('13800000002', '测试买家B', $a['id']);
|
||||
$this->ensureAgent($a['id'], 1);
|
||||
$this->ensureAgent($b['id'], 1);
|
||||
$this->openVenue($b['id']);
|
||||
$this->recountTeam($b['id']);
|
||||
$this->recountTeam($a['id']);
|
||||
|
||||
$aAgent = Db::name('shopro_commission_agent')->where('user_id', $a['id'])->find();
|
||||
$bAgent = Db::name('shopro_commission_agent')->where('user_id', $b['id'])->find();
|
||||
$output->writeln('A=' . $a['id'] . ' users=' . intval($aAgent['child_user_count_1'] ?? 0) . '/' . intval($aAgent['child_user_count_all'] ?? 0) . ' agents=' . intval($aAgent['child_agent_count_1'] ?? 0) . '/' . intval($aAgent['child_agent_count_all'] ?? 0));
|
||||
$output->writeln('B=' . $b['id'] . ' is_venue=' . intval($bAgent['is_venue'] ?? 0) . ' level=' . intval($bAgent['level'] ?? 0));
|
||||
$output->writeln('shopro:partner:demo fix-team done');
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function openVenue($userId)
|
||||
{
|
||||
$now = time();
|
||||
$changed = Db::name('shopro_commission_agent')->where('user_id', $userId)->update([
|
||||
'is_venue' => 1,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
if ($changed) {
|
||||
LogModel::add($userId, 'agent', ['type' => 'identity', 'field' => 'is_venue', 'value' => 1], null, '演示开通酒道馆');
|
||||
}
|
||||
}
|
||||
|
||||
protected function recountTeam($userId)
|
||||
{
|
||||
(new AgentService($userId))->runAgentUpgradePlan(false);
|
||||
}
|
||||
|
||||
protected function formulaOf($row)
|
||||
{
|
||||
$ext = $row['ext'] ?? '';
|
||||
if (is_string($ext)) {
|
||||
$ext = json_decode($ext, true) ?: [];
|
||||
}
|
||||
return $ext['formula'] ?? ($ext['skip_reason'] ?? '');
|
||||
}
|
||||
|
||||
protected function ensureUser($mobile, $nickname, $parentId = 0)
|
||||
{
|
||||
$user = Db::name('user')->where('mobile', $mobile)->find();
|
||||
if ($user) {
|
||||
if ($parentId && intval($user['parent_user_id']) !== intval($parentId)) {
|
||||
Db::name('user')->where('id', $user['id'])->update(['parent_user_id' => $parentId]);
|
||||
$user['parent_user_id'] = $parentId;
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
$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)
|
||||
{
|
||||
$agent = Db::name('shopro_commission_agent')->where('user_id', $userId)->find();
|
||||
$now = time();
|
||||
if ($agent) {
|
||||
Db::name('shopro_commission_agent')->where('user_id', $userId)->update([
|
||||
'level' => $level,
|
||||
'status' => 'normal',
|
||||
]);
|
||||
return;
|
||||
}
|
||||
Db::name('shopro_commission_agent')->insert([
|
||||
'user_id' => $userId,
|
||||
'level' => $level,
|
||||
'status' => 'normal',
|
||||
'apply_num' => 1,
|
||||
'become_time' => $now,
|
||||
'createtime' => $now,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function createPaidOrder($userId, $goods, $payFee)
|
||||
{
|
||||
$now = time();
|
||||
$orderId = Db::name('shopro_order')->insertGetId([
|
||||
'type' => 'goods',
|
||||
'order_sn' => 'PD' . date('YmdHis') . 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_demo' => 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_demo' => 1], JSON_UNESCAPED_UNICODE),
|
||||
'createtime' => $now,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
return [
|
||||
'id' => $orderId,
|
||||
'user_id' => $userId,
|
||||
'pay_fee' => $payFee,
|
||||
'paid_time' => $now,
|
||||
'status' => 'paid',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\console;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\Db;
|
||||
use app\common\library\Menu;
|
||||
|
||||
class PartnerInstall extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('shopro:partner:install')
|
||||
->setDescription('导入合伙股东表结构/配置,并升级后台菜单');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$prefix = config('database.prefix') ?: 'fa_';
|
||||
$output->writeln('prefix=' . $prefix);
|
||||
|
||||
$this->ensureAgentColumns($prefix, $output);
|
||||
$this->runSqlFile($prefix, $output);
|
||||
$this->applyBusinessConfig($output);
|
||||
$this->enableCommissionGoods($output);
|
||||
$this->backfillTasters($output);
|
||||
$this->backfillLastSelfOrder($output);
|
||||
$this->upgradeMenu($output);
|
||||
$this->clearConfigCache($output);
|
||||
$output->writeln('shopro:partner:install done');
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function ensureAgentColumns($prefix, Output $output)
|
||||
{
|
||||
$table = $prefix . 'shopro_commission_agent';
|
||||
$columns = [
|
||||
'is_venue' => "tinyint(1) NOT NULL DEFAULT 0 COMMENT '酒道馆'",
|
||||
'is_county' => "tinyint(1) NOT NULL DEFAULT 0 COMMENT '区县代理'",
|
||||
'county_code' => "varchar(20) NOT NULL DEFAULT '' COMMENT '区县编码'",
|
||||
'direct_order_seq' => "int(11) NOT NULL DEFAULT 0 COMMENT '直推合格单累计'",
|
||||
'madow' => "decimal(20,8) NOT NULL DEFAULT 0.00000000 COMMENT '马豆'",
|
||||
'voucher_total' => "decimal(12,2) NOT NULL DEFAULT 0.00 COMMENT '兑换券总额'",
|
||||
'voucher_released' => "decimal(12,2) NOT NULL DEFAULT 0.00 COMMENT '兑换券已释放'",
|
||||
'voucher_used' => "decimal(12,2) NOT NULL DEFAULT 0.00 COMMENT '兑换券已使用'",
|
||||
'last_self_order_time' => "bigint(16) DEFAULT NULL COMMENT '本人最近合格订单时间'",
|
||||
];
|
||||
foreach ($columns as $name => $ddl) {
|
||||
if ($this->columnExists($table, $name)) {
|
||||
$output->writeln("skip column {$name}");
|
||||
continue;
|
||||
}
|
||||
Db::execute("ALTER TABLE `{$table}` ADD COLUMN `{$name}` {$ddl}");
|
||||
$output->writeln("add column {$name}");
|
||||
}
|
||||
$indexes = [
|
||||
'idx_venue' => 'is_venue',
|
||||
'idx_county' => 'is_county',
|
||||
'idx_last_self_order' => 'last_self_order_time',
|
||||
];
|
||||
foreach ($indexes as $idx => $col) {
|
||||
if ($this->indexExists($table, $idx)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Db::execute("ALTER TABLE `{$table}` ADD KEY `{$idx}` (`{$col}`)");
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('index ' . $idx . ' ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function runSqlFile($prefix, Output $output)
|
||||
{
|
||||
$file = (defined('ADDON_PATH') ? ADDON_PATH : ROOT_PATH . 'addons/') . 'shopro/upgrade/partner_v2.sql';
|
||||
if (!is_file($file)) {
|
||||
$output->writeln('sql file missing');
|
||||
return;
|
||||
}
|
||||
$sql = str_replace('__PREFIX__', $prefix, file_get_contents($file));
|
||||
$sql = preg_replace('/^--.*$/m', '', $sql);
|
||||
$parts = array_filter(array_map('trim', explode(';', $sql)));
|
||||
foreach ($parts as $part) {
|
||||
if ($part === '' || stripos($part, 'ALTER TABLE') !== false && stripos($part, 'shopro_commission_agent') !== false) {
|
||||
continue;
|
||||
}
|
||||
if (stripos($part, 'SET @') === 0) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
Db::execute($part);
|
||||
$output->writeln('ok: ' . mb_substr(preg_replace('/\s+/', ' ', $part), 0, 80));
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('skip: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function upgradeMenu(Output $output)
|
||||
{
|
||||
try {
|
||||
$info = get_addon_info('shopro');
|
||||
$class = get_addon_class('shopro');
|
||||
$addon = new $class($info);
|
||||
if (method_exists($addon, 'upgrade')) {
|
||||
$addon->upgrade();
|
||||
$output->writeln('menu upgraded');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('menu upgrade fail: ' . $e->getMessage());
|
||||
try {
|
||||
$shopro = new \ReflectionClass(\addons\shopro\Shopro::class);
|
||||
$method = $shopro->getMethod('getMenu');
|
||||
$method->setAccessible(true);
|
||||
$menu = $method->invoke(null);
|
||||
Menu::upgrade('shopro', $menu['new']);
|
||||
$output->writeln('menu upgraded via reflection');
|
||||
} catch (\Exception $ex) {
|
||||
$output->writeln('menu fail: ' . $ex->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function applyBusinessConfig(Output $output)
|
||||
{
|
||||
$force = [
|
||||
'shop.commission.partner_mode' => '1',
|
||||
'shop.commission.partner_enable' => '1',
|
||||
'shop.commission.agent_check' => '0',
|
||||
];
|
||||
foreach ($force as $code => $value) {
|
||||
$row = Db::name('shopro_config')->where('code', $code)->find();
|
||||
if ($row) {
|
||||
Db::name('shopro_config')->where('code', $code)->update(['value' => $value]);
|
||||
} else {
|
||||
Db::name('shopro_config')->insert([
|
||||
'code' => $code,
|
||||
'parent_code' => 'shop.commission',
|
||||
'name' => $code,
|
||||
'type' => 'boolean',
|
||||
'value' => $value,
|
||||
'weigh' => 80,
|
||||
]);
|
||||
}
|
||||
$output->writeln("config {$code}={$value}");
|
||||
}
|
||||
|
||||
$become = Db::name('shopro_config')->where('code', 'shop.commission.become_agent')->find();
|
||||
if ($become) {
|
||||
Db::name('shopro_config')->where('code', 'shop.commission.become_agent')->update([
|
||||
'value' => json_encode(['type' => 'user', 'value' => ''], JSON_UNESCAPED_UNICODE),
|
||||
]);
|
||||
$output->writeln('config become_agent=user');
|
||||
}
|
||||
|
||||
$form = Db::name('shopro_config')->where('code', 'shop.commission.agent_form')->find();
|
||||
if ($form) {
|
||||
$val = json_decode($form['value'], true);
|
||||
if (!is_array($val)) {
|
||||
$val = [];
|
||||
}
|
||||
$val['status'] = '0';
|
||||
Db::name('shopro_config')->where('code', 'shop.commission.agent_form')->update([
|
||||
'value' => json_encode($val, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
|
||||
]);
|
||||
$output->writeln('config agent_form.status=0');
|
||||
}
|
||||
|
||||
$level = Db::name('shopro_config')->where('code', 'shop.commission.level')->find();
|
||||
if ($level && intval($level['value']) < 1) {
|
||||
Db::name('shopro_config')->where('code', 'shop.commission.level')->update(['value' => '1']);
|
||||
$output->writeln('config level=1');
|
||||
}
|
||||
}
|
||||
|
||||
protected function enableCommissionGoods(Output $output)
|
||||
{
|
||||
$goodsIds = Db::name('shopro_goods')->where('status', 'up')->column('id');
|
||||
$count = 0;
|
||||
foreach ($goodsIds as $goodsId) {
|
||||
$row = Db::name('shopro_commission_goods')->where('goods_id', $goodsId)->find();
|
||||
if ($row) {
|
||||
if (intval($row['status']) !== 1) {
|
||||
Db::name('shopro_commission_goods')->where('goods_id', $goodsId)->update(['status' => 1]);
|
||||
$count++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Db::name('shopro_commission_goods')->insert([
|
||||
'goods_id' => $goodsId,
|
||||
'self_rules' => 0,
|
||||
'commission_rules' => '{}',
|
||||
'status' => 1,
|
||||
'commission_config' => null,
|
||||
'commission_order_status' => 1,
|
||||
]);
|
||||
$count++;
|
||||
}
|
||||
$output->writeln("commission goods enabled: {$count}/" . count($goodsIds));
|
||||
}
|
||||
|
||||
protected function backfillTasters(Output $output)
|
||||
{
|
||||
$userIds = Db::name('user')->column('id');
|
||||
$now = time();
|
||||
$count = 0;
|
||||
foreach ($userIds as $userId) {
|
||||
$exists = Db::name('shopro_commission_agent')->where('user_id', $userId)->find();
|
||||
if ($exists) {
|
||||
continue;
|
||||
}
|
||||
Db::name('shopro_commission_agent')->insert([
|
||||
'user_id' => $userId,
|
||||
'level' => 1,
|
||||
'status' => 'normal',
|
||||
'apply_num' => 1,
|
||||
'become_time' => $now,
|
||||
'createtime' => $now,
|
||||
'updatetime' => $now,
|
||||
]);
|
||||
$count++;
|
||||
}
|
||||
$output->writeln("backfill tasters: {$count}");
|
||||
}
|
||||
|
||||
protected function backfillLastSelfOrder(Output $output)
|
||||
{
|
||||
$rows = Db::name('shopro_order')
|
||||
->where('status', 'in', ['paid', 'completed'])
|
||||
->where('paid_time', '>', 0)
|
||||
->field('user_id, max(paid_time) as last_paid')
|
||||
->group('user_id')
|
||||
->select();
|
||||
$count = 0;
|
||||
foreach ($rows as $row) {
|
||||
$agent = Db::name('shopro_commission_agent')->where('user_id', $row['user_id'])->find();
|
||||
if (!$agent || !empty($agent['last_self_order_time'])) {
|
||||
continue;
|
||||
}
|
||||
Db::name('shopro_commission_agent')->where('user_id', $row['user_id'])->update([
|
||||
'last_self_order_time' => intval($row['last_paid']),
|
||||
]);
|
||||
$count++;
|
||||
}
|
||||
$output->writeln("backfill last_self_order_time: {$count}");
|
||||
}
|
||||
|
||||
protected function clearConfigCache(Output $output)
|
||||
{
|
||||
$codes = [
|
||||
'shop.commission',
|
||||
'shop.commission.partner_mode',
|
||||
'shop.commission.partner_enable',
|
||||
'shop.commission.become_agent',
|
||||
'shop.commission.agent_form',
|
||||
'shop.commission.agent_check',
|
||||
'shop.commission.level',
|
||||
];
|
||||
foreach ($codes as $code) {
|
||||
cache('config:' . $code, null);
|
||||
}
|
||||
$output->writeln('config cache cleared');
|
||||
}
|
||||
|
||||
protected function columnExists($table, $column)
|
||||
{
|
||||
$database = config('database.database');
|
||||
$row = Db::query("SELECT COUNT(*) AS c FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND COLUMN_NAME = ?", [$database, $table, $column]);
|
||||
return intval($row[0]['c'] ?? 0) > 0;
|
||||
}
|
||||
|
||||
protected function indexExists($table, $index)
|
||||
{
|
||||
$database = config('database.database');
|
||||
$row = Db::query("SELECT COUNT(*) AS c FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ?", [$database, $table, $index]);
|
||||
return intval($row[0]['c'] ?? 0) > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\console;
|
||||
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\console\input\Option;
|
||||
use think\console\Command;
|
||||
use addons\shopro\service\partner\DailySettleService;
|
||||
|
||||
class PartnerSettle extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('shopro:partner:settle')
|
||||
->addOption('date', 'd', Option::VALUE_OPTIONAL, '结算日期 Y-m-d,默认昨天')
|
||||
->addOption('rerun', 'r', Option::VALUE_NONE, '删除当天日结后重算')
|
||||
->setDescription('合伙股东日结(马豆/馆县均分/兑换券释放)');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$date = $input->getOption('date');
|
||||
$rerun = (bool) $input->getOption('rerun');
|
||||
$output->writeln('partner settle start ' . ($date ?: 'yesterday') . ($rerun ? ' rerun' : ''));
|
||||
try {
|
||||
$report = (new DailySettleService())->run($date, $rerun);
|
||||
$output->writeln('partner settle done: ' . json_encode($report ? $report->toArray() : [], JSON_UNESCAPED_UNICODE));
|
||||
return 0;
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('partner settle fail: ' . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户