<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/21
 * Time: 10:42
 */

namespace App\Modules\User\Services;

use Redis;
use Hashids;
use App\Modules\Book\Services\BookConfigService;
use App\Modules\Book\Models\Chapter;
use DB;

class ReadRecordService
{

    private static $not_uid_key = ['last_read','send_order_id','sign_count','sign_counts','sign_day','smart_push','inner_send_order_id'];

    /**
     * 获取
     * @param $uid
     * @return array
     */
    public static function getReadRecord_($uid)
    {
        $read_bids = Redis::hgetall('book_read:' . $uid);
        $res = [];
        $i = 0;
        foreach ($read_bids as $key => $v) {
            $record = explode('_', $v);
            $latest_read_cid = $record[0];
            $book_name = $record[1];
            $chapter_name = $record[2];
            $latest_read_time = $record[count($record) - 1];
            $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time, 'chapter_name' => $chapter_name];
            $i++;
        }
        usort($res, function ($a, $b) {
            if ($a['time'] >= $b['time']) return -1;
            return 1;

        });
        return $res;
    }


    /**
     * 获取 升级版
     * @param $uid
     * @return array
     */
    public static function getReadRecord($uid)
    {
        $read_bids = Redis::hgetall('book_read:' . $uid);
        $res = [];
        $i = 0;
        //self::delBookNameAndChapter($uid);
        foreach ($read_bids as $key => $v) {
            if(in_array($key,self::$not_uid_key)){
                continue;
            }
            $record = explode('_', $v);
            $latest_read_cid = $record[0];
            $latest_read_time = $record[count($record) - 1];
            $book_name = self::bid2BookName($key);
            $chapter_name = self::cid2ChapterName($latest_read_cid);
            $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time, 'chapter_name' => $chapter_name];
            $i++;
        }
        usort($res, function ($a, $b) {
            if ($a['time'] >= $b['time']) return -1;
            return 1;

        });
        return $res;
    }

    /**
     * 新增
     * @param $uid
     * @param $bid
     * @param $cid
     * @param $book_name
     * @param $chapter_name
     */
    public static function addReadRecord_($param)
    {
        $uid = $param['uid'];
        $bid = $param['bid'];
        $cid = $param['cid'];
        $book_name = $param['book_name'];
        $chapter_name = $param['chapter_name'];
        Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
        //Redis::hset('book_read:'.$uid, $bid, $cid."_".time());
        Redis::hset('book_read:' . $uid, $bid, "{$cid}_{$book_name}_{$chapter_name}_" . time());
    }

    /**
     * 添加阅读记录升级版
     * @param $param
     */
    public static function addReadRecord($param)
    {
        $uid = $param['uid'];
        $bid = $param['bid'];
        $cid = $param['cid'];

        $book_name = isset($param['book_name'])?$param['book_name']:'';
        $chapter_name = isset($param['chapter_name'])?$param['chapter_name']:'';
        $book_key = 'wap:string:book:'.$bid;
        $chapter_key = 'wap:string:chapter:'.$cid;
        if($book_name){
            Redis::setex($book_key,3600,$book_name);
        }
        if($chapter_name){
            Redis::setex($chapter_key,3600,$chapter_name);
        }

        Redis::hset('book_read:' . $uid, 'last_read', "{$bid}_{$cid}_" . time());
        //Redis::hset('book_read:'.$uid, $bid, $cid."_".time());
        Redis::hset('book_read:' . $uid, $bid, "{$cid}_" . time());
    }

    /**
     * 删除
     * @param $uid
     * @param $bid
     */
    public static function delReadRecord($uid, $bid)
    {
        if (Redis::hexists('book_read:' . $uid, $bid)) {
            Redis::hdel('book_read:' . $uid, $bid);
        }
    }

    /**
     * 获取最近一条阅读记录
     * @param $uid
     */
    public static function getFirstReadRecord_($uid){
        $all = self::getReadRecord($uid);
        if(empty($all)) return [];
        $first = $all[0];
        if(!$first) return [];
        if(!isset($first['bid']))  return [];
        try{
            //$bid = Hashids::encode($first['bid']);
            $bid = $first['bid'];
            $book_info = BookConfigService::getBookById($bid);
            $cid = $first['cid'];
            $book_name = $first['book_name'];
            $res = [
                'url' => '/reader?bid='.$bid.'&cid='.$cid,
                'book_name'=>$book_name,
                'cover' =>$book_info->cover,
                'channel_name'=>$book_info->channel_name,
            ];
        }catch (\Exception $e){
            $res = [];
        }

        return $res;
    }

    /**
     * 获取最近一条阅读记录(升级版)
     * @param $uid
     * @return array
     */
    public static function getFirstReadRecord($uid){
        self::delBookBase($uid);
        //Redis::hget('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
        $record = Redis::hget('book_read:' . $uid, 'last_read');
        if($record){
            $record_arr = explode('_',$record);
            $bid = $record_arr[0];
            $cid = $record_arr[1];
            $book_info = BookConfigService::getBookById($bid);
            $book_name = isset($book_info->book_name)?$book_info->book_name:'';
            $cover = isset($book_info->cover)?$book_info->cover:'';
            $channel_name = isset($book_info->channel_name)?$book_info->channel_name:'';
            $res = [
                'url' => '/reader?bid='.$bid.'&cid='.$cid,
                'book_name'=>$book_name,
                'cover' =>$cover,
                'channel_name'=>$channel_name,
                'bid'=>$bid,
            ];
            return $res;
        }
        return [];
    }

    /**
     * 获取简单阅读记录
     * @param $uid
     * @return int
     */
    public static function getSimpleFirstReadRecord($uid){
        try{
            $record = Redis::hget('book_read:' . $uid, 'last_read');
            if($record){
                $record_arr = explode('_',$record);
                $bid = $record_arr[0];
                return (int)$bid;
            }
        }catch (\Exception $e){

        }
        return 0;
    }

    /**
     * 获取客服消息点击数
     * @param $uid
     */
    public static function getCustomerMsgClickNum($channel_id,$from,$date){
        $key = "fromcustomermsgenter:distribution_channel_id:".$channel_id.'from:'.$from;
        return Redis::hget($key,$date);
    }

    /**
     * 获取某本书的阅读记录
     */
    public static function getRecordByUidBid($uid,$bid){
        return Redis::hget('book_read:' . $uid, $bid);
    }

    /**
     * 根据bid获取书名
     * @param $bid
     * @return bool|null|string
     */
    public static function bid2BookName($bid){
        $book_name = null;
        if(is_null($book_name)){
            $book_key = 'wap:string:book:'.$bid;
            $book_name = Redis::get($book_key);
            Redis::EXPIRE($book_key,3600);
            if(!$book_name){
                $book_name = '';
                $book_info = BookConfigService::getBookById($bid);
                if($book_info && isset($book_info->book_name)){
                    $book_name = $book_info->book_name;
                }
            }
        }
        return $book_name;
    }

    /**
     * 根据cid获取章节名
     * @param $cid
     * @return bool|null|string
     */
    public static function cid2ChapterName($cid){
        $chapter_name = null;
        if(is_null($chapter_name)){
            $chapter_key = 'wap:string:chapter:'.$cid;
            $chapter_name = Redis::get($chapter_key);
            Redis::EXPIRE($chapter_key,3600);
            if(!$chapter_name){
                $chapter_name = '';
                $chapter_info = Chapter::getChapterNameById($cid);
                if($chapter_info && isset($chapter_info->name)){
                    $chapter_name = $chapter_info->name;
                }
            }
        }
        return $chapter_name;
    }

    /**
     * 删除阅读记录中的书名和章节名
     * @param $uid
     * @param $record
     */
    public static function delBookNameAndChapter($uid){
        //Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
        $base_record = Redis::hget('book_base:' . $uid, 'last_read');
        if($base_record){
            $record_arr = explode('_',$base_record);
            $c = count($record_arr);
            if($c>3){
                $bid = $record_arr[0];
                $cid = $record_arr[1];
                $time = $record_arr[$c-1];
                Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_" . $time);
            }
        }
        $records = Redis::hgetall('book_read:' . $uid);
        foreach ($records as $key => $v) {
            $record = explode('_', $v);
            $count = count($record);
            if($count >3){
                $latest_read_cid = $record[0];
                $book_name = $record[1];
                $chapter_name = $record[2];
                $latest_read_time = $record[$count - 1];
                Redis::hset('book_read:' . $uid, $key, "{$latest_read_cid}_" . $latest_read_time);

                $book_key = 'wap:string:book:'.$key;
                $chapter_key = 'wap:string:chapter:'.$latest_read_cid;
                Redis::set($book_key,$book_name);
                Redis::set($chapter_key,$chapter_name);
            }
        }
    }

    public static function delBookBase($uid){
        $base_record = Redis::hget('book_base:' . $uid, 'last_read');
        if($base_record){
            Redis::del('book_base:' . $uid);
            Redis::hset('book_read:' . $uid, 'last_read', $base_record);
        }
    }

    /**
     * 获取简单阅读记录只有bid
     * @param int $uid
     * @return array
     */
    public static function getSimpleReadRecord(int $uid):array
    {
        $read_bids = Redis::hgetall('book_read:' . $uid);
        $res = [];
        if(!$read_bids) {
            return $res;
        }
        foreach ($read_bids as $key => $v) {
            if(in_array($key,self::$not_uid_key)){
            	continue;
            }
            array_push($res,$key);
        }
        return $res;
    }


    public static function ReadRecordStatistical(int $uid,int $distribution_channel_id,string $from){
        try{
            DB::table('temp_read_active')->insert([
                'uid'=>$uid,
                'distribution_channel_id'=>$distribution_channel_id,
                'from'=>$from,
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s'),
            ]);
        }catch (\Exception $e){

        }
    }

    /**
     * 获取当前的send_order_id
     * @param int $uid
     * @return int
     */
    public static function getSendOrderId(int $uid){
        try{
            $send_order_id = Redis::hget('book_read:' . $uid,'send_order_id');
            if($send_order_id)
                return (int)$send_order_id;
        }catch (\Exception $e){

        }
        return 0;
    }

    /**
     * 设置内部派单
     * @param $uid
     * @param $inner_order_id
     */
    public static function setInnerSendOrderId($uid,$inner_order_id){
        try{
            Redis::hset('book_read:' . $uid,'inner_send_order_id',$inner_order_id);
        }catch (\Exception $e){}
    }

    /**
     * 获取内部派单
     * @param $uid
     * @return string
     */
    public static function getInnerSendOrderId($uid){
        try{
            $inner_send_order_id = Redis::hget('book_read:' . $uid,'inner_send_order_id');
            if($inner_send_order_id){
                return $inner_send_order_id;
            }
            return '';
        }catch (\Exception $e){}
        return '';
    }
    /**
     * 签到日期
     * @param int $uid
     * @return mixed
     */
    public static function getSignDay(int $uid){
        try{
            return Redis::hget('book_read:' . $uid,'sign_day');
        }catch (\Exception $e){}
        return -1;
    }

    public static function setSignDay(int $uid){
        return Redis::hset('book_read:' . $uid,'sign_day',date('Y-m-d'));
    }

    /**
     * 签到次数和日期
     * @param int $uid
     */
    public static function sign(int $uid,bool $is_incr):void{
        try{
            if($is_incr){
                Redis::hincrby('book_read:' . $uid,'sign_counts',1);
            }else{
                self::setSignCount($uid,1);
            }
            self::setSignDay($uid);
        }catch (\Exception $e){

        }
    }

    /**
     * @param int $uid
     * @return int
     */
    public static function getSignCount(int $uid){
        try{
            $count = Redis::hget('book_read:' . $uid,'sign_counts');
            if($count){
                return $count;
            }
        }catch (\Exception $e){

        }
        return 0;
    }

    /**
 * 获取简单签到次数
 * @param int $uid
 * @return int
 */
    public static function getSignCountSimple(int $uid){
        try{
            $count = Redis::hget('book_read:' . $uid,'sign_counts');
            if($count){
                return (int)$count;
            }
            return 0;
        }catch (\Exception $e){
        }
        return 0;
    }

    public static function setSignCount(int $uid,int $count){
        Redis::hset('book_read:' . $uid,'sign_counts',$count);
    }

    public static function setSmartPush($uid,$bid){
        $old = self::getSmartPush($uid);
        if($old && !in_array($bid,$old)){
            array_push($old,$bid);
            $bid_str = implode(',',$old);
            return Redis::hset('book_read:' . $uid,'smart_push',$bid_str);
        }else{
            return Redis::hset('book_read:' . $uid,'smart_push',$bid);
        }
    }

    public static function getSmartPush(int $uid):array{
        $res =  Redis::hget('book_read:' . $uid,'smart_push');
        if($res){
            return explode(',',$res);
        }
        return [];
    }
}