StatisticsNewUserRepository.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * 新增统计仓库.
  4. *
  5. * @author nj
  6. * @date 2021-07-23 10:34
  7. */
  8. namespace App\Repositories\Statistics;
  9. use App\Models\Mongo\PayLogsModel;
  10. use App\Models\Mongo\UsersModel;
  11. use App\Models\Mysql\AgentAttrsDayModel;
  12. use App\Models\Mysql\AgentAttrsModel;
  13. use Carbon\Carbon;
  14. use Jenssegers\Mongodb\Eloquent\Model;
  15. use DB;
  16. class StatisticsNewUserRepository
  17. {
  18. /**
  19. * 计算某一个天的值
  20. * @param mixed $date 日期.
  21. * @param array $conditions 查询条件.
  22. * @return int|mixed
  23. */
  24. public function computeOneDay($date, array $conditions = [])
  25. {
  26. $channelUid = isset($conditions['channel_uid']) ? $conditions['channel_uid'] : null;
  27. $momAppUid = isset($conditions['mom_app_uid']) ? $conditions['mom_app_uid'] : null;
  28. $agentUid = isset($conditions['agent_uid']) ? $conditions['agent_uid'] : null;
  29. $deviceType = data_get($conditions, 'device_type', '');
  30. $agentUid = data_get($conditions, 'agent_uid', '');
  31. $isDeduction = data_get($conditions, 'is_deduction', null);
  32. $dayStartObj = Carbon::parse($date)->startOfDay();
  33. $dayEndObj = Carbon::parse($date)->endOfDay();
  34. $model = new UsersModel();
  35. if (!is_null($channelUid)) {
  36. $model = $model->where(['channel_uid' => $channelUid]);
  37. }
  38. if (!is_null($momAppUid)) {
  39. $model = $model->where(['mom_app_uid' => $momAppUid]);
  40. }
  41. if (!is_null($agentUid)) {
  42. $model = $model->where(['agent_uid' => $agentUid]);
  43. }
  44. if (!empty($deviceType)) {
  45. $model = $model->where(['device_type' => $deviceType]);
  46. }
  47. if (!is_null($isDeduction)) {
  48. $model = $model->where(['is_deduction' => $isDeduction]);
  49. }
  50. if (!empty($agentUid)) {
  51. $model = $model->where(['agent_uid' => $agentUid]);
  52. }
  53. // 日期查询放最后.
  54. $t1 = $dayStartObj;
  55. $t2 = $dayEndObj;
  56. $model = $model->whereBetween('created_at', [$t1, $t2]);
  57. $result = $model->count();
  58. return $result;
  59. }
  60. /**
  61. * 计算时间段内的值
  62. * @param mixed $startDate 日期.
  63. * @param mixed $endDate 日期.
  64. * @param array $conditions 查询条件.
  65. * @return int|mixed
  66. */
  67. public function computeSomeDays($startDate, $endDate = null, array $conditions = [])
  68. {
  69. $channelUid = isset($conditions['channel_uid']) ? $conditions['channel_uid'] : null;
  70. $momAppUid = isset($conditions['mom_app_uid']) ? $conditions['mom_app_uid'] : null;
  71. $agentUid = isset($conditions['agent_uid']) ? $conditions['agent_uid'] : null;
  72. $deviceType = data_get($conditions, 'device_type', '');
  73. $agentUid = data_get($conditions, 'agent_uid', '');
  74. $isDeduction = data_get($conditions, 'is_deduction', null);
  75. $startDateObj = Carbon::parse($startDate);
  76. $startYmd = $startDateObj->toDateString();
  77. $startDayObj = (clone $startDateObj)->startOfDay();
  78. $endDateObj = null;
  79. $endDayObj = null;
  80. $endYmd = '';
  81. if (!empty($endDate)) {
  82. $endDateObj = Carbon::parse($endDate);
  83. $endYmd = $endDateObj->toDateString();
  84. $endDayObj = (clone $endDateObj)->endOfDay();
  85. if ($startDateObj > $endDateObj) {
  86. // 时间溢出.
  87. return 0;
  88. }
  89. }
  90. $model = new UsersModel();
  91. if (!is_null($channelUid)) {
  92. $model = $model->where(['channel_uid' => $channelUid]);
  93. }
  94. if (!is_null($momAppUid)) {
  95. $model = $model->where(['mom_app_uid' => $momAppUid]);
  96. }
  97. if (!is_null($agentUid)) {
  98. $model = $model->where(['agent_uid' => $agentUid]);
  99. }
  100. if (!empty($deviceType)) {
  101. $model = $model->where(['device_type' => $deviceType]);
  102. }
  103. if (!is_null($isDeduction)) {
  104. $model = $model->where(['is_deduction' => $isDeduction]);
  105. }
  106. if (!empty($agentUid)) {
  107. $model = $model->where(['agent_uid' => $agentUid]);
  108. }
  109. // 日期查询放最后.
  110. if (!empty($endDate)) {
  111. if ($endYmd != $startYmd) {
  112. $model = $model->whereNested(function (Model $query) use ($startDayObj, $endDayObj) {
  113. $t1 = $startDayObj;
  114. $t2 = $endDayObj;
  115. $query->whereBetween('created_at', [$t1, $t2]);
  116. });
  117. } else {
  118. $tmpEndDayObj = (clone $startDayObj)->endOfDay();
  119. $t1 = $startDayObj;
  120. $t2 = $tmpEndDayObj;
  121. $model = $model->whereBetween('created_at', [$t1, $t2]);
  122. }
  123. } else {
  124. $t = $startDayObj;
  125. $model = $model->where('created_at', '>=', $t);
  126. }
  127. $result = $model->count();
  128. return $result;
  129. }
  130. /**
  131. * 根据根用户订单增量更新代理属性一次性(不把下级的累加进来)
  132. * @param $logId
  133. * @return false
  134. */
  135. public function updateAgentAttrsOnceByRootOrder($logId)
  136. {
  137. $logObj = UsersModel::where(['_id' => $logId])->first();
  138. if (empty($logObj->_id) || empty($logObj->agent_uid)) {
  139. return false;
  140. }
  141. $agentUid = $logObj->agent_uid;
  142. if (empty($agentUid)) {
  143. // 没有渠道,不叠加属性.
  144. return false;
  145. }
  146. $result = false;
  147. DB::beginTransaction();
  148. try {
  149. // 加悲观锁,因为2个仓库可能同时更新表 , 临时取消悲观锁.
  150. $agent = AgentAttrsModel::where(['agent_uid' => $agentUid])->firstOrFail();
  151. $todayDate = Carbon::now()->toDateString();
  152. $logDateYmd = Carbon::parse($logObj->created_at)->toDateString();
  153. $isNewToday = false;
  154. if ((empty($agent->td_date) || $agent->td_date != $todayDate) && $todayDate == $logDateYmd) {
  155. $isNewToday = true;
  156. $agent->td_date = $todayDate;
  157. }
  158. $agent = self::updateCommonAttrsFields($agent, $logObj, false);
  159. if ($agent->td_date == $todayDate && $todayDate == $logDateYmd) {
  160. // 更新td字段开头的.
  161. $agent = self::updateCommonAttrsFields($agent, $logObj, true, $isNewToday);
  162. }
  163. $result = $agent->save();
  164. if ($result) {
  165. // 对day表进行处理.
  166. $dayAttrsObj = AgentAttrsDayModel::whereDate('td_date', $logDateYmd)->where(['agent_uid' => $agentUid])->first();
  167. if (empty($dayAttrsObj)) {
  168. $setarr = [
  169. 'agent_uid' => $agentUid,
  170. 'td_date' => $logDateYmd,
  171. 'is_used' => 0,
  172. ];
  173. $dayAttrsObj = AgentAttrsDayModel::create($setarr);
  174. }
  175. $dayAttrsObj = self::updateCommonAttrsFields($dayAttrsObj, $logObj, true, false);
  176. // $fields = $agent->getCommonUsersSubffixFields();
  177. // foreach ($fields as $field) {
  178. // $tdFieldName = 'td_'. $field;
  179. // $dayAttrsObj->{$tdFieldName} = $agent->{$tdFieldName}; // 重新给day表赋值
  180. // }
  181. $dayAttrsObj->save();
  182. }
  183. DB::commit();
  184. } catch (\Exception $e) {
  185. DB::rollBack();
  186. logger('[error][StatisticsNewUserRepository][updateAgentAttrsOnceByRootOrder]', ['msg' => $e->getMessage(), 'log_id' => $logId, 'agent_uid' => $agentUid, 'detail' => $e->getTrace()]);
  187. }
  188. return $result;
  189. }
  190. /**
  191. * 更新订单公共字段.
  192. * @param AgentAttrsModel $agent
  193. * @param UsersModel $logObj
  194. * @param false $isForToday
  195. * @param false $isNewToday
  196. * @return mixed
  197. */
  198. private function updateCommonAttrsFields($agent, $logObj, $isForToday = false, $isNewToday = false)
  199. {
  200. $fields = $agent->getCommonUsersSubffixFields();
  201. foreach ($fields as $field) {
  202. ${$field} = $isForToday ? 'td_' . $field : $field;
  203. if ($isNewToday) {
  204. // 重制今日字段数据属性.
  205. $agent->{${$field}} = 0;
  206. }
  207. }
  208. $agent->{$users_total} = $agent->{$users_total} + 1;
  209. if ($logObj->device_type == 'A') {
  210. $agent->{$users_android_total} = $agent->{$users_android_total} + 1;
  211. } elseif ($logObj->device_type == 'I') {
  212. $agent->{$users_ios_total} = $agent->{$users_ios_total} + 1;
  213. } elseif ($logObj->device_type == 'H') {
  214. $agent->{$users_h5_total} = $agent->{$users_h5_total} + 1;
  215. }
  216. if ($logObj->is_deduction == 1) {
  217. // 此没被扣量,就记录.
  218. $agent->{$users_deduction} = $agent->{$users_deduction} + 1;
  219. if ($logObj->device_type == 'A') {
  220. $agent->{$users_android_deduction} = $agent->{$users_android_deduction} + 1;
  221. } elseif ($logObj->device_type == 'I') {
  222. $agent->{$users_ios_deduction} = $agent->{$users_ios_deduction} + 1;
  223. } elseif ($logObj->device_type == 'H') {
  224. $agent->{$users_h5_deduction} = $agent->{$users_h5_deduction} + 1;
  225. }
  226. }
  227. return $agent;
  228. }
  229. }