<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/2/27
 * Time: 上午10:17
 */

namespace message;

use Log;
use Redis;

class publisher
{
    private $mConfig;
    private $mRedis;
    private $mConnected;

    public function __construct()
    {
        $this->mConfig = C('redis');
        $this->mRedis = new Redis();
        $ret = $this->mRedis->pconnect($this->mConfig['master']['host'], $this->mConfig['master']['port']);
        if($ret == false) {
            Log::record("redis 连接失败.",Log::ERR);
            $this->mConnected = false;
        } else {
            $this->mConnected = true;
        }
    }
    public function __destruct()
    {
        if($this->mConnected) {
            $this->mRedis->close();
        }
    }
    public function modify_index_tabs()
    {
        if($this->mConnected) {
            $this->mRedis->publish('ch_index',serialize(array('type'=>'tags','value' => 0)));
        }
    }
    public function modify_index_brands()
    {
        if($this->mConnected) {
            $this->mRedis->publish('ch_index',serialize(array('type'=>'brands','value' => 0)));
        }
    }
    public function modify_index_payfcode()
    {
        if($this->mConnected) {
            $this->mRedis->publish('ch_index',serialize(array('type'=>'pay_fcode','value' => 0)));
        }
    }

    public function modify_index_presentfcode()
    {
        if($this->mConnected) {
            $this->mRedis->publish('ch_index',serialize(array('type'=>'present_fcode','value' => 0)));
        }
    }
    public function modify_index_special()
    {
        if($this->mConnected) {
            $this->mRedis->publish('ch_index',serialize(array('type'=>'special','value' => 0)));
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function modify_goods()
    {
        if($this->mConnected) {
            $this->mRedis->publish('goods',serialize(array('type'=>'common_info','value' => 0)));
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function modify_activity_fullsent()
    {
        if($this->mConnected) {
            $this->mRedis->publish('activity',serialize(array('type'=>'full_sent','value' => 0)));
        }
    }
    public function modify_activity_groupbuy()
    {
        if($this->mConnected) {
            dcache('all_groupbuy','mb_activity');
            $this->mRedis->publish('activity',serialize(array('type'=>'groupbuy','value' => 0)));
        }
    }
    public function modify_activity_limit()
    {
        if($this->mConnected) {
            dcache('all_limitime','mb_activity');
            $this->mRedis->publish('activity',serialize(array('type'=>'limit','value' => 0)));
        }
    }
    public function modify_activity_recommend_goods()
    {
        if($this->mConnected) {
            dcache('all_recommend_goods','mb_activity');
            $this->mRedis->publish('activity',serialize(array('type'=>'recommend_goods','value' => 0)));
        }
    }
    public function modify_activity_bundling()
    {
        if($this->mConnected) {
            dcache('all_bundling','mb_activity');
            $this->mRedis->publish('activity',serialize(array('type'=>'bundling','value' => 0)));
        }
    }
    public function modify_activity_recommend_combo()
    {
        if($this->mConnected) {
            dcache('all_recommend_combo','mb_activity');
            $this->mRedis->publish('activity',serialize(array('type'=>'recommend_combo','value' => 0)));
        }
    }
    public function modify_activity_goods_gift()
    {
        if($this->mConnected) {
            dcache('all_goods_gift','mb_activity');
            $this->mRedis->publish('activity',serialize(array('type'=>'goods_gift','value' => 0)));
        }
    }
    public function modify_activity_optional_goods()
    {
        if($this->mConnected) {
            $this->mRedis->publish('activity',serialize(array('type'=>'optional_goods','value' => 0)));
        }
    }
    public function modify_activity_goods_sampler()
    {
        if($this->mConnected) {
            $this->mRedis->publish('activity',serialize(array('type'=>'goods_sampler','value' => 0)));
        }
    }

    public function modify_activity()
    {
        $this->modify_activity_fullsent();
        $this->modify_activity_groupbuy();
        $this->modify_activity_limit();
        $this->modify_activity_recommend_goods();
        $this->modify_activity_bundling();
        $this->modify_activity_recommend_combo();
        $this->modify_activity_goods_gift();
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function modify_seracher_init()
    {
        if($this->mConnected) {
            $this->mRedis->publish('searcher',serialize(array('type'=>'init','value' => 0)));
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    public function add_inviter($user,$inviter)
    {
        if($this->mConnected)
        {
            $user = intval($user);
            $inviter = intval($inviter);

            $this->mRedis->publish('inviter',serialize(array('type'=>'add','params' => ['user_id' => $user,'inviter_id' => $inviter])));
        }
    }

    public function add_follow($user, $fellows)
    {
        if($this->mConnected)
        {
            $user = intval($user);
            $uids = [];
            foreach ($fellows as $uid) {
                $uid = intval($uid);
                if($uid > 0) {
                    $uids[] = $uid;
                }
            }

            $this->mRedis->publish('follow',serialize(array('type'=>'add','params' => ['user_id' => $user,'friends' => $uids])));
        }
    }

    public function del_follow($user, $fellows)
    {
        if($this->mConnected)
        {
            $user = intval($user);
            $uids = [];
            foreach ($fellows as $uid) {
                $uid = intval($uid);
                if($uid > 0) {
                    $uids[] = $uid;
                }
            }

            $this->mRedis->publish('follow',serialize(array('type'=>'del','params' => ['user_id' => $user,'friends' => $uids])));
        }
    }
    public function add_special($user,$specials)
    {
        if($this->mConnected)
        {
            $user = intval($user);
            $this->mRedis->publish('special',serialize(array('type'=>'add','params' => ['user_id' => $user,'specials' => $specials])));
        }
    }

    public function del_special($user,$specials)
    {
        if($this->mConnected)
        {
            $user = intval($user);
            $this->mRedis->publish('special',serialize(array('type'=>'del','params' => ['user_id' => $user,'specials' => $specials])));
        }
    }
}