SendStatisticsList.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Jobs;
  3. use App\Modules\OfficialAccount\Models\ForceSubscribeUsers;
  4. use App\Modules\OfficialAccount\Models\OfficialAccount;
  5. use App\Modules\Statistic\Models\SendOrderBreadevenStat;
  6. use App\Modules\User\Services\UserDivisionCpcPropertyService;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. class SendStatisticsList extends Job implements ShouldQueue
  11. {
  12. use InteractsWithQueue, SerializesModels;
  13. private $data;
  14. /**
  15. * Create a new job instance.
  16. *
  17. * @return void
  18. */
  19. public function __construct($data)
  20. {
  21. $data['send_time']=date("Y-m-d H:i:s");
  22. $this->data = $data;
  23. }
  24. /**
  25. * Execute the job.
  26. *
  27. * @return void
  28. */
  29. public function handle()
  30. {
  31. if($this->data['type'] == 'order_info'){
  32. $this->orderInfo($this->data['data']);
  33. }elseif($this->data['type'] == 'visit'){
  34. $this->visit($this->data['data']['openid']);
  35. }elseif($this->data['type'] == 'subscribe'){
  36. $this->subscribe($this->data['data']['uid']);
  37. }
  38. else{
  39. $this->subscribe_info($this->data['data']);
  40. }
  41. }
  42. private function orderInfo($order_info){
  43. $uid = $order_info['uid'];
  44. \Log::info('orderInfo_start:'.$uid);
  45. $sub_info = ForceSubscribeUsers::where('uid',$uid)->select('official_account_id','appid','created_at','distribution_channel_id')->first();
  46. if(is_null($sub_info)){
  47. \Log::info('sub_info_is_null:'.$uid);
  48. return ;
  49. }
  50. $official = OfficialAccount::where('appid',$sub_info->appid)->select('id','nickname')->first();
  51. $date = date('Y-m-d',strtotime($sub_info->created_at));
  52. $old = $this->getDateRes($date,$sub_info->distribution_channel_id,$official->id);
  53. $pay_end = $order_info['pay_end_at'];
  54. $sub_timestamp = strtotime($sub_info->created_at);
  55. if($pay_end){
  56. $pay_end_timestamp = strtotime($pay_end);
  57. }else{
  58. $pay_end_timestamp = time();
  59. }
  60. $del = $pay_end_timestamp-$sub_timestamp;
  61. if($old){
  62. if($del <= 86400){
  63. $old->recharge_amount_in_one_day = $old->recharge_amount_in_one_day+$order_info['price'];
  64. $old->recharge_amount_in_three_days = $old->recharge_amount_in_three_days+$order_info['price'];
  65. $old->recharge_amount_in_one_month = $old->recharge_amount_in_one_month+$order_info['price'];
  66. $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
  67. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  68. }
  69. if($del > 86400 && $del<= 86400*3){
  70. $old->recharge_amount_in_three_days = $old->recharge_amount_in_three_days+$order_info['price'];
  71. $old->recharge_amount_in_one_month = $old->recharge_amount_in_one_month+$order_info['price'];
  72. $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
  73. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  74. }
  75. if($del > 86400*3 && $del <= 30*86400){
  76. $old->recharge_amount_in_one_month = $old->recharge_amount_in_one_month+$order_info['price'];
  77. $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
  78. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  79. }
  80. if($del > 30*86400 && $del <= 60*86400){
  81. $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
  82. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  83. }
  84. if($del> 60*86400 && $del <= 90*86400){
  85. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  86. }
  87. $old->save();
  88. }else{
  89. $nickname = '未知';
  90. $official_account_id = 0;
  91. if($official && isset($official->nickname)){
  92. $nickname = $official->nickname;
  93. }
  94. if($official && isset($official->id)){
  95. $official_account_id = $official->id;
  96. }
  97. $data = [];
  98. $data['recharge_amount_in_one_day'] = 0;
  99. $data['recharge_amount_in_three_days'] = 0;
  100. $data['recharge_amount_in_one_month'] = 0;
  101. $data['recharge_amount_in_two_months'] = 0;
  102. $data['recharge_amount_in_three_months'] = 0;
  103. $data['distribution_channel_id'] = $sub_info->distribution_channel_id;
  104. $data['official_account_id'] = $official_account_id;
  105. $data['official_account_name'] = $nickname;
  106. $data['date'] = $date;
  107. $data['force_user_num'] = 0;
  108. $data['total_force_user_num'] = 0;
  109. $data['unsubscribe_in_one_day_user_num'] =0;
  110. if($del <= 86400){
  111. $data['recharge_amount_in_one_day'] = $order_info['price'];
  112. $data['recharge_amount_in_three_days'] = $order_info['price'];
  113. $data['recharge_amount_in_one_month'] = $order_info['price'];
  114. $data['recharge_amount_in_two_months'] = $order_info['price'];
  115. $data['recharge_amount_in_three_months'] = $order_info['price'];
  116. }
  117. if($del > 86400 && $del<= 86400*3){
  118. $data['recharge_amount_in_three_days'] = $order_info['price'];
  119. $data['recharge_amount_in_one_month'] = $order_info['price'];
  120. $data['recharge_amount_in_two_months'] = $order_info['price'];
  121. $data['recharge_amount_in_three_months'] = $order_info['price'];
  122. }
  123. if($del > 86400*3 && $del <= 30*86400){
  124. $data['recharge_amount_in_one_month'] = $order_info['price'];
  125. $data['recharge_amount_in_two_months'] = $order_info['price'];
  126. $data['recharge_amount_in_three_months'] = $order_info['price'];
  127. }
  128. if($del > 30*86400 && $del <= 60*86400){
  129. $data['recharge_amount_in_two_months'] = $order_info['price'];
  130. $data['recharge_amount_in_three_months'] = $order_info['price'];
  131. }
  132. if($del> 60*86400 && $del <= 90*86400){
  133. $data['recharge_amount_in_three_months'] = $order_info['price'];
  134. }
  135. SendOrderBreadevenStat::create($data);
  136. }
  137. }
  138. private function subscribe_info($subscribe_info){
  139. $subscribe_info = (array) $subscribe_info;
  140. $date = date('Y-m-d',strtotime($subscribe_info['created_at']));
  141. $official = OfficialAccount::where('appid',$subscribe_info['appid'])->select('id','nickname')->first();
  142. $old = $this->getDateRes($date,$subscribe_info['distribution_channel_id'],$official->id);
  143. if($old){
  144. if( (strtotime($subscribe_info['unsubscribe_time']) - strtotime($subscribe_info['created_at']) ) < 86400){
  145. $old->unsubscribe_in_one_day_user_num +=1;
  146. $old->save();
  147. }
  148. }else{
  149. $data = [];
  150. $nickname = '未知';
  151. $official_account_id = 0;
  152. if($official && isset($official->nickname)){
  153. $nickname = $official->nickname;
  154. }
  155. if($official && isset($official->id)){
  156. $official_account_id = $official->id;
  157. }
  158. $data['recharge_amount_in_one_day'] = 0;
  159. $data['recharge_amount_in_three_days'] = 0;
  160. $data['recharge_amount_in_one_month'] = 0;
  161. $data['recharge_amount_in_two_months'] = 0;
  162. $data['recharge_amount_in_three_months'] = 0;
  163. $data['distribution_channel_id'] = $subscribe_info['distribution_channel_id'];
  164. $data['official_account_id'] = $official_account_id;
  165. $data['official_account_name'] = $nickname;
  166. $data['date'] = $date;
  167. $data['force_user_num'] = 0;
  168. $data['total_force_user_num'] = 0;
  169. if( (strtotime($subscribe_info['unsubscribe_time']) - strtotime($subscribe_info['created_at']) ) < 86400){
  170. $data['unsubscribe_in_one_day_user_num'] =1;
  171. }
  172. SendOrderBreadevenStat::create($data);
  173. }
  174. }
  175. private function subscribe($uid){
  176. UserDivisionCpcPropertyService::afterForceSubscribe($uid);
  177. }
  178. private function visit($openid){
  179. $user = UserDivisionCpcPropertyService::userLevel($openid);
  180. if(!$user){
  181. return ;
  182. }
  183. if($user->type != 'NEW'){
  184. return ;
  185. }
  186. $level = UserDivisionCpcPropertyService::getUserSubscribeAndChargeInfoByOpenid($openid);
  187. if(!$level) return ;
  188. if($level['type'] == $user->type) return ;
  189. UserDivisionCpcPropertyService::update($openid,$level['property'],$level['type']);
  190. }
  191. private function getDateRes($date,$distribution_channel_id,$official_account_id){
  192. return SendOrderBreadevenStat::where('date',$date)
  193. ->where('distribution_channel_id',$distribution_channel_id)
  194. ->where('official_account_id',$official_account_id)
  195. ->first();
  196. }
  197. }