<?php
/**
 * 验证手机号是否存在
 * @param string $mobile 手机号
 * @param string $type 查询类型（1会员 2店铺）
 * @return array
 */
/*function checkmobile($mobile,$type=1){
	$map = array();
	if($type == 1){
		$map['mobile'] = array('eq',$mobile);
		$id = M('Member')->where($map)->getField('id');
	}elseif($type == 2){
		$map['mobile'] = array('eq',$mobile);
		$id = M('ShopsAccount')->where($map)->getField('id');
	}
	return $id;
}*/
function send_care($id,$page=0,$pagesize=100){
	if(!$id)return false;
	$map['id'] = $id;
	$info = M('MemberCare')->where($map)->find();
	$map = array();
	$map['name'] = 'FIXED_SIGNATURE';
	$sign = M('Config')->where($map)->getField('value');
	//短信查询
	$smsclass = new \Common\Vendor\Sms('Hanchaosms');
	$ret =$smsclass->balance();//条数
	$sms_num = $ret['data'];
	if($info){
		if($info['type'] == 1){//节日类型
			//发送对象
			if($info['send_member'] == 1)$m_map['is_tmp']=0;	
			if($info['send_member'] == 2)$m_map['is_tmp']=1;
			
			//等级限制
			if($info['rank_limit'] != 0){$m_map['rank_id']=$info['rank_limit'];}
			
			$member_count = M('Member')->where($m_map)->count();
			if($sms_num < $member_count && $info['sms_message'] == 1){
				$return['status'] = 3;
				$return['msg'] = '当前短信条数不足，无法发送短信，请进入应用市场充值购买';
				return $return;
			}
			if($member_count>0){
				$member_list = M('Member')->where($m_map)->limit($page,$pagesize)->select();
				if($member_list){
					if($info['send_type'] == 1){
						foreach($member_list as $key=>$val){
							account_change($val['id'],$info['points'],4,13,'节日大派送');
							$u_name = idtoname($val['id'],'Member','real_name;username;nickname;mobile');
							$coupons_name='';
							$rep_arr=array('[关怀名称]'=>$info['festival_name'],'[会员名称]'=>$u_name,'[卡券名称]'=>$coupons_name,'[积分数量]'=>Intval($info['points']));
							$content=strtr($info['content'],$rep_arr);
							if($info['wx_message'] == 1){
								$code_data = array();
								$code_data['to'] = $val['id'];
								$code_data['user_name'] = $u_name;
								$code_data['goods_name'] = $info['points'].'积分';
								$code_data['send_time'] = date('Y-m-d H:i:s',NOW_TIME);
								$code_data['remark']=$content;
								sendbytheme($code_data,'MEMBER_CARE');
							}
							if($info['sms_message'] == 1){
								if(strpos($info['content'],'[') !== false){
									$content = $content.'【'.$sign.'】';
								}else{
									$content = $content.'退订回N'.'【'.$sign.'】';
								}
								$send_data['form'] = 1;
								$send_data['title'] = '';
								$send_data['content'] = $content;
								$send_data['type'] = 1;
								$send_data['is_yingxiao'] = 1;
								$send_data['addtime'] = NOW_TIME;
								$send_data['touid'] = $val['id'];
								$send_data['to'] = $val['mobile'];
								send_method(2,$send_data);
							}
						}unset($key,$val);
					}
					if($info['send_type'] == 2){
						kaquan($info,$member_list);
					}
					$return['id'] = $id;
					$return['status'] = 1;
					$return['count'] = $member_count;
					$return['page'] = $page+$pagesize;
					$jindu = $page/$pagesize/ceil($member_count/$pagesize)*100;
					$return['jindu'] = sprintf("%.2f",$jindu);
					return $return;
				}else{
					$m_care['id'] = $info['id'];
					$m_care['status'] = 1;
					$m_care['send_time'] = NOW_TIME;
					$m_care['message_num'] = $member_count;
					M('MemberCare')->save($m_care);
					$return['count'] = 0;
					$return['data'] = $member_count;
					$return['status'] = 2;
					return $return;
				}
				
			}else{
				$m_care['id'] = $info['id'];
				$m_care['status'] = 1;
				$m_care['send_time'] = NOW_TIME;
				$m_care['message_num'] = $member_count;
				M('MemberCare')->save($m_care);
				$return['count'] = 0;
				$return['data'] = $member_count;
				$return['status'] = 2;
				return $return;
			}	
			
		}
		if($info['type'] == 2){//生日
			if($info['send_birthday']>10){
				$me_map['birthday'] = array('like','%-'.$info['send_birthday'].'-%');
			}else{
				$me_map['birthday'] = array('like','%-0'.$info['send_birthday'].'-%');
			}
			$member_count = M('Member')->where($me_map)->count();
			if($sms_num < $member_count && $info['sms_message'] == 1){
				$return['status'] = 3;
				$return['msg'] = '当前短信条数不足，无法发送短信，请进入应用市场充值购买';
				return $return;
			}
			if($member_count>0){
				$member_list = M('Member')->where($me_map)->limit($page,$pagesize)->select();
				if($member_list){
					if($info['send_type'] == 1){
						foreach($member_list as $key=>$val){
							account_change($val['id'],$info['points'],4,13,'生日大派送');
							$u_name = idtoname($val['id'],'Member','real_name;username;nickname;mobile');
							$coupons_name='';
							$rep_arr=array('[关怀名称]'=>$info['festival_name'],'[会员名称]'=>$u_name,'[卡券名称]'=>$coupons_name,'[积分数量]'=>Intval($info['points']));
							$content=strtr($info['content'],$rep_arr);
							if($info['wx_message'] == 1){
								$code_data = array();
								$code_data['to'] = $val['id'];
								$code_data['user_name'] = $u_name;
								$code_data['goods_name'] = $info['points'].'积分';
								$code_data['send_time'] = date('Y-m-d H:i:s',NOW_TIME);
								$code_data['remark']=$content;
								sendbytheme($code_data,'MEMBER_CARE');
							}
							if($info['sms_message'] == 1){
								if(strpos($info['content'],'[') !== false){
									$content = $content.'【'.$sign.'】';
								}else{
									$content = $content.'退订回N'.'【'.$sign.'】';
								}
								$send_data['form'] = 1;
								$send_data['title'] = '';
								$send_data['content'] = $content;
								$send_data['type'] = 1;
								$send_data['addtime'] = NOW_TIME;
								$send_data['is_yingxiao'] = 1;
								$send_data['touid'] = $val['id'];
								$send_data['to'] = $val['mobile'];
								send_method(2,$send_data);
							}
						}unset($key,$val);
					}
					if($info['send_type'] == 2){
						kaquan($info,$member_list);
					}
					$return['id'] = $id;
					$return['count'] = $member_count;
					$return['status'] = 1;
					$return['page'] = $page+$pagesize;
					$jindu = $page/$pagesize/ceil($member_count/$pagesize)*100;
					$return['jindu'] = sprintf("%.2f",$jindu);
					return $return;
				}else{
					$m_care['id'] = $info['id'];
					$m_care['status'] = 1;
					$m_care['send_time'] = NOW_TIME;
					$m_care['message_num'] = $member_count;
					M('MemberCare')->save($m_care);
					$return['count'] = 0;
					$return['data'] = $member_count;
					$return['status'] = 2;
					return $return;
				}	
				
			}else{
				$m_care['id'] = $info['id'];
				$m_care['status'] = 1;
				$m_care['send_time'] = NOW_TIME;
				$m_care['message_num'] = $member_count;
				M('MemberCare')->save($m_care);
				$return['count'] = 0;
				$return['data'] = $member_count;
				$return['status'] = 2;
				return $return;
			}	
		}
		if($info['type'] == 3){
			if($info['rank_limit'] != 0){$m_map['rank_id']=$info['rank_limit'];}
			$member_count = M('Member')->where($m_map)->count();
			if($sms_num < $member_count && $info['sms_message'] == 1){
				$return['status'] = 3;
				$return['msg'] = '当前短信条数不足，无法发送短信，请进入应用市场充值购买';
				return $return;
			}
			if($member_count>0){
				$member_list = M('Member')->where($m_map)->limit($page,$pagesize)->select();
				if($member_list){
					foreach($member_list as $key=>$val){
						
						$u_name = idtoname($val['id'],'Member','real_name;username;nickname;mobile');
						$coupons_name='';
						$rep_arr=array('[关怀名称]'=>$info['festival_name'],'[会员名称]'=>$u_name,'[卡券名称]'=>$coupons_name,'[积分数量]'=>Intval($info['points']));
						$content=strtr($info['content'],$rep_arr);
						if($info['sms_message'] == 1){
							if(strpos($info['content'],'[') !== false){
								$content = $content.'【'.$sign.'】';
							}else{
								$content = $content.'退订回N'.'【'.$sign.'】';
							}
							$send_data['form'] = 1;
							$send_data['title'] = $info['festival_name'];
							$send_data['content'] = $content;
							$send_data['type'] = 1;
							$send_data['is_yingxiao'] = 1;
							$send_data['addtime'] = NOW_TIME;
							$send_data['touid'] = $val['id'];
							$send_data['to'] = $val['mobile'];
							send_method(2,$send_data);
						}
					}unset($key,$val);
					$return['id'] = $id;
					$return['status'] = 1;
					$return['count'] = $member_count;
					$return['page'] = $page+$pagesize;
					return $return;
				}else{
					$m_care['id'] = $info['id'];
					$m_care['status'] = 1;
					$m_care['send_time'] = NOW_TIME;
					$m_care['message_num'] = $member_count;
					M('MemberCare')->save($m_care);
					$return['count'] = 0;
					$return['data'] = $member_count;
					$return['status'] = 2;
					return $return;
				}
				
			}else{
				$m_care['id'] = $info['id'];
				$m_care['status'] = 1;
				$m_care['send_time'] = NOW_TIME;
				$m_care['message_num'] = $member_count;
				M('MemberCare')->save($m_care);
				$return['count'] = 0;
				$return['data'] = $member_count;
				$return['status'] = 2;
				return $return;
			}
		}
	}
}

