<?php
/**
* 推送相关
*/
defined('IN_WEB') or die( 'Include Error!');

class Push{

	/**
	* 获取推送列表
	*/
	public function getPushNewsList($param)
	{
		$table = otable::pushnews();
		$sql = " SELECT * FROM {$table} ORDER BY pn_time DESC ";
		$list = oo::commonOprDb('common')->getAll($sql, 1);
		$data = array('list' => $list, 'count' => count($list));
		return json_encode($data);
	}

  	public function getPushSettingList($param){
        $table = otable::PushSetting();
        $sql = " SELECT * FROM {$table} ORDER BY pn_time DESC ";
        $list = oo::commonOprDb('common')->getAll($sql, 1);
        $data = array('list' => $list, 'count' => count($list));
        return json_encode($data);
    }
  
	/**
	*	发送推送消息
	*/
	public function actionAddNews($param)
	{
		$title   = $param['news_title'];
		$content = $param['news_content'];
		$usertype = oo::functions()->uint($param['usertype']);
        $date = oo::functions()->escape($param['date']);
        $time = oo::functions()->escape($param['time']);
        $type = oo::functions()->escape($param['type']);
        $userList = oo::functions()->escape($param['uidlist']);
        $player   = trim($userList);
        $devicetype  = oo::functions()->uint($param['devicetype']);
        $accounttype = oo::functions()->uint($param['accounttype']);
        $tempTime    = $date.' '.$time;
        $table       = otable::PushSetting();
        $insertTitle = addslashes($title);
        $insertContent = addslashes($content);
        $sql = " INSERT INTO {$table} (pn_content,pn_title,pn_time,pn_type,pn_player,pn_player_type,pn_devicetype,pn_accounttype) VALUES('{$insertContent}', '{$insertTitle}', '{$tempTime}', '{$type}','{$player}',{$usertype} ,{$devicetype} ,{$accounttype})";
        oo::commonOprDb('common')->query($sql);
        if($type == 0){
            //入库
            $table = otable::pushnews();
            $time = time();
            $sql = " INSERT INTO {$table} VALUES(NULL, '{$insertContent}', '{$insertTitle}', {$time}, '{$player}', 1)";
            oo::commonOprDb('common')->query($sql);
            $titleTmp = json_decode($title,true);
            if(!empty($titleTmp)){
                $title = $titleTmp;
            }
            $contentTmp = json_decode($content,true);
            if(!empty($contentTmp)){
                $content = $contentTmp;
            }
            if( $usertype == 2 ){
                $uidArr = preg_split("/[\s,]+/", $player);
                oo::commonOprModel('push')->pushNewsToMultUserLang($uidArr, $title, $content);
            }else{
                oo::commonOprModel('push')->pushByTopicClassLang($devicetype, $accounttype, $title, $content);
            }
        }else{
            $sql = "SELECT max(id) AS id FROM {$table} WHERE 1";
            $ret = oo::commonOprDb('common')->getOne($sql,1);
            $temp = json_encode([
                'date'       =>$date,
                'time'       =>$time,
                'type'       =>$type,
                'devicetype' =>$devicetype,
                'accounttype'=>$accounttype,
                'title'      =>$title,
                'content'    =>$content,
                'player'     =>$player,
                'usertype'   =>$usertype,
            ],JSON_NUMERIC_CHECK);
            oo::commonOprRedis('common')->hSet(okeys::PushNew(),$ret['id'],$temp);
        }

		return json_encode(array('code' => 1));
	}

	/**
     * 删除
     * @param $param
     * Created by: Owen
     * Created on: 2019/7/16 15:08
     * Description:
     */
	public function deletePushNews($param){
        $id = oo::functions()->uint($param['id']);
        $table = otable::PushSetting();
        $sql = "DELETE FROM {$table} WHERE id={$id}";
        oo::commonOprDb('common')->query($sql);
        oo::commonOprRedis('common')->hDel(okeys::PushNew(),$id);
        return json_encode(array('code' => 1));
    }	
  
	/**
	*	流失召回列表
	*/
	public function getList($param)
	{
		$table = otable::recallback_push();
		$sql = " SELECT * FROM {$table} ORDER BY times DESC ";
		$list = oo::commonOprDb('common')->getAll($sql, 1);
		$data['list'] = $list;
		return json_encode($data); 
	}

	/**
	*	流失用户召回
	*	添加召回推送
	*/
	public function addpush($param)
	{
		$devices = $param['devices'];
		$days = oo::functions()->uint($param['days']);
		$title = $param['title'];
		$content = $param['content'];
		$name = $param['name'];
		$time = time();
		$table = otable::recallback_push();
		$sql = "INSERT INTO {$table} () VALUES(NULL, '{$devices}', $days, '{$title}', '{$content}', 0, {$time}, '{$name}', 0, 0)";
		$rs = oo::commonOprDb('common')->query($sql);
		$insertID = oo::commonOprDb('common')->insertID();//插入的ID

		//加入缓存
		$cache = array('id' => $insertID, 'devices' => $devices, 'day' => $days, 'title' => $title, 'conent' => $content);
		$key = 'XGRECALLBACK';
		oo::commonOprRedis('recall')->rPush($key, json_encode($cache));

		return json_encode(array('code' => 1));
	}
  
  	public function feedback($param){
        $uid = oo::functions()->uint($param['uid']);
        $content = oo::functions()->escape($param['content']);
        oo::commonOprModel('push')->pushNews($uid, 'push.title.feedback',$content,[],'feedback');
        $time = time();
        $message = [
            'uid'=>1,
            'toUid'=>$uid,
            'time' =>$time,
            'content'=>$content,
            'type' =>1,
        ];
        oo::commonOprRedis('common')->hSet(okeys::Chat($uid),'1#'.$time,json_encode($message));
        oo::commonOprModel('Workerman')->push($uid,array_search('Chat_sendToFriend_message',ocmd::$Cmd),$message,CHAR_REGISTER_URL);
    }
}
