<?php

if (!defined('ROOT')) exit('Can\'t Access !');

class questionmessage extends table {
    public $name='question_message';
    static $me;
    public static function getInstance() {
        if (!self::$me) {
            $class=new questionmessage();
            self::$me=$class;
        }
        return self::$me;
    }

    function getcols($act) {
        return '*';
    }

    //增加通知   当前用户ID   问题集合  问题类型  金额 子循环
    function message_add($uid,$aid,$type,$menory=0,$son=1,$user_crossall=''){
        $_question_message=new questionmessage();
        $question=question::getInstance()->getrow("id=".$aid);
        //问答类型(1充值2回复记录3采纳记录4打赏5悬赏6关注问题回复7关注采纳 )
        switch ($type)
        {
            case 1:
                $content="成功充值金额：".$menory;
                $question['title']="充值记录";
                $question['aid']=0;
                break;
            case 2:
                $content="新的回复！";
                $uid=$question['uid'];
                break;
            case 3:
                $content="你的回答已被采纳！";
                break;
            case 4:
                $content="恭喜你获得".user::getusername($uid)."的打赏：".$menory."元。";
                break;
            case 5:
                $content="恭喜你获得".user::getusername($uid)."的悬赏：".$menory."元。";
                break;
            case 6:
                $content="你关注的问题有新的回复！";
                break;
            case 7:
                $content="你关注的问题已被采纳！";
                break;
            default:
                $content="新的回复！";
        }
        $message=$_question_message->getrow("uid=".$uid." and type=".$type.' and aid='.$question['id'].' and user_crossall="'.$user_crossall.'"');
        if (($type==2 || $type==6) && is_array($message)){
            $_question_message->rec_update(array("see"=>1,"adddate"=>date('Y-m-d H:i:s', time())),"uid=".$uid." and type=".$type.' and aid='.$question['id']);  //消息通知 改为未读
        }else{
            //新增通知
            $messagedata = array();
            $messagedata['title'] = $question['title'];
            $messagedata['aid'] = $question['id'];
            $messagedata['content'] =$content;
            $messagedata['uid'] = $uid;
            $messagedata['see'] = 1;
            front::$post['user_crossall']=$user_crossall;    //跨域
            $messagedata['adddate'] = date('Y-m-d H:i:s', time());
            $messagedata['type'] =$type;
            $_question_message->rec_insert($messagedata);
        }
        if ($son && ($type==2 || $type==3) && $question['seemid']!=""){
            $seemid= explode(',', $question['seemid']);
            for($index=0;$index<count($seemid);$index++){
                $_question_message->message_add($seemid[$index],$aid,($type==2?6:7),$menory,0);

            }
        }


    }

}