<?php

namespace Act\Lib;

use Dever;

class Note
{
    # 获取message的push信息
    public function push($type, $id, $name, $link = '')
    {
        $param = array('type' => $type, 'id' => $id, 'name' => $name, 'link' => $link);

        $push = array(json_encode($param), 'jstyle://cn.jstyle.app/route?' . http_build_query($param));
        return $push;
    }
    # 获取当前用户是否预约
    public function get($uid, $id, $type)
    {
        $where['uid'] = $uid;
        $where['type'] = $type;
        $where['data_id'] = $id;
        $where['state'] = 1;
        $info = Dever::db('act/live_note')->one($where);

        if ($info && $info['state'] == 1) {
            return 1;
        } else {
            return 2;
        }
    }

    # 预约
    public function submit($uid, $id, $type, $formid = '', $system = 1)
    {
        if ($formid) {
            Dever::load('act/lib/form')->submit($uid, $formid, 1, $system);
        }
        $where['uid'] = $uid;
        $where['data_id'] = $id;
        $where['type'] = $type;
        $where['cate_id'] = $system;
        $info = Dever::db('act/live_note')->one($where);
        if (!$info) {
            if ($formid) {
                $where['formid'] = $formid;
            }
            
            Dever::db('act/live_note')->insert($where);
        } else {
            $update['cate_id'] = $system;
            $update['where_id'] = $info['id'];
            if ($formid) {
                $update['formid'] = $formid;
            }
            $update['cdate'] = time();
            if ($info['state'] == 1) {
                $update['state'] = 2;
                Dever::db('act/live_note')->update($update);
            } else {
                $update['state'] = 1;
                Dever::db('act/live_note')->update($update);
            }
        }

        return true;
    }

    # 提醒 后续实现 需要加入到cron里：act/lib/note.cron
    public function cron()
    {
        $data = Dever::db('act/live_note')->getAll();

        if ($data) {
            $time = time();
            $day = 7 * 86400;
            $date = 800;
            foreach ($data as $k => $v) {
                $table = Dever::config('base')->type_table[$v['type']];
                $info = Dever::db($table)->one($v['data_id']);
                if ($info && $v['type'] == 3 && isset($info['sdate']) && $info['sdate'] > 0 && $info['sdate'] > $time && $info['sdate'] - $time <= $date) {

                    $user = Dever::db('passport/user')->one($v['uid']);
                    if (!$user) {
                        continue;
                    }
                    Dever::db('act/live_note')->update(array('where_id' => $v['id'], 'note' => 2));

                    if (Dever::project('message')) {
                        Dever::load('message/lib/data')->push(-1, $v['uid'], '直播提醒', $info['name'] . '直播即将开始，马上观看！', 11, $v['cate_id'], 1, Dever::load('act/lib/note')->push(3, $info['id'], $info['name']));
                    }
                    
                    # 发送短消息
                    if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms')) {
                        $send = array();
                        $send['name'] = $info['name'];
                        Dever::load('sms/api.send', 'note_live', $user['mobile'], $send);
                    }

                    //$formid = Dever::load('act/lib/form')->get($v['uid'], 1);
                    $formid = $v['formid'];
                    if ($formid) {
                        # 发送模板消息
                        $wechat = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'type' => 1, 'system_id' => $v['cate_id']));
                        if ($wechat && Dever::project('wechat_applet')) {
                            $send['key'] = 'note_live';
                            $send['project_id'] = $v['cate_id'];
                            $send['touser'] = $wechat['openid'];
                            $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($v['uid']) . ',' . $v['type'] . ',' . $v['id'];
                            $send['data'] = array
                            (
                                'keyword1' => array('value' => $info['name']),
                                'keyword2' => array('value' => $info['name'] . '直播即将开始，马上观看！'),
                            );
                            $send['data'] = json_encode($send['data']);
                            $send['form_id'] = $formid;
                            Dever::load('wechat_applet/msg.send', $send);
                        }
                    }
                }
            }
        }

        return 'ok';
    }

    public function cron_api()
    {
        
    }
}