<?php

namespace Act\Lib;

use Dever;

class Invite
{
    # 获取小刊的邀请排行
    public function getList($uid, $id, $type = 4, $num = false)
    {
        $key = 'invite_' . $uid . '_' . $type . '_' . $id;
        //$data = Dever::cache($key);
        $data = array();
        if (!$data) {
        	$where['source_uid'] = $uid;
            $where['type'] = $type;
            $where['data_id'] = $id;
            if ($num > 0) {
                $where['page'] = array(6, 'list');
            }
            $where['avatar'] = 'null';
            $data = Dever::db('act/invite')->getAll($where);

            if ($data) {
                //Dever::cache($key, $data, 900);
            }
        }

        /*
        if ($data) {
            foreach ($data as $k => $v) {
                $user = Dever::load('passport/api')->info($v['uid']);
                $data[$k]['username'] = $user['username'];
                $data[$k]['avatar'] = $user['avatar'];
            }
        }
        */
        return $data;
    }

    # 获取小刊的邀请数量
    public function getTotal($uid, $id, $type = 4)
    {
        $where['source_uid'] = $uid;
        $where['type'] = $type;
        $where['data_id'] = $id;
        $data = Dever::db('act/invite')->total($where);

        return $data;
    }

    public function submit($source_uid, $uid, $id, $type = 4)
    {
        $where['source_uid'] = $source_uid;
        $where['uid'] = $uid;
        $where['data_id'] = $id;
        $where['type'] = $type;
        $table = 'act/invite';
        $info = Dever::db($table)->one($where);
        if (!$info) {
            Dever::db($table)->insert($where);
        }

        return true;
    }
}