DataAnalysisChapterService.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2018/11/19
  6. * Time: 11:20
  7. */
  8. namespace App\Modules\Statistic\Services;
  9. use App\Modules\Statistic\Models\DataAnalysisChapter;
  10. use DB;
  11. class DataAnalysisChapterService
  12. {
  13. /**
  14. * 用户是否可以加入到表中的信息
  15. * @param int $type
  16. * @param int $uid
  17. * @return array
  18. */
  19. public static function getAccessInfo(int $uid){
  20. $sql_fromat = "SELECT users.id,users.send_order_id,send_orders.charge_type,c.uid,users.created_at,b.order_count,d.subscribe_time,e.type,e.counts
  21. from users
  22. LEFT JOIN send_orders on users.send_order_id = send_orders.id
  23. LEFT JOIN data_analysis_short_and_long_chapter c on users.id=c.uid
  24. LEFT JOIN (SELECT COUNT(*) as order_count from orders where uid = %s and `status` = 'PAID') b on 1=1
  25. LEFT JOIN (SELECT created_at as subscribe_time from force_subscribe_users WHERE uid =%s LIMIT 1 )d on 1=1
  26. left join (SELECT type,COUNT(*) as counts FROM data_analysis_short_and_long_chapter GROUP BY `type` ORDER BY counts LIMIT 1 ) e on 1=1
  27. WHERE users.id = %s";
  28. $result = DB::select(sprintf($sql_fromat,$uid,$uid,$uid));
  29. if($result && isset($result[0])){
  30. return (array)$result[0];
  31. }
  32. return [];
  33. }
  34. public static function createUser(int $uid,int $type,int $distribution_channel_id){
  35. try{
  36. DataAnalysisChapter::create([
  37. 'uid'=>$uid,
  38. 'type'=>$type,
  39. 'distribution_channel_id'=>$distribution_channel_id
  40. ]);
  41. }catch (\Exception $e){}
  42. }
  43. /**
  44. * 根据用户获取类型
  45. * @param int $uid
  46. * @return int
  47. */
  48. public static function getByUid(int $uid){
  49. $result = DataAnalysisChapter::where('uid',$uid)->select('type')->first();
  50. if($result && $result->type){
  51. return (int)$result->type;
  52. }
  53. return 0;
  54. }
  55. /**
  56. * 根据用户类型获取uid集合
  57. */
  58. public static function getByTypes($types):array {
  59. return DataAnalysisChapter::whereIn('type',$types)->select('uid')->groupBy('uid')->get()->pluck('uid')->all();
  60. }
  61. }