diff --git a/README_AUTO_OFFSHELF.md b/README_AUTO_OFFSHELF.md
new file mode 100644
index 0000000..ef047fd
--- /dev/null
+++ b/README_AUTO_OFFSHELF.md
@@ -0,0 +1,65 @@
+# 盒子自动下架功能文档
+
+## 功能说明
+
+该功能实现了当盒子的利润率低于配置阈值时自动下架盒子,以保证系统的盈利能力。
+
+## 实现逻辑
+
+1. 当盒子的抽奖次数达到配置的阈值后,系统开始检测该盒子的利润情况
+2. 计算公式:利润率 = (订单总价值 - 出货总价值) / 订单总价值 * 100%
+3. 如果利润率低于配置的下架利润阈值,则自动下架盒子
+4. 下架记录会保存到日志表中
+5. 系统会自动排除测试用户的数据,确保计算结果准确反映真实用户的情况
+
+## 安装步骤
+
+### 1. 创建日志表
+
+执行以下命令创建日志表:
+
+```bash
+php think CreateOffshelfLogTable
+```
+
+### 2. 添加定时任务
+
+将以下定时任务添加到系统的crontab中:
+
+```bash
+# 每小时执行一次盒子自动下架检测
+* */1 * * * cd /www/wwwroot/192.168.195.11 && php think AutoGoodsOffshelf >> /www/wwwroot/192.168.195.11/runtime/log/offshelf_$(date +\%Y\%m\%d).log 2>&1
+```
+
+## 使用说明
+
+### 在盒子编辑/添加页面中配置:
+
+1. **自动下架开关**:开启或关闭自动下架功能
+2. **下架利润值(%)**:当利润率低于此值时,盒子将自动下架
+3. **下架抽数阈值**:当盒子抽奖次数达到此阈值后,开始检测利润率
+
+### 查看下架记录
+
+可以在 `goods_offshelf_log` 表中查看所有自动下架的记录,包含以下信息:
+
+- 盒子ID
+- 当前利润率
+- 配置的下架利润阈值
+- 订单总价值
+- 出货总价值
+- 下架时间
+
+## 手动执行
+
+如需手动执行自动下架检测,可运行以下命令:
+
+```bash
+php think AutoGoodsOffshelf
+```
+
+## 注意事项
+
+1. 确保盒子配置了正确的下架利润值和抽数阈值
+2. 下架后的盒子状态将变为"已下架",需要手动重新上架
+3. 日志文件保存在 `runtime/log/` 目录下,文件名格式为 `offshelf_YYYYMMDD.log`
\ No newline at end of file
diff --git a/app/admin/controller/Statistics.php b/app/admin/controller/Statistics.php
index ae4db18..a6813fb 100755
--- a/app/admin/controller/Statistics.php
+++ b/app/admin/controller/Statistics.php
@@ -52,11 +52,26 @@ class Statistics extends Base
// 获取商品ID列表
$goodList = GoodsModel::where($whe)->column('id');
if (empty($goodList)) {
- return View::fetch("Statistics/profit", ['list' => [], 'count' => 0, 'page' => '']);
+
+ View::assign([
+
+ 'sum_dingdan' => 0,
+ 'sum_shiji' => 0,
+ 'sum_chuhuo' => 0,
+ 'price_all' => 0,
+ 'sum_shijilirun' => 0,
+ 'sum_dingdanlirun' => 0,
+ 'use_money_all' => 0,
+ 'order_total_all' => 0,
+ 'list' => [],
+ 'count' => 0,
+ 'page' => '',
+ ]);
+ return View::fetch("Statistics/profit");
}
// 获取测试用户ID列表
- $userList = User::where('istest', 1)->column('id');
+ $userList = User::where('istest', '>', 0)->column('id');
// 订单筛选条件
$whe3[] = ['user_id', 'not in', $userList];
@@ -165,6 +180,8 @@ class Statistics extends Base
$lirun = round($orderStats->order_zhe_total - $goodslistMoney, 2);
$liruns = round(($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon) - $goodslistMoney, 2);
+ $order_zhe_total=$orderStats->order_zhe_total;
+ $order_total=($orderStats->count_price + $orderStats->count_yue + $orderStats->use_integral + $orderStats->use_score + $orderStats->use_coupon);
return [
'order_count' => round($orderStats->count, 2),
'count_price' => round($orderStats->count_price, 2),
@@ -184,8 +201,8 @@ class Statistics extends Base
'goodslist_price' => round($goodslistMoney, 2),
'lirun' => $lirun,
'liruns' => $liruns,
- 'lirulv' => $goodslistMoney == 0 ? 0 : round(($lirun / $goodslistMoney) * 100, 2),
- 'lirulvs' => $goodslistMoney == 0 ? 0 : round(($liruns / $goodslistMoney) * 100, 2),
+ 'lirulv' => $goodslistMoney == 0 ? 0 : round(($order_zhe_total - $goodslistMoney) / $order_zhe_total * 100, 2),
+ 'lirulvs' => $goodslistMoney == 0 ? 0 : round(($order_total - $goodslistMoney) / $order_total * 100, 2),
];
@@ -200,7 +217,7 @@ class Statistics extends Base
public function DataStand(Request $request)
{
$userCount = User::count("id");
- $userList = User::where('istest', '=', 1)->field('id')->select();
+ $userList = User::where('istest', '>', 0)->field('id')->select();
$userArray = array_column($userList->toArray(), 'id');
// 本日充值金额
// $order_today = OrderModel::whereNotIn('user_id', $userArray)->where('status', '=', 1)->whereBetweenTime('addtime', $ranges['today_start'], $ranges['today_end'])->sum('price');
diff --git a/app/admin/view/Statistics/profit.html b/app/admin/view/Statistics/profit.html
index 480822c..45d4c3b 100755
--- a/app/admin/view/Statistics/profit.html
+++ b/app/admin/view/Statistics/profit.html
@@ -179,7 +179,7 @@
订单利润:
{if condition="$vo['liruns'] < 0"}
{$vo['liruns']}
- 订单利润率: {$vo['liruns']}%
+ 订单利润率: {$vo['lirulvs']}%
{elseif condition="$vo['liruns'] >= 0"}
{$vo['liruns']}
订单利润率: {$vo['lirulvs']}%
diff --git a/app/command/AutoGoodsOffshelf.php b/app/command/AutoGoodsOffshelf.php
new file mode 100644
index 0000000..97797bc
--- /dev/null
+++ b/app/command/AutoGoodsOffshelf.php
@@ -0,0 +1,74 @@
+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('自动下架任务执行完成!');
+ }
+}
\ No newline at end of file
diff --git a/app/command/CreateOffshelfLogTable.php b/app/command/CreateOffshelfLogTable.php
new file mode 100644
index 0000000..f9961f1
--- /dev/null
+++ b/app/command/CreateOffshelfLogTable.php
@@ -0,0 +1,60 @@
+setName('create:offshelf:log:table')
+ ->setDescription('创建盒子自动下架日志表');
+ }
+
+ protected function execute(Input $input, Output $output)
+ {
+ $output->writeln('开始创建盒子下架日志表...');
+
+ // 检查表是否已存在
+ $tableExists = false;
+ try {
+ $result = Db::query("SHOW TABLES LIKE 'goods_offshelf_log'");
+ $tableExists = !empty($result);
+ } catch (\Exception $e) {
+ $output->writeln('检查表是否存在时出错:' . $e->getMessage());
+ }
+
+ if ($tableExists) {
+ $output->writeln('盒子下架日志表已存在,无需创建。');
+ return;
+ }
+
+ // 创建日志表
+ try {
+ Db::execute("
+ CREATE TABLE `goods_offshelf_log` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `goods_id` int(11) NOT NULL COMMENT '盒子ID',
+ `profit_rate` decimal(10,2) NOT NULL COMMENT '当前利润率',
+ `xiajia_lirun` decimal(10,2) NOT NULL COMMENT '配置的下架利润阈值',
+ `order_total` decimal(10,2) NOT NULL COMMENT '订单总价值',
+ `goods_total` decimal(10,2) NOT NULL COMMENT '出货总价值',
+ `create_time` int(11) NOT NULL COMMENT '下架时间',
+ PRIMARY KEY (`id`),
+ KEY `idx_goods_id` (`goods_id`),
+ KEY `idx_create_time` (`create_time`)
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='盒子自动下架日志表';
+ ");
+
+ $output->writeln('盒子下架日志表创建成功!');
+ } catch (\Exception $e) {
+ $output->writeln('创建表时出错:' . $e->getMessage());
+ }
+ }
+}
\ No newline at end of file
diff --git a/config/console.php b/config/console.php
index 8a87421..cb7d6f1 100755
--- a/config/console.php
+++ b/config/console.php
@@ -5,5 +5,7 @@ return [
'RankMonth' => 'app\command\RankMonth',#月榜定榜
'Zd' => 'app\command\Zd',#自动发货
'UserStatisticsDay' => 'app\command\UserStatisticsDay',#自动发货
+ 'AutoGoodsOffshelf' => 'app\command\AutoGoodsOffshelf',#自动下架利润率过低的盒子
+ 'CreateOffshelfLogTable' => 'app\command\CreateOffshelfLogTable',#创建盒子自动下架日志表
],
];