SendStatisticsList.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // \Log::info('SendStatisticsList data is');
  37. // \Log::info($this->data);
  38. $this->subscribe($this->data['data']['uid']);
  39. }
  40. else{
  41. $this->subscribe_info($this->data['data']);
  42. }
  43. }
  44. private function orderInfo($order_info){
  45. $uid = $order_info['uid'];
  46. \Log::info('orderInfo_start:'.$uid);
  47. $sub_info = ForceSubscribeUsers::where('uid',$uid)->select('official_account_id','appid','created_at','distribution_channel_id')->first();
  48. if(is_null($sub_info)){
  49. \Log::info('sub_info_is_null:'.$uid);
  50. return ;
  51. }
  52. $official = OfficialAccount::where('appid',$sub_info->appid)->select('id','nickname')->first();
  53. $date = date('Y-m-d',strtotime($sub_info->created_at));
  54. $old = $this->getDateRes($date,$sub_info->distribution_channel_id,$official->id);
  55. $pay_end = $order_info['pay_end_at'];
  56. $sub_timestamp = strtotime($sub_info->created_at);
  57. if($pay_end){
  58. $pay_end_timestamp = strtotime($pay_end);
  59. }else{
  60. $pay_end_timestamp = time();
  61. }
  62. $del = $pay_end_timestamp-$sub_timestamp;
  63. if($old){
  64. if($del <= 86400){
  65. $old->recharge_amount_in_one_day = $old->recharge_amount_in_one_day+$order_info['price'];
  66. $old->recharge_amount_in_three_days = $old->recharge_amount_in_three_days+$order_info['price'];
  67. $old->recharge_amount_in_one_month = $old->recharge_amount_in_one_month+$order_info['price'];
  68. $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
  69. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  70. }
  71. if($del > 86400 && $del<= 86400*3){
  72. $old->recharge_amount_in_three_days = $old->recharge_amount_in_three_days+$order_info['price'];
  73. $old->recharge_amount_in_one_month = $old->recharge_amount_in_one_month+$order_info['price'];
  74. $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
  75. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  76. }
  77. if($del > 86400*3 && $del <= 30*86400){
  78. $old->recharge_amount_in_one_month = $old->recharge_amount_in_one_month+$order_info['price'];
  79. $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
  80. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  81. }
  82. if($del > 30*86400 && $del <= 60*86400){
  83. $old->recharge_amount_in_two_months = $old->recharge_amount_in_two_months+$order_info['price'];
  84. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  85. }
  86. if($del> 60*86400 && $del <= 90*86400){
  87. $old->recharge_amount_in_three_months = $old->recharge_amount_in_three_months+$order_info['price'];
  88. }
  89. $old->save();
  90. }else{
  91. $nickname = '未知';
  92. $official_account_id = 0;
  93. if($official && isset($official->nickname)){
  94. $nickname = $official->nickname;
  95. }
  96. if($official && isset($official->id)){
  97. $official_account_id = $official->id;
  98. }
  99. $data = [];
  100. $data['recharge_amount_in_one_day'] = 0;
  101. $data['recharge_amount_in_three_days'] = 0;
  102. $data['recharge_amount_in_one_month'] = 0;
  103. $data['recharge_amount_in_two_months'] = 0;
  104. $data['recharge_amount_in_three_months'] = 0;
  105. $data['distribution_channel_id'] = $sub_info->distribution_channel_id;
  106. $data['official_account_id'] = $official_account_id;
  107. $data['official_account_name'] = $nickname;
  108. $data['date'] = $date;
  109. $data['force_user_num'] = 0;
  110. $data['total_force_user_num'] = 0;
  111. $data['unsubscribe_in_one_day_user_num'] =0;
  112. if($del <= 86400){
  113. $data['recharge_amount_in_one_day'] = $order_info['price'];
  114. $data['recharge_amount_in_three_days'] = $order_info['price'];
  115. $data['recharge_amount_in_one_month'] = $order_info['price'];
  116. $data['recharge_amount_in_two_months'] = $order_info['price'];
  117. $data['recharge_amount_in_three_months'] = $order_info['price'];
  118. }
  119. if($del > 86400 && $del<= 86400*3){
  120. $data['recharge_amount_in_three_days'] = $order_info['price'];
  121. $data['recharge_amount_in_one_month'] = $order_info['price'];
  122. $data['recharge_amount_in_two_months'] = $order_info['price'];
  123. $data['recharge_amount_in_three_months'] = $order_info['price'];
  124. }
  125. if($del > 86400*3 && $del <= 30*86400){
  126. $data['recharge_amount_in_one_month'] = $order_info['price'];
  127. $data['recharge_amount_in_two_months'] = $order_info['price'];
  128. $data['recharge_amount_in_three_months'] = $order_info['price'];
  129. }
  130. if($del > 30*86400 && $del <= 60*86400){
  131. $data['recharge_amount_in_two_months'] = $order_info['price'];
  132. $data['recharge_amount_in_three_months'] = $order_info['price'];
  133. }
  134. if($del> 60*86400 && $del <= 90*86400){
  135. $data['recharge_amount_in_three_months'] = $order_info['price'];
  136. }
  137. SendOrderBreadevenStat::create($data);
  138. }
  139. }
  140. private function subscribe_info($subscribe_info){
  141. $subscribe_info = (array) $subscribe_info;
  142. $date = date('Y-m-d',strtotime($subscribe_info['created_at']));
  143. $official = OfficialAccount::where('appid',$subscribe_info['appid'])->select('id','nickname')->first();
  144. $old = $this->getDateRes($date,$subscribe_info['distribution_channel_id'],$official->id);
  145. if($old){
  146. if( (strtotime($subscribe_info['unsubscribe_time']) - strtotime($subscribe_info['created_at']) ) < 86400){
  147. $old->unsubscribe_in_one_day_user_num +=1;
  148. $old->save();
  149. }
  150. }else{
  151. $data = [];
  152. $nickname = '未知';
  153. $official_account_id = 0;
  154. if($official && isset($official->nickname)){
  155. $nickname = $official->nickname;
  156. }
  157. if($official && isset($official->id)){
  158. $official_account_id = $official->id;
  159. }
  160. $data['recharge_amount_in_one_day'] = 0;
  161. $data['recharge_amount_in_three_days'] = 0;
  162. $data['recharge_amount_in_one_month'] = 0;
  163. $data['recharge_amount_in_two_months'] = 0;
  164. $data['recharge_amount_in_three_months'] = 0;
  165. $data['distribution_channel_id'] = $subscribe_info['distribution_channel_id'];
  166. $data['official_account_id'] = $official_account_id;
  167. $data['official_account_name'] = $nickname;
  168. $data['date'] = $date;
  169. $data['force_user_num'] = 0;
  170. $data['total_force_user_num'] = 0;
  171. if( (strtotime($subscribe_info['unsubscribe_time']) - strtotime($subscribe_info['created_at']) ) < 86400){
  172. $data['unsubscribe_in_one_day_user_num'] =1;
  173. }
  174. SendOrderBreadevenStat::create($data);
  175. }
  176. }
  177. private function subscribe($uid){
  178. if(!is_numeric($uid)) return ;
  179. //UserDivisionCpcPropertyService::afterForceSubscribe($uid);
  180. }
  181. private function visit($openid){
  182. $user = UserDivisionCpcPropertyService::userLevel($openid);
  183. if(!$user){
  184. return ;
  185. }
  186. if($user->type != 'NEW'){
  187. return ;
  188. }
  189. $level = UserDivisionCpcPropertyService::getUserSubscribeAndChargeInfoByOpenid($openid);
  190. if(!$level) return ;
  191. if($level['type'] == $user->type) return ;
  192. UserDivisionCpcPropertyService::update($openid,$level['property'],$level['type']);
  193. }
  194. private function getDateRes($date,$distribution_channel_id,$official_account_id){
  195. return SendOrderBreadevenStat::where('date',$date)
  196. ->where('distribution_channel_id',$distribution_channel_id)
  197. ->where('official_account_id',$official_account_id)
  198. ->first();
  199. }
  200. }