<?php
/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
--	Application Market Customer & Customer_Product & Customer_Update
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- */

/*
 * 客户开通流程
 * $params = array('m_customer_mobile' => '客户手机号', 'm_product_id' => '产品ID', 'card_num' => '卡号', 'quota' => '充次|充值', 'added' => '增值', 'remark' => '备注', 'extend_json' => '扩展JSON')
*/
function funcMarketCustomerAccountProcess($params){
	global $configs;
	global $db;

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

	$IFEXEC = true;

	/* 基于客户手机号获取平台用户信息 */
	$userRS = funcSystemUserRead(array('user_account' => $params['m_customer_mobile']));
	if($userRS['success']){
		$userRS = array('user_id' => $userRS['data'][0]['user_id'], 'user_account' => $userRS['data'][0]['user_account'], 'core_member_id' => $userRS['data'][0]['core_member_id']);
		
		/* 基于客户手机号获取市场客户信息 */
		$customerRS = funcMarketCustomerRead(array('m_customer_mobile' => $params['m_customer_mobile']));
		if(!$customerRS['success']){
			/* 若不存在则创建 */
			$res = funcMarketCustomerAdd(array('join_customer_m_member_id' => 0, 'join_customer_member_core_id' => $userRS['core_member_id'], 'customer_status' => 'ACTIVED', 'customer_category' => '普通客户', 'm_customer_real_name' => substr($params['m_customer_mobile'], -4) . "客户", 'm_customer_mobile' => $params['m_customer_mobile']));
			if($res['success']){
				$customerRS = array('m_customer_id' => $res['data']['m_customer_id'], 'm_customer_mobile' => $params['m_customer_mobile']);
			}else{
				$returns['errorcode'] = "901"; //客户信息不存在
				$IFEXEC = false;
			}
		}else{
			$customerRS = $customerRS['data'][0];
		}

		if($IFEXEC){
			$productRS = funcMarketProductRead(array('m_product_id' => $params['m_product_id']));
			if($productRS['success']){
				$productRS = $productRS['data'][0];

				$joinRS = funcMarketCustomerProductRead(array('join_mcp_m_customer_id' => $customerRS['m_customer_id'], 'join_mcp_m_product_id' => $productRS['m_product_id']));
				if(!$joinRS['success']){
					/* 执行开通账户 */
					$returns = funcMarketCustomerProductAddProcess(array(
						'join_mcp_m_product_id' => $productRS['m_product_id'],
						'join_mcp_m_customer_id' => $customerRS['m_customer_id'],
						'join_mcp_card_num' => $params['m_card_num'],
						'join_mcp_enabled_date' => date('Y-m-d'),
						'join_mcp_amount' => $params['quota'],
						'join_mcp_amount_added' => $params['added'],
						'join_mcp_consume' => 0,
						'join_mcp_inbound' => 0,
						'join_mcp_remark' => $params['remark'],
						'join_mcp_extend_json' => $params['extend_json']
					));
					if($returns['success']){
						if($params['m_card_num'] != ''){
							funcMarketAssignUpdProcess(array('wheres' => "m_assign_card_num = '" . $params['m_card_num'] . "'", "m_assign_status" => "ACTIVED"));
						}
					}
				}else{
					$returns['errorcode'] = "903"; //客户的业务产品关联账户已存在
					$IFFEXEC = false;
				}
			}else{
				$returns['errorcode'] = "902"; //业务产品不存在
				$IFFEXEC = false;
			}
		}
	}else{
		$returns['errorcode'] = "900"; //平台未注册
	}

	$returns['msg'] = ($returns['msg']) ? $returns['msg'] : $configs['errors'][$returns['errorcode']];
	return $returns;
}

