| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- <?php
- /**
- * 统计用户属性相关.
- *
- * @author nj
- * @date 2021-08-15 16:25
- */
- namespace App\Repositories\Statistics;
- use App\Constants\PayConstant;
- use App\Models\Mongo\AgentAttrsLogsModel;
- use App\Models\Mongo\AgentAttrsTodayModel;
- use App\Models\Mongo\AttrsIncLogsModel;
- use App\Repositories\AgentRepository;
- use App\Repositories\ChannelRepository;
- use Carbon\Carbon;
- use Illuminate\Support\Str;
- use DB;
- class StatisticsAgentAttrsRepository
- {
- /**
- * 定义支付仓库.
- * @var null|StatisticsPayRepository
- */
- protected $payRepo = null;
- /**
- * 定义新用户仓库
- * @var null|StatisticsNewUserRepository
- */
- protected $userRepo = null;
- /**
- * 余额变化日志仓库
- * @var null|BalanceLogsRepository
- */
- protected $balanceLogsRepo = null;
- /**
- * 日活仓库.
- * @var null|StatisticsActiveRepository
- */
- protected $activeRepo = null;
- /**
- * 代理仓库.
- * @var null|AgentRepository
- */
- protected $agentRepo = null;
- /**
- * 渠道仓库.
- * @var null|ChannelRepository
- */
- protected $channelRepo = null;
- /**
- * 从数据源根计算
- * @param $date
- * @param string $channelUid
- * @return false
- */
- public function rootJob($date, $channelUid = '')
- {
- $originChannelUid = $channelUid; // 保存入参原始渠道UID.
- $date = Carbon::parse($date)->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;
- }
- }
|