<?php

namespace web\api;

class message {
    static function sendmail($db, $chan, $uid, $mail, $sendtimes, $mailname, $mailcontent) {
        if (empty($mail)) {
            $csql = new \ciy\sql('ap_usr_ext');
            $csql->where('id', $uid);
            $uextrow = $db->getone($csql);
            if (!is_array($uextrow))
                return false;
            $mail = $uextrow['email'];
        }
        if (empty($mail))
            return false;
        //在sendtimes到时间且donetimes<10发送。
        //发送成功donetimes=time
        //发送失败donetimes++，sendtimes=time+600

        //发送完成donetimes+=100
        //通知失败donetimes-=99，sendtimes=time+600
        $updata = array();
        $updata['vuser'] = $uid;
        $updata['email'] = $mail;
        $updata['chan'] = $chan;
        $updata['addtimes'] = tostamp();
        $updata['sendtimes'] = $sendtimes;
        $updata['donetimes'] = 0;
        $updata['name'] = $mailname;
        $updata['content'] = $mailcontent;
        $csql = new \ciy\sql('ap_usr_sendmail');
        if ($db->insert($csql, $updata) === false)
            throw new \Exception('新建邮件失败:' . $db->error);
    }
    static function sendsms($db, $chan, $uid, $mobile, $sendtimes, $jsoncontent) {
        if (empty($mobile)) {
            $csql = new \ciy\sql('ap_user');
            $csql->where('id', $uid);
            $uextrow = $db->getone($csql);
            if (!is_array($uextrow))
                return false;
                $mobile = $uextrow['mobile'];
        }
        if (empty($mobile))
            return false;
        $updata = array();
        $updata['vuser'] = $uid;
        $updata['mobile'] = $mobile;
        $updata['chan'] = $chan;
        $updata['addtimes'] = tostamp();
        $updata['sendtimes'] = $sendtimes;
        $updata['donetimes'] = 0;
        $updata['content'] = $jsoncontent;
        $csql = new \ciy\sql('ap_usr_sendsms');
        if ($db->insert($csql, $updata) === false)
            throw new \Exception('新建短信失败:' . $db->error);
    }
}
