<?php
@header("Content-type: text/html; charset=utf-8");
require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'common'.DIRECTORY_SEPARATOR.'db.class.php');
require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'common'.DIRECTORY_SEPARATOR.'session_helper.php');
require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'wall'.DIRECTORY_SEPARATOR.'biaoqing.php');
require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'flag_model.php');
require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'common'.DIRECTORY_SEPARATOR.'function.php');

$action=$_GET['action'];
switch ($action){
	case 'setdesignated':
		setdesignated();
		break;
	case 'setband':
		setband();
		break;
	case 'getdesignated':
		getdesignated();
		break;
	
}
//设置内定
function setdesignated(){
	$openid=isset($_POST['openid'])?strval($_POST['openid']):'';
	$awardid=isset($_POST['awardid'])?intval($_POST['awardid']):0;
	$designated=isset($_POST['designated'])?intval($_POST['designated']):0;
	$from=isset($_POST['from'])?strval($_POST['from']):'';
	if($openid==''){
		echo returnmsg(-1,"用户openid格式不正确");
		return ;
	}
	if($awardid==0){
		echo returnmsg(-2,"奖品id不正确");
		return ;
	}
	if($from==''){
		echo returnmsg(-3,"没有选择活动类型");
		return ;
	}
	if($designated==0){
		//echo '{"errno":-3,"message":"内定状态不正确"}';
		return ;
	}
	$zjlist_m=new M('zjlist');
	$where='openid="'.$openid.'" and awardid='.$awardid;
	$zjlist=$zjlist_m->find($where);
	if(!empty($zjlist)){
		//修改现有的内定设置
		if($designated>1){
			$data=array('designated'=>$designated);
			$result=$zjlist_m->update($where,$data);
		}else{
			$result=$zjlist_m->delete($where);
		}
		
		if($result){
			echo returnmsg(1,"内定设置修改成功");
			return ;
		}else{
			echo returnmsg(-4,"内定设置修改失败");
			return ;
		}
	}else{
		if($designated==1){
			return ;
		}
		//新增内定设置
		$data=array('openid'=>$openid,'awardid'=>$awardid,'designated'=>$designated,'status'=>1,'verifycode'=>'','fromplug'=>$from);
		$result=$zjlist_m->add($data);
		if($result){
			echo returnmsg(2,"内定设置成功");
			return ;
		}else{
			echo returnmsg(-5,"内定设置失败");
			return ;
		}
	}
}
//获取已有的内定设置
function getdesignated(){
	$openid=isset($_POST['openid'])?strval($_POST['openid']):'';
	$view_zjlist_m=new M('view_zjlist');
	$where='openid="'.$openid.'" and designated>1';// and awardid='.$awardid;
	$view_zjlist=$view_zjlist_m->select($where);
	
	
	if(empty($view_zjlist)){
		// echo '{"code":0,"message":"没有数据"}';
		echo returnmsg(0,"没有数据");
		return;
	}
	
	$load=Loader::getInstance();
	$load->model('Plugs_model');
	$cjplugs=$load->plugs_model->getChoujiangPlug();

	$plugs=array();
	foreach($cjplugs as $v){
		$plugs[$v['name']]=$v['title'];
	}
	foreach($view_zjlist as $k=>$v){
		$view_zjlist[$k]['cjtype']=$plugs[$v['fromplug']];
	}

	echo returnmsg(1,"",$view_zjlist);
	// echo json_encode($data);
	return;
}
//设置审核状态和删除用户数据
function setband(){
	$openid=isset($_POST['openid'])?strval($_POST['openid']):'';
	$status=isset($_POST['status'])?intval($_POST['status']):0;
	if($openid==''){
		echo returnmsg(-1,"用户openid格式不正确");
		return;
	}
	if($status==0){
		echo returnmsg(-2,"设置的状态不正确");
		return;
	}
	if($status==3){
		deletesign();
		return;
	}

	// $data=array('status'=>$status);
	$flag_model=new Flag_model();
	$userinfo=$flag_model->getUserinfo($openid);
	unset($userinfo['id']);
	$userinfo['status']=$status;
	if($status==1 && empty($userinfo['signorder'])){
		$userinfo['signorder']=$flag_model->getMaxSignorder()+1;
	}
	$result=$flag_model->saveUserinfo($userinfo);

	if($result){
		echo returnmsg(1,"用户状态修改成功");
		return;
	}else{
		echo returnmsg(-3,"设用户状态修改失败");
		return;
	}
}

function deletesign(){
	$openid=isset($_POST['openid'])?strval($_POST['openid']):'';
	$flag_model=new Flag_model();
	$flag=$flag_model->getUserinfo($openid);
	
	//删除中奖信息
	require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'lottery_model.php');
	$lottery_model=new Lottery_model();
	$lottery_model->deleteUserRecord($openid);

	//删除红包信息
	$redpacket_users_m=new M('redpacket_users');
	$redpacket_users_m->update('`userid`='.$flag['id'],array('userid'=>null,'updated_at'=>null));
	$redpacket_orders_m=new M('redpacket_orders');
	$redpacket_orders_m->delete('`re_openid`="'.$openid.'"');
	$redpacket_order_return_m=new M('redpacket_order_return');
	$redpacket_order_return_m->delete('`re_openid`="'.$openid.'"');
	//删除摇一摇信息
	$shake_toshake_m=new M('shake_toshake');
	$shake_toshake_m->delete('`openid`="'.$openid.'"');
	
	//删除上墙数据
	require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'wall_model.php');
	$wall_model=new Wall_model();
	$wall_model->deleteWallMessageByOpenid($openid);
	//删除投票数据
	require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'vote_model.php');
	$vote_model=new Vote_model();
	$vote_model->deleteRecordByOpenid($openid);

	//删除数钱结果数据
	require_once(dirname(__FILE__) .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'shuqian_model.php');
	$shuqian_model=new Shuqian_model();
	$shuqian_model->deleteShuqianRecordByOpenid($openid);

	//删除幸运手机号
	$xingyunshoujihao_m=new M('xingyunshoujihao');
	$xingyunshoujihao_m->delete('`openid`="'.$openid.'"');
	//删除签到信息
	$flag_m=new M('flag');
	$flag_m->delete('`openid`="'.$openid.'"');
	echo returnmsg(2,"该用户删除成功");
	return ;
}