<?php
/**
 * Created by PhpStorm.
 * User: xmy 280564871@qq.com
 * Date: 2017/4/7
 * Time: 13:59
 */

namespace Mobile\Model;
use Think\Model;
class ShareRecordModel extends Model
{


	/**
     * 构造函数
     * @param string $name 模型名称
     * @param string $tablePrefix 表前缀
     * @param mixed $connection 数据库连接信息
     */
    public function __construct($name = '', $tablePrefix = '', $connection = '') {
        /* 设置默认的表前缀 */
        $this->tablePrefix ='tab_';
        /* 执行构造方法 */
        parent::__construct($name, $tablePrefix, $connection);
    }

    /**
	 * 添加邀请好友注册记录
	 * @param $invite_account   邀请人账号
	 * @param $user_account     被邀请人账号
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function addShareRecord($invite_account,$user_account){
		$data['invite_id'] = get_user_id($invite_account);
		$data['invite_account'] = $invite_account;
		$data['user_id'] = get_user_id($user_account);
		$data['user_account'] = $user_account;
		$data['create_time'] = time();
		$data['award_coin'] = 0;
		return $this->add($data);
	}
    
	/**
	 * 获取我的邀请记录
	 * @param $invite_id
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getInviteRecord($map,$p=1)
	{
		$page = intval($p);
		$page = $page ? $page : 1; //默认显示第一页数据
		$row = 10;
		$data['data'] = $this
			->field("invite_id,invite_account,count(DISTINCT user_id) as num,create_time,sum(award_coin) as award_coin")
			->where($map)
			->group("invite_id")
			->order("create_time desc")
			->page($page,$row)
			->select();

		$data['count'] = $this
			->where($map)
			->group("invite_id")
			->order("create_time desc")
			->count();

		return $data;
	}

	/**
	 * 获取用户邀请统计
	 * @param $invite_id
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getUserInviteInfo($invite_id){
		$map['invite_id'] = $invite_id;
		$data = $this->field("count(distinct user_id) as invite_num,sum(award_coin) as award_coin")
			->where($map)
			->group("invite_id")
			->find();
		return $data;
	}

	/**
	 * 获取详情
	 * @param $map
	 * @param $p
	 * @return mixed
	 * author: xmy 280564871@qq.com
	 */
	public function getDetail($map,$p=1){
		$page = intval($p);
		$page = $page ? $page : 1; //默认显示第一页数据
		$row = 10;
		$data['data'] = $this->field("id,user_id,user_account,sum(award_coin) as coin,create_time")
			->where($map)
			->order("create_time desc")
			->group("user_id")
			->page($page,$row)
			->select();

		$data['count'] = $this->where($map)->group("user_id")->count();

		return $data;
	}

}