PayChannelJob.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * 描述.
  4. *
  5. * @author nj
  6. * @date 2021-08-02 12:21
  7. */
  8. namespace App\Jobs\Statistics;
  9. use App\Constants\PayConstant;
  10. use App\Models\Mysql\StatisticsPayModel;
  11. use App\Repositories\Statistics\StatisticsPayRepository;
  12. use Carbon\Carbon;
  13. use Illuminate\Bus\Queueable;
  14. use Illuminate\Contracts\Queue\ShouldQueue;
  15. use Illuminate\Foundation\Bus\Dispatchable;
  16. use Illuminate\Queue\InteractsWithQueue;
  17. use Illuminate\Queue\SerializesModels;
  18. use App\Models\Mysql\ChannelModel;
  19. class PayChannelJob implements ShouldQueue
  20. {
  21. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. /**
  23. * 定义仓库.
  24. * @var null|StatisticsPayRepository
  25. */
  26. protected $repo = null;
  27. /**
  28. * 日期.
  29. * @var string
  30. */
  31. protected $date = '';
  32. protected $channelUid = null;
  33. /**
  34. * Create a new job instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct(string $date, $channelUid = null)
  39. {
  40. $this->date = $date;
  41. $this->channelUid = $channelUid;
  42. }
  43. /**
  44. * Execute the job.
  45. *
  46. * @return void
  47. */
  48. public function handle()
  49. {
  50. $date = Carbon::parse($this->date)->toDateString();
  51. if (empty($this->channelUid)) {
  52. // 统计全部.
  53. $result = self::runJob($date);
  54. } else {
  55. // 统计渠道或者官方
  56. $result = self::runJob($date, $this->channelUid);
  57. }
  58. }
  59. /**
  60. * 执行统计.
  61. * @param string $date 日期
  62. * @param null $channelUid 渠道UID
  63. * @return bool
  64. */
  65. protected function runJob($date, $channelUid = null)
  66. {
  67. $date = Carbon::parse($date)->toDateString();
  68. $orderTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid]);
  69. $orderSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'pay_status' => PayConstant::SUCCESS]);
  70. $orderFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'pay_status' => PayConstant::FAIL]);
  71. $orderMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid]);
  72. $orderSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'pay_status' => PayConstant::SUCCESS]);
  73. $androidTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'A']);
  74. $androidSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]);
  75. $androidFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'pay_status' => PayConstant::FAIL]);
  76. $androidMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'A']);
  77. $androidSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'A', 'pay_status' => PayConstant::SUCCESS]);
  78. $iosTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'I']);
  79. $iosSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]);
  80. $iosFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'pay_status' => PayConstant::FAIL]);
  81. $iosMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'I']);
  82. $iosSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'I', 'pay_status' => PayConstant::SUCCESS]);
  83. $h5Total = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'H']);
  84. $h5Success = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]);
  85. $h5Failed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'pay_status' => PayConstant::FAIL]);
  86. $h5Money = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'H']);
  87. $h5SuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'device_type' => 'H', 'pay_status' => PayConstant::SUCCESS]);
  88. $deductionTotal = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0]);
  89. $deductionSuccess = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]);
  90. $deductionFailed = $this->getRepo()->computeOneDay('count', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::FAIL]);
  91. $deductionMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0]);
  92. $deductionSuccessMoney = $this->getRepo()->computeOneDay('money', $date, ['channel_uid' => $channelUid, 'is_deduction' => 0, 'pay_status' => PayConstant::SUCCESS]);
  93. $item = [
  94. 'date' => $date,
  95. 'order_total' => $orderTotal,
  96. 'order_success' => $orderSuccess,
  97. 'order_failed' => $orderFailed,
  98. 'order_money' => $orderMoney,
  99. 'order_success_money' => $orderSuccessMoney,
  100. 'android_total' => $androidTotal,
  101. 'android_success' => $androidSuccess,
  102. 'android_failed' => $androidFailed,
  103. 'android_money' => $androidMoney,
  104. 'android_success_money' => $androidSuccessMoney,
  105. 'ios_total' => $iosTotal,
  106. 'ios_success' => $iosSuccess,
  107. 'ios_failed' => $iosFailed,
  108. 'ios_money' => $iosMoney,
  109. 'ios_success_money' => $iosSuccessMoney,
  110. 'h5_total' => $h5Total,
  111. 'h5_success' => $h5Success,
  112. 'h5_failed' => $h5Failed,
  113. 'h5_money' => $h5Money,
  114. 'h5_success_money' => $h5SuccessMoney,
  115. 'deduction_total' => $deductionTotal,
  116. 'deduction_success' => $deductionSuccess,
  117. 'deduction_failed' => $deductionFailed,
  118. 'deduction_money' => $deductionMoney,
  119. 'deduction_success_money' => $deductionSuccessMoney,
  120. ];
  121. $sqlChannelUid = '';
  122. $item['mom_app_uid'] = ''; // 预先设置母包UID
  123. $item['agent_uid'] = ''; // 预先定义代理UID
  124. if (!is_null($channelUid)) {
  125. $item['channel_uid'] = $channelUid;
  126. $sqlChannelUid = $channelUid;
  127. if (!empty($item['channel_uid'])) {
  128. $channelObj = ChannelModel::where(['uid' => $item['channel_uid']])->first();
  129. if (!empty($channelObj)) {
  130. // 补充母包UID
  131. $item['mom_app_uid'] = $channelObj->mom_app_uid;
  132. $item['agent_uid'] = $channelObj->agent_uid;
  133. }
  134. }
  135. $info = StatisticsPayModel::whereDate('date', '=', $date)
  136. ->where('channel_uid', '=', $sqlChannelUid)
  137. ->first();
  138. } else {
  139. $info = StatisticsPayModel::whereDate('date', '=', $date)
  140. ->whereNull('channel_uid')
  141. ->where('mom_app_uid', '=', '')
  142. ->first();
  143. }
  144. if (!empty($info)) {
  145. $info->order_total = $orderTotal;
  146. $info->order_success = $orderSuccess;
  147. $info->order_failed = $orderFailed;
  148. $info->order_money = $orderMoney;
  149. $info->order_success_money = $orderSuccessMoney;
  150. $info->android_total = $androidTotal;
  151. $info->android_success = $androidSuccess;
  152. $info->android_failed = $androidFailed;
  153. $info->android_money = $androidMoney;
  154. $info->android_success_money = $androidSuccessMoney;
  155. $info->ios_total = $iosTotal;
  156. $info->ios_success = $iosSuccess;
  157. $info->ios_failed = $iosFailed;
  158. $info->ios_money = $iosMoney;
  159. $info->ios_success_money = $iosSuccessMoney;
  160. $info->h5_total = $h5Total;
  161. $info->h5_success = $h5Success;
  162. $info->h5_failed = $h5Failed;
  163. $info->h5_money = $h5Money;
  164. $info->h5_success_money = $h5SuccessMoney;
  165. $info->deduction_total = $deductionTotal;
  166. $info->deduction_success = $deductionSuccess;
  167. $info->deduction_failed = $deductionFailed;
  168. $info->deduction_money = $deductionMoney;
  169. $info->deduction_success_money = $deductionSuccessMoney;
  170. $result = $info->save();
  171. } else {
  172. $result = StatisticsPayModel::create($item);
  173. }
  174. // Todo.
  175. self::notify();
  176. return boolval($result);
  177. }
  178. /**
  179. * 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
  180. */
  181. private function notify()
  182. {
  183. // Todo 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
  184. }
  185. /**
  186. * 获取仓库.
  187. * @return StatisticsPayRepository
  188. */
  189. private function getRepo()
  190. {
  191. if (!$this->repo instanceof StatisticsPayRepository) {
  192. $this->repo = new StatisticsPayRepository();
  193. }
  194. return $this->repo;
  195. }
  196. }