using System;
using System.Collections.Generic;
using System.Web;

namespace Stat.BLL
{
    /// <summary>
    /// 
    /// </summary>
    public class ViewProvince
    {
        static Stat.IDAL.IViewProvince dal = Stat.DALFactory.AbstractFactory.GetInstance().CreateProvince();

        /// <summary>
        /// 国家省份
        /// </summary>
        /// <param name="model"></param> 
        /// <returns></returns>
        internal static List<Model.ViewItem_Int> GetItem(Model.StatInfo model)
        {
            return dal.GetItem(model);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List<Model.StatItem> Get(Model.StatInfo model)
        {
            string cName = "V_Province_" + model.WebID + "_" + model.StartTime + "_" + model.EndTime;
            if (HttpContext.Current.Cache.Get(cName) == null)
            {

                List<Model.StatItem> list = dal.GetList(model);
                //
                if (list == null || list.Count < 1)
                {
                    //详细数据
                    list = Report.Rp_Int.SingleDay(GetItem(model), Stat.Items.Area.Get());
                }
                else
                {
                    //多天
                    list = Report.Rp_Int.MultiDay(list);
                }
                //
                foreach (Model.StatItem item in list)
                {
                    item.Name = item.Name ?? "其他地区";
                    item.Name = Stat.Items.Area.NumberToName(item.ID.ToString());
                }
                //
                HttpContext.Current.Cache.Insert(cName, list, null, DateTime.MaxValue, TimeSpan.FromSeconds(10.0), System.Web.Caching.CacheItemPriority.Low, null);
                return list;
            }
            return (List<Model.StatItem>)HttpContext.Current.Cache.Get(cName);//从缓存中读取

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<Model.StatItem> GetChina(List<Model.StatItem> list)
        {
            List<Model.StatItem> chinaList = new List<Stat.Model.StatItem>();
            string[,] arr = Stat.Items.Province.Get();
            //
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                Model.StatItem model = new Stat.Model.StatItem();

                List<Model.StatItem> itemList = list.FindAll(delegate(Model.StatItem sunItem) { return (sunItem.Name == arr[i, 1]); });

                if (itemList != null && itemList.Count > 0)
                {
                    model = itemList[0];
                    model.AreaNo = arr[i, 0];
                }
                else
                {
                    model.AreaNo = arr[i, 0];
                    model.Name = arr[i, 1];
                }
                chinaList.Add(model);
            }
            //
            if (chinaList.Count > 0)
            {
                chinaList.Sort(delegate(Model.StatItem x, Model.StatItem y) { return Comparer<Int64>.Default.Compare(y.PvCount, x.PvCount); });
            }
            //
            return chinaList;
        }

    }
}
