<?php
/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--	Application Market Member
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */

function funcMarketMemberAddProcess($params){
	global $configs;
	global $connDB;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());

	$memberRS = funcMarketMemberRead(array('m_member_mobile' => $params['m_member_mobile']));
	if(!$memberRS['success']){
		$returns = funcMarketMemberAdd($params);
		if($returns['success']){
			/* 同步至后台用户 */
		}
	}else{
		$returns['errorcode'] = "300";
	}	

	$returns['msg'] = $configs['errors'][$returns['errorcode']];
	return $returns;
}
function funcMarketMemberAdd($params){
	global $configs;
	global $connDB;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());

	/* 验证 */

	/* 写入 */
	$inserts = array();
	if(isset($params['join_member_m_site_id']))$inserts['join_member_m_site_id'] = $params['join_member_m_site_id'];
	if(isset($params['join_super_m_member_id']))$inserts['join_super_m_member_id'] = $params['join_super_m_member_id'];
	if(isset($params['join_member_role_id']))$inserts['join_member_role_id'] = $params['join_member_role_id'];
	if(isset($params['join_member_core_id']))$inserts['join_member_core_id'] = $params['join_member_core_id'];
	if(isset($params['join_member_admin_id']))$inserts['join_member_admin_id'] = $params['join_member_admin_id'];
	if(isset($params['m_member_status']))$inserts['m_member_status'] = $params['m_member_status'];
	if(isset($params['m_member_category']))$inserts['m_member_category'] = $params['m_member_category'];
	if(isset($params['m_member_real_name']))$inserts['m_member_real_name'] = $params['m_member_real_name'];
	if(isset($params['m_member_mobile']))$inserts['m_member_mobile'] = $params['m_member_mobile'];
	if(isset($params['m_member_is_teamleader']))$inserts['m_member_is_teamleader'] = $params['m_member_is_teamleader'];
	if(isset($params['m_member_is_superadmin']))$inserts['m_member_is_superadmin'] = $params['m_member_is_superadmin'];
	if(isset($params['m_member_remark']))$inserts['m_member_remark'] = $params['m_member_remark'];
	if(isset($params['m_member_extend_json']))$inserts['m_member_extend_json'] = $params['m_member_extend_json'];
	$inserts['m_member_addtimes'] = (isset($params['m_member_addtimes']) && $params['m_member_addtimes']) ? $params['m_member_addtimes'] : time();
	$sql = executeSQL($inserts, array('mode' => "insert", 'table' => "yx_market_member"));
    $idx = execSQL($sql, "insertid");
	if($idx){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
        $returns['data'] = array('m_member_id' => $idx);
	}

	$returns['msg'] = $configs['errors'][$returns['errorcode']] . (($returns['msg']) ? $returns['msg'] : '');
	return $returns;
}
function funcMarketMemberUpdProcess($params){
	global $configs;
	global $connDB;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());


	$returns = funcMarketMemberUpd($params);

	$returns['msg'] = $configs['errors'][$returns['errorcode']];
	return $returns;
}
function funcMarketMemberUpd($params){
	global $configs;
	global $connDB;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());

	/* 验证 */


	/* 写入 */
	$data = array();
	if(isset($params['join_member_m_site_id']))$data['join_member_m_site_id'] = $params['join_member_m_site_id'];
	if(isset($params['join_super_m_member_id']))$data['join_super_m_member_id'] = $params['join_super_m_member_id'];
	if(isset($params['join_member_role_id']))$data['join_member_role_id'] = $params['join_member_role_id'];
	if(isset($params['join_member_core_id']))$data['join_member_core_id'] = $params['join_member_core_id'];
	if(isset($params['join_member_admin_id']))$data['join_member_admin_id'] = $params['join_member_admin_id'];
	if(isset($params['m_member_status']))$data['m_member_status'] = $params['m_member_status'];
	if(isset($params['m_member_category']))$data['m_member_category'] = $params['m_member_category'];
	if(isset($params['m_member_real_name']))$data['m_member_real_name'] = $params['m_member_real_name'];
	if(isset($params['m_member_mobile']))$data['m_member_mobile'] = $params['m_member_mobile'];
	if(isset($params['m_member_is_teamleader']))$data['m_member_is_teamleader'] = $params['m_member_is_teamleader'];
	if(isset($params['m_member_is_superadmin']))$data['m_member_is_superadmin'] = $params['m_member_is_superadmin'];
	if(isset($params['m_member_remark']))$data['m_member_remark'] = $params['m_member_remark'];
	if(isset($params['m_member_extend_json']))$data['m_member_extend_json'] = $params['m_member_extend_json'];
	if(isset($params['m_member_addtimes']))$data['m_member_addtimes'] = $params['m_member_addtimes'];

	if(!isset($params['wheres'])){
		$params['wheres'] = "m_member_id = " . $params['m_member_id'];
	}

	$sql = executeSQL($data, array('mode' => "update", 'table' => "yx_market_member", 'where' => $params['wheres']));
	if(execSQL($sql)){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
	}

	$returns['msg'] = $configs['errors'][$returns['errorcode']] . (($returns['msg']) ? $returns['msg'] : '');
	return $returns;
}
function funcMarketMemberDelProcess($params){
	global $configs;
	global $connDB;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array());


	$returns = funcMarketMemberDel($params);

	$returns['msg'] = $configs['errors'][$returns['errorcode']] . (($returns['msg']) ? $returns['msg'] : '');
	return $returns;
}
function funcMarketMemberDel($params){
	global $configs;
	global $connDB;

	$returns = array('success' => false, 'errorcode' => '400', 'data' => array(), 'page' => array());

	/* 验证 */

	/* 执行 */	
	$sql  = "delete from yx_market_member ";

	if(!isset($params['wheres'])){
		$sql .= "where m_member_id = " . $params['m_member_id'];
	}else{
		$sql .= "where " . $params['wheres'];
	}
	if(execSQL($sql)){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
	}
	$returns['msg'] = $configs['errors'][$returns['errorcode']] . (($returns['msg']) ? $returns['msg'] : '');
	return $returns;
}
function funcMarketMemberRead($params){
	global $configs;
	global $connDB;

	$returns = array('success' => false, 'errorcode' => '401', 'data' => array(), 'page' => array());

	$sql  = "select a.*, b.* from yx_market_member a ";
	$sql .= "left outer join yx_market_site b on a.join_member_m_site_id = b.m_site_id ";

	/* 查询条件 */
	if(isset($params['m_member_id']) && $params['m_member_id']){
		$wheres = " m_member_id = " . $params['m_member_id'];
	}else{
		$wheres = " (m_member_id > 0 or m_member_id is null)";
	}

	if(isset($params['join_member_m_site_id'])){
		$wheres .= " and (join_member_m_site_id = " . $params["join_member_m_site_id"] . ")";
	}

	if(isset($params['join_super_m_member_id'])){
		$wheres .= " and (join_super_m_member_id = " . $params["join_super_m_member_id"] . ")";
	}

	if(isset($params['join_member_role_id'])){
		$wheres .= " and (join_member_role_id = " . $params["join_member_role_id"] . ")";
	}

	if(isset($params['join_member_core_id'])){
		$wheres .= " and (join_member_core_id = '" . $params['join_member_core_id'] . "')";
	}

	if(isset($params['join_member_admin_id'])){
		$wheres .= " and (join_member_admin_id = " . $params["join_member_admin_id"] . ")";
	}

	if(isset($params['m_member_status'])){
		$wheres .= " and (m_member_status = '" . $params['m_member_status'] . "')";
	}

	if(isset($params['m_member_category'])){
		$wheres .= " and (m_member_category = '" . $params['m_member_category'] . "')";
	}

	if(isset($params['m_member_real_name'])){
		$wheres .= " and (m_member_real_name = '" . $params['m_member_real_name'] . "')";
	}

	if(isset($params['m_member_mobile'])){
		$wheres .= " and (m_member_mobile = '" . $params['m_member_mobile'] . "')";
	}

	if(isset($params['m_member_is_teamleader'])){
		$wheres .= " and (m_member_is_teamleader = '" . $params['m_member_is_teamleader'] . "')";
	}

	if(isset($params['m_member_is_superadmin'])){
		$wheres .= " and (m_member_is_superadmin = '" . $params['m_member_is_superadmin'] . "')";
	}

	if(isset($params['m_member_remark'])){
		$wheres .= " and (m_member_remark = '" . $params['m_member_remark'] . "')";
	}

	if(isset($params['m_member_extend_json'])){
	}

	if(isset($params['search_key']) && $params['search_key']){
		$wheres .= "(m_member_real_name like '%" . $params['search_key'] . "%' or m_member_mobile like '%" . $params['search_key'] . "%' or m_member_remark like '%" . $params['search_key'] . "%')";
	}

	/* 外部其他条件 */
	if(isset($params['wheres']) && $params['wheres']){
		$wheres .= " and " . $params['wheres'];
	}

	/* 排序设置 */
	if(isset($params['orderby']) && $params['orderby']){
		$orders = " order by " . $params['orderby'];
	}else{
		$orders = " order by m_member_id desc";
	}

	/* 监测并获取规范数据 */
	$pagecount       = (isset($params['pagecount']) && $params['pagecount']) ? $params['pagecount'] : $configs['database']['config']['pagecount'];
	$returns['page'] = doPage($sql, $wheres, $pagecount);
	if($returns['page']['counts'] > 0){
		/* 当前页码 */
		$returns['page']['current'] = (isset($params['pageno'])) ? $params['pageno'] : 1;
		$limits = doPageSQL(array('pageno' => $params['pageno'], 'pagecount' => $pagecount, 'limit' => $params['limit']));

		if($returns['page']['counts'] > 0){
			/* 具备数据 */
			$dataSQL = "select * from (" . $sql . ") data where " . $wheres . $orders . $limits;
	
			$returns['data'] = doSQL($dataSQL);
	
			$returns['data'] = funcFormatResultData(array('data' => $returns['data'], 'datafield' => 'm_member_status', 'datatimes' => 'm_member_addtimes'));
	
			$returns['success'] = true;
			$returns['errorcode'] = "200";
		}
	}
	//$returns['sql'] = "select * from (" . $sql . ") data where " . $wheres . $orders . $limits;

	$returns['msg'] = $configs['errors'][$returns['errorcode']];
	return $returns;
}


?>