/*
 * 客户消费
 * $params = array('m_customer_mobile' => '客户手机号', 'join_consume_m_product_id' => '产品ID', 'm_consume_num' => '次数|金额', 'm_consume_status' => '状态', 'm_consume_category' => '分类', 'm_consume_order' => '', 'm_consume_remark' => '')
*/
function funcMarketCustomerAccountConsume($params){
	global $configs;
	global $db;

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

	$IFEXEC = true;

	if($params['serial_customer_product_id']){
		$joinRS = funcMarketCustomerProductRead(array('serial_customer_product_id' => $params['serial_customer_product_id']));
	}else{
		$customerRS = funcMarketCustomerRead(array('m_customer_mobile' => $params['m_customer_mobile']));
		if($customerRS['success']){
			$customerRS = $customerRS['data'][0];
			
			/* 获取账户 */
			$joinRS = funcMarketCustomerProductRead(array('join_mcp_m_customer_id' => $customerRS['m_customer_id'], 'join_mcp_m_product_id' => $params['join_consume_m_product_id']));
		}else{
			$IFEXEC = false;
			$returns['errorcode'] = "905"; //客户信息不存在
		}
	}

	if($IFEXEC){
		if($joinRS['success']){
			$joinRS = $joinRS['data'][0];

			/* 校验额度 */
			$remainder = $joinRS['join_mcp_amount'] - $params['m_consume_num'];
			if($remainder < 0){
				$amount = 0;
				$remainder = $joinRS['join_mcp_amount_added'] + $remainder;
				if($remainder >= 0){
					$added = $remainder;
				}
			}else{
				$amount = $remainder;
				$added = $joinRS['join_mcp_amount_added'];
			}

			if($remainder >= 0){
				/* 执行消费 */
				$res = funcMarketCustomerProductUpdProcess(array(
					'serial_customer_product_id' => $joinRS['serial_customer_product_id'],
					'join_mcp_amount' => $amount,
					'join_mcp_amount_added' => $added,
					'join_mcp_consume' => $joinRS['join_mcp_consume'] + $params['m_consume_num']
				));
				if($res['success']){
					/* 写入消费记录 */
					$returns = funcMarketConsumeAddProcess(array(
						'join_consume_m_product_id' => $joinRS['join_mcp_m_product_id'],
						'join_consume_m_customer_id' => $joinRS['join_mcp_m_customer_id'],
						'join_consume_m_site_id' => $params['join_consume_m_site_id'],
						'join_consume_m_member_id' => $params['join_consume_m_member_id'],
						'join_consume_operate_m_member_id' => $params['join_consume_operate_m_member_id'],
						'm_consume_status' => $params['m_consume_status'],
						'm_consume_category' => $params['m_consume_category'],
						'm_consume_card_num' => $params['m_consume_card_num'],
						'm_consume_times' => ($params['m_consume_times']) ? $params['m_consume_times'] : date('Y-m-d H:i:s'),
						'm_consume_num' => $params['m_consume_num'],
						'm_consume_order' => $params['m_consume_order'],
						'm_consume_remark' => $params['m_consume_remark'],
						'm_consume_extend_json' => $params['m_consume_extend_json'],
						'm_consume_addtimes' => time(),
					));
					if($returns['success']){
						if($params['m_consume_card_num'] != ''){
							funcMarketAssignUpdProcess(array('wheres' => "m_assign_card_num = '" . $params['m_consume_card_num'] . "'", "m_assign_status" => "USED"));
						}
					}
				}
			}else{
				$returns['errorcode'] = "906"; //余额不足
			}
		}else{
			$returns['errorcode'] = "904"; //关联账户不存在
		}		
	}


	$returns['msg'] = ($returns['msg']) ? $returns['msg'] : $configs['errors'][$returns['errorcode']];
	return $returns;
}

