123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?php
- /**
- * Created by PhpStorm.
- * User: hp
- * Date: 2017/11/21
- * Time: 10:42
- */
- namespace App\Modules\User\Services;
- use App\Modules\User\Models\User;
- use DB;
- use GuzzleHttp\Client;
- use GuzzleHttp\Promise;
- use App\Modules\Order\Models\Order;
- class UserService
- {
- /**
- * 创建用户
- * $params
- * 必穿参数 openid、unionid、distribution_channel_id
- * 可传 province、city、country、headimgurl、send_order_id、sex、register_ip
- */
- static function addUser($params)
- {
- return User::addUser($params);
- }
- static function getUserList($limit, $offset)
- {
- return User::getUserList($limit, $offset);
- }
- static function getUserByNickAndChannelId($distribution_channel_id, $nickname)
- {
- return User::getUserByNickAndChannelId($distribution_channel_id, $nickname);
- }
- /**
- * 获取用户信息列表
- * @param $pageSize 每页显示的条数
- */
- static function getPaginationList($pageSize = 20)
- {
- return User::getPaginationList($pageSize);
- }
- /**
- * 获取用户信息列表
- * @param $pageSize 每页显示的条数
- */
- static function getUsers($pageSize = 20, $careStatus)
- {
- return User::getPaginationList($pageSize, $careStatus);
- }
- /**
- * 根据id获取用户信息
- * @return user object
- */
- static function getById($id)
- {
- return User::getById($id);
- }
- /**
- * 根据id获取用户信息
- * @return user object
- */
- static function getUserDataById($id)
- {
- return User::getUserDataById($id);
- }
- /**
- * 根据id获取用户信息
- * @return user object
- */
- static function getUserDataDetailById($id)
- {
- return User::getUserDataDetailById($id);
- }
- /**
- * 根据id获取用户名称
- * @return string [用户昵称]
- */
- static function getNicknameById($id)
- {
- return User::getById($id)->nickname;
- }
- /**
- * 更新用户信息
- * @param int $id 用户ID
- * @param array $params 可传 province、city、country、headimgurl、sex
- * @return boolen
- */
- static function updateInfo($id, $params)
- {
- \Log::info('~~~~~~~~~~~~~~~~~====update User====~~~~~~~~~~~~~~~~' . "\n");
- \Log::info($id);
- \Log::info($params);
- return User::updateInfo($id, $params);
- }
- /**
- * 根据union和分销id获取用户信息
- * @param $union
- * @param $channel_id
- * @return mixed
- */
- static function getUserByUnionAndChannelId($openid, $channel_id)
- {
- return User::where('openid', $openid)->where('distribution_channel_id', $channel_id)->select('id', 'openid', 'unionid')->first();
- }
- /**
- * 用户余额充值
- * @param $uid
- * @param $fee
- * @return mixed
- */
- static function addBalance($uid, $fee, $charge, $given)
- {
- return User::addBalance($uid, $fee, $charge, $given);
- }
- /**
- * 查询推广注册用户总数
- * @param $send_order_id
- * @return mixed
- */
- static function getPromotionTotal($send_order_id)
- {
- return User::getPromotionTotal($send_order_id);
- }
- /**
- * 查询渠道某天注册用户总数
- * @param $channel_id
- * @param $date
- * @return mixed
- */
- static function getChannelDayTotal($channel_id, $date)
- {
- return User::getChannelDayTotal($channel_id, $date);
- }
- /**
- * 查询渠道注册用户总数
- * @param $channel_id
- * @return mixed
- */
- static function getChannelTotal($channel_id)
- {
- return User::getChannelTotal($channel_id);
- }
- /**
- * 查询渠道某段时间注册用户总数
- * @param $channel_id
- * @param $startDate
- * @param $endDate
- * @return mixed
- */
- static function getChannelDayToDayTotal($channel_id, $startDate = '', $endDate = '')
- {
- return User::getChannelDayToDayTotal($channel_id, $startDate, $endDate);
- }
- /**
- * 查询渠道某段时间注册用户总数
- * @param array $channelIds
- * @param $startDate
- * @param $endDate
- * @return mixed
- */
- static function getChannelsDayToDayTotal($channelIds, $startDate = '', $endDate = '')
- {
- return User::getChannelsDayToDayTotal($channelIds, $startDate, $endDate);
- }
- /**
- * 查询注册用户总数
- * @param $params
- */
- static function getTotalCount($params)
- {
- return User::getTotalCount($params);
- }
- /**
- * 查询渠道是否有登录
- * @param $channelId
- * @param $start_time
- * @param $end_time
- * @return int
- */
- static function judgeUserYesterdayLogin($channelId, $start_time, $end_time)
- {
- $user_behavior = DB::table('channel_operate_record')
- ->where([
- ['distribution_channel_id', '=', $channelId],
- ['created_at', '>=', $start_time],
- ['created_at', '<=', $end_time]
- ])
- ->first();
- if ($user_behavior) return 1;
- return 0;
- }
- /**
- * @param $date
- * @param $company_id
- * @return mixed
- */
- static function getCompanyDayTotal($date, $company_id)
- {
- $end_date = date('Y-m-d', strtotime($date) + 86400);
- $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
- ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
- ->where([
- ['users.created_at', '>=', $date],
- ['users.created_at', '<', $end_date],
- ['channel_users.company_id', '=', $company_id]
- ])
- ->groupBy('channel_users.company_id')
- ->select(DB::raw("count(users.id) as register_sum"))
- ->first();
- if ($info) return $info->register_sum;
- //\Log::info('getCompanyDayTotal error:' . $company_id);
- return $info;
- }
- static function getCompanyTotal($company_id)
- {
- $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
- ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
- ->where('channel_users.company_id', $company_id)
- ->groupBy('channel_users.company_id')
- ->select(DB::raw("count(users.id) as register_sum"))
- ->first();
- if ($info) return $info->register_sum;
- //\Log::error('getCompanyTotal error:' . $company_id);
- return $info;
- }
- static function transfromBalanceByUser($from_uid, $to_uid)
- {
- $add_balance_sql = "update users,(select * from users where id =" . $from_uid . ") as u2
- set users.balance = users.balance + u2.balance,
- users.reward_balance = users.reward_balance +u2.reward_balance,
- users.charge_balance = users.charge_balance +u2.charge_balance where users.id =" . $to_uid;
- $res = DB::update($add_balance_sql);
- return User::updateInfo($from_uid, ['balance' => 0, 'reward_balance' => 0, 'charge_balance' => 0]);
- }
-
- public static function rfmLevel($uid,$register_timestamp){
- $register_time_diff = time()-$register_timestamp;
- if($register_time_diff < 86400) return [];
- $order_result = Order::where('uid',$uid)->where('status','PAID')->select('id','price','created_at')->orderBy('id','desc')->get();
- if($order_result->count() <= 0){
- return ['r'=>0,'f'=>0,'m'=>0];
- }
- if($register_time_diff < 3*86400){
- $after_one_day_charge = [];
- $charge_max_amount = 0;
- $in_one_day_charge = [];
- $in_one_hour_charge = [];
- foreach ($order_result as $item){
- $charge_max_amount = $item->price > $charge_max_amount? $item->price:$charge_max_amount;
- if( ($item->created_at->timestamp - $register_timestamp) >= 86400 ){
- $after_one_day_charge[] = $item->price;
- }else{
- $in_one_day_charge[] = $item->price;
- if( ($item->created_at->timestamp - $register_timestamp) <= 3600 ){
- $in_one_hour_charge[] = $item->price;
- }
- }
- }
- if($after_one_day_charge_count = count($after_one_day_charge) >0){
- return ['r'=>2,'f'=>$after_one_day_charge_count>1 ? 2:1,'m'=>$charge_max_amount>30 ?2:1];
- }elseif(count($in_one_day_charge) >0){
- if($in_one_day_charge_count = count($in_one_day_charge) == count($in_one_hour_charge)){
- return ['r'=>1,'f'=>$in_one_day_charge_count >1?2:1,'m'=>$charge_max_amount>30 ?2:1];
- }else{
- return ['r'=>2,'f'=>$in_one_day_charge_count >1?2:1,'m'=>$charge_max_amount>30 ?2:1];
- }
- }
- }
- if($register_time_diff < 18*86400){
- $after_three_day_charge = [];
- $charge_max_amount = 0;
- $in_three_day_charge = [];
- $in_one_hour_charge = [];
- foreach ($order_result as $item){
- $charge_max_amount = $item->price > $charge_max_amount? $item->price:$charge_max_amount;
- if( ($item->created_at->timestamp - $register_timestamp) >= 3*86400 ){
- $after_three_day_charge[] = $item->price;
- }else{
- $in_three_day_charge[] = $item->price;
- if( ($item->created_at->timestamp - $register_timestamp) <= 3600 ){
- $in_one_hour_charge[] = $item->price;
- }
- }
- }
- if($after_three_day_charge_count = count($after_three_day_charge) >0){
- return ['r'=>2,'f'=>$after_three_day_charge_count>1 ? 2:1,'m'=>$charge_max_amount>30 ?2:1];
- }elseif(count($in_three_day_charge) >0){
- if($in_three_day_charge_count = count($in_three_day_charge) == count($in_one_hour_charge)){
- return ['r'=>1,'f'=>$in_three_day_charge_count >1?2:1,'m'=>$charge_max_amount>30 ?2:1];
- }else{
- return ['r'=>2,'f'=>$in_three_day_charge_count >1?2:1,'m'=>$charge_max_amount>30 ?2:1];
- }
- }
- }
- $latest_18_day_charge_count = $order_result->where('created_at','>=',date('Y-m-d H:i:s',time()-360*3600))->count();
- $latest_18_day_charge_max_amount = $order_result->where('created_at','>=',date('Y-m-d H:i:s',time()-360*3600))->max('price');
- if(time() - $order_result->first()->created_at->timestamp <= 360*3600){
- return ['r'=>2,'f'=>$latest_18_day_charge_count >1?2:1,'m'=>$latest_18_day_charge_max_amount>30 ?2:1];
- }
- return ['r'=>1,'f'=>$latest_18_day_charge_count >1?2:1,'m'=>$latest_18_day_charge_max_amount>30 ?2:1];
- }
-
- }
|