| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- /**
- * 描述.
- *
- * @author nj
- * @date 2021-08-02 12:21
- */
- namespace App\Jobs\Statistics;
- use App\Models\Mysql\StatisticsActiveModel;
- use App\Models\Mysql\StatisticsNewUsersModel;
- use App\Repositories\Statistics\StatisticsNewUserRepository;
- use Carbon\Carbon;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use App\Models\Mysql\ChannelModel;
- class NewUsersChannelJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- /**
- * 定义仓库.
- * @var null|StatisticsNewUserRepository
- */
- protected $repo = null;
- /**
- * 日期.
- * @var string
- */
- protected $date = '';
- protected $channelUid = null;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct(string $date, $channelUid = null)
- {
- $this->date = $date;
- $this->channelUid = $channelUid;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- $date = Carbon::parse($this->date)->toDateString();
- if (is_null($this->channelUid)) {
- // 统计全部.
- $result = self::runJob($date);
- } else {
- // 统计渠道或者官方
- $result = self::runJob($date, $this->channelUid);
- }
- }
- /**
- * 执行统计.
- * @param string $date 日期
- * @param null $channelUid 渠道UID
- * @return bool
- */
- protected function runJob($date, $channelUid = null)
- {
- $date = Carbon::parse($date)->toDateString();
- $total = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid]);
- $android = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'A']);
- $ios = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'I']);
- $h5 = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'H']);
- // 单独扣量的
- $deductionTotal = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'is_deduction' => 0]);
- $deductionAndroid = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'is_deduction' => 0]);
- $deductionIos = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'is_deduction' => 0]);
- $deductionH5 = $this->getRepo()->computeOneDay($date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'is_deduction' => 0]);
- $item = [
- 'date' => $date,
- 'total' => $total,
- 'android' => $android,
- 'ios' => $ios,
- 'h5' => $h5,
- 'deduction_total' => $deductionTotal,
- 'deduction_android' => $deductionAndroid,
- 'deduction_ios' => $deductionIos,
- 'deduction_h5' => $deductionH5,
- ];
- $sqlChannelUid = '';
- $item['mom_app_uid'] = ''; // 预先设置母包UID
- $item['agent_uid'] = ''; // 预先定义代理UID
- if (!is_null($channelUid)) {
- $item['channel_uid'] = $channelUid;
- $sqlChannelUid = $channelUid;
- if (!empty($item['channel_uid'])) {
- $channelObj = ChannelModel::where(['uid' => $item['channel_uid']])->first();
- if (!empty($channelObj)) {
- // 补充母包UID
- $item['mom_app_uid'] = $channelObj->mom_app_uid;
- $item['agent_uid'] = $channelObj->agent_uid;
- }
- }
- $info = StatisticsNewUsersModel::whereDate('date', '=', $date)
- ->where('channel_uid', '=', $sqlChannelUid)
- ->first();
- } else {
- $info = StatisticsNewUsersModel::whereDate('date', '=', $date)
- ->whereNull('channel_uid')
- ->where('mom_app_uid', '=', '')
- ->first();
- }
- if (!empty($info)) {
- $info->total = $total;
- $info->android = $android;
- $info->ios = $ios;
- $info->h5 = $h5;
- $info->deduction_total = $deductionTotal;
- $info->deduction_android = $deductionAndroid;
- $info->deduction_ios = $deductionIos;
- $info->deduction_h5 = $deductionH5;
- $result = $info->save();
- } else {
- $result = StatisticsNewUsersModel::create($item);
- }
- // Todo.
- self::notify();
- return boolval($result);
- }
- /**
- * 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
- */
- private function notify()
- {
- // Todo 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
- }
- /**
- * 获取仓库.
- * @return StatisticsNewUserRepository
- */
- private function getRepo()
- {
- if (!$this->repo instanceof StatisticsNewUserRepository) {
- $this->repo = new StatisticsNewUserRepository();
- }
- return $this->repo;
- }
- }
|