<?php
/**
 * UserおよびCharacterのクラス処理
 * 関連: UserMst
 * $Id$
 * @package jp.fishow.handler
 */

class UserHandler{
	/**
	 * Mobileからユーザー取得
	 * @param String uid
	 * @return UserMst / null
	 */
	public static function getUserByMobile($mobile) {
		$param = array();
		$param["mobile"] = $mobile;
		$param["delete_flg"] = false;
		$tmp_user_list = UserMst::getList($param, "update_date", "desc", 0, 1);
		if (count($tmp_user_list) > 0) {
			return $tmp_user_list[0];
		}
		return null;
	}

	/**
	 * 我的学员用户列表总数获取
 	 * @param array param
	 * @return int
	 */
	public static function getUserCount($param = null){
		$db = &KoalaDBManager::getInstance();
		$sql = "select count(*) as user_count from user_mst  where delete_flg=false";
		if (array_key_exists("channel_no", $param)) {
			$sql .= " and channel_no='{$param['channel_no']}'";
		}
		if (array_key_exists("status", $param)) {
			$sql .= " and status='{$param['status']}'";
		}
		if (array_key_exists("mobile", $param)) {
			$sql .= " and mobile like '%{$param['mobile']}%'";
		}
		if (array_key_exists("nickname", $param)) {
			$sql .= " and nickname like '%{$param['nickname']}%'";
		}
		if (array_key_exists("account_id", $param)) {
			$sql .= " and account_id='{$param['account_id']}'";
		}
		$result = $db->executeQuery($sql);
		return $result[0]['user_count'];
	}
	
	/**
	 * 学员列表获取
 	 * @param array param
	 * @param string order_key
	 * @param string direction
	 * @param int offset
	 * @param int limit
	 * @return List()
	 */
	public static function getUserList($param = null,  $order_key = "id", $direction = "DESC", $offset = null, $page_row = null){
		$db = &KoalaDBManager::getInstance();
		$sql = "select * from user_mst  where delete_flg=false";
		if (array_key_exists("channel_no", $param)) {
			$sql .= " and channel_no='{$param['channel_no']}'";
		}
		if (array_key_exists("status", $param)) {
			$sql .= " and status='{$param['status']}'";
		}
		if (array_key_exists("mobile", $param)) {
			$sql .= " and mobile like '%{$param['mobile']}%'";
		}
		if (array_key_exists("nickname", $param)) {
			$sql .= " and nickname like '%{$param['nickname']}%'";
		}
		if (array_key_exists("account_id", $param)) {
			$sql .= " and account_id='{$param['account_id']}'";
		}
		if(!empty($order_key)) {
			$sql .= " order by " . $order_key . " " . $direction;
		}
		if (!empty ($page_row)) {
			$sql .= " limit " . $page_row;
		}		
		if (!empty ($offset)) {
			$sql .= " offset " . $offset . ";";
		}

		return $db->executeQuery($sql);
	}	

	/** 获取用户账户余额
	 * @param int id
	 * @return array
	 */
	 public static function getUserBalance($user_id){
	 	$db = &KoalaDBManager::getInstance();
	 	$sql = "select sum(money) as balance from user_money_log where delete_flg = false and user_id = {$user_id} and status <> 'FAIL'";
	 	$result = $db->executeQuery($sql);
	 	if(count($result) > 0){
	 		return $result[0]["balance"];
	 	} else {
	 		return 0.00;
	 	}
	 }
	 
	/** 获取用户充值总额
	 * @param int id
	 * @return array
	 */
	 public static function getUserSum($user_id){
	 	$db = &DivinationsDBManager::getInstance();
	 	$sql = "select sum(money) as money from donation_dat where delete_flg = false and user_id = {$user_id} and status = 'SUCCESS' and donation_id <> 0";
	 	$result = $db->executeQuery($sql);
	 	if(count($result) > 0){
	 		return $result[0]["money"];
	 	} else {
	 		return 0.00;
	 	}
	 } 
	 
	 /** 
		*用户提现操作
		* @param array param
		* @return int
	 */
	 public static function payCashing($appid, $openid, $nickname, $amount){
		//付款测试
		$obj = array();
		$obj["mch_appid"] = $appid;
		$obj["mchid"] = WECHAT_PAY_MCHID;
		$obj["nonce_str"] = PasswordMaker::chars(31,31);
		$obj["partner_trade_no"] = date("YmdHis").rand(10000,99999);
		$obj["openid"] = $openid;
		$obj["check_name"] = "NO_CHECK";
		$obj["re_user_name"] = $nickname;
		$obj["amount"] = $amount;
		$obj["desc"] = "收益提现[".date("Y-m-d") . "]";
		$obj["spbill_create_ip"] = SERVER_IP;

		$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";

		$pay_obj = new WxPay();
		$result = $pay_obj->pay($url, $obj);
		
		ErrorLogger::doOutput("UserHandler...payCashing....appid=[" . $appid . "]", 0);
		ErrorLogger::doOutput("UserHandler...payCashing....openid=[" . $openid . "]", 0);
		ErrorLogger::doOutput("UserHandler...payCashing....amount=[" . $amount . "]", 0);
		ErrorLogger::doOutput("UserHandler...payCashing....partner_trade_no=[" . $obj["partner_trade_no"] . "]", 0);
		ErrorLogger::doOutput("UserHandler...payCashing....return_code=" . $result["return_code"], 0);
		ErrorLogger::doOutput("UserHandler...payCashing....result_code=" . $result["result_code"], 0);
		
		if($result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
			return true;
		}
		ErrorLogger::doOutput("UserHandler...payCashing....err_code=" . $result["err_code"], 0);
		ErrorLogger::doOutput("UserHandler...payCashing....err_code_des=" . $result["err_code_des"], 0);
		return false;
	 }

	 /** 获取用户充值价格
	 * @param int user_id
	 * @return int
	 */
	 public static function getAmountByUserId($user_id){
	 	$db = &DivinationsDBManager::getInstance();
	 	$sql = " select sum(amount) as all_amount from user_course_dat where delete_flg = false and status='SUCCESS' and user_id = {$user_id}";
	 	$result =  $db->executeQuery($sql);
	 	if(count($result) > 0){
	 		return $result[0]['all_amount'];
	 	} else {
	 		return '';
	 	}
	 }
}
?>