108 lines
4.4 KiB
PHP
Executable File
108 lines
4.4 KiB
PHP
Executable File
<?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, xiajia_jine,
|
||
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;
|
||
}
|
||
|
||
// 计算利润:订单价值 - 出货价值
|
||
$profit = $goods['order_zhe_total'] - $goods['goodslist_money'];
|
||
|
||
// 下架标记
|
||
$needOffshelf = false;
|
||
$offshelfReason = '';
|
||
|
||
// 计算利润率
|
||
$profitRate = ($profit / $goods['order_zhe_total']) * 100;
|
||
|
||
// 基础日志信息
|
||
$baseLogInfo = "盒子ID: {$goods['id']} | 订单总额: {$goods['order_zhe_total']} | 出货总额: {$goods['goodslist_money']} | 利润: {$profit} | 利润率: ".round($profitRate, 2)."% | 抽数: {$goods['choushu']}";
|
||
|
||
// 分别判断下架金额和利润率条件
|
||
// 1. 下架金额判断
|
||
if (!empty($goods['xiajia_jine']) && $goods['xiajia_jine'] > 0) {
|
||
if ($profit < 0 && abs($profit) > $goods['xiajia_jine']) {
|
||
$needOffshelf = true;
|
||
$offshelfReason = "{$baseLogInfo} | 下架原因: 利润为{$profit},低于下架金额负{$goods['xiajia_jine']}";
|
||
}
|
||
}
|
||
|
||
// 2. 利润率判断(即使有下架金额也进行判断)
|
||
if (!$needOffshelf && $goods['xiajia_lirun'] > 0) {
|
||
if ($profitRate < $goods['xiajia_lirun']) {
|
||
$needOffshelf = true;
|
||
$offshelfReason = "{$baseLogInfo} | 下架原因: 利润率为".round($profitRate, 2)."%,低于下架阈值{$goods['xiajia_lirun']}%";
|
||
}
|
||
}
|
||
|
||
// 需要下架的处理
|
||
if ($needOffshelf) {
|
||
// 更新盒子状态为已下架(状态值为0)
|
||
Db::name('goods')->where('id', $goods['id'])->update(['status' => 0]);
|
||
|
||
// 计算利润率用于记录
|
||
$profitRate = ($profit / $goods['order_zhe_total']) * 100;
|
||
|
||
// 记录下架日志
|
||
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(),
|
||
'remarks' => $offshelfReason,
|
||
'is_read'=>0
|
||
]);
|
||
|
||
$count++;
|
||
$output->writeln("盒子ID: {$goods['id']} {$offshelfReason} 已自动下架");
|
||
}
|
||
}
|
||
|
||
$output->writeln("本次共下架 {$count} 个盒子");
|
||
$output->writeln('自动下架任务执行完成!');
|
||
}
|
||
}
|