startOfDay(); $dayEndObj = Carbon::parse($date)->endOfDay(); $model = new UsersModel(); if (!is_null($channelUid)) { $model = $model->where(['channel_uid' => $channelUid]); } if (!is_null($momAppUid)) { $model = $model->where(['mom_app_uid' => $momAppUid]); } if (!is_null($agentUid)) { $model = $model->where(['agent_uid' => $agentUid]); } if (!empty($deviceType)) { $model = $model->where(['device_type' => $deviceType]); } if (!is_null($isDeduction)) { $model = $model->where(['is_deduction' => $isDeduction]); } if (!empty($agentUid)) { $model = $model->where(['agent_uid' => $agentUid]); } // 日期查询放最后. $t1 = $dayStartObj; $t2 = $dayEndObj; $model = $model->whereBetween('created_at', [$t1, $t2]); $result = $model->count(); return $result; } /** * 计算时间段内的值 * @param mixed $startDate 日期. * @param mixed $endDate 日期. * @param array $conditions 查询条件. * @return int|mixed */ public function computeSomeDays($startDate, $endDate = null, array $conditions = []) { $channelUid = isset($conditions['channel_uid']) ? $conditions['channel_uid'] : null; $momAppUid = isset($conditions['mom_app_uid']) ? $conditions['mom_app_uid'] : null; $agentUid = isset($conditions['agent_uid']) ? $conditions['agent_uid'] : null; $deviceType = data_get($conditions, 'device_type', ''); $agentUid = data_get($conditions, 'agent_uid', ''); $isDeduction = data_get($conditions, 'is_deduction', null); $startDateObj = Carbon::parse($startDate); $startYmd = $startDateObj->toDateString(); $startDayObj = (clone $startDateObj)->startOfDay(); $endDateObj = null; $endDayObj = null; $endYmd = ''; if (!empty($endDate)) { $endDateObj = Carbon::parse($endDate); $endYmd = $endDateObj->toDateString(); $endDayObj = (clone $endDateObj)->endOfDay(); if ($startDateObj > $endDateObj) { // 时间溢出. return 0; } } $model = new UsersModel(); if (!is_null($channelUid)) { $model = $model->where(['channel_uid' => $channelUid]); } if (!is_null($momAppUid)) { $model = $model->where(['mom_app_uid' => $momAppUid]); } if (!is_null($agentUid)) { $model = $model->where(['agent_uid' => $agentUid]); } if (!empty($deviceType)) { $model = $model->where(['device_type' => $deviceType]); } if (!is_null($isDeduction)) { $model = $model->where(['is_deduction' => $isDeduction]); } if (!empty($agentUid)) { $model = $model->where(['agent_uid' => $agentUid]); } // 日期查询放最后. if (!empty($endDate)) { if ($endYmd != $startYmd) { $model = $model->whereNested(function (Model $query) use ($startDayObj, $endDayObj) { $t1 = $startDayObj; $t2 = $endDayObj; $query->whereBetween('created_at', [$t1, $t2]); }); } else { $tmpEndDayObj = (clone $startDayObj)->endOfDay(); $t1 = $startDayObj; $t2 = $tmpEndDayObj; $model = $model->whereBetween('created_at', [$t1, $t2]); } } else { $t = $startDayObj; $model = $model->where('created_at', '>=', $t); } $result = $model->count(); return $result; } /** * 根据根用户订单增量更新代理属性一次性(不把下级的累加进来) * @param $logId * @return false */ public function updateAgentAttrsOnceByRootOrder($logId) { $logObj = UsersModel::where(['_id' => $logId])->first(); if (empty($logObj->_id) || empty($logObj->agent_uid)) { return false; } $agentUid = $logObj->agent_uid; if (empty($agentUid)) { // 没有渠道,不叠加属性. return false; } $result = false; DB::beginTransaction(); try { // 加悲观锁,因为2个仓库可能同时更新表 , 临时取消悲观锁. $agent = AgentAttrsModel::where(['agent_uid' => $agentUid])->firstOrFail(); $todayDate = Carbon::now()->toDateString(); $logDateYmd = Carbon::parse($logObj->created_at)->toDateString(); $isNewToday = false; if ((empty($agent->td_date) || $agent->td_date != $todayDate) && $todayDate == $logDateYmd) { $isNewToday = true; $agent->td_date = $todayDate; } $agent = self::updateCommonAttrsFields($agent, $logObj, false); if ($agent->td_date == $todayDate && $todayDate == $logDateYmd) { // 更新td字段开头的. $agent = self::updateCommonAttrsFields($agent, $logObj, true, $isNewToday); } $result = $agent->save(); if ($result) { // 对day表进行处理. $dayAttrsObj = AgentAttrsDayModel::whereDate('td_date', $logDateYmd)->where(['agent_uid' => $agentUid])->first(); if (empty($dayAttrsObj)) { $setarr = [ 'agent_uid' => $agentUid, 'td_date' => $logDateYmd, 'is_used' => 0, ]; $dayAttrsObj = AgentAttrsDayModel::create($setarr); } $dayAttrsObj = self::updateCommonAttrsFields($dayAttrsObj, $logObj, true, false); // $fields = $agent->getCommonUsersSubffixFields(); // foreach ($fields as $field) { // $tdFieldName = 'td_'. $field; // $dayAttrsObj->{$tdFieldName} = $agent->{$tdFieldName}; // 重新给day表赋值 // } $dayAttrsObj->save(); } DB::commit(); } catch (\Exception $e) { DB::rollBack(); logger('[error][StatisticsNewUserRepository][updateAgentAttrsOnceByRootOrder]', ['msg' => $e->getMessage(), 'log_id' => $logId, 'agent_uid' => $agentUid, 'detail' => $e->getTrace()]); } return $result; } /** * 更新订单公共字段. * @param AgentAttrsModel $agent * @param UsersModel $logObj * @param false $isForToday * @param false $isNewToday * @return mixed */ private function updateCommonAttrsFields($agent, $logObj, $isForToday = false, $isNewToday = false) { $fields = $agent->getCommonUsersSubffixFields(); foreach ($fields as $field) { ${$field} = $isForToday ? 'td_' . $field : $field; if ($isNewToday) { // 重制今日字段数据属性. $agent->{${$field}} = 0; } } $agent->{$users_total} = $agent->{$users_total} + 1; if ($logObj->device_type == 'A') { $agent->{$users_android_total} = $agent->{$users_android_total} + 1; } elseif ($logObj->device_type == 'I') { $agent->{$users_ios_total} = $agent->{$users_ios_total} + 1; } elseif ($logObj->device_type == 'H') { $agent->{$users_h5_total} = $agent->{$users_h5_total} + 1; } if ($logObj->is_deduction == 1) { // 此没被扣量,就记录. $agent->{$users_deduction} = $agent->{$users_deduction} + 1; if ($logObj->device_type == 'A') { $agent->{$users_android_deduction} = $agent->{$users_android_deduction} + 1; } elseif ($logObj->device_type == 'I') { $agent->{$users_ios_deduction} = $agent->{$users_ios_deduction} + 1; } elseif ($logObj->device_type == 'H') { $agent->{$users_h5_deduction} = $agent->{$users_h5_deduction} + 1; } } return $agent; } }