/*
 * 客户充值
 * $params = array('m_customer_mobile' => '客户手机号', 'join_inbound_m_product_id' => '产品ID', 'm_inbound_num' => '次数|金额', 'm_inbound_added' => '增值', 'm_inbound_status' => '状态', 'm_inbound_category' => '分类', 'm_inbound_order' => '', 'm_inbound_remark' => '')
*/
function funcMarketCustomerAccountRecharge($params){
	global $configs;
	global $db;

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

	$IFEXEC = true;

	if($params['serial_customer_product_id']){
		$joinRS = funcMarketCustomerProductRead(array('serial_customer_product_id' => $params['serial_customer_product_id']));
	}else{
		$customerRS = funcMarketCustomerRead(array('m_customer_mobile' => $params['m_customer_mobile']));
		if($customerRS['success']){
			$customerRS = $customerRS['data'][0];
			
			/* 获取账户 */
			$joinRS = funcMarketCustomerProductRead(array('join_mcp_m_customer_id' => $customerRS['m_customer_id'], 'join_inbound_m_product_id' => $params['join_inbound_m_product_id']));
		}else{
			$IFEXEC = false;
			$returns['errorcode'] = "905"; //客户信息不存在
		}
	}

	if($IFEXEC){
		if($joinRS['success']){
			$joinRS = $joinRS['data'][0];
	
			/* 校验额度 */
			$amount = $params['m_inbound_num'];
			$added  = $params['m_inbound_added'];
	
			/* 执行消费 */
			$res = funcMarketCustomerProductUpdProcess(array(
				'serial_customer_product_id' => $joinRS['serial_customer_product_id'],
				'join_mcp_amount' => $joinRS['join_mcp_amount'] + $amount,
				'join_mcp_amount_added' => $joinRS['join_mcp_amount_added'] + $added,
				'join_mcp_inbound' => $joinRS['join_mcp_inbound'] + $params['m_inbound_num']
			));
			if($res['success']){
				/* 写入消费记录 */
				$returns = funcMarketInboundAddProcess(array(
					'join_inbound_m_product_id' => $joinRS['join_mcp_m_product_id'],
					'join_inbound_m_customer_id' => $joinRS['join_mcp_m_customer_id'],
					'join_inbound_m_site_id' => $params['join_inbound_m_site_id'],
					'join_inbound_m_member_id' => $params['join_inbound_m_member_id'],
					'join_inbound_operate_m_member_id' => $params['join_inbound_operate_m_member_id'],
					'm_inbound_status' => $params['m_inbound_status'],
					'm_inbound_category' => $params['m_inbound_category'],
					'm_inbound_card_num' => $params['m_inbound_card_num'],
					'm_inbound_times' => ($params['m_inbound_times']) ? $params['m_inbound_times'] : date('Y-m-d H:i:s'),
					'm_inbound_num' => $params['m_inbound_num'],
					'm_inbound_added' => $params['m_inbound_added'],
					'm_inbound_order' => $params['m_inbound_order'],
					'm_inbound_remark' => $params['m_inbound_remark'],
					'm_inbound_extend_json' => $params['m_inbound_extend_json'],
					'm_inbound_addtimes' => time(),
				));
			}
		}else{
			$returns['errorcode'] = "904"; //关联账户不存在
		}
	}

	$returns['msg'] = ($returns['msg']) ? $returns['msg'] : $configs['errors'][$returns['errorcode']];
	return $returns;
}

/*
 * 客户注销
 * $params = array()
*/
function funcMarketCustomerAccountRevoke($params){
	global $configs;
	global $db;

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



	$returns['msg'] = ($returns['msg']) ? $returns['msg'] : $configs['errors'][$returns['errorcode']];
	return $returns;	
}

