| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- /**
- * 描述.
- *
- * @author nj
- * @date 2021-08-02 12:21
- */
- namespace App\Jobs\Statistics;
- use App\Constants\PayConstant;
- use App\Models\Mysql\StatisticsPayModel;
- use App\Repositories\Statistics\StatisticsPayRepository;
- 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 PayChannelJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- /**
- * 定义仓库.
- * @var null|StatisticsPayRepository
- */
- 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 (empty($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();
- $orderTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid]);
- $orderSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'pay_status' => PayConstant::SUCCESS]);
- $orderFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'pay_status' => PayConstant::FAIL]);
- $orderMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid]);
- $orderSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'pay_status' => PayConstant::SUCCESS]);
- $androidTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'A']);
- $androidSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]);
- $androidFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'pay_status' => PayConstant::FAIL]);
- $androidMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'A']);
- $androidSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]);
- $iosTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'I']);
- $iosSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]);
- $iosFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'pay_status' => PayConstant::FAIL]);
- $iosMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'I']);
- $iosSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]);
- $h5Total = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'H']);
- $h5Success = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]);
- $h5Failed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'pay_status' => PayConstant::FAIL]);
- $h5Money = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'H']);
- $h5SuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]);
- $deductionTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0]);
- $deductionSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]);
- $deductionFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::FAIL]);
- $deductionMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0]);
- $deductionSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]);
- $item = [
- 'date' => $date,
- 'order_total' => $orderTotal,
- 'order_success' => $orderSuccess,
- 'order_failed' => $orderFailed,
- 'order_money' => $orderMoney,
- 'order_success_money' => $orderSuccessMoney,
- 'android_total' => $androidTotal,
- 'android_success' => $androidSuccess,
- 'android_failed' => $androidFailed,
- 'android_money' => $androidMoney,
- 'android_success_money' => $androidSuccessMoney,
- 'ios_total' => $iosTotal,
- 'ios_success' => $iosSuccess,
- 'ios_failed' => $iosFailed,
- 'ios_money' => $iosMoney,
- 'ios_success_money' => $iosSuccessMoney,
- 'h5_total' => $h5Total,
- 'h5_success' => $h5Success,
- 'h5_failed' => $h5Failed,
- 'h5_money' => $h5Money,
- 'h5_success_money' => $h5SuccessMoney,
- 'deduction_total' => $deductionTotal,
- 'deduction_success' => $deductionSuccess,
- 'deduction_failed' => $deductionFailed,
- 'deduction_money' => $deductionMoney,
- 'deduction_success_money' => $deductionSuccessMoney,
- ];
- $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 = StatisticsPayModel::whereDate('date', '=', $date)
- ->where('channel_uid', '=', $sqlChannelUid)
- ->first();
- } else {
- $info = StatisticsPayModel::whereDate('date', '=', $date)
- ->whereNull('channel_uid')
- ->where('mom_app_uid', '=', '')
- ->first();
- }
- if (!empty($info)) {
- $info->order_total = $orderTotal;
- $info->order_success = $orderSuccess;
- $info->order_failed = $orderFailed;
- $info->order_money = $orderMoney;
- $info->order_success_money = $orderSuccessMoney;
- $info->android_total = $androidTotal;
- $info->android_success = $androidSuccess;
- $info->android_failed = $androidFailed;
- $info->android_money = $androidMoney;
- $info->android_success_money = $androidSuccessMoney;
- $info->ios_total = $iosTotal;
- $info->ios_success = $iosSuccess;
- $info->ios_failed = $iosFailed;
- $info->ios_money = $iosMoney;
- $info->ios_success_money = $iosSuccessMoney;
- $info->h5_total = $h5Total;
- $info->h5_success = $h5Success;
- $info->h5_failed = $h5Failed;
- $info->h5_money = $h5Money;
- $info->h5_success_money = $h5SuccessMoney;
- $info->deduction_total = $deductionTotal;
- $info->deduction_success = $deductionSuccess;
- $info->deduction_failed = $deductionFailed;
- $info->deduction_money = $deductionMoney;
- $info->deduction_success_money = $deductionSuccessMoney;
- $result = $info->save();
- } else {
- $result = StatisticsPayModel::create($item);
- }
- // Todo.
- self::notify();
- return boolval($result);
- }
- /**
- * 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
- */
- private function notify()
- {
- // Todo 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
- }
- /**
- * 获取仓库.
- * @return StatisticsPayRepository
- */
- private function getRepo()
- {
- if (!$this->repo instanceof StatisticsPayRepository) {
- $this->repo = new StatisticsPayRepository();
- }
- return $this->repo;
- }
- }
|