function kaquan($info,$member_list){
	$map = array();
	$map['name'] = 'FIXED_SIGNATURE';
	$sign = M('Config')->where($map)->getField('value');
	$coupons = M('Coupons')->where('id='.$info['coupons_id'])->find();
	$add_data['coupons_id'] = $coupons['id'];
	$add_data['card_id'] = $coupons['card_id'];
	$add_data['rank_id'] = $coupons['rank_id'];
	$add_data['discount'] = $coupons['discount'];
	$add_data['reduce_cost'] = $coupons['reduce_cost'];
	$add_data['least_cost'] = $coupons['least_cost'];
	$add_data['begin_timestamp'] = $coupons['begin_timestamp'];
	$add_data['end_timestamp'] = $coupons['end_timestamp'];
	$add_data['add_time'] = time();
	foreach($member_list as $key=>$val){
		$add_data['uid'] = $val['id'];
		$add_data['code'] = base_order().$val['id'];
		$add = M('CouponsGet')->add($add_data);
		if($add)M('Coupons')->where('id='.$info['coupons_id'])->setDec('quantity',1);
		if($val['wxid'])$wxid[] = $val['wxid'];
		$goods_name = idtoname($info['coupons_id'],'Coupons','title');
		
		$u_name = idtoname($val['id'],'Member','real_name;username;nickname;mobile');
		$rep_arr=array('[关怀名称]'=>$info['festival_name'],'[会员名称]'=>$u_name,'[卡券名称]'=>$goods_name,'[积分数量]'=>Intval($info['points']));
		$content=strtr($info['content'],$rep_arr);
		if($info['wx_message'] == 1){
			if(strpos($info['content'],'[') !== false){
				$content = $content.'【'.$sign.'】';
			}else{
				$content = $content.'退订回N'.'【'.$sign.'】';
			}
			$code_data = array();
			$code_data['to'] = $val['id'];
			$code_data['user_name'] = $val['nickname'];
			$code_data['goods_name'] =$goods_name;
			$code_data['send_time'] = date('Y-m-d H:i:s',NOW_TIME);
			$code_data['remark']=$content;
			sendbytheme($code_data,'MEMBER_CARE');
		}
		if($info['sms_message'] == 1){
			$send_data['form'] = 1;
			$send_data['title'] = '';
			$send_data['content'] = $content;
			$send_data['type'] = 1;
			$send_data['is_yingxiao'] = 1;
			$send_data['addtime'] = NOW_TIME;
			$send_data['touid'] = $val['id'];
			$send_data['to'] = $val['mobile'];
			send_method(2,$send_data);
		}
	}unset($key,$val);
	//微信发券
	$Weixin = new \Common\Vendor\WeixinCoupon();
	if(count($wxid)){
		$wxcard = $Weixin->wxcard($wxid,$add_data['card_id']);
	}
	
}
function change_status($name,$id,$status=0){
	if(!$name||!$id)return false;
	$map = array();
	$map['id'] = $id;
	$map['status'] = $status;
	$ret = M($name)->save($map);
	return $ret;
}
function calculation($money=0,$uid){
	$return=array('status'=>1,'msg'=>'','data'=>array('actual_money'=>0.00,'points'=>0));
	if(!$uid)return $return;
	$map = array();
	$map['id'] = $uid;
	$rank_id = M('Member')->where($map)->getField('rank_id');
	$map = array();
	$rank_limit = "'0,".$rank_id."'";
	$map['rank_limit'] = array('IN',$rank_limit);
	$map['status'] = 1;
	$map['consumption_type'] = array('neq',2);
	$map['money_limit'] = array('elt',$money);
	$map['consumption_type'] = 1;
	$info = M('ManypointsRule')->where($map)->field(true)->order('money_limit desc')->find();
	$sell_points=C("RECHARGE_SELL_INTEGRAL_RATE");
	if($info){
		$return['data']['actual_money'] = $money;
		$return['data']['points'] = Intval($money*$sell_points/100*$info['give_num']);
	}else{
		$return['data']['actual_money'] = $money;
		$return['data']['points'] = Intval($money*$sell_points/100);
	}
	$map['consumption_type'] = 3;
	$info_m = M('ManypointsRule')->where($map)->field(true)->order('money_limit desc')->find();
	if($info_m){
		$return['data']['actual_money'] = $money+$info_m['give_num'];
	}
	return $return;
}
/**
  * 调用广告app用
  * @param  $adid 广告位ID
  * @param  $catid 分类id
  * @param  $cacheHour 缓存小时数
  * @param  $isjs  是否是js调用
  * @param  $poster_num  广告数
  * @return string
  */
