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


function funcMarketSiteAddProcess($params){
	global $configs;
	global $db;

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

	if(!$params['m_site_code']){
		$params['m_site_code'] = "S-" . funcComRandom("rand", array('num' => 8));
	}

	$siteRS = funcMarketSiteRead(array('wheres' => "(m_site_code = '" . $params['m_site_code'] . "' or m_site_name = '" . $params['m_site_name'] . "')"));

	if(!$siteRS['success']){
		$returns = funcMarketSiteAdd($params);
	}else{
		$returns['errorcode'] = "300";
	}	

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

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

	/* 验证 */

	/* 写入 */
	$inserts = array();
	if(isset($params['join_site_m_member_id']))$inserts['join_site_m_member_id'] = $params['join_site_m_member_id'];
	if(isset($params['m_site_status']))$inserts['m_site_status'] = $params['m_site_status'];
	if(isset($params['m_site_category']))$inserts['m_site_category'] = $params['m_site_category'];
	if(isset($params['m_site_code']))$inserts['m_site_code'] = $params['m_site_code'];
	if(isset($params['m_site_name']))$inserts['m_site_name'] = $params['m_site_name'];
	if(isset($params['m_site_city']))$inserts['m_site_city'] = $params['m_site_city'];
	if(isset($params['m_site_position']))$inserts['m_site_position'] = $params['m_site_position'];
	if(isset($params['m_site_remark']))$inserts['m_site_remark'] = $params['m_site_remark'];
	if(isset($params['m_site_extend_json']))$inserts['m_site_extend_json'] = $params['m_site_extend_json'];
	$inserts['m_site_addtimes'] = (isset($params['m_site_addtimes']) && $params['m_site_addtimes']) ? $params['m_site_addtimes'] : time();
	$sql = executeSQL($inserts, array('mode' => "insert", 'table' => "yx_market_site"));
	$idx = execSQL($sql, "insertid");
	if($idx){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
		$returns['data'] = array('m_site_id' => $idx);
	}

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

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


	$returns = funcMarketSiteUpd($params);

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

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

	/* 验证 */


	/* 写入 */
	$data = array();
	if(isset($params['join_site_m_member_id']))$data['join_site_m_member_id'] = $params['join_site_m_member_id'];
	if(isset($params['m_site_status']))$data['m_site_status'] = $params['m_site_status'];
	if(isset($params['m_site_category']))$data['m_site_category'] = $params['m_site_category'];
	if(isset($params['m_site_code']))$data['m_site_code'] = $params['m_site_code'];
	if(isset($params['m_site_name']))$data['m_site_name'] = $params['m_site_name'];
	if(isset($params['m_site_city']))$data['m_site_city'] = $params['m_site_city'];
	if(isset($params['m_site_position']))$data['m_site_position'] = $params['m_site_position'];
	if(isset($params['m_site_remark']))$data['m_site_remark'] = $params['m_site_remark'];
	if(isset($params['m_site_extend_json']))$data['m_site_extend_json'] = $params['m_site_extend_json'];
	if(isset($params['m_site_addtimes']))$data['m_site_addtimes'] = $params['m_site_addtimes'];

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

	$sql = executeSQL($data, array('mode' => "update", 'table' => "yx_market_site", '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 funcMarketSiteDelProcess($params){
	global $configs;
	global $db;

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


	$returns = funcMarketSiteDel($params);

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

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

	/* 验证 */

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

	if(!isset($params['wheres'])){
		$sql .= "where m_site_id = " . $params['m_site_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 funcMarketSiteRead($params){
	global $configs;
	global $db;

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

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

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

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

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

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

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

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

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

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

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

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

	if(isset($params['search_key']) && $params['search_key']){
		$wheres .= "(m_site_name like '%" . $params['search_key'] . "%' or m_site_position like '%" . $params['search_key'] . "%' or m_site_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_site_id desc";
	}

	/* 监测并获取规范数据 */
	$pagecount       = (isset($params['pagecount']) && $params['pagecount']) ? $params['pagecount'] : $configs['database']['config']['pagecount'];
	$returns['page'] = doPage($sql, $wheres);
	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' => '', 'datatimes' => ''));
	
			$returns['success'] = true;
			$returns['errorcode'] = "200";
		}
	}
	//$returns['sql'] = "select * from (" . $sql . ") data where " . $wheres . $orders . $limits;

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

?>