<?php

// 指定删除圈子成员接口
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_delete_circle_member.php.php....Start.", 0);

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$userId = ParamUtil::getRequestNumber("userId", 0);//指定删除的用户ID
$circleId = ParamUtil::getRequestNumber("circleId", 0);//圈子id

$result = array();

//参数检查
if(empty($unionId) || empty($userId) || empty($circleId)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

//判断用户是否存在
$param = array();
$param['unionid'] = $unionId;
$param['delete_flg'] = false;
$userList = UserMst::getList($param,'id','desc', 0, 1);
if(empty($userList)) {
	$result["message"] = "用户不存在！";
	responseNG($result);
}

$userMst = $userList[0];

//判断圈子是否存在
$circleDat = CircleDat::getById($circleId);
if(empty($circleDat)) {
	$result["message"] = "圈子不存在！";
	responseNG($result);
}

//只有圈主才可以删除成员
if($circleDat->owner_id != $userMst->id) {
	$result["message"] = "只有圈主才可以删除！";
	responseNG($result);
}

//不可删除圈主
if($userMst->id == $userId) {
	$result["message"] = "不能删除圈主！";
	responseNG($result);
}

//删除成员处理
$param = array();
$param['circle_id'] = $circleDat->id;
$param['user_id'] = $userId;
$param['delete_flg'] = false;
$memberList = CircleMemberDat::getList($param, "id", "asc", 0, 1);
if(!empty($memberList)) {
	$memberList[0]->delete_flg = true;
	$memberList[0]->save();
	
	//圈子成员数 -1 
	$circleDat->member_count--;
	$circleDat->save();
}


//组装返回数据
$result["message"] = "删除成功！";

ErrorLogger::doOutput("Compass...ajax_delete_circle_member.php.php....End.", 0);

//返回结果
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}
?>