where("id",$user_id)->dec("vr",$orderInfo['price'])->save();//1.0jbp旧表 // Db::name("user")->where("uid",$user_id)->dec("vr",$orderInfo['price'])->save();//2.0新表 $result = true; if ($result) { $invite_id = $goods_of_info['spread_id']; $zs_ylj = 0; //养老金赋值 if ($goods_of_info['money'] == 1180) { //修改订单到账状态 $zs_ylj = round(700 * 0.05, 2);//计算的养老金 金额 } elseif ($goods_of_info['money'] == 11800) { $zs_ylj = round(7000 * 0.05, 2);//计算的养老金 金额 } elseif ($goods_of_info['money'] == 3540) { $zs_ylj = round(2100 * 0.05, 2);//计算的养老金 金额 } elseif ($goods_of_info['money'] == 10620) { $zs_ylj = round(6300 * 0.05, 2);//计算的养老金 金额 } elseif ($goods_of_info['money'] == 9440) { $zs_ylj = round(5600 * 0.05, 2);//八单计算的养老金 金额 } elseif ($goods_of_info['money'] == 2360) { $zs_ylj = round(1400 * 0.05, 2);//两单计算的养老金 金额 } $uid = $user_id; $new_user_id = $uid; if (empty($new_user_id)) { abort(0, "当前绑定会员查询错误"); } $new_nums = Db::name("old_user_of_goods")->where("user_id", $uid)->sum("money"); //增加过后权益对比 获取当前权益等级 $old_level_id = Db::name("old_user_jbp")->where("id", $uid)->find(); $old_level_info = Db::name("old_user_level")->where("id", $old_level_id['user_level_id'])->find(); $level_info = []; $level_list = Db::name("old_user_level")->order("money desc")->select(); //计算会员等级 升级 foreach ($level_list as $key => $value) { # code... if ($new_nums >= $value['money']) { $level_info = $value; break; } } $zs_gb = $orderInfo['price'] * 3; $zs_withdraw_quota = $orderInfo['price']; if ($old_level_info['id'] == $level_info['id']) { //未到达升级权益 不改变用户等级 $update_status = false; } else { $update_status = true; $user_level = $level_info['id']; //var_dump( $user_level); Db::name("old_user_jbp")->where("id", $new_user_id)->save(['user_level_id' => $user_level]);//1.0jbp旧表 Db::name("user")->where("uid", $new_user_id)->save(['user_level_id_old' => $user_level]);//2.0新表 // todo 同时改变他的权益 } // Db::name("old_user_jbp")->where("id",$new_user_id)->inc("withdraw_quota",$zs_withdraw_quota)->save();//1.0jbp旧表 // Db::name("user")->where("uid",$new_user_id)->save(['withdraw_quota_old'=>$zs_withdraw_quota]);//2.0新表 // 下发邀请人赠送的GB // $zs_invite_gb = round($orderInfo['price']*0.3,2); // //todo // Db::name("old_user_jbp")->where("id",$invite_id)->inc("gb",$zs_invite_gb)->save();//1.0jbp旧表 // Db::name("user")->where("uid",$invite_id)->inc("score",$zs_invite_gb)->save();//2.0新表 //$old_have_pension = Db::name("old_user_jbp")->where("id",$invite_id)->value("have_pension"); //检查是不是订单价格是1180或者11800,更改VR if ($goods_of_info['money'] == 1180 || $goods_of_info['money'] == 11800 || $goods_of_info['money'] == 3540 || $goods_of_info['money'] == 10620 || $goods_of_info['money'] == 9440 || $goods_of_info['money'] == 2360) { //修改订单到账状态 Db::name("old_user_of_goods")->where("order_id", $goods_of_info1['id'])->save(['status' => 1, 'pay_gb' => 0, 'pay_vr' => $pay_vr1, 'pay_bt' => 0, 'create_time' => time()]); } } if ($result) { //更改订单状态 Db::commit(); $new_user_name = Db::name("old_user_jbp")->where("id", $new_user_id)->value("user_name"); $new_user_name = empty($new_user_name) ? "" : $new_user_name; //记录日志信息 if ($user_id == $invite_id) { //报单中心=推荐人 GB记录日志有问题需要传入指定参数去计算 //$money = Db::name("old_user_jbp")->where("id",$user_id)->value("gb"); //$appoint_money = $money - $zs_invite_gb; if ($goods_of_info['money'] == 1180 || $goods_of_info['money'] == 11800 || $goods_of_info['money'] == 3540 || $goods_of_info['money'] == 10620 || $goods_of_info['money'] == 9440 || $goods_of_info['money'] == 2360) { $this->change_user_log66($user_id, "vr", 2, $pay_vr1, "declare_expend", '权益消费VR-' . $new_user_name); } } else { if ($goods_of_info['money'] == 1180 || $goods_of_info['money'] == 11800 || $goods_of_info['money'] == 3540 || $goods_of_info['money'] == 10620 || $goods_of_info['money'] == 9440 || $goods_of_info['money'] == 2360) { $this->change_user_log66($user_id, "vr", 2, $pay_vr1, "declare_expend", '权益消费VR-' . $new_user_name); } } // $this->change_user_log66($new_user_id,'gb',1,$zs_gb,"levle_update_zs","权益升级达标赠送"); $this->change_user_log66($new_user_id, 'withdraw_quota_old', 1, $zs_withdraw_quota, "levle_update_zs", "权益升级达标赠送"); // $this->change_user_log66($invite_id,'gb',1,$zs_invite_gb,"levle_update_zs","邀请-".$new_user_name); // echo 123;return; //处理查看是否有待发放佣金 // $zs_withdraw_quota Db::name("old_user_jbp")->where("id", $new_user_id)->inc("total_withdraw_quota", $zs_withdraw_quota)->save();//1.0jbp旧表 Db::name("user")->where("uid", $new_user_id)->inc("total_withdraw_quota_old", $zs_withdraw_quota)->save(); // $this->releaseTobeAmount($new_user_id,$zs_withdraw_quota); // $this->success("支付成功",['is_cash'=>0]); } Db::rollback(); // $this->error("操作失败"); } function change_user_log66($user_id, $type, $change_status, $money, $routine, $remark = "", $is_admin = 1, $appoint_money = 0) { $transition = [ 'pv' => 1, "gb" => 2, "df" => 3, "vr" => 4, "bt" => 5, "withdraw_quota_old" => 6, "unsettled_pension" => 7, "amount_old" => 8 ]; $routineInfo = Db::name("old_routine_log_of_key")->where("key", $routine)->find(); if (empty($routineInfo)) { return false; } $key_id = isset($routineInfo['id']) ? $routineInfo['id'] : 0; //再去查询变更前的 金额 if ($appoint_money > 0) { $alter_money = $appoint_money; } else { //再去查询变更前的 金额 $alter_money = Db::name("user")->where('uid', $user_id)->value($type); } if ($change_status == 1) { //新增前 $alter_money = $alter_money - $money; } else if ($change_status == 2) { //减少前 $alter_money = $alter_money + $money; } $inser_data = []; $inser_data['user_id'] = $user_id; $inser_data['type'] = $transition[$type]; $inser_data['change_status'] = $change_status; $inser_data['money'] = $money; $inser_data['routine_log_of_key_id'] = $key_id; $inser_data['remark'] = $remark; $inser_data['is_admin'] = $is_admin; $inser_data['create_time'] = time(); $inser_data['alter_money'] = isset($alter_money) ? $alter_money : 0; Db::name("old_user_log")->insert($inser_data); } //添加推广佣金/养老金日志(养老金金额) public function bill_user_log($uid, $name, $num, $mark) { $con_data = Db::name('user')->where("uid", $uid)->find(); //如果是会员专区就即刻到账 Db::name('user')->where("uid", $con_data['uid'])->update(['brokerage_price' => $con_data['brokerage_price'] + $num]); $bill = [ 'uid' => $uid, 'link_id' => 0,//关联订单id 'pm' => 1,//0:支出,1:获得 'title' => $name,//账单标题 'category' => 'now_money',//明细种类 'type' => 'commission',//明细类型 'number' => $num,//明细数字 'balance' => 0,//剩余 'mark' => $mark,//备注 'create_time' => date('Y-m-d H:i:s'),//添加时间 'status' => 1,//0待确定,1有效,-1无效 'commission_type' => 8,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费 //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金 //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’ 'order_sn' => 0,//订单号 'tripartite' => 0,// 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上 'mer_id' => 0,//商户id 'source' => 0,//1线下 0线上 'order_type' => 1,//1自营 2 第三方 订单类型 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入 'take_time' => '',//结算时间 'district_id' => '',// 'street_id' => '',// 'month' => '',//月份 ]; Db::name('user_bill')->insert($bill); } public function lst2(Request $request) { $startTime = $request->param('startTime', null); $endTime = $request->param('endTime', null); $isTest = $request->param('isTest', 1); $maxAllocationAmount = $request->param('maxAllocationAmount', null); // 查询测试人员账号信息 //18973958920 //15186694339 //18833633835 //19525470098 //17756507016 //18226705337 $testAccount = ['18973958920', '15186694339', '18833633835', '19525470098', '17756507016', '18226705337', '17855366921']; $removeUserList = Db::name("user") ->whereIn('account', $testAccount) ->where("status", "<>", 0) ->where('is_del', '<>', 1) ->select() ->toArray(); $removeUserIdList = array_column($removeUserList, 'uid'); if (empty($startTime) || empty($endTime)) { $timeMap = $this->getTime('last_week'); $startTime = $timeMap['startTime'] . ' 00:00:00'; $endTime = $timeMap['endTime'] . ' 23:59:59'; } $zeroLineUidList = [17]; $result = []; foreach ($zeroLineUidList as $uid) { //查询当前团队的用户列表 $sql = "select getUserLevelId($uid) as uids"; //\think\facade\Log::info("lplpDB".$sql); try { $spread_user_list = Db::query($sql); } catch (\Exception $exception) { $result[$uid] = $exception->getMessage(); continue; } $spread_user_list = array_diff(explode(",", trim($spread_user_list[0]['uids'], '$,')), $removeUserIdList); if (empty($spread_user_list)) { $result[$uid] = '无团队成员1'; continue; } // 定义分红金额 $performance = $this->getTeamPvByTime($startTime, $endTime, $spread_user_list); // 分红金额 - 红包金额 // $redEnvelope = $this->getRedEnvelopeByTime($startTime, $endTime, $spread_user_list); $redEnvelope = 0.00; $weekRecord = bcsub($performance, $redEnvelope, 2); if (!is_null($maxAllocationAmount)) { $weekRecord = $maxAllocationAmount; } if ($weekRecord <= 0) { $result[$uid] = '无效分红金额'; continue; } $originator_award = round(($weekRecord * 0.05), 2); $lianc_list = Db::name("old_user_lianc")->order("id desc")->field("id,number,title")->select()->toArray(); $lianc_list_arr = array_column($lianc_list, null, 'id'); $list = Db::name("user") ->whereIn('uid', $spread_user_list) ->where("originator_id_old", ">", 0) ->where("status", 1) ->field("uid,originator_id_old") ->select() ->toArray(); $user_arr = []; foreach ($list as $key => $value) { # code... $user_arr[$value['originator_id_old']]['people'][] = $value['uid']; $user_arr[$value['originator_id_old']]['money'] = $lianc_list_arr[$value['originator_id_old']]['number']; $user_arr[$value['originator_id_old']]['title'] = $lianc_list_arr[$value['originator_id_old']]['title']; $user_arr[$value['originator_id_old']]['id'] = $lianc_list_arr[$value['originator_id_old']]['id']; } //处理计算权重 和平均分配的金额 $total_num = 0; $count_num = 0; foreach ($user_arr as $key => $value) { $user_arr[$key]['people_count'] = count($user_arr[$key]['people']); # code... // floor(($weekRecord * ($value['profit_rate'] / 100)) * 100) / 100; // $total_num += round($value['money'] * count($value['people']) / 1000, 2); $total_num += floor(($value['money'] * count($value['people'])) * 100) / 100; if (isset($user_arr[$key]['total_num'])) { $user_arr[$key]['total_num'] += floor(($value['money'] * count($value['people'])) * 100) / 100; } else { $user_arr[$key]['total_num'] = 0; } $count_num += count($value['people']); } foreach ($user_arr as $key => $value) { # code... // $rate = round((count($value['people']) * $value['money']) / $total_num, 2) / 1000; $rate = floor(($value['money'] * count($value['people']) / $total_num) * 100) / 100; $user_arr[$key]['rate'] = $rate; $distribution_money = floor($originator_award * $rate * 100) / 100; $user_arr[$key]['distribution_money'] = $distribution_money; // $user_arr[$key]['people_award'] = round($distribution_money / count($value['people']), 2);//可分配佣金 $user_arr[$key]['people_award'] = floor($distribution_money / count($value['people']) * 100) / 100;//可分配佣金 } $count = 0; foreach ($user_arr as $key => $value) { $distribution_money = $value['distribution_money']; # code... foreach ($value['people'] as $k => $v) { # code... $award = $value['people_award']; //90给佣金,10给复购金 $yj_90 = $award * 0.9; $fg_10 = $award * 0.1; if ($isTest == 0) { Db::name("user")->where("uid", $v)->inc("brokerage_price", $yj_90)->save(); $res = Db::name("user")->where("uid", $v)->inc("total_amount_old", $yj_90)->save(); Db::name("user")->where("uid", $v)->inc("amount_old", $yj_90)->save();//不用冻结金额和提现额度来限制佣金发放 $res3 = Db::name("user")->where("uid", $v)->inc("fugou", $fg_10)->save(); $res = true; if ($res) { $$count = $count + 1; $this->change_user_log($v, 'amount_old', 1, $yj_90, "relation_profits", "联创分红入账" . "-" . $value['title']); //入账 $week_data = []; $week_data['user_id'] = $v; $week_data['user_lianc_id'] = $value['id']; $week_data['money'] = $yj_90; $week_data['remark'] = "联创分红入账主动执行" . $yj_90; $week_data['all_record'] = $originator_award; $week_data['all_amount'] = $distribution_money; $week_data['create_time'] = time(); Db::name("old_user_originator")->insert($week_data); $userdatassss = db::name("user")->where("uid", $v)->find(); $datas_log = [ 'note' => "名称:" . $userdatassss['nickname'] . " 用户:" . $v . " 新增联创佣金:" . $yj_90, 'create_time' => date('Y-m-d H:i:s') ]; Db::name('test_log')->insert($datas_log); //数据存到2.0数据表中 $this->bill_user_log111111($v, "联创分佣", $yj_90, "联创明细入账", 12);//股权分佣明细2.0系统 $this->fugou_user_log($v, $fg_10, 16, 1, 1);//复购金明细入账 } else { // .. } } } } $result[$uid] = $user_arr; } return json($result); } //添加复购金 public function fugou_user_log($uid, $num, $order_id, $type_inc_des, $status = -1) { $fugou_data = Db::name('user')->where("uid", $uid)->find(); $bill = [ 'uid' => $uid, 'number' => $num,//数量 'status' => $status,//复购金状态1-有效 0-失效 -1待确认 'mark' => "复购金",//备注 'desc' => "10%作为复购金",//描述 'order_id' => $order_id,//订单id 'surplus' => 0,//剩余 'order_type' => 11,//关联订单ID 'type' => $type_inc_des,//复购金类型 :1赠送,2消耗 'settlement_time' => date('Y-m-d H:i:s'),//添加时间, 'addtime' => time(), ]; Db::name('user_sign_fugou')->insert($bill); } //添加平台分佣2.0系统收益明细日志(养老金金额) public function bill_user_log111111($uid, $name, $num, $mark, $comm) { $con_data = Db::name('user')->where("uid", $uid)->find(); //如果是会员专区就即刻到账 // Db::name('user')->where("uid",$con_data['uid'])->update(['brokerage_price'=> $con_data['brokerage_price'] + $num]); $bill = [ 'uid' => $uid, 'link_id' => 0,//关联订单id 'pm' => 1,//0:支出,1:获得 'title' => $name,//账单标题 'category' => 'now_money',//明细种类 'type' => 'commission',//明细类型 'number' => $num,//明细数字 'balance' => 0,//剩余 'mark' => $mark,//备注 'create_time' => date('Y-m-d H:i:s'),//添加时间 'status' => 1,//0待确定,1有效,-1无效 'commission_type' => $comm,//佣金类型 1代理费 2 消费佣金 3直推奖√ 4辖区佣金\r\n5 养老金√ 6 广告费 //7 跨店奖励√ 8平台奖励 9渠道商\r\n10 推荐创客收益√ 11分红奖金√ 12代理区域收益√ 13消费循环佣金 //14-推荐代理收益√ 15邀请小区团长升级√ 16大v分享奖√ 17创客合伙人√ 18积分奖励√ 19创客补贴√’ 'order_sn' => 0,//订单号 'tripartite' => 0,// 'type_shop' => 0,//0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上 'mer_id' => 0,//商户id 'source' => 0,//1线下 0线上 'order_type' => 1,//1自营 2 第三方 订单类型 'is_red_brokerage' => 0,//1红积分对冲佣金 0正常佣金 'gzc' => 3,//养老金是否已进入公证处log表0:未进入。1:已进入 'take_time' => '',//结算时间 'district_id' => '',// 'street_id' => '',// 'month' => '',//月份 ]; Db::name('user_bill')->insert($bill); } //上周 public function getWeekStartEnd() { $now = time(); // 计算上周日 00:00 的时间戳 $last_sunday_start = strtotime('last Sunday', $now); // 计算这周一 00:00 的时间戳 $this_monday_start = strtotime('this Monday', $last_sunday_start); // 上周日 23:59:59 的时间戳,即这周一 00:00 $last_sunday_end = $this_monday_start - 1; // 上周一 00:00 的时间戳,即上周日 24:00 $last_monday_start = $this_monday_start - 604800; return [$last_monday_start, $last_sunday_end]; } //本周 // public function getWeekStartEnd() // { // $now = time(); // // 计算本周一 00:00 的时间戳 // $monday_start = strtotime('last Monday', $now); // if (date('w', $now) == 1) { // // 如果今天是周一,直接取今天的 00:00 // $monday_start = strtotime('today', $now); // } // // 计算本周日 23:59:59 的时间戳 // $sunday_end = strtotime('next Sunday', $now) + 86399; // return [$monday_start,$sunday_end]; // } public function change_user_log($user_id, $type, $change_status, $money, $routine, $remark = "", $is_admin = 1, $appoint_money = 0) { $transition = [ 'pv' => 1, "gb" => 2, "df" => 3, "vr" => 4, "bt" => 5, "withdraw_quota" => 6, "unsettled_pension" => 7, "amount_old" => 8 ]; $routineInfo = Db::name("old_routine_log_of_key")->where("key", $routine)->find(); if (empty($routineInfo)) { return false; } $key_id = isset($routineInfo['id']) ? $routineInfo['id'] : 0; if ($appoint_money > 0) { $alter_money = $appoint_money; } else { //再去查询变更前的 金额 $alter_money = Db::name("user")->where('uid', $user_id)->value($type); } if ($change_status == 1) { //新增前 $alter_money = $alter_money - $money; } else if ($change_status == 2) { //减少前 $alter_money = $alter_money + $money; } if ($alter_money < 0) { $alter_money = 0; } $inser_data = []; $inser_data['user_id'] = $user_id; $inser_data['type'] = $transition[$type]; $inser_data['change_status'] = $change_status; $inser_data['money'] = $money; $inser_data['routine_log_of_key_id'] = $key_id; $inser_data['remark'] = $remark; $inser_data['is_admin'] = $is_admin; $inser_data['create_time'] = time(); $inser_data['alter_money'] = isset($alter_money) ? $alter_money : 0; Db::name("old_user_log")->insert($inser_data); } /** * 释放待发放佣金 */ public function releaseTobeAmount($user_id, $amount, $output) { $userInfo = Db::name("user")->find($user_id); if (empty($userInfo)) { return false; } $withdraw_quota = empty($userInfo['withdraw_quota_old']) ? 0 : $userInfo['withdraw_quota_old']; $old_amount = empty($userInfo['tobe_amount_old']) ? 0 : $userInfo['tobe_amount_old']; Db::name("user")->where("uid", $user_id)->save(['tobe_amount_old' => 0]); $amount += $old_amount; $inc_amount = 0; $inc_tobe_amount = 0; $dec_withdraw_quota = 0; if (($amount - $withdraw_quota) > 0) { $diff_money = $amount - $withdraw_quota; $inc_amount = $withdraw_quota; $dec_withdraw_quota = $withdraw_quota; $inc_tobe_amount = $diff_money; } elseif (($amount - $withdraw_quota) == 0) { $inc_amount = $withdraw_quota; $dec_withdraw_quota = $withdraw_quota; } else { // <0的情况 $diff_money = $withdraw_quota - $amount; $inc_amount = $amount; $dec_withdraw_quota = $amount; } if ($inc_tobe_amount != 0) { $res = Db::name("user")->where("uid", $user_id)->inc("tobe_amount_old", $inc_tobe_amount)->fetchSql()->inc("amount", $inc_amount)->dec("withdraw_quota_old", $dec_withdraw_quota)->save(); } else { $res = Db::name("user")->where("uid", $user_id)->inc("amount_old", $inc_amount)->fetchSql()->dec("withdraw_quota_old", $dec_withdraw_quota)->save(); } //记录日志 提现额度消费 if ($dec_withdraw_quota > 0) { $this->change_user_log($user_id, 'withdraw_quota', 2, $dec_withdraw_quota, "conversion_tobe_amount", "佣金额度兑换佣金"); } } public function getTime($timePeriod) { // 定义时间区间 $now = time(); $startTime = ''; $endTime = ''; if ($timePeriod == 'last_week') { // 上周的时间区间 $startTime = date('Y-m-d', strtotime('monday last week', $now)); // 上周星期一的开始时间 $endTime = date('Y-m-d', strtotime('sunday last week', $now)); // 上周星期日的结束时间 } elseif ($timePeriod == 'this_week') { // 这周的时间区间 $startTime = date('Y-m-d', strtotime('monday this week', $now)); // 这周星期一的开始时间 $endTime = date('Y-m-d', strtotime('sunday this week', $now)); // 这周星期日的结束时间 } // return ['startTime' => '2025-03-10', 'endTime' => '2025-03-23']; return ['startTime' => $startTime, 'endTime' => $endTime]; } public function getTeamPvByTime($startTime, $endTime, $userIdList = []) { // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单 // 0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上9话费10线下 $pvLogList = Db::name("user_pv_log") ->whereIn('uid', $userIdList) // ->whereIn('order_type', [0]) ->where('status', '=', 1) ->whereBetween("settlement_time", [$startTime, $endTime]) ->whereNotNull("settlement_time") // 排除 pay_time 为 NULL 的订单 ->select() ->toArray(); // 计算总业绩 $totalPv = 0; foreach ($pvLogList as $pvLog) { $totalPv = bcadd($totalPv, $pvLog['pv'], 2); } return $totalPv; } public function getRedEnvelopeByTime($startTime, $endTime, $removeUserIdList = []) { // 查询指定时间区间内的所有订单,并排除 pay_time 为 NULL 的订单 $redEnvelopeList = Db::name("user_sign_hongbao") ->whereNotIn('uid', $removeUserIdList) ->where('status', 1) ->whereBetween("settlement_time", [$startTime, $endTime]) ->select() ->toArray(); return floor(array_sum(array_column($redEnvelopeList, 'number')) * 100) / 100; } }