UserDivisionPropertyService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2019/3/22
  6. * Time: 10:53
  7. */
  8. namespace App\Modules\User\Services;
  9. use App\Modules\User\Models\UserDivisionProperty;
  10. use DB;
  11. class UserDivisionPropertyService
  12. {
  13. const PROPERTY='property';
  14. public static function getUserProperty($uid){
  15. if(($result = self::getUserPropertyFromRedis($uid))) return $result;
  16. if(($result = self::getUserPropertyFromTable($uid))) return $result;
  17. return self::getUserPropertyFromStats($uid);
  18. }
  19. public static function getUserPropertyFromRedis($uid){
  20. return ReadRecordService::getByField($uid,self::PROPERTY);
  21. }
  22. public static function getUserPropertyFromTable($uid){
  23. $result = UserDivisionProperty::where('uid',$uid)->where('is_enable',1)->select('property','created_at')->first();
  24. if($result){
  25. return $result->property;
  26. }
  27. return '';
  28. }
  29. public static function getUserPropertyFromStats($uid){
  30. $sql = "SELECT f.uid,f.subscribe_time,f.appid,(o.price) FROM force_subscribe_users f LEFT JOIN orders o on f.uid = o.uid and o.`status` = 'PAID'
  31. and o.created_at>=f.subscribe_time and o.created_at <= DATE_ADD(f.subscribe_time,INTERVAL 3 day) where f.is_subscribed = 1 and f.uid = %s ";
  32. $res = DB::select(sprintf($sql,$uid));
  33. if(!$res){
  34. return '';
  35. }
  36. $data = [];
  37. foreach ($res as $value){
  38. $data[] = ['subscribe_time'=>$value->subscribe_time,'appid'=>$value->appid,'price'=>$value->price];
  39. }
  40. if(!$data) return '';
  41. $collection = collect($data);
  42. $sort = $collection->groupBy('appid')->sortByDesc(function($item, $key){
  43. return $item[0]['subscribe_time'];
  44. });
  45. foreach ($sort as $key=>$v){
  46. if(isset($v[0]) && !empty($v[0])){
  47. $charge_amount = $v[0]['price']?$v[0]['price']:0;
  48. if($charge_amount >=50){
  49. self::createUser($uid,'high');
  50. ReadRecordService::setByField($uid,self::PROPERTY,'high');
  51. return 'high';
  52. }
  53. if(time()-strtotime($v[0]['subscribe_time']) >= 3*86400){
  54. //强关时间大于3天
  55. if($charge_amount >=50){
  56. self::createUser($uid,'high');
  57. ReadRecordService::setByField($uid,self::PROPERTY,'high');
  58. return 'high';
  59. }elseif($charge_amount>2){
  60. self::createUser($uid,'medium');
  61. ReadRecordService::setByField($uid,self::PROPERTY,'medium');
  62. return 'medium';
  63. }elseif ($charge_amount==2){
  64. self::createUser($uid,'low');
  65. ReadRecordService::setByField($uid,self::PROPERTY,'low');
  66. return 'low';
  67. }else{
  68. self::createUser($uid,'none');
  69. ReadRecordService::setByField($uid,self::PROPERTY,'none');
  70. return 'none';
  71. }
  72. }
  73. }
  74. }
  75. return '';
  76. }
  77. public static function createUser($uid,$property){
  78. UserDivisionProperty::where('uid',$uid)->update(['is_enable'=>0]);
  79. UserDivisionProperty::create([
  80. 'uid'=>$uid,
  81. 'property'=>$property,
  82. 'is_enable'=>1
  83. ]);
  84. }
  85. public static function getUserBindPhoneStatus($uid,$page){
  86. $value_format = 'sign_readrecord_%s_%s';
  87. if( !($property = self::getUserProperty($uid)) ){
  88. return '';
  89. }
  90. $result = [
  91. 'sign'=>0,
  92. 'readrecord'=>0,
  93. 'is_bind'=>0,
  94. 'bind_day'=>0
  95. ];
  96. $info = ReadRecordService::getByField($uid,'bind_phone_status');
  97. if($info){
  98. $info = json_decode($info,1);
  99. }else{
  100. $page == 'sign' && $result['sign'] = 1;
  101. $page == 'readrecord' && $result['readrecord'] = 1;
  102. ReadRecordService::setByField($uid,'bind_phone_status',json_encode($result));
  103. }
  104. return $result;
  105. }
  106. }