206 lines
7.6 KiB
PHP
Executable File
206 lines
7.6 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\command;
|
|
|
|
use app\common\model\CardLevel;
|
|
use app\common\model\Goods;
|
|
use app\common\model\GoodsLock;
|
|
use app\common\model\ItemCard;
|
|
use app\common\model\OrderListSend;
|
|
use app\common\model\OrderSale;
|
|
use app\common\model\ProductOrder;
|
|
use app\common\model\ProductOrderList;
|
|
use app\common\model\ProfitPay;
|
|
use app\common\model\Shang;
|
|
use app\common\model\User;
|
|
use app\common\model\UserLevelCoupon;
|
|
use app\common\model\UserRecharge;
|
|
use app\common\model\Ads;
|
|
use app\common\model\CouponReceive as CouponReceiveModel;
|
|
use think\facade\Log;
|
|
use app\common\model\GoodsList;
|
|
use app\common\model\Order;
|
|
use app\common\model\OrderList;
|
|
use app\api\controller\Notify;
|
|
use think\console\Command;
|
|
use think\console\Input;
|
|
use think\console\input\Argument;
|
|
use think\console\input\Option;
|
|
use think\console\Output;
|
|
use think\facade\Db;
|
|
use app\common\service\RewardService;
|
|
class FlwOpen extends Command
|
|
{
|
|
|
|
|
|
protected function configure()
|
|
{
|
|
$this->setName('FlwOpen')->setDescription('福利屋开奖');
|
|
}
|
|
//cd /www/wwwroot/192.168.195.11 && php think FlwOpen
|
|
protected function execute(Input $input, Output $output)
|
|
{
|
|
//php /www/wwwroot/xinglanmh.shequtuangou.vip think FlwOpen
|
|
|
|
Log::info('福利屋开奖' . date('Y-m-d H:i:s', time()));
|
|
echo ('福利屋开奖' . date('Y-m-d H:i:s', time())) . PHP_EOL;
|
|
|
|
$start_time = time();
|
|
// 当前时间-1分钟
|
|
$end_time = time() - 60;
|
|
$goodslist = Goods::where('status', 1)->where('is_flw', 1)
|
|
->where('is_open', 0)
|
|
->where('open_time', '<=', $start_time)
|
|
// ->where('open_time', '>=', $end_time)
|
|
->select()
|
|
->toArray();
|
|
foreach ($goodslist as $k => $v) {
|
|
Log::info('福利屋开奖$goods_id' . $v['id'] . '福利屋开奖$goods_title' . $v['title']);
|
|
echo ('福利屋开奖$goods_id' . $v['id'] . '福利屋开奖$goods_title' . $v['title']) . PHP_EOL;
|
|
|
|
$this->special_prize_notice_time($v['id'], $v['title']);
|
|
|
|
}
|
|
$start_time = date('Y-m-d H:i:s', time());
|
|
$output->writeln('开奖结束' . $start_time);
|
|
}
|
|
|
|
/**
|
|
* 特殊奖品开奖 LAST 最终赏 全局赏 到时间开奖修改此处
|
|
* @param $order 订单信息
|
|
* @param $first_count
|
|
*/
|
|
protected function special_prize_notice_time($goods_id, $goods_title)
|
|
{
|
|
|
|
echo ('福利屋开奖$goods_id' . $goods_id . '福利屋开奖$goods_title' . $goods_title) . PHP_EOL;
|
|
$res = [];
|
|
$num = 0; #第几箱
|
|
print_r('$goods_id' . $goods_id);
|
|
//获取所有的奖品
|
|
$goods_list = GoodsList::where('goods_id', '=', $goods_id)
|
|
->where('num', '=', $num)
|
|
->select()
|
|
->toArray();
|
|
|
|
$goods_list_all = [];
|
|
//
|
|
foreach ($goods_list as $k => $v) {
|
|
$stock = $v['stock'];
|
|
if ($stock > 0) {
|
|
for ($i = 0; $i < $stock; $i++) {
|
|
$goods_list_all[] = $v;
|
|
}
|
|
}
|
|
}
|
|
//打散全部奖品
|
|
shuffle($goods_list_all);
|
|
shuffle($goods_list_all);
|
|
//抽奖人数
|
|
$all_order_list = OrderList::where('goods_id', '=', $goods_id)
|
|
->select()
|
|
->toArray();
|
|
// 如果没有参与用户,直接返回
|
|
if (empty($all_order_list)) {
|
|
|
|
echo ('福利屋开奖完成没有用户参加, goods_id: ' . $goods_id) . PHP_EOL;
|
|
Goods::where(['id' => $goods_id])->update(['is_open' => 1, 'status' => 3]);
|
|
return $res;
|
|
}
|
|
|
|
// 打散参与用户
|
|
shuffle($all_order_list);
|
|
$prize_index = 0;
|
|
$prize_count = count($goods_list_all);
|
|
|
|
// 遍历所有参与用户
|
|
foreach ($all_order_list as $user) {
|
|
$order_info = OrderList::find($user['id']);
|
|
// 如果还有奖品可抽
|
|
try {
|
|
// 开启事务
|
|
Db::startTrans();
|
|
|
|
if ($prize_index < $prize_count) {
|
|
$prize = $goods_list_all[$prize_index];
|
|
|
|
// 检查库存
|
|
$current_stock = GoodsList::where(['id' => $prize['id']])->value('surplus_stock');
|
|
if ($current_stock <= 0) {
|
|
throw new \Exception('奖品库存不足');
|
|
}
|
|
|
|
// 更新订单信息
|
|
$order_info['goodslist_id'] = $prize['id'];
|
|
$order_info['goodslist_title'] = $prize['title'];
|
|
$order_info['goodslist_imgurl'] = $prize['imgurl'];
|
|
$order_info['goodslist_money'] = $prize['money'];
|
|
$order_info['goodslist_type'] = $prize['goods_type'];
|
|
$order_info['shang_id'] = $prize['shang_id'];
|
|
$order_info['prize_code'] = $prize['reward_id'];
|
|
if ($order_info['status'] != 0 && $order_info['goodslist_type'] == 1) {
|
|
$order_info['status'] = 0;
|
|
$order_info['recovery_num'] = '';
|
|
}
|
|
// 减少库存
|
|
$update_stock = GoodsList::where(['id' => $prize['id']])
|
|
->where('surplus_stock', '>', 0)
|
|
->dec('surplus_stock')
|
|
->update();
|
|
|
|
if (!$update_stock) {
|
|
throw new \Exception('更新库存失败');
|
|
}
|
|
|
|
// 发放奖品
|
|
RewardService::sendReward($user['user_id'], $prize['reward_id'], $goods_title . '开奖');
|
|
echo ('发放奖品成功 goods_id: ' . $goods_id . ', user_id: ' . $user['user_id'] . ', reward_id: ' . $prize['reward_id'] . ' 奖品名称:' . $prize['title']) . PHP_EOL;
|
|
$prize_index++;
|
|
} else {
|
|
// 轮空处理
|
|
$order_info['goodslist_id'] = 0;
|
|
$order_info['goodslist_title'] = '轮空';
|
|
$order_info['goodslist_money'] = 0;
|
|
$order_info['shang_id'] = 0;
|
|
$order_info['prize_code'] = '';
|
|
}
|
|
|
|
// 保存订单信息
|
|
if (!$order_info->save()) {
|
|
throw new \Exception('保存订单信息失败');
|
|
}
|
|
|
|
// 提交事务
|
|
Db::commit();
|
|
|
|
} catch (\Exception $e) {
|
|
// 回滚事务
|
|
Db::rollback();
|
|
echo ('福利屋开奖失败 goods_id: ' . $goods_id . ', user_id: ' . $user['user_id'] . ', error: ' . $e->getMessage()) . PHP_EOL;
|
|
// 继续处理下一个用户
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// 检查是否所有用户都已完成开奖
|
|
$unprocessed_count = OrderList::where('goods_id', $goods_id)
|
|
->where('goodslist_id', 0)
|
|
->where('goodslist_title', '')
|
|
->count();
|
|
Goods::where(['id' => $goods_id])->update(['is_open' => 1, 'status' => 3]);
|
|
if ($unprocessed_count == 0) {
|
|
// 更新福利屋状态为已开奖
|
|
|
|
echo ('福利屋开奖完成 goods_id: ' . $goods_id) . PHP_EOL;
|
|
Log::info('福利屋开奖完成 goods_id: ' . $goods_id . PHP_EOL);
|
|
} else {
|
|
echo ('福利屋开奖未完成 goods_id: ' . $goods_id . ', 未处理用户数: ' . $unprocessed_count) . PHP_EOL;
|
|
Log::info('福利屋开奖未完成 goods_id: ' . $goods_id . ', 未处理用户数: ' . $unprocessed_count);
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
|
|
|
|
}
|