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


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

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

	$assignRS = funcMarketAssignRead(array('m_assign_card_num' => $params['m_assign_card_num']));

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

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

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

	/* 写入 */
	$inserts = array();
	if(isset($params['join_assign_m_site_id']))$inserts['join_assign_m_site_id'] = $params['join_assign_m_site_id'];
	if(isset($params['join_assign_m_member_id']))$inserts['join_assign_m_member_id'] = $params['join_assign_m_member_id'];
	if(isset($params['join_holder_m_member_id']))$inserts['join_holder_m_member_id'] = $params['join_holder_m_member_id'];
	if(isset($params['m_assign_status']))$inserts['m_assign_status'] = $params['m_assign_status'];
	if(isset($params['m_assign_card_num']))$inserts['m_assign_card_num'] = $params['m_assign_card_num'];
	if(isset($params['m_assign_card_pswb']))$inserts['m_assign_card_pswb'] = $params['m_assign_card_pswb'];
	if(isset($params['m_assign_card_qrcode']))$inserts['m_assign_card_qrcode'] = $params['m_assign_card_qrcode'];
	if(isset($params['m_assign_allocate_json']))$inserts['m_assign_allocate_json'] = $params['m_assign_allocate_json'];
	if(isset($params['m_assign_extend_json']))$inserts['m_assign_extend_json'] = $params['m_assign_extend_json'];
	$inserts['m_assign_addtimes'] = (isset($params['m_assign_addtimes']) && $params['m_assign_addtimes']) ? $params['m_assign_addtimes'] : time();
	$sql = executeSQL($inserts, array('mode' => "insert", 'table' => "yx_market_assign"));
	$idx = execSQL($sql, "insertid");
	if($idx){
		$returns['success'] = true;
		$returns['errorcode'] = "200";
		$returns['data'] = array('m_assign_id' => $idx);
	}

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

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


	$returns = funcMarketAssignUpd($params);

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

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

	/* 写入 */
	$data = array();
	if(isset($params['join_assign_m_site_id']))$data['join_assign_m_site_id'] = $params['join_assign_m_site_id'];
	if(isset($params['join_assign_m_member_id']))$data['join_assign_m_member_id'] = $params['join_assign_m_member_id'];
	if(isset($params['join_holder_m_member_id']))$data['join_holder_m_member_id'] = $params['join_holder_m_member_id'];
	if(isset($params['m_assign_status']))$data['m_assign_status'] = $params['m_assign_status'];
	if(isset($params['m_assign_card_num']))$data['m_assign_card_num'] = $params['m_assign_card_num'];
	if(isset($params['m_assign_card_pswb']))$data['m_assign_card_pswb'] = $params['m_assign_card_pswb'];
	if(isset($params['m_assign_card_qrcode']))$data['m_assign_card_qrcode'] = $params['m_assign_card_qrcode'];
	if(isset($params['m_assign_allocate_json']))$data['m_assign_allocate_json'] = $params['m_assign_allocate_json'];
	if(isset($params['m_assign_extend_json']))$data['m_assign_extend_json'] = $params['m_assign_extend_json'];
	if(isset($params['m_assign_addtimes']))$data['m_assign_addtimes'] = $params['m_assign_addtimes'];

	if(!$params['wheres']){
		if(isset($params['m_assign_id_multi']) && $params['m_assign_id_multi']){
			$params['wheres'] = "m_assign_id in (" . $params['m_assign_id_multi'] . ")";
		}
		if(isset($params['m_assign_id']) && $params['m_assign_id']){
			$params['wheres'] = "m_assign_id = " . $params['m_assign_id'];
		}
	}

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

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


	$returns = funcMarketAssignDel($params);

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

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

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

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

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

	$sql  = "select a.*, b.*, c.* from yx_market_assign a ";
	$sql .= "left outer join yx_market_site b on a.join_assign_m_site_id = b.m_site_id ";
	$sql .= "left outer join yx_market_member c on a.join_assign_m_member_id = c.m_member_id ";

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

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

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

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

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

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

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

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

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

	if(isset($params['m_assign_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_assign_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' => '', '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;
}

?>