<?php

// 圈子创建第一步
require_once ("../user_include.inc");

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

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$title = ParamUtil::getRequestString("title");//圈子名称
$needCheck = ParamUtil::getRequestBoolean("needCheck", false);//圈子名称
$longitude = ParamUtil::getRequestString("longitude");//用户位置的经度
$latitude = ParamUtil::getRequestString("latitude");//用户位置的纬度

$result = array();

//参数检查
if(empty($unionId) || empty($title) || empty($longitude) || empty($latitude)) {
	$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];

//是否已经有圈子
$param = array();
$param['owner_id'] = $userMst->id;
$param['delete_flg'] = false;
$circleCount = CircleDat::getListCount($param);
if($circleCount > 0) {
	$result["message"] = "只能创建一个圈子！";
	responseNG($result);
}

//调用腾讯LGS服务解析用户地址信息
$tmpUrl = '/ws/geocoder/v1?key=' . LBS_KEY . '&location=' . $latitude . ',' . $longitude . LBS_SECRET;
$md5Str = md5($tmpUrl);
$lbsUrl = 'https://apis.map.qq.com/ws/geocoder/v1?key=' . LBS_KEY . '&location=' . $latitude . ',' . $longitude . '&sig=' . $md5Str;
$lbsObj =  file_get_contents($lbsUrl);
$jsonObj = json_decode($lbsObj); //对JSON格式的字符串进行编码
$lbsArray = get_object_vars($jsonObj);//转换成数组
$resultObj = $lbsArray['result'];
//地理位置获取正常的情况下设定。 156：中国代码
if($lbsArray['status'] == "0" && $resultObj->ad_info->nation_code==156) {
	//保存到用户当前地理位置中
	$province = $resultObj->address_component->province;
	$city = $resultObj->address_component->city;
	$district = $resultObj->address_component->district;
}

//建立圈子
$circleDat = new CircleDat();
$circleDat->title = $title;
$circleDat->need_check = $needCheck;
$circleDat->member_count = 1;
$circleDat->longitude = $longitude;
$circleDat->latitude = $latitude;
$circleDat->province = $province;
$circleDat->city = $city;
$circleDat->district = $district;
$circleDat->owner_id = $userMst->id;
$circleDat->save();


//返回创建的圈子
$param = array();
$param['owner_id'] = $userMst->id;
$param['delete_flg'] = false;
$circleList = CircleDat::getList($param, "id", "desc", 0, 1);
if(!empty($circleList)) {
	$circleDat = $circleList[0];
	//同步插入圈子成员表
	$circleMemberDat = new CircleMemberDat();
	$circleMemberDat->circle_id = $circleDat->id;
	$circleMemberDat->user_id = $userMst->id;
	$circleMemberDat->name = $userMst->name;
	$circleMemberDat->save();
	
	//返回数据
	$result["message"] = "创建成功！";
	$result["circleDat"] = $circleDat;
	responseOK($result);
}

$result["message"] = "系统错误！";
responseNG($result);

ErrorLogger::doOutput("Compass...ajax_circle_new1.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;
}
?>