UserDivisionCpcPropertyService.php 8.2 KB

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