/* -- -- -- -- -- -- -- -- -- -- -- -- Customer -- -- -- -- -- -- -- -- -- -- -- -- -- -- */

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

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

	$customerRS = funcMarketCustomerRead(array('m_customer_mobile' => $params['m_customer_mobile']));

	if(!$customerRS['success']){
		$returns = funcMarketCustomerAdd($params);
		
		if($returns['success']){
			/* 写入更新记录 */
			try{
				funcMarketCustomerUpdateAddProcess(array(
					'join_mcu_m_customer_id' => $returns['data']['m_customer_id'],
					'join_mcu_m_member_id' => $params['join_customer_m_member_id'],
					'join_mcu_src_data' => '创建客户',
					'join_mcu_extend_json' => '{}',
					'join_mcu_addtimes' => time(),
				));
			}catch(Exception $e){
				
			}
		}
	}else{
		$returns['errorcode'] = "300";
	}	

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

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

	/* 写入 */
	$inserts = array();
	if(isset($params['join_customer_m_member_id']))$inserts['join_customer_m_member_id'] = $params['join_customer_m_member_id'];
	if(isset($params['join_customer_member_core_id']))$inserts['join_customer_member_core_id'] = $params['join_customer_member_core_id'];
	if(isset($params['m_customer_followup_json']))$inserts['m_customer_followup_json'] = $params['m_customer_followup_json'];
	if(isset($params['m_customer_status']))$inserts['m_customer_status'] = $params['m_customer_status'];
	if(isset($params['m_customer_category']))$inserts['m_customer_category'] = $params['m_customer_category'];
	if(isset($params['m_customer_intention']))$inserts['m_customer_intention'] = $params['m_customer_intention'];
	if(isset($params['m_customer_real_name']))$inserts['m_customer_real_name'] = $params['m_customer_real_name'];
	if(isset($params['m_customer_mobile']))$inserts['m_customer_mobile'] = $params['m_customer_mobile'];
	if(isset($params['m_customer_telephone']))$inserts['m_customer_telephone'] = $params['m_customer_telephone'];
	if(isset($params['m_customer_gender']))$inserts['m_customer_gender'] = $params['m_customer_gender'];
	if(isset($params['m_customer_birth_year']))$inserts['m_customer_birth_year'] = $params['m_customer_birth_year'];
	if(isset($params['m_customer_birth_date']))$inserts['m_customer_birth_date'] = $params['m_customer_birth_date'];
	if(isset($params['m_customer_birth_json']))$inserts['m_customer_birth_json'] = $params['m_customer_birth_json'];
	if(isset($params['m_customer_cert']))$inserts['m_customer_cert'] = $params['m_customer_cert'];
	if(isset($params['m_customer_marital']))$inserts['m_customer_marital'] = $params['m_customer_marital'];
	if(isset($params['m_customer_address']))$inserts['m_customer_address'] = $params['m_customer_address'];
	if(isset($params['m_customer_career']))$inserts['m_customer_career'] = $params['m_customer_career'];
	if(isset($params['m_customer_agency']))$inserts['m_customer_agency'] = $params['m_customer_agency'];
	if(isset($params['m_customer_source']))$inserts['m_customer_source'] = $params['m_customer_source'];
	if(isset($params['m_customer_source_type']))$inserts['m_customer_source_type'] = $params['m_customer_source_type'];
	if(isset($params['m_customer_remark']))$inserts['m_customer_remark'] = $params['m_customer_remark'];
	if(isset($params['m_customer_extend_json']))$inserts['m_customer_extend_json'] = $params['m_customer_extend_json'];
	$inserts['m_customer_addtimes'] = (isset($params['m_customer_addtimes']) && $params['m_customer_addtimes']) ? $params['m_customer_addtimes'] : time();
	$sql = executeSQL($inserts, array('mode' => "insert", 'table' => "yx_market_customer"));

	$idx = execSQL($sql, "insertid");
	if($idx){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
		$returns['data'] = array('m_customer_id' => $idx);
	}

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

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

	$returns = funcMarketCustomerUpd($params);
	
	if($returns['success'] && $params['m_customer_id']){
		/* 写入更新记录 */
		try{
			funcMarketCustomerUpdateAddProcess(array(
				'join_mcu_m_customer_id' => $params['m_customer_id'],
				'join_mcu_m_member_id' => ($params['join_customer_m_member_id']) ? $params['join_customer_m_member_id'] : 0,
				'join_mcu_src_data' => funcComArrayToHttp($params),
				'join_mcu_extend_json' => '{}',
				'join_mcu_addtimes' => time(),
			));
		}catch(Exception $e){
			
		}
	}
	$returns['msg'] = ($returns['msg']) ? $returns['msg'] : $configs['errors'][$returns['errorcode']];
	return $returns;
}
function funcMarketCustomerUpd($params){
	global $configs;
	global $db;

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

	/* 验证 */


	/* 写入 */
	$data = array();
	if(isset($params['join_customer_m_member_id']))$data['join_customer_m_member_id'] = $params['join_customer_m_member_id'];
	if(isset($params['join_customer_member_core_id']))$data['join_customer_member_core_id'] = $params['join_customer_member_core_id'];
	if(isset($params['m_customer_followup_json']))$data['m_customer_followup_json'] = $params['m_customer_followup_json'];
	if(isset($params['m_customer_status']))$data['m_customer_status'] = $params['m_customer_status'];
	if(isset($params['m_customer_category']))$data['m_customer_category'] = $params['m_customer_category'];
	if(isset($params['m_customer_intention']))$data['m_customer_intention'] = $params['m_customer_intention'];
	if(isset($params['m_customer_real_name']))$data['m_customer_real_name'] = $params['m_customer_real_name'];
	if(isset($params['m_customer_mobile']))$data['m_customer_mobile'] = $params['m_customer_mobile'];
	if(isset($params['m_customer_telephone']))$data['m_customer_telephone'] = $params['m_customer_telephone'];
	if(isset($params['m_customer_gender']))$data['m_customer_gender'] = $params['m_customer_gender'];
	if(isset($params['m_customer_birth_year']))$data['m_customer_birth_year'] = $params['m_customer_birth_year'];
	if(isset($params['m_customer_birth_date']))$data['m_customer_birth_date'] = $params['m_customer_birth_date'];
	if(isset($params['m_customer_birth_json']))$data['m_customer_birth_json'] = $params['m_customer_birth_json'];
	if(isset($params['m_customer_cert']))$data['m_customer_cert'] = $params['m_customer_cert'];
	if(isset($params['m_customer_marital']))$data['m_customer_marital'] = $params['m_customer_marital'];
	if(isset($params['m_customer_address']))$data['m_customer_address'] = $params['m_customer_address'];
	if(isset($params['m_customer_career']))$data['m_customer_career'] = $params['m_customer_career'];
	if(isset($params['m_customer_agency']))$data['m_customer_agency'] = $params['m_customer_agency'];
	if(isset($params['m_customer_source']))$data['m_customer_source'] = $params['m_customer_source'];
	if(isset($params['m_customer_source_type']))$data['m_customer_source_type'] = $params['m_customer_source_type'];
	if(isset($params['m_customer_remark']))$data['m_customer_remark'] = $params['m_customer_remark'];
	if(isset($params['m_customer_extend_json']))$data['m_customer_extend_json'] = $params['m_customer_extend_json'];
	if(isset($params['m_customer_addtimes']))$data['m_customer_addtimes'] = $params['m_customer_addtimes'];

	if(!$params['wheres']){
		$params['wheres'] = "m_customer_id = " . $params['m_customer_id'];
	}
	$sql = executeSQL($data, array('mode' => "update", 'table' => "yx_market_customer", '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 funcMarketCustomerDelProcess($params){
	global $configs;
	global $db;

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


	$returns = funcMarketCustomerDel($params);

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

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

	/* 验证 */

	/* 执行 */	
	$sql  = "delete from yx_market_customer ";
	if(!$params['wheres']){
		$sql .= "where m_customer_id = " . $params['m_customer_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 funcMarketCustomerRead($params){
	global $configs;
	global $db;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	if(isset($params['search_key']) && $params['search_key']){
		$wheres .= "(m_customer_real_name like '%" . $params['search_key'] . "%' or m_customer_mobile like '%" . $params['search_key'] . "%' or m_customer_telephone like '%" . $params['search_key'] . "%' or m_customer_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_customer_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;
}

/* -- -- -- -- -- -- -- -- -- -- -- -- Customer Product -- -- -- -- -- -- -- -- -- -- -- -- -- -- */

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

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


	$returns = funcMarketCustomerProductAdd($params);

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

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

	/* 验证 */

	/* 写入 */
	$inserts = array();
	if(isset($params['join_mcp_m_product_id']))$inserts['join_mcp_m_product_id'] = $params['join_mcp_m_product_id'];
	if(isset($params['join_mcp_m_customer_id']))$inserts['join_mcp_m_customer_id'] = $params['join_mcp_m_customer_id'];
	if(isset($params['join_mcp_card_num']))$inserts['join_mcp_card_num'] = $params['join_mcp_card_num'];
	if(isset($params['join_mcp_enabled_date']))$inserts['join_mcp_enabled_date'] = $params['join_mcp_enabled_date'];
	if(isset($params['join_mcp_amount']))$inserts['join_mcp_amount'] = $params['join_mcp_amount'];
	if(isset($params['join_mcp_amount_added']))$inserts['join_mcp_amount_added'] = $params['join_mcp_amount_added'];
	if(isset($params['join_mcp_consume']))$inserts['join_mcp_consume'] = $params['join_mcp_consume'];
	if(isset($params['join_mcp_inbound']))$inserts['join_mcp_inbound'] = $params['join_mcp_inbound'];
	if(isset($params['join_mcp_remark']))$inserts['join_mcp_remark'] = $params['join_mcp_remark'];
	if(isset($params['join_mcp_extend_json']))$inserts['join_mcp_extend_json'] = $params['join_mcp_extend_json'];
	$inserts['join_mcp_addtimes'] = (isset($params['join_mcp_addtimes']) && $params['join_mcp_addtimes']) ? $params['join_mcp_addtimes'] : time();
	$sql = executeSQL($inserts, array('mode' => "insert", 'table' => "yx_market_customer_product"));
	$returns['sql'] = $sql;
	$idx = execSQL($sql, "insertid");
	if($idx){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
		$returns['data'] = array('serial_customer_product_id' => $idx);
	}

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

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


	$returns = funcMarketCustomerProductUpd($params);

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

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

	/* 验证 */


	/* 写入 */
	$data = array();
	if(isset($params['join_mcp_m_product_id']))$data['join_mcp_m_product_id'] = $params['join_mcp_m_product_id'];
	if(isset($params['join_mcp_m_customer_id']))$data['join_mcp_m_customer_id'] = $params['join_mcp_m_customer_id'];
	if(isset($params['join_mcp_card_num']))$data['join_mcp_card_num'] = $params['join_mcp_card_num'];
	if(isset($params['join_mcp_enabled_date']))$data['join_mcp_enabled_date'] = $params['join_mcp_enabled_date'];
	if(isset($params['join_mcp_amount']))$data['join_mcp_amount'] = $params['join_mcp_amount'];
	if(isset($params['join_mcp_amount_added']))$data['join_mcp_amount_added'] = $params['join_mcp_amount_added'];
	if(isset($params['join_mcp_consume']))$data['join_mcp_consume'] = $params['join_mcp_consume'];
	if(isset($params['join_mcp_inbound']))$data['join_mcp_inbound'] = $params['join_mcp_inbound'];
	if(isset($params['join_mcp_remark']))$data['join_mcp_remark'] = $params['join_mcp_remark'];
	if(isset($params['join_mcp_extend_json']))$data['join_mcp_extend_json'] = $params['join_mcp_extend_json'];
	if(isset($params['join_mcp_addtimes']))$data['join_mcp_addtimes'] = $params['join_mcp_addtimes'];

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

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

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


	$returns = funcMarketCustomerProductDel($params);

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

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

	/* 验证 */

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

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

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

	$sql  = "select a.*, b.*, c.* from yx_market_customer_product a ";
	$sql .= "left outer join yx_market_product b on a.join_mcp_m_product_id = b.m_product_id ";
	$sql .= "left outer join yx_market_customer c on a.join_mcp_m_customer_id = c.m_customer_id ";
	

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

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

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

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

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

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

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

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

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

	if(isset($params['join_mcp_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 serial_customer_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;
}


/* -- -- -- -- -- -- -- -- -- -- -- -- Customer Update -- -- -- -- -- -- -- -- -- -- -- -- -- -- */

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

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


	$returns = funcMarketCustomerUpdateAdd($params);

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

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

	/* 验证 */

	/* 写入 */
	$inserts = array();
	if(isset($params['join_mcu_m_customer_id']))$inserts['join_mcu_m_customer_id'] = $params['join_mcu_m_customer_id'];
	if(isset($params['join_mcu_m_member_id']))$inserts['join_mcu_m_member_id'] = $params['join_mcu_m_member_id'];
	if(isset($params['join_mcu_src_data']))$inserts['join_mcu_src_data'] = $params['join_mcu_src_data'];
	if(isset($params['join_mcu_extend_json']))$inserts['join_mcu_extend_json'] = $params['join_mcu_extend_json'];
	$inserts['join_mcu_addtimes'] = (isset($params['join_mcu_addtimes'])) ? $params['join_mcu_addtimes'] : time();
	$sql = executeSQL($inserts, array('mode' => "insert", 'table' => "yx_market_customer_update"));
	$idx = execSQL($sql, "insertid");
	if($idx){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
		$returns['data'] = array('serial_customer_update_id' => $idx);
	}

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

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


	$returns = funcMarketCustomerUpdateUpd($params);

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

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

	/* 验证 */


	/* 写入 */
	$data = array();
	if(isset($params['join_mcu_m_customer_id']))$data['join_mcu_m_customer_id'] = $params['join_mcu_m_customer_id'];
	if(isset($params['join_mcu_m_member_id']))$data['join_mcu_m_member_id'] = $params['join_mcu_m_member_id'];
	if(isset($params['join_mcu_src_data']))$data['join_mcu_src_data'] = $params['join_mcu_src_data'];
	if(isset($params['join_mcu_extend_json']))$data['join_mcu_extend_json'] = $params['join_mcu_extend_json'];
	if(isset($params['join_mcu_addtimes']))$data['join_mcu_addtimes'] = $params['join_mcu_addtimes'];

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

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

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


	$returns = funcMarketCustomerUpdateDel($params);

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

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

	/* 验证 */

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

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

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

	$sql = "select * from yx_market_customer_update";

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

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

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

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

	if(isset($params['join_mcu_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 serial_customer_update_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;
}


/* -- -- -- -- -- -- -- -- -- -- -- -- System User -- -- -- -- -- -- -- -- -- -- -- -- -- -- */
function funcSystemUserRead($params){
	global $configs;
	global $db;

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

	$sql = "select * from yx_user";

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

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

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

	/* 外部其他条件 */
	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 user_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['msg'] = $configs['errors'][$returns['errorcode']];
	return $returns;
}
?>