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


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

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


	$returns = funcMarketProductAdd($params);

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

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

	/* 验证 */

	/* 写入 */
	$inserts = array();
	if(isset($params['m_product_service']))$inserts['m_product_service'] = $params['m_product_service'];
	if(isset($params['m_product_status']))$inserts['m_product_status'] = $params['m_product_status'];
	if(isset($params['m_product_category']))$inserts['m_product_category'] = $params['m_product_category'];
	if(isset($params['m_product_name']))$inserts['m_product_name'] = $params['m_product_name'];
	if(isset($params['m_product_remark']))$inserts['m_product_remark'] = $params['m_product_remark'];
	if(isset($params['m_product_once_json']))$inserts['m_product_once_json'] = $params['m_product_once_json'];
	if(isset($params['m_product_amount_json']))$inserts['m_product_amount_json'] = $params['m_product_amount_json'];
	if(isset($params['m_product_addition_json']))$inserts['m_product_addition_json'] = $params['m_product_addition_json'];
	if(isset($params['m_product_extend_json']))$inserts['m_product_extend_json'] = $params['m_product_extend_json'];
	$inserts['m_product_addtimes'] = (isset($params['m_product_addtimes']) && $params['m_product_addtimes']) ? $params['m_product_addtimes'] : time();
	$sql = executeSQL($inserts, array('mode' => "insert", 'table' => "yx_market_product"));
	$idx = execSQL($sql, "insertid");
	if($idx){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
		$returns['data'] = array('m_product_id' => $idx);
	}

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

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


	$returns = funcMarketProductUpd($params);

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

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

	/* 验证 */


	/* 写入 */
	$data = array();
	if(isset($params['m_product_service']))$data['m_product_service'] = $params['m_product_service'];
	if(isset($params['m_product_status']))$data['m_product_status'] = $params['m_product_status'];
	if(isset($params['m_product_category']))$data['m_product_category'] = $params['m_product_category'];
	if(isset($params['m_product_name']))$data['m_product_name'] = $params['m_product_name'];
	if(isset($params['m_product_remark']))$data['m_product_remark'] = $params['m_product_remark'];
	if(isset($params['m_product_once_json']))$data['m_product_once_json'] = $params['m_product_once_json'];
	if(isset($params['m_product_amount_json']))$data['m_product_amount_json'] = $params['m_product_amount_json'];
	if(isset($params['m_product_addition_json']))$data['m_product_addition_json'] = $params['m_product_addition_json'];
	if(isset($params['m_product_extend_json']))$data['m_product_extend_json'] = $params['m_product_extend_json'];
	if(isset($params['m_product_addtimes']))$data['m_product_addtimes'] = $params['m_product_addtimes'];

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

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

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


	$returns = funcMarketProductDel($params);

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

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

	/* 验证 */

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

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

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

	$sql = "select * from yx_market_product";

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

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

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

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

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

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

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

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

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

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

	if(isset($params['search_key']) && $params['search_key']){
		$wheres .= "(m_product_name like '%" . $params['search_key'] . "%' or m_product_service like '%" . $params['search_key'] . "%' or m_product_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_product_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;
}

?>