<?php
/* 快捷商城系统
 * 版本号：2.0
 * 官网：http://www.kjcms.cn
 * 2015-02-16
 */
class mod_report_top extends inc_mod_mall {
	//店铺销售排行榜
	function get_shop_top() {
		$obj_db = cls_obj::db();
		$where  = " where order_state>=0";

		$mode = fun_get::get("mode");
		if($mode == 'year') {
			$year = fun_get::get("year" , date("Y"));
			$where .= " and left(order_datetime,4)='" . $year . "'";
		} else if($mode == 'month') {
			$year = fun_get::get("year" , date("Y"));
			$month = fun_get::get("month" , date("m"));
			if(strlen($month)<2) $month = "0" . $month;
			$date = $year . "-" . $month;
			$where .= " and left(order_datetime,7)='" . $date . "'";
		} else {
			$date = fun_get::get("date" , date("Y-m-d"));
			$where .= " and left(order_datetime,10)='" . $date . "'";
		}
		$arr_top = $arr_shop_id = array();
		$obj_result = $obj_db->select("select order_shop_id,sum(order_total_pay) as total,count(1) as num from " . cls_config::DB_PRE . "mall_order" . $where . " group by order_shop_id order by total limit 0,10");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_shop_id[] = $obj_rs["order_shop_id"];
			$arr_top[] = $obj_rs;
		}
		if(count($arr_shop_id)>0) {
			$str_ids = implode("," , $arr_shop_id);
			$obj_result = $obj_db->select("select shop_id,shop_name from " . cls_config::DB_PRE . "mall_shop where shop_id in(" . $str_ids . ")");
			while($obj_rs = $obj_db->fetch_array($obj_result)) {
				$arr_shopname["id_" . $obj_rs['shop_id']] = $obj_rs["shop_name"];
			}
			for($i = 0; $i < count($arr_top) ; $i++) {
				$arr_top[$i]['shop_name'] = (isset($arr_shopname['id_' . $arr_top[$i]['order_shop_id']])) ? $arr_shopname['id_' . $arr_top[$i]['order_shop_id']] : '';
			}
		}
		return $arr_top;
	}
	//用户销售排行榜
	function get_user_top() {
		$obj_db = cls_obj::db();
		$where  = " where order_state>=0";

		$mode = fun_get::get("mode");
		if($mode == 'year') {
			$year = fun_get::get("year" , date("Y"));
			$where .= " and left(order_datetime,4)='" . $year . "'";
		} else if($mode == 'month') {
			$year = fun_get::get("year" , date("Y"));
			$month = fun_get::get("month" , date("m"));
			if(strlen($month)<2) $month = "0" . $month;
			$date = $year . "-" . $month;
			$where .= " and left(order_datetime,7)='" . $date . "'";
		} else {
			$date = fun_get::get("date" , date("Y-m-d"));
			$where .= " and eft(order_datetime,10)='" . $date . "'";
		}
		$arr_top = $arr_user_id = array();
		$obj_result = $obj_db->select("select order_user_id,sum(order_total_pay) as total,count(1) as num from " . cls_config::DB_PRE . "mall_order" . $where . " group by order_shop_id order by total limit 0,10");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_user_id[] = $obj_rs["order_user_id"];
			$arr_top[] = $obj_rs;
		}
		if(count($arr_user_id)>0) {
			$str_ids = implode("," , $arr_user_id);
			$obj_result = $obj_db->select("select user_id,user_name from " . cls_config::DB_PRE . "sys_user where user_id in(" . $str_ids . ")");
			while($obj_rs = $obj_db->fetch_array($obj_result)) {
				$arr_username["id_" . $obj_rs['user_id']] = $obj_rs["user_name"];
			}
			for($i = 0; $i < count($arr_top) ; $i++) {
				$arr_top[$i]['user_name'] = (isset($arr_username['id_' . $arr_top[$i]['order_user_id']])) ? $arr_username['id_' . $arr_top[$i]['order_user_id']] : '';
			}
		}
		return $arr_top;
	}
}