| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- /**
- * 新增统计仓库.
- *
- * @author nj
- * @date 2021-07-23 10:34
- */
- namespace App\Repositories\Statistics;
- use App\Models\Mongo\PayLogsModel;
- use App\Models\Mongo\UsersModel;
- use App\Models\Mysql\AgentAttrsDayModel;
- use App\Models\Mysql\AgentAttrsModel;
- use Carbon\Carbon;
- use Jenssegers\Mongodb\Eloquent\Model;
- use DB;
- class StatisticsNewUserRepository
- {
- /**
- * 计算某一个天的值
- * @param mixed $date 日期.
- * @param array $conditions 查询条件.
- * @return int|mixed
- */
- public function computeOneDay($date, 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);
- $dayStartObj = Carbon::parse($date)->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;
- }
- }
|