manghe/app/command/AutoGoodsOffshelf.php
2025-04-10 02:46:53 +08:00

74 lines
3.0 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare (strict_types = 1);
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;
class AutoGoodsOffshelf extends Command
{
protected function configure()
{
// 指令配置
$this->setName('AutoGoodsOffshelf')
->setDescription('自动下架利润率过低的盒子');
}
protected function execute(Input $input, Output $output)
{
$output->writeln('开始检查需要下架的盒子...');
// 执行SQL查询获取需要检查的盒子列表
$goodsList = Db::query("
SELECT *,
(SELECT sum(order_zhe_total) FROM `order` WHERE status = 1 AND goods_id = g.id and user_id not in (select id from `user` where istest>0)) as order_zhe_total,
IFNULL((SELECT sum(goodslist_money) FROM order_list WHERE goods_id = g.id and user_id not in (select id from `user` where istest>0)), 0) goodslist_money
FROM (
SELECT id, xiajia_lirun, xiajia_auto_coushu,
IFNULL((SELECT count(1) FROM order_list WHERE goods_id = goods.id and user_id not in (select id from `user` where istest>0)), 0) AS choushu
FROM goods
WHERE is_auto_xiajia = 1 AND `status` = 1
) AS g
WHERE choushu > 0 AND choushu >= xiajia_auto_coushu
");
$count = 0;
// 遍历盒子列表,计算利润率并判断是否需要下架
foreach ($goodsList as $goods) {
// 如果出货价值为0跳过
if (empty($goods['goodslist_money'])) {
continue;
}
// 计算利润率:(订单价值 - 出货价值) / 订单价值 * 100
$profit = $goods['order_zhe_total'] - $goods['goodslist_money'];
$profitRate = ($profit / $goods['order_zhe_total']) * 100;
// 如果利润率小于配置的下架利润值,则下架盒子
if ($profitRate < $goods['xiajia_lirun']) {
// 更新盒子状态为已下架(状态值为0)
Db::name('goods')->where('id', $goods['id'])->update(['status' => 0]);
// 记录下架日志
Db::name('goods_offshelf_log')->insert([
'goods_id' => $goods['id'],
'profit_rate' => $profitRate,
'xiajia_lirun' => $goods['xiajia_lirun'],
'order_total' => $goods['order_zhe_total'],
'goods_total' => $goods['goodslist_money'],
'create_time' => time()
]);
$count++;
$output->writeln("盒子ID: {$goods['id']} 当前利润率: {$profitRate}% 下架阈值: {$goods['xiajia_lirun']}% 已自动下架");
}
}
$output->writeln("本次共下架 {$count} 个盒子");
$output->writeln('自动下架任务执行完成!');
}
}