<?php 
namespace Common\Model;
use Think\Model;
class TaskModel extends Model
{
	//
	//列表
	public function get_list($map='',$chenktask=false,$uid='')
	{
		$list = $this->where($map)->order('sort DESC, id ASC')->select();
		if ($chenktask && $uid>0 )
		{
			foreach ($list as $key => $v)
			{
				 
					$v['chenktask']=$this->chenk_task($uid,$v['id']);
					if ($v['chenktask'])
					{
						$v['chenktask']=1;
					}else{
						$v['chenktask']=0;
					}
				$li[]=$v;
			}
			return $li;
		}
		return $list;
	}
	//每日首次登陆
	public function exe_first_login($uid='')
	{
		$task=$this->get_task_one(13);
		if ($task)
		{
			$uid=intval($uid);
			$today=strtotime('today');
			$map['uid']=$uid;
			$map['tid']=$task['id'];
			$map['addtime']  = array('GT',$today);
			$Log_task = M('Log_task') -> where($map)->limit(1)->find();
			if (empty($Log_task))
			{
				$this->fulfill_task($task['id'],$uid);
				if ($task['gold']>0)
				{
				D('User')->edit_user_gold(1,$uid,$task['gold'],'每日首次登陆');
				}
				if ($task['exp']>0)
				{
				D('User')->edit_user_gold(2,$uid,$task['exp'],'每日首次登陆');
				}
			}
		}
	}
	//首次注册
	public function exe_first_reg($uid='')
	{
		$task=$this->get_task_one(1);
		if ($task)
		{
			$uid=intval($uid);
			$today=strtotime('today');
			$map['uid']=$uid;
			$map['tid']=$task['id'];
			$Log_task = M('Log_task') -> where($map)->limit(1)->find();
			if (empty($Log_task))
			{
				$this->fulfill_task($task['id'],$uid);
				if ($task['gold']>0)
				{
				D('User')->edit_user_gold(1,$uid,$task['gold'],'注册赠送');
				}
				if ($task['exp']>0)
				{
				D('User')->edit_user_gold(2,$uid,$task['exp'],'注册赠送');
				}
			}
		}
	}
	//发布问题
	public function exe_add_question($uid='')
	{
		$task=$this->get_task_one(8);
		if ($task)
		{
				$this->fulfill_task($task['id'],$uid);
				if ($task['gold']>0)
				{
				D('User')->edit_user_gold(1,$uid,$task['gold'],'发表问题获得奖励');
				}
				if ($task['exp']>0)
				{
				D('User')->edit_user_gold(2,$uid,$task['exp'], '发表问题获得奖励');
				}
		}
	}
	//发布文章
	public function exe_add_article($uid='')
	{
		$task=$this->get_task_one(10);
		if ($task)
		{
				$this->fulfill_task($task['id'],$uid);
				if ($task['gold']>0)
				{
				D('User')->edit_user_gold(1,$uid,$task['gold'],'分享文章获得奖励');
				}
				if ($task['exp']>0)
				{
				D('User')->edit_user_gold(2,$uid,$task['exp'], '分享文章获得奖励');
				}
		}
	}
	//发表回复
	public function exe_reply_question($uid='')
	{
		$task=$this->get_task_one(9);
		if ($task)
		{
				$this->fulfill_task($task['id'],$uid);
				if ($task['gold']>0)
				{
				D('User')->edit_user_gold(1,$uid,$task['gold'],'回答问题获得奖励');
				}
				if ($task['exp']>0)
				{
				D('User')->edit_user_gold(2,$uid,$task['exp'], '回答问题获得奖励');
				}
		}
	}
	//采纳答案，提问人获得奖励
	public function exe_adoption_answer($uid='')
	{
		$task=$this->get_task_one(12);
		if ($task)
		{
				$this->fulfill_task($task['id'],$uid);
				if ($task['gold']>0)
				{
				D('User')->edit_user_gold(1,$uid,$task['gold'],'采纳答案系统奖励');
				}
				if ($task['exp']>0)
				{
				D('User')->edit_user_gold(2,$uid,$task['exp'], '采纳答案系统奖励');
				}
		}
	}
	//采纳答案，答题人获得奖励
	public function exe_reply_adoption($uid='')
	{
		$task=$this->get_task_one(11);
		if ($task)
		{
				$this->fulfill_task($task['id'],$uid);
				if ($task['gold']>0)
				{
				D('User')->edit_user_gold(1,$uid,$task['gold'],'答案被采纳系统奖励');
				}
				if ($task['exp']>0)
				{
				D('User')->edit_user_gold(2,$uid,$task['exp'], '答案被采纳系统奖励');
				}
		}
	}
	//完善基本资料
	public function exe_user_specialty($uid='')
	{
		$task=$this->get_task_one(2);
		$chenk=$this->chenk_task($uid,2);
		if ($task && empty($chenk))
		{
				$this->fulfill_task($task['id'],$uid);
				if ($task['gold']>0)
				{
				D('User')->edit_user_gold(1,$uid,$task['gold'],'完善基本资料');
				}
				if ($task['exp']>0)
				{
				D('User')->edit_user_gold(2,$uid,$task['exp'], '完善基本资料');
				}
		}
	}
	//上传会员头像
	public function exe_user_avatar($uid='')
	{
		$task=$this->get_task_one(3);
		$chenk=$this->chenk_task($uid,3);
		if ($task && empty($chenk))
		{
				$this->fulfill_task($task['id'],$uid);
				if ($task['gold']>0)
				{
				D('User')->edit_user_gold(1,$uid,$task['gold'],'上传头像');
				}
				if ($task['exp']>0)
				{
				D('User')->edit_user_gold(2,$uid,$task['exp'], '上传头像');
				}
		}
	}
	//增加完成任务日志
	public function fulfill_task($taskid='',$uid='')
	{
		$data['tid']      =intval($taskid);
		$data['uid']      =intval($uid);
		$data['addtime']  =time();
		$data['entrance'] = C('UU_LOG_ENTRANCE');
		M("LogTask")->add($data);
		return true;
	}
	//获取一个有效任务
	public function get_task_one($id='',$dispay='1')
	{
		$map['id']=intval($id);
		$map['is_display']=1;
		$task= $this -> where($map)->find();
		return $task;
	}
	//检测任务是否完成
	public function chenk_task($uid='',$tid='')
	{
		$map['uid']=intval($uid);
		$map['tid']=intval($tid);
		$task= M("LogTask") -> where($map)->find();
		return $task;
	}
 
}
?>