toDateString(); $dateIsoObj = Carbon::parse($date)->startOfDay(); $channelObj = null; // 预定定义渠道对象. // 当前渠道所属代理. $currAgent = null; $currAgentUid = ''; $conditionChannelUid = ''; // 定义条件查询渠道UID if (!empty($channelUid)) { $channelObj = $this->getChannelRepo()->makeModel()->query()->where(['uid' => $channelUid])->first(); if (!empty($channelObj->id)) { $currAgent = $this->getAgentRepo()->makeModel()->query()->where(['uid' => $channelObj->agent_uid])->first(); if (empty($currAgent->id)) { // 没有代理不执行 return false; } $currAgentUid = $channelObj->agent_uid; $conditionChannelUid = $channelUid; } else { // 没有这个渠道不统计 return false; } } // 日活 $dauTotal = $this->getActiveRepo()->countOneDayActive($date, $conditionChannelUid); $dauAndroid = $this->getActiveRepo()->countOneDayActive($date, $conditionChannelUid, 'A'); $dauIos = $this->getActiveRepo()->countOneDayActive($date, $conditionChannelUid, 'I'); $dauH5 = $this->getActiveRepo()->countOneDayActive($date, $conditionChannelUid, 'H'); // 用户 $userTotal = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid]); $userAndroid = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A']); $userIos = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I']); $userH5 = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H']); // 用户单独扣量的 $userDeductionTotal = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 1]); $userDeductionAndroid = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A', 'is_deduction' => 1]); $userDeductionIos = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I', 'is_deduction' => 1]); $userDeductionH5 = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H', 'is_deduction' => 1]); // 订单支付 $orderTotal = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid]); $orderSuccess = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'pay_status' => PayConstant::SUCCESS]); $orderFailed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'pay_status' => PayConstant::FAIL]); $orderMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid]); $orderSuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'pay_status' => PayConstant::SUCCESS]); $androidTotal = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A']); $androidSuccess = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]); $androidFailed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A', 'pay_status' => PayConstant::FAIL]); $androidMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A']); $androidSuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]); $iosTotal = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I']); $iosSuccess = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]); $iosFailed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I', 'pay_status' => PayConstant::FAIL]); $iosMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I']); $iosSuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]); $h5Total = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H']); $h5Success = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]); $h5Failed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H', 'pay_status' => PayConstant::FAIL]); $h5Money = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H']); $h5SuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]); $deductionTotal = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0]); $deductionSuccess = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]); $deductionFailed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::FAIL]); $deductionMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0]); $deductionSuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]); // 当日累计. $incItem = $baseInfo = [ 'date' => $dateIsoObj, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now(), 'agent_uid' => $currAgentUid, // 代理UID,空字符串,代表没有. 'order_total' => $orderTotal, // 累计订单数 'order_success' => $orderSuccess, // 累计成功订单数. 'order_failed' => $orderFailed, // 累计失败订单数 'order_money' => $orderMoney, // 关累计订单金额 'order_success_money' => $orderSuccessMoney, // 累计成功金额 'dau_total' => $dauTotal, // 累计日活 'dau_android' => $dauAndroid, // 累计Android日活 'dau_ios' => $dauIos, // 累计iOS日活 'dau_h5' => $dauH5, // 累计h5日活 'android_total' => $androidTotal, // 累计android订单数 'android_success' => $androidSuccess, // 累计android成功订单数 'android_failed' => $androidFailed, // 累计android失败订单数 'android_money' => $androidMoney, // 累计android总金额 'android_success_money' => $androidSuccessMoney, // 累计android成功订单金额. 'ios_total' => $iosTotal, // 累计ios订单数 'ios_success' => $iosSuccess, // 累计ios成功订单数 'ios_failed' => $iosFailed, // 累计ios失败订单数 'ios_money' => $iosMoney, // 累计ios总金额 'ios_success_money' => $iosSuccessMoney, // 累计ios成功订单金额 'h5_total' => $iosTotal, // 累计h5订单数 'h5_success' => $iosSuccess, // 累计h5成功订单数 'h5_failed' => $iosFailed, // 累计h5失败订单数 'h5_money' => $iosMoney, // 累计h5总金额 'h5_success_money' => $iosSuccessMoney, // 累计h5成功订单金额 'deduction_total' => $deductionTotal, //累计渠道扣量后的总订单数 'deduction_success' => $deductionSuccess, // 计渠道扣量后的成功订单数 'deduction_failed' => $deductionFailed, // 累计渠道扣量后的失败单数 'deduction_money' => $deductionMoney, // 累计渠道扣量后的总金额 'deduction_success_money' => $deductionSuccessMoney, 'commission_money' => 0, // 累计代理提成金额 'withdraw_money' => isset($currUserAttrs->withdraw_money) ? floatval(number_format($currUserAttrs->withdraw_money, 2)) : 0, // Todo 累计提现金额, 依赖xiaomum 'users_total' => $userTotal, // 累计用户数量 'users_android_total' => $userAndroid, // 累计android用户数量 'users_ios_total' => $userIos, // 累计ios用户数量 'users_h5_total' => $userH5, // 累计h5用户数量 'users_deduction' => $userDeductionTotal, // 累计扣量用户数量 'users_android_deduction' => $userDeductionAndroid, // 累计android扣量用户数量 'users_ios_deduction' => $userDeductionIos, // 累计ios扣量用户数量 'users_h5_deduction' => $userDeductionH5, // 累计h5扣量用户数量 'commission_ratio' => isset($currAgent->commission_ratio) ? intval($currAgent->commission_ratio) : 0, // 本人提成比例, 没代理就是0 'commission_diff_ratio' => isset($currAgent->commission_diff_ratio) ? intval($currAgent->commission_diff_ratio) : 0, // 没代理就是0 ]; // 处理当前用户. if (!empty($channelUid)) { // 非顶级. $incItem['commission_money'] = floatval(number_format($baseInfo['deduction_success_money'] * ($currAgent->commission_ratio / 100), 2)); // 当前渠道的当前用户用commission_ratio计算 } if (empty($channelUid)) { $incItem['commission_ratio'] = $incItem['commission_diff_ratio'] = 100; // 官方,提成100%. $incItem['commission_money'] = $baseInfo['deduction_success_money'] * 1; // 官方,提成100%. } $startOfDate = Carbon::parse($date)->startOfDay(); $recordObj = AttrsIncLogsModel::where(['date' => $startOfDate, 'root_agent_uid' => $currAgentUid])->first(); if (empty($recordObj)) { // 没有就先写进去, 风险:如果这个任务中途失败,就再也无法继续冲头跑今天的这个root user. $recordData = [ 'created_at' => Carbon::now(), 'date' => $startOfDate, 'root_agent_uid' => $currAgentUid, ]; AttrsIncLogsModel::create($recordData); } else { // 有记录了,不能跑了,防止重复叠加. return false; } $info = AgentAttrsLogsModel::where('date', '=', $startOfDate)->where('agent_uid', '=', $currAgentUid)->first(); if (empty($info)) { $result = AgentAttrsLogsModel::create($incItem); } else { // 有当前的日志,则追加下级的增量. foreach ($incItem as $k => $v) { if (!in_array($k, self::getIncExceptFields()) && !Str::startsWith($k, 'td_')) { $info->{$k} += $v; } $info->updated_at = Carbon::now(); } $result = $info->save(); } // 递归累加 self::loopParent($date, $baseInfo, $currAgent); } private function loopParent($date, $baseInfo, $currAgent = null) { $startOfDateObj = Carbon::parse($date)->startOfDay(); $dateUtcObj = $startOfDateObj; if (!empty($currAgent->parent_uid)) { $parentAgent = $this->getAgentRepo()->makeModel()->query()->where(['uid' => $currAgent->parent_uid])->first(); if (!empty($parentAgent->id)) { $incItem = $baseInfo; $info = AgentAttrsLogsModel::where('date', '=', $dateUtcObj)->where('agent_uid', '=', $currAgent->parent_uid)->first(); $parentDiffRatio = !empty($currAgent->commission_diff_ratio) ? $currAgent->commission_diff_ratio : 0; $incItem['agent_uid'] = $parentAgent->uid; $incItem['order_total'] = $baseInfo['order_total']; $incItem['order_success'] = $baseInfo['order_success']; $incItem['order_failed'] = $baseInfo['order_failed']; $incItem['order_money'] = $baseInfo['order_money']; $incItem['order_success_money'] = $baseInfo['order_success_money']; $incItem['dau_total'] = $baseInfo['dau_total']; $incItem['dau_android'] = $baseInfo['dau_android']; $incItem['dau_ios'] = $baseInfo['dau_ios']; $incItem['dau_h5'] = $baseInfo['dau_h5']; $incItem['android_total'] = $baseInfo['android_total']; $incItem['android_failed'] = $baseInfo['android_failed']; $incItem['android_money'] = $baseInfo['android_money']; $incItem['android_success_money'] = $baseInfo['android_success_money']; $incItem['ios_total'] = $baseInfo['ios_total']; $incItem['ios_success'] = $baseInfo['ios_success']; $incItem['ios_failed'] = $baseInfo['ios_failed']; $incItem['ios_money'] = $baseInfo['ios_money']; $incItem['ios_success_money'] = $baseInfo['ios_success_money']; $incItem['h5_total'] = $baseInfo['h5_total']; $incItem['h5_success'] = $baseInfo['h5_success']; $incItem['h5_failed'] = $baseInfo['h5_failed']; $incItem['h5_money'] = $baseInfo['h5_money']; $incItem['h5_success_money'] = $baseInfo['h5_success_money']; $incItem['deduction_total'] = $baseInfo['deduction_total']; $incItem['deduction_success'] = $baseInfo['ios_money']; $incItem['deduction_failed'] = $baseInfo['deduction_failed']; $incItem['deduction_money'] = $baseInfo['deduction_money']; $incItem['deduction_success_money'] = $baseInfo['deduction_success_money']; $incItem['commission_money'] = floatval(number_format($baseInfo['deduction_success_money'] * ($parentDiffRatio / 100), 2)); // 提成差 $incItem['withdraw_money'] = isset($parentAgentAttrs->withdraw_money) ? floatval(number_format($parentAgentAttrs->withdraw_money, 2)) : 0; $incItem['users_total'] = $baseInfo['users_total']; $incItem['users_android_total'] = $baseInfo['users_android_total']; $incItem['users_ios_total'] = $baseInfo['users_ios_total']; $incItem['users_h5_total'] = $baseInfo['users_h5_total']; $incItem['users_deduction'] = $baseInfo['users_deduction']; $incItem['users_android_deduction'] = $baseInfo['users_android_deduction']; $incItem['users_ios_deduction'] = $baseInfo['users_ios_deduction']; $incItem['users_h5_deduction'] = $baseInfo['users_h5_deduction']; $incItem['commission_ratio'] = isset($parentAgentAttrs->commission_ratio) ? intval($parentAgentAttrs->commission_ratio) : 0; $incItem['commission_diff_ratio'] = isset($parentAgentAttrs->commission_diff_ratio) ? intval($parentAgentAttrs->commission_diff_ratio) : 0; if (empty($info)) { $result = AgentAttrsLogsModel::create($incItem); } else { // 有当前的日志,则追加下级的增量. foreach ($incItem as $k => $v) { if (!in_array($k, self::getIncExceptFields()) && !Str::startsWith($k, 'td_')) { $info->{$k} += $v; } $info->updated_at = Carbon::now(); } $result = $info->save(); } // 递归累加 self::loopParent($date, $baseInfo, $parentAgent); } } } /** * 今天单次任务. * @param $date * @param string $channelUid * @return false */ public function todayJob($date, $channelUid = '') { $originChannelUid = $channelUid; // 保存入参原始渠道UID. $conditionChannelUid = $originChannelUid; // 统计条件参数. $date = Carbon::parse($date)->toDateString(); $dateIsoObj = Carbon::parse($date)->startOfDay(); $channelObj = null; // 预定定义渠道对象. // 当前渠道所属代理. $currAgent = null; $currAgentUid = ''; if (!empty($channelUid)) { $channelObj = $this->getChannelRepo()->makeModel()->query()->where(['uid' => $channelUid])->first(); if (empty($channelObj)) { // 数据库没有这个渠道,则不执行计算. return false; } } if (!empty($channelObj->id)) { $conditionChannelUid = $channelUid; $currAgent = $this->getAgentRepo()->makeModel()->query()->where(['uid' => $channelObj->agent_uid])->first(); if (empty($currAgent->id)) { // 没有代理,就不执行. return false; } $currAgentUid = $currAgent->uid; } // 日活 $dauTotal = $this->getActiveRepo()->countOneDayActive($date, $conditionChannelUid); $dauAndroid = $this->getActiveRepo()->countOneDayActive($date, $conditionChannelUid, 'A'); $dauIos = $this->getActiveRepo()->countOneDayActive($date, $conditionChannelUid, 'I'); $dauH5 = $this->getActiveRepo()->countOneDayActive($date, $conditionChannelUid, 'H'); // 用户 $userTotal = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid]); $userAndroid = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A']); $userIos = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I']); $userH5 = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H']); // 用户单独扣量的 $userDeductionTotal = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 1]); $userDeductionAndroid = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A', 'is_deduction' => 1]); $userDeductionIos = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I', 'is_deduction' => 1]); $userDeductionH5 = $this->getUserRepo()->computeOneDay($date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H', 'is_deduction' => 1]); // 订单支付 $orderTotal = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid]); $orderSuccess = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'pay_status' => PayConstant::SUCCESS]); $orderFailed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'pay_status' => PayConstant::FAIL]); $orderMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid]); $orderSuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'pay_status' => PayConstant::SUCCESS]); $androidTotal = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A']); $androidSuccess = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]); $androidFailed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A', 'pay_status' => PayConstant::FAIL]); $androidMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A']); $androidSuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]); $iosTotal = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I']); $iosSuccess = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]); $iosFailed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I', 'pay_status' => PayConstant::FAIL]); $iosMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I']); $iosSuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]); $h5Total = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H']); $h5Success = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]); $h5Failed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H', 'pay_status' => PayConstant::FAIL]); $h5Money = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H']); $h5SuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]); $deductionTotal = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0]); $deductionSuccess = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]); $deductionFailed = $this->getPayRepo()->computeOneDay('count', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::FAIL]); $deductionMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0]); $deductionSuccessMoney = $this->getPayRepo()->computeOneDay('money', $date, ['channel_uid' => $conditionChannelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]); // 当日累计. $incItem = $baseInfo = [ 'date' => $dateIsoObj, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now(), 'agent_uid' => $currAgentUid, // 代理用户uid,非自增, 如果是空代表总后台 'td_order_total' => $orderTotal, // 累计订单数 'td_order_success' => $orderSuccess, // 累计成功订单数. 'td_order_failed' => $orderFailed, // 累计失败订单数 'td_order_money' => $orderMoney, // 关累计订单金额 'td_order_success_money' => $orderSuccessMoney, // 累计成功金额 'td_dau_total' => $dauTotal, // 累计日活 'td_dau_android' => $dauAndroid, // 累计Android日活 'td_dau_ios' => $dauIos, // 累计iOS日活 'td_dau_h5' => $dauH5, // 累计H5日活 'td_android_total' => $androidTotal, // 累计android订单数 'td_android_success' => $androidSuccess, // 累计android成功订单数 'td_android_failed' => $androidFailed, // 累计android失败订单数 'td_android_money' => $androidMoney, // 累计android总金额 'td_android_success_money' => $androidSuccessMoney, // 累计android成功订单金额. 'td_ios_total' => $iosTotal, // 累计ios订单数 'td_ios_success' => $iosSuccess, // 累计ios成功订单数 'td_ios_failed' => $iosFailed, // 累计ios失败订单数 'td_ios_money' => $iosMoney, // 累计ios总金额 'td_ios_success_money' => $iosSuccessMoney, // 累计ios成功订单金额 'td_h5_total' => $h5Total, // 累计h5订单数 'td_h5_success' => $h5Success, // 累计h5成功订单数 'td_h5_failed' => $h5Failed, // 累计h5失败订单数 'td_h5_money' => $h5Money, // 累计h5总金额 'td_h5_success_money' => $h5SuccessMoney, // 累计ios成功订单金额 'td_deduction_total' => $deductionTotal, //累计渠道扣量后的总订单数 'td_deduction_success' => $deductionSuccess, // 计渠道扣量后的成功订单数 'td_deduction_failed' => $deductionFailed, // 累计渠道扣量后的失败单数 'td_deduction_money' => $deductionMoney, // 累计渠道扣量后的总金额 'td_deduction_success_money' => $deductionSuccessMoney, 'td_commission_money' => 0, // 累计代理提成金额 'td_users_total' => $userTotal, // 累计用户数量 'td_users_android_total' => $userAndroid, // 累计android用户数量 'td_users_ios_total' => $userIos, // 累计ios用户数量 'td_users_h5_total' => $userH5, // 累计h5用户数量 'td_users_deduction' => $userDeductionTotal, // 累计扣量用户数量 'td_users_android_deduction' => $userDeductionAndroid, // 累计扣量android用户数量 'td_users_ios_deduction' => $userDeductionIos, // 累计扣量ios用户数量 'td_users_h5_deduction' => $userDeductionH5, // 累计扣量h5用户数量 'commission_ratio' => isset($currAgent->commission_ratio) ? intval($currAgent->commission_ratio) : 0, 'commission_diff_ratio' => isset($currAgent->commission_diff_ratio) ? intval($currAgent->commission_diff_ratio) : 0, ]; // 处理当前用户. if (!empty($currAgentUid)) { // 非顶级. $incItem['td_commission_money'] = floatval(number_format($baseInfo['td_deduction_success_money'] * ($currAgent->commission_ratio / 100), 2)); // 当前渠道的当前用户用commission_ratio计算 } if (empty($channelUid)) { $incItem['commission_ratio'] = $incItem['commission_diff_ratio'] = 100; // 官方,提成100%. $incItem['td_commission_money'] = $baseInfo['td_deduction_success_money'] * 1; // 官方,提成100%. } $startOfDate = Carbon::parse($date)->startOfDay(); $info = AgentAttrsTodayModel::where('date', '=', $startOfDate)->where('agent_uid', '=', $currAgentUid)->first(); if (empty($info)) { AgentAttrsTodayModel::create($incItem); } else { // 有当前的日志,则追加下级的增量. foreach ($incItem as $k => $v) { if (!in_array($k, self::getIncExceptFields())) { $info->{$k} += $v; } $info->updated_at = Carbon::now(); } $info->save(); } // 递归上级. self::todayLoopParent($date, $baseInfo, $currAgent); } /** * 循环上级(今天的) * @param $date * @param $baseInfo * @param null $currAgent */ private function todayLoopParent($date, $baseInfo, $currAgent = null) { $startOfDateObj = Carbon::parse($date)->startOfDay(); $dateUtcObj = $startOfDateObj; if (!empty($currAgent->parent_uid)) { $parentAgent = $this->getAgentRepo()->makeModel()->query()->where(['uid' => $currAgent->parent_uid])->first(); if (!empty($parentAgent->id)) { $incItem = $baseInfo; $info = AgentAttrsTodayModel::where('date', '=', $dateUtcObj)->where('agent_uid', '=', $currAgent->parent_uid)->first(); $parentDiffRatio = !empty($currAgent->commission_diff_ratio) ? $currAgent->commission_diff_ratio : 0; $incItem['agent_uid'] = $parentAgent->uid; $incItem['td_order_total'] = $baseInfo['td_order_total']; $incItem['td_order_success'] = $baseInfo['td_order_success']; $incItem['td_order_failed'] = $baseInfo['td_order_failed']; $incItem['td_order_money'] = $baseInfo['td_order_money']; $incItem['td_order_success_money'] = $baseInfo['td_order_success_money']; $incItem['td_dau_total'] = $baseInfo['td_dau_total']; $incItem['td_dau_android'] = $baseInfo['td_dau_android']; $incItem['td_dau_ios'] = $baseInfo['td_dau_ios']; $incItem['td_dau_h5'] = $baseInfo['td_dau_h5']; $incItem['td_android_total'] = $baseInfo['td_android_total']; $incItem['td_android_failed'] = $baseInfo['td_android_failed']; $incItem['td_android_money'] = $baseInfo['td_android_money']; $incItem['td_android_success_money'] = $baseInfo['td_android_success_money']; $incItem['td_ios_total'] = $baseInfo['td_ios_total']; $incItem['td_ios_success'] = $baseInfo['td_ios_success']; $incItem['td_ios_failed'] = $baseInfo['td_ios_failed']; $incItem['td_ios_money'] = $baseInfo['td_ios_money']; $incItem['td_ios_success_money'] = $baseInfo['td_ios_success_money']; $incItem['td_h5_total'] = $baseInfo['td_h5_total']; $incItem['td_h5_success'] = $baseInfo['td_h5_success']; $incItem['td_h5_failed'] = $baseInfo['td_h5_failed']; $incItem['td_h5_money'] = $baseInfo['td_h5_money']; $incItem['td_h5_success_money'] = $baseInfo['td_h5_success_money']; $incItem['td_deduction_total'] = $baseInfo['td_deduction_total']; $incItem['td_deduction_success'] = $baseInfo['td_ios_money']; $incItem['td_deduction_failed'] = $baseInfo['td_deduction_failed']; $incItem['td_deduction_money'] = $baseInfo['td_deduction_money']; $incItem['td_deduction_success_money'] = $baseInfo['td_deduction_success_money']; $incItem['td_commission_money'] = floatval(number_format($baseInfo['td_deduction_success_money'] * ($parentDiffRatio / 100), 2)); // 提成差 $incItem['td_users_total'] = $baseInfo['td_users_total']; $incItem['td_users_android_total'] = $baseInfo['td_users_android_total']; $incItem['td_users_ios_total'] = $baseInfo['td_users_ios_total']; $incItem['td_users_h5_total'] = $baseInfo['td_users_h5_total']; $incItem['td_users_deduction'] = $baseInfo['td_users_deduction']; $incItem['td_users_android_deduction'] = $baseInfo['td_users_android_deduction']; $incItem['td_users_ios_deduction'] = $baseInfo['td_users_ios_deduction']; $incItem['td_users_h5_deduction'] = $baseInfo['td_users_h5_deduction']; $incItem['commission_ratio'] = isset($parentAgent->commission_ratio) ? intval($parentAgent->commission_ratio) : 0; $incItem['commission_diff_ratio'] = isset($parentAgent->commission_diff_ratio) ? intval($parentAgent->commission_diff_ratio) : 0; if (empty($info)) { AgentAttrsTodayModel::create($incItem); } else { // 有当前的日志,则追加下级的增量. foreach ($incItem as $k => $v) { if (!in_array($k, self::getIncExceptFields())) { $info->{$k} += $v; } $info->updated_at = Carbon::now(); } $info->save(); } // 继续无限递归 self::todayLoopParent($date, $baseInfo, $parentAgent); } } } /** * 初始化今天的数据(清空). * @param $date * @return bool|int|null * @throws \Exception */ public function initTodayMongo($date) { $startOfDateObj = Carbon::parse($date)->startOfDay(); return AgentAttrsTodayModel::where('date', $startOfDateObj)->delete(); } /** * 获取仓库. * @return StatisticsPayRepository */ private function getPayRepo() { if (!$this->payRepo instanceof StatisticsPayRepository) { $this->payRepo = new StatisticsPayRepository(); } return $this->payRepo; } /** * 获取新用户仓库. * @return StatisticsNewUserRepository */ private function getUserRepo() { if (!$this->userRepo instanceof StatisticsNewUserRepository) { $this->userRepo = new StatisticsNewUserRepository(); } return $this->userRepo; } /** * 获取排除的字段. * @return string[] */ private function getIncExceptFields() { $fields = ['date', 'created_at', 'updated_at', 'uid', 'agent_uid', 'commission_ratio', 'commission_diff_ratio', 'withdraw_money']; return $fields; } /** * 获取余额变化日志仓库. * @return BalanceLogsRepository */ private function getBalanceLogsRepo() { if (!$this->balanceLogsRepo instanceof BalanceLogsRepository) { $this->balanceLogsRepo = new BalanceLogsRepository(); } return $this->balanceLogsRepo; } /** * 获取日活仓库. * @return StatisticsActiveRepository */ private function getActiveRepo() { if (!$this->activeRepo instanceof StatisticsActiveRepository) { $this->activeRepo = new StatisticsActiveRepository(); } return $this->activeRepo; } /** * 获取代理仓库. * @return AgentRepository|\Illuminate\Contracts\Foundation\Application|mixed|null */ private function getAgentRepo() { if (!$this->agentRepo instanceof AgentRepository) { $this->agentRepo = app(AgentRepository::class); } return $this->agentRepo; } /** * 获取渠道仓库. * @return ChannelRepository|\Illuminate\Contracts\Foundation\Application|mixed|null */ private function getChannelRepo() { if (!$this->channelRepo instanceof ChannelRepository) { $this->channelRepo = app(ChannelRepository::class); } return $this->channelRepo; } }