function showadsapp($adid,$catid='0',$cacheHour=12,$isjs='0',$poster_num=''){
    if(empty($adid))return false;
    $cityid=0;//用于多城市扩展
    $cachefile='adPoster'.$adid.$isjs.$catid.$cityid.'app';
    $cachecontent=S($cachefile);
    if($cachecontent && $cacheHour>0){return $cachecontent;}
    else{
        $map=array();$adcontent='';
        $map['status']=1;
        if($adid)$map['id']=$adid;
        $info=M('PosterPosition')->where($map)->field(true)->find();
        $where=array();
        $where['position_id'] =$info['id'];
        $where['status'] =1;
        if(!empty($catid)){GLOBAL $GoodsCategory;$catson=explode(',',$GoodsCategory[$catid]['allson']);$where['catid'] =array('in',$catson);}
        $where['_string'] = '(start_time <'.NOW_TIME.' OR start_time =0) and (end_time >'.NOW_TIME.' OR end_time =0)';
        if(!empty($poster_num)){$info['number']=$poster_num;}
        $ad_list=M('Poster')->where($where)->field(true)->order('sort desc,id desc')->limit($info['number'])->select();
        if(!empty($ad_list)){
            $appad_list = array();
            foreach($ad_list as $k => $v){
                $appad_list[$k]['code'] = thumb($v['code'],$info['width'],$info['height']);
                $appad_list[$k]['urldata']['type'] = 'error';
                if(str_exists($v['url'],'http://')||str_exists($v['url'],'https://')){
                    $appad_list[$k]['urldata']['type'] = 'http';
                    $appad_list[$k]['urldata']['url'] = $v['url'];
                    continue;
                }
                if(str_exists($v['url'],'url:')){
                    $arr=explode(':',$v['url']);
                    $urlrules=M("UrlRules")->where('id='.$arr[1])->find();
                    if(empty($urlrules))continue;
                    $is_url=array();
                    $is_url[]='module='.$urlrules['module'];
                    if($urlrules['is_module'] == 1 && !empty($arr[2])){
                        //$is_url[]=$urlrules['urlparam'].'='.$arr[2];
                        $is_url[]='id='.$arr[2];//转化为app统一的id
                    }
                    if($urlrules['is_param'] == 1 && !empty($arr[3])){
                        $is_url[]=$arr[3];//app只接收name和keyword，其它标识不解析
                    }
                    $isurl=implode('&',$is_url);//将数组拼接成字符串
                    $appad_list[$k]['urldata']['type'] = 'appurl';
                    $appad_list[$k]['urldata']['url'] = $isurl;
                }
            }
            $cachecontent = $appad_list;
        }
        S($cachefile,$cachecontent,60*60*$cacheHour);return $cachecontent;
    }
}	
?>