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

namespace Stat.BLL
{
    /// <summary>
    /// 
    /// </summary>
    public class ViewReturnuser
    {
        static Stat.IDAL.IViewReturnuser dal = Stat.DALFactory.AbstractFactory.GetInstance().CreateViewReturnuser();


        /// <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_Returnuser_" + 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_Class.SingleDay(GetItem(model));
                }
                else
                {
                    //过往数据
                    list = Report.Rp_Int.MultiDay(list);
                    list.RemoveAt(0);//移除总数
                }
                //
                List<Model.StatItem> newList = new List<Stat.Model.StatItem>();
                newList.Add(new Stat.Model.StatItem());
                if (list != null && list.Count > 0)
                {
                    newList[0].PvCount = list.Sum(s => s.PvCount);
                    newList[0].IpCount = list.Sum(s => s.IpCount);
                    newList[0].UvCount = list.Sum(s => s.UvCount);
                    newList[0].PerPV = Convert.ToSingle(newList[0].PvCount) / (newList[0].UvCount == 0 ? 1 : newList[0].UvCount);

                    for (int i = 0; i <= 10; i++)
                    {
                        Model.StatItem newModel = new Stat.Model.StatItem();
                        //Name
                        if (i == 0) { newModel.Name = "首次来访"; }
                        else if (i == 10) { newModel.Name = "10次以上"; }
                        else { newModel.Name = i + " 次回访"; }

                        //符合
                        List<Model.StatItem> itemList = list.FindAll(delegate(Model.StatItem sunItem)
                        {
                            if (i < 10)
                            {
                                return (sunItem.ID == i);
                            }
                            else
                            {
                                return (sunItem.ID >= i);
                            }
                        });
                        //
                        newModel.PvCount = itemList.Sum(s => s.PvCount);
                        newModel.IpCount = itemList.Sum(s => s.IpCount);
                        newModel.UvCount = itemList.Sum(s => s.UvCount);
                        newModel.PerPV = Convert.ToSingle(newModel.PvCount) / (newModel.UvCount == 0 ? 1 : newModel.UvCount);
                        //百分比（次数/总次数）
                        newModel.Percent = Convert.ToSingle(newModel.PvCount) / (newList[0].PvCount == 0 ? 1 : newList[0].PvCount) * 100;
                        //
                        newList.Add(newModel);
                    }
                }
                //
                HttpContext.Current.Cache.Insert(cName, newList, null, DateTime.MaxValue, TimeSpan.FromSeconds(3.0), System.Web.Caching.CacheItemPriority.Low, null);
                return newList;
            }
            return (List<Model.StatItem>)HttpContext.Current.Cache.Get(cName);//从缓存中读取
        }
    }
}
