manghe/app/api/controller/Notify.php
2025-03-21 19:14:08 +00:00

1962 lines
81 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\api\controller;
use app\common\model\CardLevel;
use app\common\model\Goods;
use app\common\model\GoodsList;
use app\common\model\GoodsLock;
use app\common\model\ItemCard;
use app\common\model\Order;
use app\common\model\OrderList;
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 think\facade\Db;
use app\common\model\CouponReceive as CouponReceiveModel;
use app\common\model\GoodsExtendList as GoodsExtendListModel;
/**
* Class Notify 同步处理
* @package app\index\controller
*/
class Notify extends Base
{
static $shang_prize_id = [10, 33];#抽奖赏品id
static $shang_give_first_id = 1;#赠送(FIRST赏)赏品id
static $shang_give_arr = [2, 3, 4];#赠送(LAST赏 最终赏 全局赏)赏品id
static $shang_give_w_id = 4;#赠送(全局赏)赏品id
static $shang_give_quan_id = 5;#赠送(拳王赏)赏品id
static $secretKey = null;
static $secretKeyAccount = null;
public function initialize()
{
// 获取微信支付配置
$wxpayConfig = \app\common\helper\WxPayHelper::getWxPayConfig();
$merchant = $wxpayConfig['merchant'];
// 使用随机商户的密钥
static::$secretKey = $merchant['keys'];
// 获取公众号密钥
static::$secretKeyAccount = getConfig('wechatofficialaccount')['keys'];
}
public function ceshi()
{
$order['goods_id'] = 18;
$order['num'] = 1;
$order['order_type'] = 3;
// dd(11);
$this->quan_prize_notice($order);
}
/**
* 特殊奖品开奖 拳王赏
* @param $order 订单信息
*/
protected function quan_prize_notice($order)
{
$res = [];
$goods_id = $order['goods_id'];
$num = $order['num'];
$order_type = $order['order_type'];
#盒子
$goods_info = Goods::field('prize_num,stock')->where(['id' => $goods_id])->find();
#特殊奖品存在where('goods_id', '=', $goods_id)
$special_prize = GoodsList::where('goods_id', '=', $goods_id)
->where('num', '=', $num)
->where('surplus_stock', '>', 0)
->where('shang_id', '=', self::$shang_give_quan_id)
->order('prize_num desc')
->select();
// dd($special_prize);
$user_ids = [];
foreach ($special_prize as $value) {
$surplus_give_stock = $value ? $value['surplus_stock'] : 0;
if ($value && $surplus_give_stock > 0) {
#符合条件的订单
$quan_prize_num = $value['prize_num'];
// dd($quan_prize_num);
// var_dump($quan_prize_num);
$prize_num_data = OrderList::field('id,user_id,count(`id`) as all_num')
->where('goods_id', '=', $goods_id)
->where('num', '=', $num)
->where('order_type', '=', $order_type)
->where('shang_id', '=', self::$shang_give_w_id)
->having('all_num', '>=', $quan_prize_num)
->group('user_id')
->select()->toArray();
$prize_num_data_arr = [];
foreach ($prize_num_data as $val) {
if ($val['all_num'] >= $quan_prize_num && !in_array($val['user_id'], $user_ids)) {
$prize_num_data_arr[] = $val;
}
}
// dd($prize_num_data_arr);
if ($prize_num_data_arr) {
// dd($prize_num_data);
#多个随机
shuffle($prize_num_data_arr);
$quan_prize_info = $prize_num_data_arr[0];
$user_id = $quan_prize_info['user_id'];
$user_ids[] = $quan_prize_info['user_id'];
$order_goods = [
'order_id' => 0,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => $goods_id,
'num' => $num,
'shang_id' => $value['shang_id'],
'goodslist_id' => $value['id'],
'goodslist_title' => $value['title'],
'goodslist_imgurl' => $value['imgurl'],
'goodslist_price' => $value['price'],
'goodslist_money' => $value['money'],
'goodslist_type' => $value['goods_type'],
'goodslist_sale_time' => $value['sale_time'],
'addtime' => time(),
'prize_code' => $value['prize_code'],
'order_type' => $order_type,
'order_list_id' => 0,
];
// var_dump($order_goods);
#新增奖品列表
$res[] = OrderList::insert($order_goods);
#减少库存
$res[] = GoodsList::field('surplus_stock')
->where(['id' => $value['id']])
->dec('surplus_stock')
->update();
// var_dump($res);
}
}
}
#赏品
$goods_list_zd = GoodsList::where(['goods_id' => $goods_id])
->where(['num' => 1])
->select()->toArray();
if ($goods_list_zd) {
#循环数据
$save_sports_data = [];
$start_num = $goods_info['stock'] + 1;
for ($i = $start_num; $i <= $start_num; $i++) {
foreach ($goods_list_zd as $k => $v) {
unset($v['id']);
unset($v['num']);
$v['num'] = $i;
$v['surplus_stock'] = $v['stock'];
$save_sports_data[] = $v;
}
}
#添加赏品
$res[] = GoodsList::insertAll($save_sports_data);
#增加库存
$res[] = Goods::where(['id' => $goods_id])->inc('stock', 1)->update();
}
return $res;
}
/**
*
* 获取支付结果通知数据
* return array
*/
public function order_notify()
{
#获取通知的数据
$xml = file_get_contents("php://input");
if (empty($xml)) {
return false;
}
#转成数组
$data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$sign = $data['sign'];
unset($data['sign']);
$newSign = $this->MakeSign($data);
$newSignAccount = $this->MakeSignAccount($data);
#验证签名
if ($sign == $newSign || $sign == $newSignAccount) {
//如果成功返回了
if ($data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') {
$user = User::where('openid', $data['openid'])->find();
try {
Db::name('wxpay_log')->insert([
'order_no' => $data['out_trade_no'],
'content' => json_encode($data),
'type' => 1,
'user_id' => $user['id'],
'addtime' => time(),
]);
} catch (\Throwable $e) {
$this->CallbackSuccess();
}
$out_trade_no = $data['out_trade_no'];//订单号
$table = $data['attach'];#表名
if ($table == 'user_recharge') {#余额充值
$orderInfo = UserRecharge::where('order_num', '=', $out_trade_no)
->where(['status' => 1])
->find();
#这个订单存在状态对
if ($orderInfo && $orderInfo['status'] == 1) {
$user_id = $orderInfo['user_id'];
$money = $orderInfo['money'];
Db::startTrans();
#修改订单状态
$res[] = UserRecharge::field('status,pay_time')
->where(['id' => $orderInfo['id']])->update([
'status' => 2,
'pay_time' => time(),
]);
$res[] = User::changeMoney($user_id, $money, 2, '在线充值');
#记录微信支付
$res[] = ProfitPay::insert([
'user_id' => $user_id,
'order_num' => $out_trade_no,
'change_money' => $money,
'content' => '微信支付',
'pay_type' => 1,#1微信 2支付宝
'addtime' => time(),
]);
if (resCheck($res)) {
Db::commit();
} else {
Db::rollback();
}
}
} elseif ($table == 'order_yfs' || $table == 'order_lts' || $table == 'order_zzs') {#抽赏一番赏 擂台赏
$orderInfo = Order::where('order_num', '=', $out_trade_no)
->where('status', '=', 0)
->find();
#这个订单存在状态对
if ($orderInfo && $orderInfo['status'] == 0) {
$user_id = $orderInfo['user_id'];
$price = $orderInfo['price'];
Db::startTrans();
try {
#开盒子
$res[] = $this->drawprize_notice($user_id, $orderInfo['id'], $orderInfo['goods_id'], $orderInfo['num']);
$this->wx_gf_fahuo($user_id, $orderInfo['order_num']);
#广告收益回调
if (!empty($orderInfo['click_id']) && !empty($orderInfo['ad_id'])) {
$order_ad_num = Order::where('user_id', '=', $user_id)->where('status', '=', 1)->where('ad_id', '>', 0)->where('click_id', '>', 0)->where('price', '>', 0)->count();
if ($order_ad_num <= 1) {
$this->ad_notify($orderInfo['ad_id'], $orderInfo['click_id']);
}
}
} catch (\Throwable $e) {
Db::rollback();
#卡单了
Order::field('kd_is')->where(['id' => $orderInfo['id']])
->update(['kd_is' => 1]);
#通知微信
$this->CallbackSuccess();
}
#记录微信支付
$res[] = ProfitPay::insert([
'user_id' => $user_id,
'order_num' => $out_trade_no,
'change_money' => $price,
'content' => '购买盒子' . $orderInfo['goods_title'],
'pay_type' => 1,#1微信 2支付宝
'addtime' => time(),
]);
if (resCheck($res)) {
Db::commit();
} else {
Db::rollback();
}
}
} elseif ($table == 'order_wxs') {#抽赏无限赏
$orderInfo = Order::where('order_num', '=', $out_trade_no)
->where('status', '=', 0)
->find();
#这个订单存在状态对
if ($orderInfo && $orderInfo['status'] == 0) {
$user_id = $orderInfo['user_id'];
$price = $orderInfo['price'];
Db::startTrans();
$res[] = $this->infinite_drawprize_notice($user_id, $orderInfo['id'], $orderInfo['goods_id']);
#记录微信支付
$res[] = ProfitPay::insert([
'user_id' => $user_id,
'order_num' => $out_trade_no,
'change_money' => $price,
'content' => '购买盒子' . $orderInfo['goods_title'],
'pay_type' => 1,#1微信 2支付宝
'addtime' => time(),
]);
$this->wx_gf_fahuo($user_id, $orderInfo['order_num']);
#广告收益回调
if (!empty($orderInfo['click_id']) && !empty($orderInfo['ad_id'])) {
$order_ad_num = Order::where('user_id', '=', $user_id)->where('status', '=', 1)->where('ad_id', '>', 0)->where('click_id', '>', 0)->where('price', '>', 0)->count();
if ($order_ad_num <= 1) {
$this->ad_notify($orderInfo['ad_id'], $orderInfo['click_id']);
}
}
if (resCheck($res)) {
Db::commit();
} else {
Db::rollback();
}
}
} elseif ($table == 'order_ckj') {#抽赏抽卡机
$orderInfo = Order::where('order_num', '=', $out_trade_no)
->where('status', '=', 0)
->where('order_type', '=', 4)
->find();
#这个订单存在状态对
if ($orderInfo && $orderInfo['status'] == 0) {
$user_id = $orderInfo['user_id'];
$price = $orderInfo['price'];
Db::startTrans();
$res[] = $this->cardextractor_drawprize_notice($user_id, $orderInfo['id'], $orderInfo['goods_id']);
#记录微信支付
$res[] = ProfitPay::insert([
'user_id' => $user_id,
'order_num' => $out_trade_no,
'change_money' => $price,
'content' => '购买盒子' . $orderInfo['goods_title'],
'pay_type' => 1,#1微信 2支付宝
'addtime' => time(),
]);
$this->wx_gf_fahuo($user_id, $orderInfo['order_num']);
if (resCheck($res)) {
Db::commit();
} else {
Db::rollback();
}
}
} elseif ($table == 'order_list_send') {#背包发货
$orderInfo = OrderListSend::where('send_num', '=', $out_trade_no)
->where('status', '=', 0)
->find();
#这个订单存在状态对
if ($orderInfo && $orderInfo['status'] == 0) {
$user_id = $orderInfo['user_id'];
$freight = $orderInfo['freight'];
Db::startTrans();
$res[] = $this->reward_order_handle($user_id, $orderInfo['id']);
#记录微信支付
$res[] = ProfitPay::insert([
'user_id' => $user_id,
'order_num' => $out_trade_no,
'change_money' => $freight,
'content' => '背包发货',
'pay_type' => 1,#1微信 2支付宝
'addtime' => time(),
]);
$this->wx_gf_fahuo($user_id, $orderInfo['order_num']);
if (resCheck($res)) {
Db::commit();
} else {
Db::rollback();
}
}
} elseif ($table == 'order_js') {
}
}
}
$this->CallbackSuccess();
}
/*
广告推广
*/
public function ad_notify($ads_id, $click_id)
{
$tencent_ad = Ads::where('id', $ads_id)->find();
if (!empty($tencent_ad)) {
$result = tencent_ad_attribution($click_id, 1, $tencent_ad['account_id'], $tencent_ad['access_token'], $tencent_ad['user_action_set_id']);
dump($result);
}
}
/**
* 生成签名
* @return 签名
*/
public function MakeSign($params)
{
//签名步骤一:按字典序排序数组参数
ksort($params);
$string = $this->ToUrlParams($params);
//签名步骤二在string后加入KEY
$string = $string . "&key=" . static::$secretKey;
//签名步骤三MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$result = strtoupper($string);
return $result;
}
/**
* 生成签名
* @return 签名
*/
public function MakeSignAccount($params)
{
//签名步骤一:按字典序排序数组参数
ksort($params);
$string = $this->ToUrlParams($params);
//签名步骤二在string后加入KEY
$string = $string . "&key=" . static::$secretKeyAccount;
//签名步骤三MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$result = strtoupper($string);
return $result;
}
/**
* 将参数拼接为url: key=value&key=value
* @param $params
* @return string
*/
public function ToUrlParams($params)
{
$string = '';
if (!empty($params)) {
$array = array();
foreach ($params as $key => $value) {
$array[] = $key . '=' . $value;
}
$string = implode("&", $array);
}
return $string;
}
/**
* 接收通知成功后应答输出XML数据
* @param string $xml
*/
public function CallbackSuccess()
{
$html = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
die($html);
}
/**
* 抽赏开始抽赏======================================================
* @param int $user_id 会员id
* @param int $order_id 订单ID
* @param int $goods_id 盒子id
* @param int $num 盒子箱号
*/
public function drawprize_notice($user_id = 0, $order_id = 0, $goods_id = 0, $num = 0)
{
$res = [];
$order = Order::where(['id' => $order_id])
->where(['user_id' => $user_id])
->where(['goods_id' => $goods_id])
->where(['num' => $num])
->where(['status' => 0])
->where('order_type', 'in', [1, 3, 5, 6, 11])
->find();
if ($order) {
#套库存=============================
// $all_stock = GoodsList::field('stock')->where('goods_id', '=', $goods_id)->where('num', '=', $num)
// ->where('shang_id', 'between', self::$shang_prize_id)->sum('stock');
//
// $redis = (new \app\common\server\RedisHelper())->getRedis();
// $redis_key = 'kpw_' . $goods_id . '_' . $num;
// for ($r = 1; $r <= $order['prize_num']; $r++) {
// $redis->lPush($redis_key, 1);
// }
// $redis_len = $redis->lLen($redis_key);
// if ($redis_len >= $all_stock) {
// for ($l = 1; $l <= $order['prize_num']; $l++) {
// $redis->rPop($redis_key);
// }
// #卡单了
// $kd_is = Order::field('kd_is')
// ->where(['id' => $order['id']])
// ->update([
// 'kd_is' => 1
// ]);
// return $kd_is;
// }
#套库存=============================
#改变状态
$res[] = Order::field('status,pay_time')
->where(['id' => $order['id']])
->update([
'status' => 1,
'pay_time' => time(),
]);
#扣余额
if ($order['use_money'] > 0) {
$res[] = User::changeMoney($order['user_id'], -$order['use_money'], 3, '购买盒子' . $order['goods_title']);
}
#扣吧唧币
if ($order['use_integral'] > 0) {
$res[] = User::changeIntegral($order['user_id'], -$order['use_integral'], 2, '购买盒子' . $order['goods_title']);
}
#扣积分
if ($order['use_score'] > 0) {
$res[] = User::changeScore($order['user_id'], -$order['use_score'], 2, '购买盒子' . $order['goods_title']);
}
#判断一下优惠券
if (!empty($order['coupon_id'])) {
$coupon = CouponReceiveModel::where(['id' => $order['coupon_id'], 'status' => 0])->update(['status' => 1]);
}
#判断是否发积分
#分销奖励
$res[] = User::distribution($order);
#分销奖励
#普通奖品==========================================================
$res[] = $this->ordinary_prize_notice($order);
#普通奖品==========================================================
#判断是否发积分 发券
$res[] = User::is_integral_coupon($order);
//升级欧气值
if ($order['price'] > 0) {
User::ou_qi_level_up($order['user_id'], $order['price']);
}
#特殊奖品**********************************************************
#普通奖品余量信息
$goodslist = GoodsList::field('sum(`stock`) as stock, sum(`surplus_stock`) as surplus_stock')
->where('goods_id', '=', $goods_id)
->where('num', '=', $num)
->where('shang_id', 'between', self::$shang_prize_id)
->find();
// var_dump($goodslist['surplus_stock']);
#普通赏销量数量
$sale_count = $goodslist['stock'] - $goodslist['surplus_stock'];
#普通奖品一半
$first_count = floor($goodslist['stock'] / 2);
#FIRST
if ($order['order_type'] != 3 && $sale_count >= $first_count) {
$res[] = $this->special_first_notice($order, $first_count);
}
#LAST赏 全局赏 拳王赏
if ($goodslist['surplus_stock'] <= 0) {
// var_dump($goodslist['surplus_stock']);
$res[] = $this->special_prize_notice($order, $first_count);
// var_dump($res);
#擂台赏
if ($order['order_type'] == 3) {
$res[] = $this->quan_prize_notice($order);
}
#增加盒子库存
$goods = Goods::field('id,stock,sale_stock')
->where(['id' => $goods_id])
->find();
$save_update = [];
$goods_sale_stock = $goods['sale_stock'] + 1;
if ($goods_sale_stock >= $goods['stock']) {
$save_update['status'] = 3;
}
$save_update['sale_stock'] = $goods_sale_stock;
$res[] = Goods::field('id,status,sale_stock')
->where(['id' => $goods_id])
->update($save_update);
}
#特殊奖品**********************************************************
} else {
$res[] = 0;
}
return $res;
}
/**
* 普通奖品开奖
* @param $order 订单信息
*/
protected function ordinary_prize_notice($order)
{
$user_id = $order['user_id'];
$order_id = $order['id'];
$goods_id = $order['goods_id'];
$num = $order['num'];
$order_type = $order['order_type'];
#抽奖数量
$prize_num = $order['prize_num'];
#普通奖品信息
$ordinary_prize = GoodsList::where('goods_id', '=', $goods_id)
->where('num', '=', $num)
->where('shang_id', 'between', self::$shang_prize_id)
->order('sort desc,shang_id asc')
->select()->toArray();
if (!$ordinary_prize) {
$res[] = 0;
return $res;
}
//获取最低抽奖次数
$goods_extend_model_list = GoodsExtendListModel::getGoodsExtendList($goods_id);
#抽了多少抽
$stock = array_sum(array_column($ordinary_prize, 'stock'));
$surplus_stock = array_sum(array_column($ordinary_prize, 'surplus_stock'));
$reward_num_count = $stock - $surplus_stock;
#普通奖暂存信息
$ordinary_prize_all = [];
#普通奖暂存信息
$ordinary_prize_special_all = [];
#其他
$ordinary_prize_other = [];
foreach ($ordinary_prize as $k => $v) {
#剩余
$surplus_prize = $v['surplus_stock'];
//奖品id
$prize_code = $v['prize_code'];
$goods_list_id = $v['id'];
//扩展配置
$goods_extend_model = $goods_extend_model_list->where('goods_list_id', '=', $goods_list_id)->first();
//最低抽奖次数
$reward_num = 0;
if ($goods_extend_model != null) {
if ($goods_extend_model['rawrd_type'] == 1) {
$reward_num = $goods_extend_model->reward_num;
} else if ($goods_extend_model['rawrd_type'] == 2) {
//当前抽数
$goods_extend_model_reward_num = $goods_extend_model['reward_num'];
if ($reward_num_count < $goods_extend_model_reward_num) {
//未过期
$v['goods_extend'] = $goods_extend_model;
$ordinary_prize_special_all[] = $v;
$ordinary_prize_all[] = $v;
continue;
}
} else if ($goods_extend_model['rawrd_type'] == 3) {
//当前抽数
// if ($reward_num_count >= $goods_extend_model['reward_num'] && $reward_num_count <= $goods_extend_model['reward_num_1']) {
//未过期
$v['goods_extend'] = $goods_extend_model;
$ordinary_prize_special_all[] = $v;
$ordinary_prize_all[] = $v;
continue;
// }
}
}
$prize_code = '';
//库存数量
for ($i = 1; $i <= $surplus_prize; $i++) {
if ($reward_num_count > 0) {
if ($reward_num_count > $reward_num) {
$ordinary_prize_other[] = $v;
}
} else if ($v['reward_num'] > 0) {
if ($reward_num_count >= $v['reward_num']) {
#全部延迟出赏普通奖品
$ordinary_prize_other[] = $v;
}
} else {
#全部未延迟出赏普通奖品
$ordinary_prize_other[] = $v;
}
$ordinary_prize_all[] = $v;
}
}
$is_special = true;
if ($prize_num > count($ordinary_prize_other)) {
#全部商品
$end_ordinary_prize = $ordinary_prize_all;
$is_special = false;
} else {
#延迟出赏
$end_ordinary_prize = $ordinary_prize_other;
}
shuffle($end_ordinary_prize);
shuffle($end_ordinary_prize);
if ($is_special) {
foreach ($ordinary_prize_special_all as $kk => $vv) {
// $surplus_prize_1 = 1;
$surplus_prize_1 = $vv['surplus_stock'];
//奖品id
$goods_extend_model_1 = $vv['goods_extend'];
for ($i = 1; $i <= $surplus_prize_1; $i++) {
//指定抽数
if ($goods_extend_model_1['rawrd_type'] == 2) {
//指定抽奖数-
$randomNumber = $goods_extend_model_1['reward_num'] - $reward_num_count - 1;
//如果小于0则表示
if ($randomNumber < 0) {
$randomNumber = random_int(0, count($end_ordinary_prize));
}
if ($randomNumber >= 0) {
//表示第二个对象 goods_list_id
if (isset($end_ordinary_prize[$randomNumber]['goods_extend'])) {
$goods_extend_model_2 = $end_ordinary_prize[$randomNumber]['goods_extend'];
if ($goods_extend_model_2 != null) {
//表示相同抽数
if ($goods_extend_model_1['reward_num'] == $goods_extend_model_2['reward_num']) {
$randomNumber = random_int($randomNumber, count($end_ordinary_prize));
}
}
}
if (isset($end_ordinary_prize[$randomNumber]['prize_code']) && isset($vv['prize_code'])) {
if ($end_ordinary_prize[$randomNumber]['prize_code'] == $vv['prize_code']) {
if (count($end_ordinary_prize) > $randomNumber) {
$randomNumber = random_int($randomNumber, count($end_ordinary_prize));
} else {
$randomNumber = random_int(0, count($end_ordinary_prize));
}
}
}
array_splice($end_ordinary_prize, $randomNumber, 0, [$vv]);
}
}
//抽奖范围
if ($goods_extend_model_1['rawrd_type'] == 3) {
$randomNumber = 0;
if ($reward_num_count >= $goods_extend_model_1['reward_num'] && $reward_num_count <= $goods_extend_model_1['reward_num_1']) {
//在范围里面
$max_randomNumber = $goods_extend_model_1['reward_num_1'] - $reward_num_count - 1;
$randomNumber = random_int(0, intval($max_randomNumber));
} else if ($reward_num_count <= $goods_extend_model_1['reward_num']) {
//$goods_extend_model_1['reward_num']-$reward_num_count
$randomNumber = count($end_ordinary_prize) - 1;
}
array_splice($end_ordinary_prize, $randomNumber, 0, [$vv]);
}
}
}
}
#入库奖品
$save_order_goods = [];
#开普通奖品
for ($i = 0; $i < $prize_num; $i++) {
$ordinary_prize_info = $end_ordinary_prize[$i];
$save_order_goods[
] = [
'order_id' => $order_id,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => $goods_id,
'num' => $num,
'shang_id' => $ordinary_prize_info['shang_id'],
'goodslist_id' => $ordinary_prize_info['id'],
'goodslist_title' => $ordinary_prize_info['title'],
'goodslist_imgurl' => $ordinary_prize_info['imgurl'],
'goodslist_price' => $ordinary_prize_info['price'],
'goodslist_money' => $ordinary_prize_info['money'],
'goodslist_type' => $ordinary_prize_info['goods_type'],
'goodslist_sale_time' => $ordinary_prize_info['sale_time'],
'addtime' => time(),
'prize_code' => $ordinary_prize_info['prize_code'],
'order_type' => $order_type,
];
#减少库存
$res[] = GoodsList::field('surplus_stock')
->where(['id' => $ordinary_prize_info['id']])
->dec('surplus_stock')
->update();
}
if ($save_order_goods) {
#新增奖品列表
$res[] = OrderList::insertAll($save_order_goods);
}
return $res;
}
/**
* 特殊奖品开奖 FIRST
* @param $order 订单信息
* @param $first_count
*/
protected function special_first_notice($order, $first_count)
{
$res = [];
$goods_id = $order['goods_id'];
$num = $order['num'];
$order_type = $order['order_type'];
#特殊奖品存在
$special_prize = GoodsList::where('goods_id', '=', $goods_id)
->where('num', '=', $num)
->where('surplus_stock', '>', 0)
->where('shang_id', '=', self::$shang_give_first_id)
->find();
#剩余数量
$surplus_give_stock = $special_prize ? $special_prize['surplus_stock'] : 0;
if ($special_prize && $surplus_give_stock > 0) {
#所有奖品信息
$all_order_list = OrderList::where('goods_id', '=', $goods_id)
->where('num', '=', $num)
->where('order_type', '=', $order_type)
->where('shang_id', 'between', self::$shang_prize_id)
->order('id asc')
->limit((int) $first_count)
->select()->toArray();
shuffle($all_order_list);
shuffle($all_order_list);
$prize_info = $all_order_list[0];
$user_id = $prize_info['user_id'];
#特殊赏中奖订单id
$order_list_id = $prize_info['id'];
#中奖奖项
$order_goods = [
'order_id' => 0,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => $goods_id,
'num' => $num,
'shang_id' => $special_prize['shang_id'],
'goodslist_id' => $special_prize['id'],
'goodslist_title' => $special_prize['title'],
'goodslist_imgurl' => $special_prize['imgurl'],
'goodslist_price' => $special_prize['price'],
'goodslist_money' => $special_prize['money'],
'goodslist_type' => $special_prize['goods_type'],
'goodslist_sale_time' => $special_prize['sale_time'],
'addtime' => time(),
'prize_code' => $special_prize['prize_code'],
'order_type' => $order_type,
'order_list_id' => $order_list_id,
];
#增加销量
$res[] = GoodsList::field('surplus_stock')
->where(['id' => $special_prize['id']])
->dec('surplus_stock')
->update();
#新增奖品列表
$res[] = OrderList::insert($order_goods);
} else {
$res[] = 1;
}
return $res;
}
/**
* 特殊奖品开奖 LAST 最终赏 全局赏
* @param $order 订单信息
* @param $first_count
*/
protected function special_prize_notice($order, $first_count)
{
// $res = [];
// $goods_id = $order['goods_id'];
// $num = $order['num'];
// $order_type = $order['order_type'];
// #盒子
// $goods_info = Goods::field('prize_num,stock')->where(['id' => $goods_id])->find();
// #特殊奖品存在where('goods_id', '=', $goods_id)
// $special_prize = GoodsList::where('goods_id', '=', $goods_id)
// ->where('num', '=', $num)
// ->where('surplus_stock', '>', 0)
// ->where('shang_id', '=', self::$shang_give_quan_id)
// ->order('prize_num desc')
// ->select();
// // dd($special_prize);
// foreach ($special_prize as $value){
// $surplus_give_stock = $value ? $value['surplus_stock'] : 0;
// // dd($value);
// if ($value && $surplus_give_stock > 0) {
// #符合条件的订单
// $quan_prize_num = $value['prize_num'];
// $prize_num_data = OrderList::field('id,user_id,count(`id`) as all_num')
// ->where('goods_id', '=', $goods_id)
// ->where('num', '=', $num)
// ->where('order_type', '=', $order_type)
// ->where('shang_id', '=', self::$shang_give_w_id)
// ->having('all_num', '>=', $quan_prize_num)
// ->group('user_id')
// ->select()->toArray();
// // dd($quan_prize_num);
// if ($prize_num_data) {
// #多个随机
// shuffle($prize_num_data);
// $quan_prize_info = $prize_num_data[0];
// $user_id = $quan_prize_info['user_id'];
// $order_goods = [
// 'order_id' => 0,
// 'user_id' => $user_id,
// 'status' => 0,#0未操作 1选择兑换 2选择发货
// 'goods_id' => $goods_id,
// 'num' => $num,
// 'shang_id' => $value['shang_id'],
// 'goodslist_id' => $value['id'],
// 'goodslist_title' => $value['title'],
// 'goodslist_imgurl' => $value['imgurl'],
// 'goodslist_price' => $value['price'],
// 'goodslist_money' => $value['money'],
// 'goodslist_type' => $value['goods_type'],
// 'goodslist_sale_time' => $value['sale_time'],
// 'addtime' => time(),
// 'prize_code' => $value['prize_code'],
// 'order_type' => $order_type,
// 'order_list_id' => 0,
// ];
// #新增奖品列表
// $res[] = OrderList::insert($order_goods);
// #减少库存
// $res[] = GoodsList::field('surplus_stock')
// ->where(['id' => $special_prize['id']])
// ->dec('surplus_stock')
// ->update();
// }
// }
// #赏品
// $goods_list_zd = GoodsList::where(['goods_id' => $goods_id])
// ->where(['num' => 1])
// ->select()->toArray();
// if ($goods_list_zd) {
// #循环数据
// $save_sports_data = [];
// $start_num = $goods_info['stock'] + 1;
// for ($i = $start_num; $i <= $start_num; $i++) {
// foreach ($goods_list_zd as $k => $v) {
// unset($v['id']);
// unset($v['num']);
// $v['num'] = $i;
// $v['surplus_stock'] = $v['stock'];
// $save_sports_data[] = $v;
// }
// }
// #添加赏品
// $res[] = GoodsList::insertAll($save_sports_data);
// #增加库存
// $res[] = Goods::where(['id' => $goods_id])->inc('stock', 1)->update();
// }
// }
// return $res;
$res = [];
$goods_id = $order['goods_id'];
$num = $order['num'];
$order_type = $order['order_type'];
#特殊奖品存在
$special_prize = GoodsList::where('goods_id', '=', $goods_id)
->where('num', '=', $num)
->where('surplus_stock', '>', 0)
->where('shang_id', 'in', self::$shang_give_arr)
->select()->toArray();
$surplus_give_stock = $special_prize ? array_sum(array_column($special_prize, 'surplus_stock')) : 0;
if ($special_prize && $surplus_give_stock > 0) {
#所有奖品信息
$all_order_list = OrderList::field('id,user_id')
->where('goods_id', '=', $goods_id)
->where('num', '=', $num)
->where('order_type', '=', $order_type)
->where('shang_id', 'between', self::$shang_prize_id)
->order('id asc')
->select()->toArray();
$order_goods = [];
foreach ($special_prize as $k => $v) {
if ($v['shang_id'] == 4) {#全局赏
for ($surplus_stock_i = 0; $surplus_stock_i < $v['surplus_stock']; $surplus_stock_i++) {
$overall_order_list = $all_order_list;
shuffle($overall_order_list);
shuffle($overall_order_list);
$prize_info = $overall_order_list[0];
$user_id = $prize_info['user_id'];
#特殊赏中奖订单id
$order_list_id = $prize_info['id'];
#中奖奖项
$ordinary_prize_info = $v;
$order_goods[] = [
'order_id' => 0,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => $goods_id,
'num' => $num,
'shang_id' => $ordinary_prize_info['shang_id'],
'goodslist_id' => $ordinary_prize_info['id'],
'goodslist_title' => $ordinary_prize_info['title'],
'goodslist_imgurl' => $ordinary_prize_info['imgurl'],
'goodslist_price' => $ordinary_prize_info['price'],
'goodslist_money' => $ordinary_prize_info['money'],
'goodslist_type' => $ordinary_prize_info['goods_type'],
'goodslist_sale_time' => $ordinary_prize_info['sale_time'],
'addtime' => time(),
'prize_code' => $ordinary_prize_info['prize_code'],
'order_type' => $order_type,
'order_list_id' => $order_list_id,
];
#减少库存
$res[] = GoodsList::field('surplus_stock')
->where(['id' => $ordinary_prize_info['id']])
->dec('surplus_stock')
->update();
}
} else {
if ($v['shang_id'] == 2) {#LAST赏
$last_order_list = array_slice($all_order_list, (int) $first_count);
shuffle($last_order_list);
shuffle($last_order_list);
$prize_info = $last_order_list[0];
#中奖用户id
$user_id = $prize_info['user_id'];
#特殊赏中奖订单id
$order_list_id = $prize_info['id'];
#中奖奖项
$ordinary_prize_info = $v;
} elseif ($v['shang_id'] == 3) {#最终赏
$end_order_list = $all_order_list;
$prize_info = end($end_order_list);
#中奖用户id
$user_id = $prize_info['user_id'];
#特殊赏中奖订单id
$order_list_id = $prize_info['id'];
#中奖奖项
$ordinary_prize_info = $v;
}
$order_goods[] = [
'order_id' => 0,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => $goods_id,
'num' => $num,
'shang_id' => $ordinary_prize_info['shang_id'],
'goodslist_id' => $ordinary_prize_info['id'],
'goodslist_title' => $ordinary_prize_info['title'],
'goodslist_imgurl' => $ordinary_prize_info['imgurl'],
'goodslist_price' => $ordinary_prize_info['price'],
'goodslist_money' => $ordinary_prize_info['money'],
'goodslist_type' => $ordinary_prize_info['goods_type'],
'goodslist_sale_time' => $ordinary_prize_info['sale_time'],
'addtime' => time(),
'prize_code' => $ordinary_prize_info['prize_code'],
'order_type' => $order_type,
'order_list_id' => $order_list_id,
];
#减少库存
$res[] = GoodsList::field('surplus_stock')
->where(['id' => $ordinary_prize_info['id']])
->dec('surplus_stock')
->update();
}
}
#新增奖品列表
$res[] = OrderList::insertAll($order_goods);
} else {
$res[] = 1;
}
return $res;
}
/**
* 无限赏开始抽赏======================================================
* @param int $user_id 会员id
* @param int $order_id 订单ID
* @param int $box_id 盲盒id
*/
public function infinite_drawprize_notice($user_id = 0, $order_id = 0, $goods_id = 0, $num = 0)
{
// if()
$res = [];
$order = Order::where(['id' => $order_id])
->where(['user_id' => $user_id])
->where(['goods_id' => $goods_id])
->where(['num' => $num])
->where(['status' => 0])
->find();
if ($order == null && $num == 0) {
$order = Order::where(['id' => $order_id])
->where(['user_id' => $user_id])
->where(['goods_id' => $goods_id])
->where(['num' => 1])
->where(['status' => 0])
->find();
if ($order != null) {
$order_type1 = $order['order_type'];
if ($order_type1 == 10) {
$num = 1;
}
}
}
if ($order) {
#改变状态
$res[] = Order::field('status,pay_time')
->where(['id' => $order['id']])
->update([
'status' => 1,
'pay_time' => time(),
]);
#扣余额
if ($order['use_money'] > 0) {
$res[] = User::changeMoney($order['user_id'], -$order['use_money'], 3, '购买盒子' . $order['goods_title']);
}
#扣吧唧币
if ($order['use_integral'] > 0) {
$res[] = User::changeIntegral($order['user_id'], -$order['use_integral'], 2, '购买盒子' . $order['goods_title']);
}
#判断一下优惠券
if (!empty($order['coupon_id'])) {
$coupon = CouponReceiveModel::where(['id' => $order['coupon_id'], 'status' => 0])->update(['status' => 1]);
}
#分销奖励
$res[] = User::distribution($order);
#分销奖励
#开奖==================================================
if ($num == 1) {
$res[] = $this->infinite_shangchengshang($order);
} else {
$res[] = $this->infinite_drawprize($order);
}
#开奖==================================================
#判断是否发积分 发券
$res[] = User::is_integral_coupon($order);
#判断是否发积分
//升级欧气值
if ($order['price'] > 0) {
User::ou_qi_level_up($order['user_id'], $order['price']);
}
} else {
$res[] = 0;
}
return $res;
}
/**
* 无限赏开奖逻辑
* @param $order 订单信息
*/
protected function infinite_drawprize($order = [])
{
$user_id = $order['user_id'];#用户ID
$order_id = $order['id'];#订单ID
$goods_id = $order['goods_id'];#盒子ID
$prize_num = $order['prize_num'];#抽奖数量
$order_type = $order['order_type'];#订单类型
$whe = [];
$whe[] = ['id', '=', $order['goods_id']];
$infinite_goods = Goods::getInfo($whe, 'type');
$where = [];
$where[] = ['goods_id', '=', $goods_id];
$where[] = ['num', '=', 0];
$where[] = ['real_pro', '>', 0];
if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 1) {
//秘宝池
$where[] = ['lian_ji_type', '=', 1];
} else {
//普通
$where[] = ['lian_ji_type', '=', 0];
}
#查找奖品
$goodslist = GoodsList::field('id,shang_id,real_pro')
->where($where)
->select()->toArray();
if ($goodslist) {
#组合中奖商品
$all_goods_id = [];
foreach ($goodslist as $value) {
$real_pro = $value['real_pro'] * 10000;
for ($i = 1; $i <= $real_pro; $i++) {
$all_goods_id[] = $value['id'];
}
}
for ($i = 0; $i < $prize_num; $i++) {
#随机打乱
shuffle($all_goods_id);
shuffle($all_goods_id);
$prize_id = $all_goods_id[0];
$prize_info = GoodsList::where(['id' => $prize_id])->find();
#编号
$luck_no = OrderList::field('id')
->where('goods_id', '=', $goods_id)
->where('num', '=', 0)
->where('order_type', '=', $order_type)
->order('id desc')
->value('luck_no');
$luck_no++;
#新增记录
$save_prize_info = [
'order_id' => $order_id,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => $goods_id,
'num' => 0,
'shang_id' => $prize_info['shang_id'],
'goodslist_id' => $prize_info['id'],
'goodslist_title' => $prize_info['title'],
'goodslist_imgurl' => $prize_info['imgurl'],
'goodslist_price' => $prize_info['price'],
'goodslist_money' => $prize_info['money'],
'goodslist_type' => $prize_info['goods_type'],
'goodslist_sale_time' => $prize_info['sale_time'],
'addtime' => time(),
'prize_code' => $prize_info['prize_code'],
'order_type' => $order_type,
'luck_no' => $luck_no,
];
#入库===
$res[] = OrderList::insert($save_prize_info);
}
//去除秘宝池次数
if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 1) {
User::where('id', $user_id)->dec('mb_number', $prize_num)->update();
}
//计算怒气值
$this->rage($goods_id, $order_id, $user_id);
//领主
$this->ling_zhu($order, $order_id);
//连击赏
if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 0) {
$this->lian_ji($order, $order_id);
}
} else {
$res[] = 0;
}
return $res;
}
/**
* 无限赏开奖逻辑
* @param $order 订单信息
*/
protected function infinite_shangchengshang($order = [])
{
$user_id = $order['user_id'];#用户ID
$order_id = $order['id'];#订单ID
$goods_id = $order['goods_id'];#盒子ID
$prize_num = $order['prize_num'];#抽奖数量
$order_type = $order['order_type'];#订单类型
$whe = [];
$whe[] = ['id', '=', $order['goods_id']];
$infinite_goods = Goods::getInfo($whe, 'type');
$num = 1;
$where = [];
$where[] = ['goods_id', '=', $goods_id];
$where[] = ['num', '=', $num];
#查找奖品
$goodslist = GoodsList::field('id,shang_id,real_pro,surplus_stock')
->where($where)
->select()->toArray();
if ($goodslist) {
#组合中奖商品
$all_goods_id = [];
foreach ($goodslist as $value) {
$surplus_stock = $value['surplus_stock'];
for ($i = 1; $i <= $surplus_stock; $i++) {
$all_goods_id[] = $value['id'];
}
}
for ($i = 0; $i < $prize_num; $i++) {
$prize_id = $all_goods_id[0];
$prize_info = GoodsList::where(['id' => $prize_id])->find();
$prize_info_surplus_stock = $prize_info['surplus_stock'];
$prize_info['surplus_stock'] = $prize_info_surplus_stock - 1;
if ($prize_info_surplus_stock < 0) {
$res[] = 0;
return;
}
$prize_info->save();
#编号
$luck_no = OrderList::field('id')
->where('goods_id', '=', $goods_id)
->where('num', '=', $num)
->where('order_type', '=', $order_type)
->order('id desc')
->value('luck_no');
$luck_no++;
#新增记录
$save_prize_info = [
'order_id' => $order_id,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => $goods_id,
'num' => $num,
'shang_id' => $prize_info['shang_id'],
'goodslist_id' => $prize_info['id'],
'goodslist_title' => $prize_info['title'],
'goodslist_imgurl' => $prize_info['imgurl'],
'goodslist_price' => $prize_info['price'],
'goodslist_money' => $prize_info['money'],
'goodslist_type' => $prize_info['goods_type'],
'goodslist_sale_time' => $prize_info['sale_time'],
'addtime' => time(),
'prize_code' => $prize_info['prize_code'],
'order_type' => $order_type,
'luck_no' => $luck_no,
];
#入库===
$res[] = OrderList::insert($save_prize_info);
}
//去除秘宝池次数
if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 1) {
User::where('id', $user_id)->dec('mb_number', $prize_num)->update();
}
//计算怒气值
$this->rage($goods_id, $order_id, $user_id);
//领主
$this->ling_zhu($order, $order_id);
//连击赏
if ($infinite_goods['type'] == 9 && $order['is_mibao'] == 0) {
$this->lian_ji($order, $order_id);
}
} else {
$res[] = 0;
}
return $res;
}
//计算怒气值
public function rage($goods_id, $order_id, $user_id)
{
$order_list = OrderList::field('id')->where(['order_id' => $order_id])->find();
if (!$order_list) {
return false;
}
try {
$goods = Goods::field('id')->field('rage_is,rage,item_card_id')->where(['id' => $goods_id])->find();
if ($goods['rage_is'] == 1 && $goods['rage'] > 0 && $goods['item_card_id'] > 0) {
$order = Order::field('order_total')->where(['id' => $order_id])->find();
if ($order['order_total'] > 0) {
//获取奖品总价值
$out_price_sum = OrderList::where(['order_id' => $order_id])->sum('goodslist_money');
$rage_number = bcsub($order['order_total'], "$out_price_sum", 2);
$user_rage = Db::name('user_rage')->field('id,rage')->where(['user_id' => $user_id, 'goods_id' => $goods_id])->find();
if ($user_rage) {
$rage_number2 = bcadd((string) ($user_rage['rage']), "$rage_number", 2);
if ($rage_number2 >= $goods['rage']) {
//赠送道具卡
$item_card = ItemCard::field('id,title')->where(['id' => $goods['item_card_id']])->find();
if ($item_card) {
Db::name('user_item_card')->insert(['user_id' => $user_id, 'item_card_id' => $item_card['id'], 'title' => $item_card['title'], 'status' => 1, 'addtime' => time(), 'updatetime' => time()]);
}
Db::name('user_rage')->where(['user_id' => $user_id, 'goods_id' => $goods_id])->update(['rage' => 0, 'updatetime' => time()]);
} else {
Db::name('user_rage')->where(['user_id' => $user_id, 'goods_id' => $goods_id])->update(['rage' => $rage_number2, 'updatetime' => time()]);
}
} else {
Db::name('user_rage')->insert(['user_id' => $user_id, 'goods_id' => $goods_id, 'rage' => $rage_number, 'addtime' => time(), 'updatetime' => time()]);
}
}
}
} catch (\Exception $e) {
}
}
//领主
public function ling_zhu($order, $order_id)
{
//是否开启领主模式
$whe = [];
$whe[] = ['id', '=', $order['goods_id']];
$infinite_goods = Goods::getInfo($whe, 'type,lingzhu_is,lingzhu_fan,lingzhu_shang_id,king_user_id');
if ($infinite_goods['type'] != 8) {
return false;
}
if ($infinite_goods['lingzhu_is'] == 1) {
$whe2 = [];
$whe2[] = ['order_id', '=', $order_id];
$order_list = OrderList::getAllList($whe2, 'id,shang_id,user_id,goods_id', 'id asc');
foreach ($order_list as $k => $v) {
if ($v['shang_id'] == $infinite_goods['lingzhu_shang_id']) {
// 查找当前池子是否有领主
if ($infinite_goods['king_user_id'] != 0) {
Db::name('goods_king_rank')->where([['user_id', '=', $infinite_goods['king_user_id']], ['goods_id', '=', $v['goods_id']]])->order('id', 'desc')->limit(1)->update(['end_time' => time()]);
}
//新的领主
Goods::where(['id' => $v['goods_id']])->update(['king_user_id' => $v['user_id']]);
//多少发晋升领主
$luck_no = OrderList::where([['goods_id', '=', $v['goods_id']], ['id', '<=', $v['id']]])->count();
$rank = [
'user_id' => $v['user_id'],
'goods_id' => $v['goods_id'],
'count' => $luck_no,
'order_list_id' => $v['id'],
'addtime' => time(),
];
$result = Db::name('goods_king_rank')->insert($rank);
} else {
// 查找当前池子是否有领主
$infinite_goods = Goods::getInfo($whe, 'lingzhu_is,lingzhu_fan,lingzhu_shang_id,king_user_id');
if ($infinite_goods['king_user_id'] != 0) {
Db::name('goods_king_rank')->where([['user_id', '=', $infinite_goods['king_user_id']], ['goods_id', '=', $v['goods_id']]])->order('id', 'desc')->limit(1)->inc('z_nums', 1)->update();
$king_money = $infinite_goods['lingzhu_fan'];
if ($king_money && $king_money > 0) {
Db::name('goods_king_rank')->where([['user_id', '=', $infinite_goods['king_user_id']], ['goods_id', '=', $v['goods_id']]])->order('id', 'desc')->limit(1)->inc('money', floatval($king_money))->update();
User::changeIntegral($infinite_goods['king_user_id'], $king_money, 4, '领主收益');
}
}
}
}
}
}
//连击赏
public function lian_ji($order, $order_id)
{
//是否是连击赏
$whe = [];
$whe[] = ['id', '=', $order['goods_id']];
$infinite_goods = Goods::getInfo($whe, 'type,lian_ji_num,lian_ji_shang_id');
if ($infinite_goods['type'] != 9) {
return false;
}
if ($order['is_mibao'] == 1) {
//减少秘宝池次数
User::field('mb_number')->where(['id' => $order['user_id']])->dec('mb_number', $order['prize_num'])->update();
}
$whe2 = [];
$whe2[] = ['order_id', '=', $order_id];
$order_list = OrderList::getAllList($whe2, 'id,shang_id,goods_id,user_id', 'id asc');
foreach ($order_list as $k => $v) {
//是否已有连击
$user_goods_lian_ji = Db::name('user_goods_lian_ji')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->find();
if ($v['shang_id'] == $infinite_goods['lian_ji_shang_id']) {
if ($infinite_goods['lian_ji_num'] != 0 && $infinite_goods['lian_ji_shang_id'] != 0) {
if ($user_goods_lian_ji) {
Db::name('user_goods_lian_ji')->field('number,updatetime')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->inc('number', 1)->update(['updatetime' => time()]);
//连击次数是否满足条件
$lian_ji_number = Db::name('user_goods_lian_ji')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->limit(1)->value('number');
if ($lian_ji_number && $lian_ji_number >= $infinite_goods['lian_ji_num']) {
//赠送秘宝池次数
User::field('mb_number')->where(['id' => $v['user_id']])->inc('mb_number', 1)->update();
//清空连击次数
Db::name('user_goods_lian_ji')->field('number,updatetime')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->update(['number' => 0, 'updatetime' => time()]);
}
} else {
Db::name('user_goods_lian_ji')->insert(['user_id' => $v['user_id'], 'goods_id' => $v['goods_id'], 'number' => 1, 'addtime' => time(), 'updatetime' => time()]);
}
}
} else {
if ($user_goods_lian_ji) {
Db::name('user_goods_lian_ji')->field('number,updatetime')->where([['user_id', '=', $v['user_id']], ['goods_id', '=', $v['goods_id']]])->whereNull('deltime')->order('id', 'desc')->update(['number' => 0, 'updatetime' => time()]);
} else {
Db::name('user_goods_lian_ji')->insert(['user_id' => $v['user_id'], 'goods_id' => $v['goods_id'], 'number' => 0, 'addtime' => time(), 'updatetime' => time()]);
}
}
}
}
/**
* 抽卡机抽奖逻辑
* @param int $user_id
* @param int $order_id
* @param int $goods_id
*/
public function cardextractor_drawprize_notice($user_id = 0, $order_id = 0, $goods_id = 0)
{
$res = [];
// dd(11);
$order = Order::where(['id' => $order_id])
->where(['user_id' => $user_id])
->where(['goods_id' => $goods_id])
->where(['num' => 0])
->where(['status' => 0])
->where(['order_type' => 4])
->find();
// dd($order);
if ($order) {
#改变状态
$res[] = Order::field('status,pay_time')
->where(['id' => $order['id']])
->update([
'status' => 1,
'pay_time' => time(),
]);
#扣余额
if ($order['use_money'] > 0) {
$res[] = User::changeMoney($order['user_id'], -$order['use_money'], 3, '购买盒子' . $order['goods_title']);
}
#扣吧唧币
if ($order['use_integral'] > 0) {
$res[] = User::changeIntegral($order['user_id'], -$order['use_integral'], 2, '购买盒子' . $order['goods_title']);
}
#判断一下优惠券
if (!empty($order['coupon_id'])) {
$coupon = CouponReceiveModel::where(['id' => $order['coupon_id'], 'status' => 0])->update(['status' => 1]);
}
#判断是否发积分 发券
$res[] = User::is_integral_coupon($order);
#判断是否发积分
#分销奖励
$res[] = User::distribution($order);
#分销奖励
#开奖==================================================
$res[] = $this->cardextractor_drawprize($order);
#开奖==================================================
} else {
$res[] = 0;
}
return $res;
}
/**
* 抽卡机开奖
* @param array $order
*/
public function cardextractor_drawprize($order = [])
{
$user_id = $order['user_id'];#用户ID
$order_id = $order['id'];#订单ID
$goods_id = $order['goods_id'];#盒子ID
$prize_num = $order['prize_num'];#抽奖数量
$order_type = $order['order_type'];#订单类型
$prize_card_set = $order['prize_card_set'] ? json_decode($order['prize_card_set'], true) : '';#抽卡机必出设置
// dd($order);
#排除必出等级卡条件
$shang_where = [];
// dd(11111);
#必出卡等级
$set_prize_shang = [];
if ($prize_card_set && isset($prize_card_set['shang_id']) && isset($prize_card_set['shang_count'])) {
// dd($prize_card_set['shang_count']);
// foreach ($prize_card_set as $set_key => $set_value) {
for ($seti = 1; $seti <= $prize_card_set['shang_count']; $seti++) {
$set_prize_shang[] = $prize_card_set['shang_id'];
}
// }
// dd($set_prize_shang);
#排除必出等级卡条件
$shang_where[] = ['id', 'not in', array_unique($set_prize_shang)];
#减去必出数量
$prize_num = $prize_num - count($set_prize_shang);
}
// dd($shang_where);
#卡等级随机
$shang = CardLevel::field('id,title')
// ->where('goods_id', '=', $goods_id)
// ->where('pro', '>', 0)
->where($shang_where)
->select()->toArray();
// dd(1111)
// dd($shang);
if ($shang) {
#卡等级随机
$all_shang_id = [];
foreach ($shang as $shang_value) {
// $real_shang_pro = $shang_value['pro'] * 100;
// for ($is = 1; $is <= $real_shang_pro; $is++) {
$goods = GoodsList::where('shang_id', $shang_value['id'])->where('goods_id', '=', $goods_id)->find();
if ($goods) {
$all_shang_id[] = $shang_value['id'];
}
// }
}
// dd($all_shang_id);
#中奖卡等级
$prize_shang = [];
for ($i = 0; $i < $prize_num; $i++) {
#随机打乱
shuffle($all_shang_id);
shuffle($all_shang_id);
$prize_shang[] = $all_shang_id[0];
}
// dd($prize_shang);
$prize_shang = array_merge($prize_shang, $set_prize_shang);
shuffle($prize_shang);
$save_prize_data = [];
// dd($prize_shang);
#根据卡等级中奖奖品
foreach ($prize_shang as $prize_shang_id) {
#查找奖品
$goodslist = GoodsList::field('id,shang_id,real_pro,special_stock')
->where('goods_id', '=', $goods_id)
->where('shang_id', '=', $prize_shang_id)
->where('num', '=', 0)
->where('real_pro', '>', 0)
->where('special_stock = -100 OR special_stock > 0')
->select()->toArray();
if (empty($goodslist)) {
$res[] = 0;
return $res;
}
// dd($goodslist);
if ($goodslist) {
#组合中奖商品
$all_goods_id = [];
foreach ($goodslist as $value) {
$real_pro = $value['real_pro'] * 100;
for ($i = 1; $i <= $real_pro; $i++) {
$all_goods_id[] = $value['id'];
}
}
#随机打乱
shuffle($all_goods_id);
shuffle($all_goods_id);
$prize_id = $all_goods_id[0];
$prize_info = GoodsList::where(['id' => $prize_id])->find();
$save_prize_data[] = [
'order_id' => $order_id,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => $goods_id,
'num' => 0,
'shang_id' => $prize_info['shang_id'],
'goodslist_id' => $prize_info['id'],
'goodslist_title' => $prize_info['title'],
'goodslist_imgurl' => $prize_info['imgurl'],
'goodslist_price' => $prize_info['price'],
'goodslist_money' => $prize_info['money'],
'goodslist_type' => $prize_info['goods_type'],
'goodslist_sale_time' => $prize_info['sale_time'],
'addtime' => time(),
'prize_code' => $prize_info['prize_code'],
'order_type' => $order_type,
];
if ($prize_info['special_stock'] >= 1) {
#减少库存
$res[] = GoodsList::field('special_stock')
->where(['id' => $prize_info['id']])
->dec('special_stock')
->update();
}
} else {
$res[] = $prize_shang_id;
}
}
#入库===
$res[] = OrderList::insertAll($save_prize_data);
} else {
$res[] = 0;
}
return $res;
}
/**
* 背包发货订单处理
* @param int $user_id 用户id
* @param int $order_id 订单id
*/
public function reward_order_handle($user_id = 0, $order_id = 0)
{
$res = [];
$send_info = OrderListSend::where(['user_id' => $user_id])
->where('user_id', '=', $user_id)
->where('id', '=', $order_id)
->where('status', '=', 0)#0待支付 1待发货 2待收货 3已完成
->find();
if ($send_info) {
#改变订单状态
$res[] = OrderListSend::field('id,status,pay_time')
->where('id', '=', $order_id)
->update([
'status' => 1,#0待支付 1待发货 2待收货 3已完成
'pay_time' => time(),#支付时间
]);
#改变赏品信息
$res[] = OrderList::field('id,status,send_num,choice_time')
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 1)
->where('send_num', '=', $send_info['send_num'])
->update([
'status' => 2,
'choice_time' => time(),
]);
} else {
$res[] = 0;
}
return $res;
}
public function order_notify7()
{
$testxml = file_get_contents("php://input");
$jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
$result = json_decode($jsonxml, true);//转成数组,
// wri($result['out_trade_no'], '秒杀商城回调--' . $result['out_trade_no']);
if ($result) {
//如果成功返回了
$out_trade_no = $result['out_trade_no'];
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
// writelog($out_trade_no, "秒杀商城支付成功--");
Db::startTrans();
$res[] = $this->order_update($out_trade_no, 7);
if (resCheck($res)) {
Db::commit();
} else {
Db::rollback();
}
}
}
$html = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
die($html);
}
public static function order_update($order_num, $type)
{
$res = [];
$order_info = Db::name('kk_order')
->where(['order_no' => $order_num])
->where(['status' => 0])#0待付款 1待发货 2待收货 3确认收货
->find();
if ($order_info) {
#改变订单状态
$res[] = Db::name('kk_order')->field('id,status,pay_time')
->where(['id' => $order_info['id']])
->update([
'status' => 1,#0待付款 1待发货 2待收货 3确认收货
'pay_time' => time(),#支付时间
]);
if ($order_info['money'] > 0) {
// dd($order_info['user_id']);
User::changeMoney($order_info['user_id'], '-' . $order_info['money'], 7);
}
if ($order_info['integral'] > 0) {
User::changeIntegral($order_info['user_id'], '-' . $order_info['integral'], 4);
}
$goods_data = Db::name('kk_order_good')
->field('id,goods_id,goods_num,goods_spec_id')
->where(['order_id' => $order_info['id']])
->select()->toArray();
foreach ($goods_data as $k => $v) {
#加销量
Db::name('kk_product')
->field('id,sale_num')
->where('id', $v['goods_id'])
->inc('sale_num', $v['goods_num'])
->update();
#减库存
Db::name('kk_product_spec')
->field('id,stock')
->where('id', $v['goods_spec_id'])
->dec('stock', $v['goods_num'])
->update();
}
} else {
$res[] = 0;
}
return $res;
}
/**
* 抽奖券======================================================
* @param int $user_id 会员id
* @param int $order_id 订单ID
* @param int $box_id 盲盒id
*/
public function draw_drawprize_notice($user_id = 0, $order_id = 0, $goods_id = 0)
{
$res = [];
$order = Order::where(['id' => $order_id])
->where(['user_id' => $user_id])
->where(['goods_id' => $goods_id])
->where(['num' => 0])
->where(['status' => 0])
->where(['order_type' => 7])
->find();
if ($order) {
#改变状态
$res[] = Order::field('status,pay_time')
->where(['id' => $order['id']])
->update([
'status' => 1,
'pay_time' => time(),
]);
#扣抽奖券
if ($order['use_draw'] > 0) {
$res[] = User::changeDraw($order['user_id'], -$order['use_draw'], 3, '抽奖券消费' . $order['goods_title']);
}
#开奖==================================================
$res[] = $this->draw_drawprize($order);
#开奖==================================================
} else {
$res[] = 0;
}
return $res;
}
/**
* 无限赏开奖逻辑
* @param $order 订单信息
*/
protected function draw_drawprize($order = [])
{
$user_id = $order['user_id'];#用户ID
$order_id = $order['id'];#订单ID
$goods_id = $order['goods_id'];#盒子ID
$prize_num = $order['prize_num'];#抽奖数量
$order_type = $order['order_type'];#订单类型
#查找奖品
$goodslist = GoodsList::field('id,shang_id,real_pro')
->where('goods_id', '=', $goods_id)
->where('num', '=', 0)
->where('real_pro', '>', 0)
->select()->toArray();
if ($goodslist) {
#组合中奖商品
$all_goods_id = [];
foreach ($goodslist as $value) {
$real_pro = $value['real_pro'] * 100;
for ($i = 1; $i <= $real_pro; $i++) {
$all_goods_id[] = $value['id'];
}
}
for ($i = 0; $i < $prize_num; $i++) {
#随机打乱
shuffle($all_goods_id);
shuffle($all_goods_id);
$prize_id = $all_goods_id[0];
$prize_info = GoodsList::where(['id' => $prize_id])->find();
#编号
$luck_no = OrderList::field('id')
->where('goods_id', '=', $goods_id)
->where('num', '=', 0)
->where('order_type', '=', $order_type)
->order('id desc')
->value('luck_no');
$luck_no++;
#新增记录
$save_prize_info = [
'order_id' => $order_id,
'user_id' => $user_id,
'status' => 0,#0未操作 1选择兑换 2选择发货
'goods_id' => $goods_id,
'num' => 0,
'shang_id' => $prize_info['shang_id'],
'goodslist_id' => $prize_info['id'],
'goodslist_title' => $prize_info['title'],
'goodslist_imgurl' => $prize_info['imgurl'],
'goodslist_price' => $prize_info['price'],
'goodslist_money' => $prize_info['money'],
'goodslist_type' => $prize_info['goods_type'],
'goodslist_sale_time' => $prize_info['sale_time'],
'addtime' => time(),
'prize_code' => $prize_info['prize_code'],
'order_type' => $order_type,
'luck_no' => $luck_no,
];
#入库===
$res[] = OrderList::insert($save_prize_info);
}
} else {
$res[] = 0;
}
return $res;
}
/**
* 道具卡======================================================
* @param int $user_id 会员id
* @param int $order_id 订单ID
* @param int $box_id 盲盒id
*/
public function item_card_notice($user_id = 0, $order_id = 0, $goods_id = 0)
{
$res = [];
$order = Order::where(['id' => $order_id])
->where(['user_id' => $user_id])
->where(['goods_id' => $goods_id])
->where(['num' => 0])
->where(['status' => 0])
->find();
if ($order) {
#改变状态
$res[] = Order::field('status,pay_time')
->where(['id' => $order['id']])
->update([
'status' => 1,
'pay_time' => time(),
]);
#扣道具卡
if ($order['use_item_card'] > 0) {
$res[] = Db::name('user_item_card')->where(['user_id' => $user_id, 'status' => 1])->order('id asc')->limit($order['use_item_card'])->update(['status' => 2, 'order_id' => $order_id, 'updatetime' => time()]);
}
#开奖==================================================
$res[] = $this->draw_drawprize($order);
#开奖==================================================
} else {
$res[] = 0;
}
return $res;
}
public function wx_gf_fahuo($user_id, $order_num)
{
//微信官方发货
$wxServer = new \app\common\server\Wx($this->app);
$access_token = $wxServer->get_access_token();
$open_id = Db::name('user')->where('id', $user_id)->value('openid');
$wxServer->post_order($open_id, $access_token, $order_num);
}
}