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


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

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


	$returns = funcMarketConsumeAdd($params);

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

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

	/* 写入 */
	$inserts = array();
	if(isset($params['join_consume_m_product_id']))$inserts['join_consume_m_product_id'] = $params['join_consume_m_product_id'];
	if(isset($params['join_consume_m_customer_id']))$inserts['join_consume_m_customer_id'] = $params['join_consume_m_customer_id'];
	if(isset($params['join_consume_m_site_id']))$inserts['join_consume_m_site_id'] = $params['join_consume_m_site_id'];
	if(isset($params['join_consume_m_member_id']))$inserts['join_consume_m_member_id'] = $params['join_consume_m_member_id'];
	if(isset($params['join_consume_operate_m_member_id']))$inserts['join_consume_operate_m_member_id'] = $params['join_consume_operate_m_member_id'];
	if(isset($params['m_consume_status']))$inserts['m_consume_status'] = $params['m_consume_status'];
	if(isset($params['m_consume_category']))$inserts['m_consume_category'] = $params['m_consume_category'];
	if(isset($params['m_consume_card_num']))$inserts['m_consume_card_num'] = $params['m_consume_card_num'];
	if(isset($params['m_consume_times']))$inserts['m_consume_times'] = $params['m_consume_times'];
	if(isset($params['m_consume_num']))$inserts['m_consume_num'] = $params['m_consume_num'];
	if(isset($params['m_consume_order']))$inserts['m_consume_order'] = $params['m_consume_order'];
	if(isset($params['m_consume_remark']))$inserts['m_consume_remark'] = $params['m_consume_remark'];
	if(isset($params['m_consume_extend_json']))$inserts['m_consume_extend_json'] = $params['m_consume_extend_json'];
	if(isset($params['m_consume_addtimes']))$inserts['m_consume_addtimes'] = $params['m_consume_addtimes'];
	$sql = executeSQL($inserts, array('mode' => "insert", 'table' => "yx_market_consume"));
	$idx = execSQL($sql, "insertid");
	if($idx){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
		$returns['data'] = array('m_consume_id' => $idx);
	}

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

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


	$returns = funcMarketConsumeUpd($params);

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

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

	/* 写入 */
	$data = array();
	if(isset($params['join_consume_m_product_id']))$data['join_consume_m_product_id'] = $params['join_consume_m_product_id'];
	if(isset($params['join_consume_m_customer_id']))$data['join_consume_m_customer_id'] = $params['join_consume_m_customer_id'];
	if(isset($params['join_consume_m_site_id']))$data['join_consume_m_site_id'] = $params['join_consume_m_site_id'];
	if(isset($params['join_consume_m_member_id']))$data['join_consume_m_member_id'] = $params['join_consume_m_member_id'];
	if(isset($params['join_consume_operate_m_member_id']))$data['join_consume_operate_m_member_id'] = $params['join_consume_operate_m_member_id'];
	if(isset($params['m_consume_status']))$data['m_consume_status'] = $params['m_consume_status'];
	if(isset($params['m_consume_category']))$data['m_consume_category'] = $params['m_consume_category'];
	if(isset($params['m_consume_card_num']))$data['m_consume_card_num'] = $params['m_consume_card_num'];
	if(isset($params['m_consume_times']))$data['m_consume_times'] = $params['m_consume_times'];
	if(isset($params['m_consume_num']))$data['m_consume_num'] = $params['m_consume_num'];
	if(isset($params['m_consume_order']))$data['m_consume_order'] = $params['m_consume_order'];
	if(isset($params['m_consume_remark']))$data['m_consume_remark'] = $params['m_consume_remark'];
	if(isset($params['m_consume_extend_json']))$data['m_consume_extend_json'] = $params['m_consume_extend_json'];
	if(isset($params['m_consume_addtimes']))$data['m_consume_addtimes'] = $params['m_consume_addtimes'];

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

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

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


	$returns = funcMarketConsumeDel($params);

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

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

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

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

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

	$sql  = "select a.*, b.*, c.*, d.*, f.* from yx_market_consume a ";
	$sql .= "left outer join yx_market_product b on a.join_consume_m_product_id = b.m_product_id ";
	$sql .= "left outer join yx_market_customer c on a.join_consume_m_customer_id = c.m_customer_id ";
	$sql .= "left outer join yx_market_site d on a.join_consume_m_site_id = d.m_site_id ";
	$sql .= "left outer join yx_market_member f on a.join_consume_m_member_id = f.m_member_id ";

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

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

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

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

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

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

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

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

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

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

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

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

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

	/* 外部其他条件 */
	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_consume_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;
}


?>