|
|
@@ -9,6 +9,7 @@ namespace app\models\store;
|
|
|
|
|
|
use app\admin\model\system\ShippingTemplatesFree;
|
|
|
use app\admin\model\system\ShippingTemplatesRegion;
|
|
|
+use app\models\system\SystemUserAgentLevel;
|
|
|
use app\models\system\SystemUserLevel;
|
|
|
use crmeb\basic\BaseModel;
|
|
|
use think\db\exception\DataNotFoundException;
|
|
|
@@ -19,7 +20,7 @@ use crmeb\traits\ModelTrait;
|
|
|
use think\facade\Log;
|
|
|
use app\models\system\SystemStore;
|
|
|
use app\models\routine\RoutineTemplate;
|
|
|
-use app\models\user\{User, UserAddress, UserBill, UserSpread, WechatUser};
|
|
|
+use app\models\user\{User, UserAddress, UserBill, UserLevel, UserSpread, WechatUser};
|
|
|
use crmeb\services\{
|
|
|
SystemConfigService, WechatTemplateService, workerman\ChannelService
|
|
|
};
|
|
|
@@ -318,7 +319,7 @@ class StoreOrder extends BaseModel
|
|
|
$mer_id = [];
|
|
|
$totalNum = 0;
|
|
|
$gainIntegral = 0;
|
|
|
- $is_wholesale=-1;
|
|
|
+ $is_wholesale = -1;
|
|
|
foreach ($cartInfo as $cart) {
|
|
|
$cartIds[] = $cart['id'];
|
|
|
$totalNum += $cart['cart_num'];
|
|
|
@@ -329,8 +330,8 @@ class StoreOrder extends BaseModel
|
|
|
$cartInfoGainIntegral = isset($cart['productInfo']['give_integral']) ? bcmul($cart['cart_num'], $cart['productInfo']['give_integral'], 2) : 0;
|
|
|
$gainIntegral = bcadd($gainIntegral, $cartInfoGainIntegral, 2);
|
|
|
|
|
|
- if ($is_wholesale!= -1){
|
|
|
- if ($is_wholesale != intval($cart['productInfo']['is_wholesale']) ){
|
|
|
+ if ($is_wholesale != -1) {
|
|
|
+ if ($is_wholesale != intval($cart['productInfo']['is_wholesale'])) {
|
|
|
return self::setErrorInfo('批发商品和普通商品不能混合购买!', true);
|
|
|
}
|
|
|
}
|
|
|
@@ -435,7 +436,7 @@ class StoreOrder extends BaseModel
|
|
|
$mer_id = array_unique($mer_id);
|
|
|
if (count($mer_id) > 1) {
|
|
|
$mer_id_string = implode(',', $mer_id);
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
$mer_id_string = $mer_id[0];
|
|
|
}
|
|
|
$orderInfo = [
|
|
|
@@ -468,6 +469,7 @@ class StoreOrder extends BaseModel
|
|
|
'add_time' => time(),
|
|
|
'unique' => $key,
|
|
|
'shipping_type' => $shipping_type,
|
|
|
+ 'is_wholesale' => $is_wholesale
|
|
|
];
|
|
|
if ((string)$shipping_type === 2) {
|
|
|
$orderInfo['verify_code'] = self::getStoreCode();
|
|
|
@@ -506,7 +508,8 @@ class StoreOrder extends BaseModel
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static function createOrderPart($orderInfo){
|
|
|
+ public static function createOrderPart($orderInfo)
|
|
|
+ {
|
|
|
$user_info = User::where('uid', $orderInfo['uid'])->find();
|
|
|
$cart_ids = explode(',', $orderInfo['cart_id']);
|
|
|
|
|
|
@@ -521,25 +524,25 @@ class StoreOrder extends BaseModel
|
|
|
})
|
|
|
->toArray();
|
|
|
$grouped = array_column($cartInfo, 'ids', 'mer_id');
|
|
|
- foreach ($cartInfo as $k => $v){
|
|
|
- $total_num=0;
|
|
|
+ foreach ($cartInfo as $k => $v) {
|
|
|
+ $total_num = 0;
|
|
|
$total_price = 0;
|
|
|
$deduction_price = 0;
|
|
|
- $cartInfo = StoreCart::where('id','in',$v)->select();
|
|
|
- foreach ($cartInfo as $cart){
|
|
|
+ $cartInfo = StoreCart::where('id', 'in', $v)->select();
|
|
|
+ foreach ($cartInfo as $cart) {
|
|
|
$price = StoreProduct::where('id', $cart['product_id'])->value('price');
|
|
|
$product_price = bcmul($price, $cart['cart_num'], 2);
|
|
|
$total_price = bcadd($total_price, $product_price, 2); //商品总价
|
|
|
$total_num = bcadd($total_num, $cart['cart_num'], 2);
|
|
|
}
|
|
|
- if ($user_info['level']>0){ //会员折扣
|
|
|
+ if ($user_info['level'] > 0) { //会员折扣
|
|
|
$discount = SystemUserLevel::where('id', $user_info['level'])->value('discount');
|
|
|
$moto_price = $total_price;
|
|
|
$total_price = bcmul($total_price, bcsub(1, $discount, 2), 2);
|
|
|
$deduction_price = bcsub($moto_price, $total_price, 2); //抵扣金额
|
|
|
}
|
|
|
|
|
|
- $orderInfo =[
|
|
|
+ $orderInfo = [
|
|
|
'mer_id' => $k,
|
|
|
'cart_id' => $v,
|
|
|
'order_id' => $orderInfo['order_id'],
|
|
|
@@ -559,6 +562,7 @@ class StoreOrder extends BaseModel
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 回退积分
|
|
|
* @param $order 订单信息
|
|
|
@@ -659,6 +663,7 @@ class StoreOrder extends BaseModel
|
|
|
} while (self::be(['order_id' => $orderId]));// $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
|
|
|
return $orderId;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 生成订单唯一id
|
|
|
* @param $uid 用户uid
|
|
|
@@ -868,10 +873,195 @@ class StoreOrder extends BaseModel
|
|
|
UserBill::expend('购买商品', $order['uid'], 'now_money', 'pay_money', $order['pay_price'], $order['id'], $now_money, '支付' . floatval($order['pay_price']) . '元购买商品');
|
|
|
//支付成功后
|
|
|
event('OrderPaySuccess', [$order, $formId]);
|
|
|
+ self::userLevelReward($order, $order['uid'], 1); //激励奖 会员返现
|
|
|
+ if ($order['is_wholesale'] == 1) { //批发订单
|
|
|
+ self::wholesale($order); //批发订单返利
|
|
|
+ self::umbrella($order); //伞下返利
|
|
|
+ }
|
|
|
$res = $res1 && $resPink && UserSpread::setSpreadSure($order['uid']) && User::backOrderBrokerage($order);
|
|
|
return false !== $res;
|
|
|
}
|
|
|
|
|
|
+// 激励奖 会员返现
|
|
|
+ public static function userLevelReward($order, $uid, $type)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $userInfo = User::get($uid);
|
|
|
+ if ($userInfo['level'] > 0) {
|
|
|
+ $user_radio = UserLevel::where('id', $userInfo['level'])->value('rebate_ratio'); //用户等级返现比例
|
|
|
+ $rebate = bcmul($order['pay_price'], bcdiv($user_radio, 100, 2), 2); //用户返现金额
|
|
|
+ $blance = bcadd($userInfo['brokerage_price'], $rebate, 2); //用户佣金
|
|
|
+ if ($type == 1) {
|
|
|
+ UserBill::income('商品返利', $uid, 'brokerage_price', 'order_money', $rebate, $order['id'], $blance, '商品返利佣金');
|
|
|
+ if ($userInfo['spread_uid'] > 0) {
|
|
|
+ self::userLevelReward($order, $userInfo['spread_uid'], 2);
|
|
|
+ }
|
|
|
+ } elseif ($type == 2) { //下级推荐返利
|
|
|
+ UserBill::income('下级商品返利', $uid, 'brokerage_price', 'order_money', $rebate, $order['id'], $blance, '下级购买商品返利佣金');
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+// $rebate = 0;
|
|
|
+// $shopping_cart = json_decode($order['cart_id'], true);
|
|
|
+// 先不考虑什么商品返现比例了
|
|
|
+// foreach ($shopping_cart as $cart) {
|
|
|
+// $product_info = StoreCart::get($cart['cart_id']);
|
|
|
+// $product_radio = StoreProduct::where('id', $product_info['product_id'])->find(); //商品返现比例
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+ }
|
|
|
+ }catch (\Exception $e) {
|
|
|
+ // 处理异常
|
|
|
+ @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-会员等级激励奖报错内容\r\n", 8);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+//批发商品奖励 因为能买批发商品所以肯定有代理等级
|
|
|
+ public static function wholesale($order)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $userInfo = User::get($order['uid']);
|
|
|
+ $user_agnet = SystemUserAgentLevel::where('id', $userInfo['agent'])->find();
|
|
|
+ $one = 0; //一级直推是否比代理等级高
|
|
|
+ $two = 0; //二级推荐人是否比一级直推等级高
|
|
|
+
|
|
|
+ if ($userInfo['spread_uid'] > 0) { //有推荐人
|
|
|
+ $one_spread_info = User::get($userInfo['spread_uid']);
|
|
|
+ if ($one_spread_info['agent'] > 0) {
|
|
|
+ $one_agent = SystemUserAgentLevel::where('id', $one_spread_info['agent'])->find();
|
|
|
+ $one = $one_agent['grade'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($one_spread_info['spread_uid'] > 0) {
|
|
|
+ $two_spread_info = User::get($one_spread_info['spread_uid']);
|
|
|
+ if ($two_spread_info['agent'] > 0) {
|
|
|
+ $two_agent = SystemUserAgentLevel::where('id', $two_spread_info['agent'])->find();
|
|
|
+ $two = $two_agent['grade'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// 先算直推的
|
|
|
+ if ($userInfo['agent'] > 0 && !empty($one_spread_info)) {
|
|
|
+ if ($one_spread_info['agent'] > 0) {
|
|
|
+ if ($one_agent['grade'] > $user_agnet['grade']) { //如果直推代理等级大于自己
|
|
|
+ $discount_difference = bcsub($one_agent['discount'], $user_agnet['discount'], 2);
|
|
|
+ $price = bcmul($order['total_price'], bcdiv($discount_difference, 100, 2), 2); //直推上级获得差价
|
|
|
+ } elseif ($one_agent['discount'] <= $user_agnet['discount']) { //一级直推等级小于等于自己
|
|
|
+ $price = bcmul($order['total_price'], bcdiv($one_agent['direct_rebates'], 100, 2), 2); //直推上级获得直推返利
|
|
|
+ }
|
|
|
+ }
|
|
|
+// $blance = User::where('uid', $spread_uid)->value('brokerage_price');
|
|
|
+ $blance = bcadd($one_spread_info['brokerage_price'], $price, 2);
|
|
|
+ UserBill::income('直推代理返利', $one_spread_info['uid'], 'brokerage_price', 'agent_rebates', $price, $order['id'], $blance, '直推下级购买商品返利');
|
|
|
+ User::where('uid', $one_spread_info['uid'])->update(['brokerage_price' => $blance]);
|
|
|
+
|
|
|
+ }
|
|
|
+ $price=0;
|
|
|
+ //然后算二级推荐人的
|
|
|
+ if ($userInfo['agent'] > 0 && $two_spread_info) {
|
|
|
+ if ($two_spread_info['agent'] > 0) {
|
|
|
+ if ($two == $one) { //如果直推等级等于二级推荐人等级
|
|
|
+ if ($two > $user_agnet['grade']) { //如果二级推荐人等级高于自己
|
|
|
+ $price = bcmul($order['total_price'], bcdiv($two_agent['indirect_rebates'], 100, 2), 2); //二级推荐人获得间接比例返利
|
|
|
+ }
|
|
|
+ } elseif ($two > $one) { //如果二级推荐人等级大于直推等级
|
|
|
+ if ($two > $user_agnet['grade']) { //如果二级推荐人等级高于自己
|
|
|
+ $indirect_rebates = bcmul($two_agent['indirect_rebates'],2,2);
|
|
|
+ $price = bcmul($order['total_price'], bcdiv($indirect_rebates, 100, 2), 2); //二级推荐人获得直接推荐比例的两倍
|
|
|
+
|
|
|
+ } elseif ($two == $user_agnet['grade']) { //如果二级推荐人等级等于自己
|
|
|
+ $price = bcmul($order['total_price'], bcdiv($two_agent['indirect_rebates'], 100, 2), 2); //二级推荐人获得间接比例返利
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $blance2 = bcadd($two_spread_info['brokerage_price'], $price, 2);
|
|
|
+ UserBill::income('间接代理返利', $two_spread_info['uid'], 'brokerage_price', 'agent_rebates', $price, $order['id'], $blance2, '间接下级购买商品返利');
|
|
|
+ User::where('uid', $two_spread_info['uid'])->update(['brokerage_price' => $blance2]);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }catch (\Exception $e) {
|
|
|
+ // 处理异常
|
|
|
+ @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-代理等级批发返利报错内容\r\n", 8);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+//伞下返利
|
|
|
+ public static function umbrella($order){
|
|
|
+ try{
|
|
|
+// 获取所有伞下比例大于0的等级
|
|
|
+ $agent_level = SystemUserLevel::where('umbrella_rebates','>',0)->where('is_show',1)->where('is_del',0)->column('id');
|
|
|
+
|
|
|
+// for循环所有伞下比例大于0的等级计算他们每个等级伞下比例乘订单金额的奖池数量再除以该等级的上级人数
|
|
|
+ foreach ($agent_level as $level){
|
|
|
+ $level_uids = self::getAllSuperiors($order['uid'], $level['id']); //获取所有符合等级的上级用户UID
|
|
|
+ $level_count = count($level_uids); //符合等级的上级人数
|
|
|
+ $pool = bcmul($order['total_price'],bcdiv($level['umbrella_rebates'],100,2),2); //该等级奖池
|
|
|
+ $user_price = bcdiv($pool,$level_count,2); //该等级奖池/符合等级的上级人数
|
|
|
+ $name = SystemUserLevel::where('id',$level['id'])->value('name'); //等级名称
|
|
|
+ foreach ($level_uids as $uid) {
|
|
|
+ $brokerage_price = User::where('uid', $uid)->value('brokerage_price');
|
|
|
+ $blance = bcadd($brokerage_price, $user_price, 2);
|
|
|
+ UserBill::income('伞下团体返利', $uid, 'brokerage_price', 'umbrella_rebates', $user_price, $order['id'], $blance, $name.'伞下团队购买商品返利');
|
|
|
+ User::where('uid', $uid)->update(['brokerage_price' => $blance]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (\Exception $e) {
|
|
|
+ // 处理异常
|
|
|
+ @file_put_contents('quanju.txt', json_encode(['line' => $e->getLine(), 'message' => $e->getMessage(), 'file' => $e->getFile()]) . "-伞下团队批发返利报错内容\r\n", 8);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 递归获取所有上级(推荐人/推广人)的UID
|
|
|
+ * @param int $uid 团队成员的ID
|
|
|
+ * @param int|null $agent 代理UID,如果传入,则只返回上级列表中agent字段等于此值的UID
|
|
|
+ * @param array $allSuperiors 内部递归使用,用于存储所有上级UID,初始为空数组
|
|
|
+ * @return array 过滤后的上级UID列表
|
|
|
+ */
|
|
|
+ public static function getAllSuperiors($uid, $agent = null, &$allSuperiors = []) {
|
|
|
+ // 1. 获取当前用户的直接上级 spread_uid
|
|
|
+ // 假设 User::where('uid', $uid)->value('spread_uid') 可以获取到该用户的直接上级ID
|
|
|
+ $superiorUid = User::where('uid', $uid)->value('spread_uid');
|
|
|
+
|
|
|
+ // 2. 检查是否存在上级且上级UID不是0或null
|
|
|
+ if (!empty($superiorUid)) {
|
|
|
+ // 3. 将当前获取到的上级UID添加到结果数组中
|
|
|
+ $allSuperiors[] = $superiorUid;
|
|
|
+
|
|
|
+ // 4. 递归调用,以当前的上级作为新的 $uid,继续查找他的上级
|
|
|
+ // 注意:继续将 $agent 和 $allSuperiors 传递给下一次递归
|
|
|
+ self::getAllSuperiors($superiorUid, $agent, $allSuperiors);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 递归结束,对收集到的所有上级UID进行去重
|
|
|
+ $resultUids = array_unique($allSuperiors);
|
|
|
+
|
|
|
+ // 6. 如果传入了 $agent,则进行过滤
|
|
|
+ if (!empty($agent) && !empty($resultUids)) {
|
|
|
+
|
|
|
+ // 批量查询数据库,找出 $resultUids 中 agent 字段等于传入 $agent 的 UIDs
|
|
|
+ // 假设 agent 字段存储在 User 表中
|
|
|
+ $filteredUids = User::whereIn('uid', $resultUids)
|
|
|
+ ->where('agent', $agent)
|
|
|
+ ->column('uid');
|
|
|
+
|
|
|
+ // 返回过滤后的结果
|
|
|
+ return $filteredUids;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 7. 如果没有传入 $agent,则返回所有收集到的上级 UID
|
|
|
+ return $resultUids;
|
|
|
+ }
|
|
|
/*
|
|
|
* 线下支付消息通知
|
|
|
* 待完善
|