247 行
9.5 KiB
PHP
247 行
9.5 KiB
PHP
<?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',
|
||
];
|
||
}
|
||
}
|