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; } }