UserDivisionCpcPropertyService.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2019/4/26
  6. * Time: 10:14
  7. */
  8. namespace App\Modules\User\Services;
  9. use App\Modules\User\Models\UserDivisionCpcProperty;
  10. use DB;
  11. class UserDivisionCpcPropertyService
  12. {
  13. public static function userLevel($openid){
  14. return UserDivisionCpcProperty::where('openid',$openid)->where('is_enable',1)->select('property','earliest_subscribe_time')->first();
  15. }
  16. public static function getUserSubscribeAndChargeInfoByUid($uid){
  17. $sql_format = "SELECT u.id,f.created_at as subscribe_time,u.openid,
  18. (SELECT ifnull(sum(price),0) from orders where uid = u.id and `status` = 'PAID' LIMIT 1)as amount,
  19. (SELECT ifnull(sum(price),0) from orders where uid=u.id and `status` = 'PAID' and created_at <= DATE_ADD(f.created_at,INTERVAL 3 day)) as three_day_amount
  20. FROM users u
  21. JOIN force_subscribe_users f on u.id = f.uid
  22. WHERE u.openid in (SELECT openid from users WHERE id = %s)";
  23. $result = DB::select(sprintf($sql_format,$uid));
  24. if($result){
  25. return self::level($result);
  26. }
  27. return [];
  28. }
  29. public static function getUserSubscribeAndChargeInfoByOpenid($openid){
  30. $sql_format = "SELECT u.id,f.created_at as subscribe_time,u.openid,
  31. (SELECT ifnull(sum(price),0) from orders where uid = u.id and `status` = 'PAID' LIMIT 1)as amount,
  32. (SELECT ifnull(sum(price),0) from orders where uid=u.id and `status` = 'PAID' and created_at <= DATE_ADD(f.created_at,INTERVAL 3 day)) as three_day_amount
  33. FROM users u
  34. JOIN force_subscribe_users f on u.id = f.uid
  35. WHERE u.openid ='%s'";
  36. $result = DB::select(sprintf($sql_format,$openid));
  37. if($result){
  38. return self::level($result);
  39. }
  40. return [];
  41. }
  42. private static function level($res){
  43. $earliest_subscribe_time = $res[0]->subscribe_time;
  44. $subscribe_three_day_info = [];
  45. $subscribe_no_three_day_info = [];
  46. foreach ($res as $v){
  47. (strtotime($v->subscribe_time) < strtotime($earliest_subscribe_time)) && $earliest_subscribe_time = $v->subscribe_time;
  48. if(time()-strtotime($v->subscribe_time) >= 86400*3){
  49. array_push($subscribe_three_day_info,$v->amount);
  50. }else{
  51. array_push($subscribe_no_three_day_info,$v->three_day_amount);
  52. }
  53. }
  54. $result = [
  55. 'earliest_subscribe_time'=>$earliest_subscribe_time,
  56. 'property'=>'',
  57. 'type'=>'',
  58. 'openid'=> $res[0]->openid
  59. ];
  60. if($subscribe_three_day_info){
  61. //存量用户
  62. $result['type'] = 'CUILIANG';
  63. $amount = round(array_sum($subscribe_three_day_info)/count($subscribe_three_day_info),2);
  64. if($amount>15){
  65. $result['property'] = 'high';
  66. }elseif($amount >2){
  67. $result['property'] = 'medium';
  68. } elseif($amount >0){
  69. $result['property'] = 'low';
  70. } else{
  71. $result['property'] = 'none';
  72. }
  73. }else{
  74. //新用户
  75. $result['type'] = 'NEW';
  76. if($subscribe_no_three_day_info)
  77. $amount = max($subscribe_no_three_day_info);
  78. else
  79. $amount = 0;
  80. if($amount>=50){
  81. $result['property'] = 'high';
  82. }elseif($amount >2){
  83. $result['property'] = 'medium';
  84. } elseif($amount >0){
  85. $result['property'] = 'low';
  86. } else{
  87. $result['property'] = 'none';
  88. }
  89. }
  90. return $result;
  91. }
  92. public static function update($openid,$property='',$type=''){
  93. if(empty($property) && empty($type)) return ;
  94. $update_info = [];
  95. $property && $update_info['property'] = $property;
  96. $type && $update_info['type'] = $type;
  97. UserDivisionCpcProperty::where('openid',$openid)->where('is_enable',1)->update($update_info);
  98. }
  99. public static function createorUpdate($data){
  100. $old = UserDivisionCpcProperty::where('openid',$data['openid'])->where('is_enable',1)->first();
  101. if(!$old){
  102. UserDivisionCpcProperty::create([
  103. 'openid'=>$data['openid'] ,
  104. 'property'=>$data['property'] ,
  105. 'is_enable'=>1 ,
  106. 'type'=>$data['type'] ,
  107. 'earliest_subscribe_time'=>$data['earliest_subscribe_time']
  108. ]);
  109. }else{
  110. $old->property = $data['property'];
  111. $old->type = $data['type'];
  112. $old->save();
  113. }
  114. }
  115. public static function afterForceSubscribe($uid){
  116. $sql_format = "SELECT u.id,f.created_at as subscribe_time,u.openid,
  117. (SELECT ifnull(sum(price),0) from orders where uid = u.id and `status` = 'PAID' LIMIT 1)as amount,
  118. (SELECT ifnull(sum(price),0) from orders where uid=u.id and `status` = 'PAID' and created_at <= DATE_ADD(f.created_at,INTERVAL 3 day)) as three_day_amount
  119. FROM users u
  120. JOIN force_subscribe_users f on u.id = f.uid
  121. WHERE u.openid in (SELECT openid from users WHERE id = %s)";
  122. $res = DB::select(sprintf($sql_format,$uid));
  123. if(!$res) return ;
  124. //判断用户属性
  125. $earliest_subscribe_time = $res[0]->subscribe_time;
  126. $subscribe_total_info = [];
  127. $subscribe_no_three_day_info = [];
  128. foreach ($res as $v){
  129. (strtotime($v->subscribe_time) < strtotime($earliest_subscribe_time)) && $earliest_subscribe_time = $v->subscribe_time;
  130. if(time()-strtotime($v->subscribe_time) >= 86400*3){
  131. array_push($subscribe_total_info,$v->amount);
  132. }else{
  133. array_push($subscribe_no_three_day_info,$v->three_day_amount);
  134. }
  135. }
  136. $result = [
  137. 'earliest_subscribe_time'=>$earliest_subscribe_time,
  138. 'property'=>'',
  139. 'type'=>'',
  140. 'openid'=> $res[0]->openid
  141. ];
  142. if($subscribe_total_info){
  143. //存量用户
  144. $result['type'] = 'CUILIANG';
  145. $amount = round(array_sum($subscribe_total_info)/count($subscribe_total_info),2);
  146. if($amount>15){
  147. $result['property'] = 'high';
  148. }elseif($amount >2){
  149. $result['property'] = 'medium';
  150. } elseif($amount >0){
  151. $result['property'] = 'low';
  152. } else{
  153. $result['property'] = 'none';
  154. }
  155. }else{
  156. //新用户
  157. $result['type'] = 'NEW';
  158. if($subscribe_no_three_day_info)
  159. $amount = max($subscribe_no_three_day_info);
  160. else
  161. $amount = 0;
  162. if($amount>50){
  163. $result['property'] = 'high';
  164. }elseif($amount >2){
  165. $result['property'] = 'medium';
  166. } elseif($amount >0){
  167. $result['property'] = 'low';
  168. } else{
  169. $result['property'] = 'none';
  170. }
  171. }
  172. //保存或者创建用户属性
  173. $old = DB::table('user_division_cpc_property')->where('openid',$result['openid'])->where('is_enable',1)->first();
  174. if(!$old){
  175. UserDivisionCpcProperty::create([
  176. 'openid'=>$result['openid'] ,
  177. 'property'=>$result['property'] ,
  178. 'is_enable'=>1 ,
  179. 'type'=>$result['type'] ,
  180. 'earliest_subscribe_time'=>$result['earliest_subscribe_time'],
  181. 'updated_at'=>date('Y-m-d H:i:s'),
  182. 'created_at'=>date('Y-m-d H:i:s')
  183. ]);
  184. }else{
  185. DB::table('user_division_cpc_property')->where('openid',$result['openid'])->where('is_enable',1)->update(
  186. [
  187. 'type'=>$result['type'] ,
  188. 'property'=>$result['property'] ,
  189. 'updated_at'=>date('Y-m-d H:i:s')
  190. ]
  191. );
  192. }
  193. }
  194. }