<?php

namespace App\Modules\PersonalOp\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class DeepReadRecord extends Model
{
    protected  $table = 'deep_read_records';
    protected  $fillable = ['uid','bid','appid','first_sub_time','deep_sub_time','charge_type'];

    
    /**
     * 方法暂时弃用，才哥那边有跑脚本
     * @param unknown_type $recent_bids
     */
    public static function getDeepReCommendBids($recent_bids){
    	$now = date('Y-m-d H:i:s');
    	$today = date('Y-m-d');
    	
    	$result = [];
    	$user_deep_bid_nums = [];
    	$user_deep_bids = [];
    	$recommend_bid_nums = DB::select('
    			select d.uid,d.bid,d.deep_sub_time,d.id,
    			(select d2.bid from deep_read_records d2 where d2.uid=d.uid and d2.id <> d.id order by deep_sub_time desc limit 1 ) as recommend_bid
    			from deep_read_records d
    			where d.bid '.db_create_in($recent_bids).'
    			');
    	if(!empty($recommend_bid_nums)){
    		// 计算from_bid 频率最高的recommend_bid
    		foreach($recommend_bid_nums as $recommend_bid_num){
    			if($recommend_bid_num->recommend_bid == '') continue;
    			
    			if(isset($user_deep_bid_nums[$recommend_bid_num->bid][$recommend_bid_num->recommend_bid])) {
    				$user_deep_bid_nums[$recommend_bid_num->bid][$recommend_bid_num->recommend_bid] ++;
    			}else{
    				$user_deep_bid_nums[$recommend_bid_num->bid][$recommend_bid_num->recommend_bid] = 1;
    			}
    		}
    		
    		foreach($user_deep_bid_nums as $from_bid=>$user_deep_bid_num){
    			arsort($user_deep_bid_nums[$from_bid]);
//     			\Log::info('$user_deep_bid_nums');\Log::info($user_deep_bid_nums[$from_bid]);
    			$user_deep_bid_nums[$from_bid] = array_slice($user_deep_bid_nums[$from_bid],0,5,true);
    		}
    	}
    	$result = $user_deep_bid_nums;
    	return $result;
    }

}
