<?php
/**
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class PushMessageController extends BaseController{
	public function index(){
		$map['user_id']=array(array('GT','0'));
		$map['used_time']=0;
		$map['is_tixing']=0;
		$list=$this->lists('MemberBonus',$map);
		//echo '<pre>';	
		$date =strtotime("-7day");
		//print_r($list);
		foreach($list as $ke=>$va){
			if($list[$ke]['end_time'] >=$date && $list[$ke]['end_time'] <= NOW_TIME){
				$lists[$ke]=$va;	
			}
		}
		$this->assign('list',$lists);
		$this->display();
	}
	public function tixing(){
		$only=I('get.id');
		if($only){
			$push_info=M('MemberBonus')->alias('mb')->where('mb.bonus_id='.$only)->field('mb.*,m.mobile,m.username')->JOIN('LEFT JOIN __MEMBER__ m ON m.id=mb.user_id')->find();
			if($push_info['mobile'] && $push_info['min_goods_money']>0){
				$content1="[一小时]尊敬的".$push_info['username']."用户您好，您有一张满".$push_info['min_goods_money']."元减".$push_info['money']."元的红包即将到期，请到会员中心查看并尽快使用！";
				$content2="[一小时]尊敬的".$push_info['username']."用户您好，您有一张满".$push_info['min_goods_money']."元减".$push_info['money']."元的红包即将到期，请到<a href='{':U('Mobile/Member/bonus')'}'>会员中心</a>查看并尽快使用！";
				$sms=sendsms($push_info['mobile'],$content1);
				$wxsms=sendwxmsg($push_info['user_id'],$content2);
			}elseif($push_info['mobile'] && $push_info['min_goods_money']==0){
				$content1="[一小时]尊敬的".$push_info['username']."用户您好，您有一张无门槛".$push_info['money']."元的红包即将到期，请到会员中心查看并尽快使用！";
				$content2="[一小时]尊敬的".$push_info['username']."用户您好，您有一张无门槛".$push_info['money']."元的红包即将到期，请到<a href='{':U('Mobile/Member/bonus')'}'>会员中心</a>查看并尽快使用！";
				$sms=sendsms($push_info['mobile'],$content);
				$wxsms=sendwxmsg($push_info['user_id'],$content2);
			}elseif(!$push_info['mobile']){
				$this->error('该用户没有绑定手机号！',U('Admin/PushMessage/index'));
			}
			if($sms && $wxsms){
				$tixing['is_tixing']=1;
				$is_tixing=M('MemberBonus')->where('bonus_id='.$only)->save($tixing);
				$this->success('提醒成功！',U('Admin/PushMessage/index'));
			}else{
				$this->error('提醒失败！',U('Admin/PushMessage/index'));
			}
		}
		$info=I('post.');
		if($info){
			$i=0;
			foreach($info['id'] as $k=>$v){
				$push_info=M('MemberBonus')->alias('mb')->where('mb.bonus_id='.$v)->field('mb.*,m.mobile,m.username')->JOIN('LEFT JOIN __MEMBER__ m ON m.id=mb.user_id')->find();
				if($push_info['mobile'] && $push_info['min_goods_money']>0){
					$content1="[一小时]尊敬的".$push_info['username']."用户您好，您有一张满".$push_info['min_goods_money']."元减".$push_info['money']."元的红包即将到期，请到会员中心查看并尽快使用！";
					$content2="[一小时]尊敬的".$push_info['username']."用户您好，您有一张满".$push_info['min_goods_money']."元减".$push_info['money']."元的红包即将到期，请到<a href='{':U('Mobile/Member/bonus')'}'>会员中心</a>查看并尽快使用！";
					$sms=sendsms($push_info['mobile'],$content1);
					$wxsms=sendwxmsg($push_info['user_id'],$content2);
				}elseif($push_info['mobile'] && $push_info['min_goods_money']==0){
					$content1="[一小时]尊敬的".$push_info['username']."用户您好，您有一张无门槛".$push_info['money']."元的红包即将到期，请到会员中心查看并尽快使用！";
					$content2="[一小时]尊敬的".$push_info['username']."用户您好，您有一张无门槛".$push_info['money']."元的红包即将到期，请到<a href='{':U('Mobile/Member/bonus')'}'>会员中心</a>查看并尽快使用！";
					$sms=sendsms($push_info['mobile'],$content);
					$wxsms=sendwxmsg($push_info['user_id'],$content2);
				}elseif(!$push_info['mobile']){
					$this->error($push_info['username']."用户没有绑定手机号！",U('Admin/PushMessage/index'));
				}
				if($sms && $wxsms){
					$tixing['is_tixing']=1;
					$is_tixing[$i]=M('MemberBonus')->where('bonus_id='.$v)->save($tixing);
				}
				$i++;
			}
			if($is_tixing){
				$this->success('提醒成功！',U('Admin/PushMessage/index'));
			}else{
				$this->error('提醒失败！',U('Admin/PushMessage/index'));
			}
		}
	}
}
?>