<?php
/**全民发券
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <xiaol@sina.cn>
 */
namespace Mobile\Model;
use Think\Model;
class TuokeModel extends Model
{
    protected $tableName = 'receipt_draw';
	// 全民发券
	public function tuoke_fq($act_id){
		$map = array();
		$map['class'] = 1;
		$map['act_id'] = $act_id;
        $data = M('ReceiptDraw')->where($map)->field(true)->find();
        if($data['end_time'] <= time()){//判断活动是否结束
            $data['status'] = 1;//未结束
        }else{
            $data['status'] = 0;//结束
        }
        // 推广记录
        $memberinfo = checkuserauthtype();
        if($data['event_confine'] == 0){//新用户
            $where['receive_id'] = array('eq','0');
            $status = 0;
        }else{
            $status = 1;
        }
        $uid = $memberinfo['id'];
        $map=array();
        $map['uid'] = $uid;
        $map['act_id'] = $act_id;
        $count = M('Participation')->where($map)->field(true)->find();
        $where['send_id'] = $uid;
        $where['add_time'] = array(array('gt',$count['time']),array('lt',$data['end_time']));
        $records = M('CouponsDonation')->where($where)->field(true)->group('receive_wxid')->select();
        if($status == 0){
            $aging = $data['aging']*60;//时效转换为秒
            foreach ($records as $k => $v) {
                $reg_time = idtoname($v['receive_id'],'Member','reg_time');//注册时间
                if($reg_time){
                    $time[$v['receive_id']] = $reg_time+$aging."--".time();
                    if($reg_time+$aging >= NOW_TIME){
                        $record[] = $v;
                    }
                }else{
                    $record[] = $v;
                }
            }
        }else{
            $record = $records;
        }
        // 判断是否参与该活动
        if($count){
            $return['record'] = $record;
        }else{
            $data['canyu'] = 0;
            $return['record'] = 0;
        }
        $data['start_time'] = date('Y-m-d',$data['start_time']);
        $data['end_time'] = date('Y-m-d',$data['end_time']);
        $return['data'] = $data;
        // 规则
        $map = array();
        $map['act_id'] = $act_id;
        $prize = M('Prize')->where($map)->field(true)->select();
        $return['prize'] = $prize;
        return $return;
	}
	// 参与活动
	public function can_award($act_id){
		$map = $data = array();
		$memberinfo = checkuserauthtype();
        $data['uid'] = $map['uid'] = $memberinfo['id'];
        $data['act_id'] = $map['act_id'] = $act_id;
        $status = M('Participation')->where($map)->count();
        if($status == 0){
            $data['time'] = time();
            M('Participation')->add($data);
            $return['status'] = 0;
        }else{
            $return['status'] = 1;
        }
        return $return;
	}
	// 立即领取
	public function get_award($act_id){
		$map = array();
		$map['act_id'] = $act_id;
		$memberinfo = checkuserauthtype();
        $map['uid'] = $memberinfo['id'];
        $status = M('Participation')->where($map)->getfield('get_status');
        if($status == 1){
            $return['status'] = 0;
            $return['msg'] = '已领取！';
        }else{
            $Weixin = new \Common\Vendor\WeixinCoupon();
            $get_award = $Weixin->get_award($map);
            $s_data['get_status'] = 1;
            if($get_award)M('Participation')->where($map)->save($s_data);
            if($get_award['status']	 == 1){
                $return['status'] = 1;
                $return['msg'] = $get_award['data']['prize_name'].'领取成功！'.$get_award['mag'];
            }else{
                $return['status'] = 0;
                $return['msg'] = $get_award['mag'];
            }          
        }
        return $return;
	}
    /**
     *全民拓客
     */
    public function member_invite(){
        //全民拓客
        $res = M('Config')->field('value')->where("name='AFFILIATE'")->find();
        $val = unserialize($res['value']);
        $return['val'] = $val;
        $return['item'] = $val['item'];
        //推广
        $data = D('Common/Member')->extension();
        $return['data'] = $data;
        // 已参与的活动
        $memberinfo = checkuserauthtype();
        $map = array();
        $map['uid'] = $memberinfo['id'];
        $participation = M('Participation')->where($map)->field(true)->select();
        foreach ($participation as $k => $v) {
            $get_status[$v['act_id']] = $v['get_status'];
        }
        // 全民发券
        $map = array();
        $map['status'] = 1;
        $map['class'] = 1;
        $quans = M('ReceiptDraw')->where($map)->field('id,draw_name,start_time,end_time,logo_url')->select();
        foreach ($quans as $k => $v) {
            if($get_status[$v['id']] == 0 && isset($get_status[$v['id']])){//已领取奖励的不在列表显示
                $quan[$k] = $v;
            }elseif(!isset($get_status[$v['id']]) && $v['end_time'] >= time()){
                $quan[$k] = $v;
            }
        }
        $return['quan'] = $quan;
        return $return;
    }
    /**
     *判断登陆
     */
    public function status($method){
        $memberinfo = checkuserauthtype();
        $map = array();
        $map['id'] = $memberinfo['id'];
        $member = M('Member')->where($map)->getfield('mobile');
        $mobile = $member;
        if($mobile){//会员
            $return['status'] = 1;
            $return['url'] = U('Mobile/Extension/'.$method);
        }else{//粉丝
            $return['status'] = 0;
            $return['info'] = '您还不是会员,请先到会员中心领卡并完善资料';
            $return['url'] = U('Mobile/member/index');
        }
        return $return;
    }
    /**
     *微信卡券
     */
    public function get_coupons($param){
        $memberinfo = checkuserauthtype();
        if(empty($param['page'])){
            $param['page'] =1;
        }
        $uid = $memberinfo['id'];
        $map = array();
        $map['uid'] = $uid;
        $map['coupons_type'] = 1;
        $return['p'] =$param['page'];
        if($param['status'] == 2){
            $map['end_timestamp'] = array(array('lt',time())) ;
            $map['status'] = array('neq',1);
        }elseif($param['status'] == 1){
            $map['status'] = 1;
        }else{
            $map['end_timestamp'] = array(array('gt',time())) ;
            $map['status'] = $param['status'];
        }
        $order = 'add_time desc';
        $pagesize=empty($pagesize)?C('WEB_DEFAULT_PAGE'):$pagesize;
        $list = page_list('CouponsGet', $map, $order, true, $pagesize,'', $param['page']);
        if($list['count'] > $pagesize*$param['page']){
            $return['show'] = 1;
        }else{
            $return['show'] = 0;
        }
        if($list['list']){
            $return['status'] = 1;
            $return['data'] = $list['list'];
        }else{
            $return['status'] = 0;
        }
        $map = array();
        $map['uid'] = $uid;
        $map['coupons_type'] = 1;
        $map['end_timestamp'] = array(array('gt',time())) ;
        $map['status'] = 0;
        $return['num']['n_count'] = M('CouponsGet')->where($map)->count();
        $map['status'] = 1;unset($map['end_timestamp']);
        $return['num']['u_count'] = M('CouponsGet')->where($map)->count();
        $map['status'] = array('neq',1);$map['end_timestamp'] = array(array('lt',time()));;
        $return['num']['o_count'] = M('CouponsGet')->where($map)->count();
        return $return;
    }
	
}