/// <summary>
        /// 根据id删除资讯实体
        /// </summary>
        /// <param name="id"></param>
        public static ReturnInfo DeleteConsultById(int id)
        {
            ReturnInfo    rif = new ReturnInfo();
            StringBuilder sb  = new StringBuilder();

            using (WorkOfUnit work = new WorkOfUnit())
            {
                try
                {
                    //假如存在外键约束则先删除关联数据
                    var img = work.CreateRepository <RmationImg>().GetList(m => m.Information.Id == id);
                    if (img.Count() != 0)
                    {
                        foreach (var item in img)
                        {
                            try
                            {
                                work.CreateRepository <RmationImg>().Delete(item.Id);
                                rif.IsSuccess = work.Save() >= 1;
                            }
                            catch (Exception ex)
                            {
                                item.Lock = 1;
                                work.CreateRepository <RmationImg>().Update(item);
                                rif.IsSuccess = work.Save() >= 1;
                                sb.Append("删除失败" + item.Id + ":" + ex.Message);
                                continue;
                            }
                        }
                    }
                    work.CreateRepository <Information>().Delete(id);
                    rif.IsSuccess = work.Save() >= 1;
                }
                catch (Exception ex)
                {
                    rif.IsSuccess = false;
                    sb.Append(ex.Message);
                }
            }
            rif.ErrorInfo = sb.ToString();
            return(rif);
        }
        // GET api/values/5
        public string Get(string key)
        {
            //EF生成数据库
            //GcSiteDb db = new GcSiteDb();
            //db.Database.CreateIfNotExists();
            IPAddress[] ip  = null;
            string      url = System.Web.HttpContext.Current.Request.Url.Host.ToString();

            if (key == "E10ADC3949BA59ABBE56E057F20F883E")
            {
                //获取访问总量
                work.Save();
                //获取本机ip地址
                //string pcName = System.Net.Dns.GetHostName();
                //ip = Dns.Resolve(pcName).AddressList;
            }

            return(ip[0].ToString());
        }
Exemplo n.º 3
0
        protected void Session_End()
        {
            DateTime startTime, endTime;
            //接受后台传递的Idid = Session["id"].ToString() == null ? 0 : int.Parse(Session["id"].ToString());
            var id = 0;

            id = int.Parse(Session["id"].ToString());
            if (id != 0)
            {
                List <VisitorInfo> list = work.CreateRepository <VisitorInfo>().GetList().ToList();
                var model = list.Where(p => p.Id == int.Parse(id.ToString())).FirstOrDefault();
                int vid   = 0;
                vid                 = model.Id;
                startTime           = model.AccessTime;
                model.AccessEndTime = DateTime.Now;
                endTime             = model.AccessEndTime;
                TimeSpan ts1      = new TimeSpan(startTime.Ticks);
                TimeSpan ts2      = new TimeSpan(endTime.Ticks);
                TimeSpan ts       = ts1.Subtract(ts2).Duration();
                string   aa       = ts.ToString();
                string   H        = aa.Split(':')[0].ToString();
                string   M        = aa.Split(':')[1].ToString();
                string   S        = aa.Split(':')[2].ToString();
                Double   Duration = Double.Parse(H) * 3600 + Double.Parse(M) * 60 + Double.Parse(S);
                model.Duration = Duration;
                work.CreateRepository <VisitorInfo>().Update(model);
                int    sum      = work.CreateRepository <VisitorInfo>().GetCount(m => m.Id != 0);
                double duration = work.CreateRepository <VisitorInfo>().GetCount();
                duration = list.Sum(a => a.Duration);
                work.CreateRepository <VisitorInfo>().Update(model);
                List <WebInfo> weblist = work.CreateRepository <WebInfo>().GetList().ToList();
                var            web     = weblist.Where(p => p.Id == model.WebInfo.Id).FirstOrDefault();
                //平均时长
                double sc = duration / sum;
                //WebPv.WebTS = d         uration / sum;
                web.WebTS = (sc).ToString();
                work.CreateRepository <WebInfo>().Update(web);
                work.Save();
            }
        }
        public IEnumerable <object> Get(string key, string VisitPage, string IpAddress, string Address)
        {
            try
            {
                WebInfo web = work.CreateRepository <WebInfo>().GetFirst(m => m.WebKey == key);
                if (web != null)
                {
                    #region 获取访客信息&添加访客信息
                    //搜索引擎来源
                    var se = System.Web.HttpContext.Current.Request.UserAgent;
                    //获取浏览器信息
                    //火狐浏览器
                    if (se.Contains("Firefox"))
                    {
                        se = "火狐";
                    }
                    //谷歌浏览器
                    else if (se.Contains("Chrome"))
                    {
                        se = "谷歌";
                    }
                    //Safari浏览器(苹果浏览器)
                    else if (se.Contains("Version"))
                    {
                        se = "苹果";
                    }
                    //Opera浏览器
                    else if (se.Contains("Opera"))
                    {
                        se = "Opera";
                    }
                    //LBBROWSER浏览器(猎豹)
                    else if (se.Contains("LBBROWSER"))
                    {
                        se = "猎豹";
                    }
                    //sougou浏览器(sougou)
                    else if (se.Contains("MetaSr"))
                    {
                        se = "搜狗";
                    }
                    //Maxthon浏览器(傲游)
                    else if (se.Contains("Maxthon"))
                    {
                        se = "傲游";
                    }
                    //因特网浏览器
                    else
                    {
                        se = "未知";
                    }
                    VisitorInfo vist = new VisitorInfo();
                    vist.AccessTime    = DateTime.Now;
                    vist.VisitPage     = VisitPage;
                    vist.IpAddress     = IpAddress;
                    vist.VisitSE       = se;
                    vist.WebInfo       = web;
                    vist.Address       = Address;
                    vist.Age           = 0;
                    vist.AccessEndTime = DateTime.Now;

                    //判断用户是否访问一个就退出
                    if (IsPostUrl == string.Empty)
                    {
                        IsPostUrl = VisitPage;
                    }
                    if (IsPostUrl != VisitPage)
                    {
                        vist.PageNumber = 1;
                    }
                    //随机生成标识码(identification code) 32位字符
                    string IC = Guid.NewGuid().ToString("N");
                    var    bo = work.CreateRepository <VisitorInfo>().GetList(m => m.IC == IC);
                    if (bo.Count() > 0)
                    {
                        IC = Guid.NewGuid().ToString("N");
                    }
                    vist.IC = IC;

                    //判断用户是否相同用户
                    List <VisitorInfo> alikeCount = work.CreateRepository <VisitorInfo>().GetList(
                        m => m.IpAddress == vist.IpAddress
                        ).ToList();
                    //通过ip地址保证访客数计算
                    if (alikeCount.Count() == 0)
                    {
                        work.CreateRepository <VisitorInfo>().Insert(vist);
                        work.Save();
                        List <VisitorInfo> list = work.CreateRepository <VisitorInfo>().GetList().ToList();
                        var model = list.Where(p => p.IC == IC).FirstOrDefault();
                        int id    = 0; id = model.Id;
                        HttpContext.Current.Session["id"] = id;
                    }
                    else
                    {
                        foreach (var item in alikeCount)
                        {
                            TimeSpan span = DateTime.Now - item.AccessEndTime;
                            //判断该ip地址访客访问时间是否超过24小时
                            int temp = Convert.ToInt32(span.TotalHours);
                            if (temp >= 24 && item.Lock == 0)
                            {
                                item.Lock = 1;
                                work.CreateRepository <VisitorInfo>().Update(item);
                                work.Save();
                                work.CreateRepository <VisitorInfo>().Insert(vist);
                                work.Save();
                                List <VisitorInfo> list = work.CreateRepository <VisitorInfo>().GetList().ToList();
                                var model = list.Where(p => p.IC == IC).FirstOrDefault();
                                int id    = 0; id = model.Id;
                                HttpContext.Current.Session["id"] = id;
                            }
                        }
                    }

                    #endregion

                    #region 获取PV信息&添加PV信息
                    FlowComputer flow = new FlowComputer();
                    flow.VisitPage = VisitPage;
                    flow.WebHost   = System.Web.HttpContext.Current.Request.Url.Host.ToString();
                    string str = HttpContext.Current.Request.UrlReferrer.ToString();
                    //判断是否搜索引擎链接
                    if (searchkey.IsSearchEnginesGet(str))
                    {
                        //取得搜索关键字
                        flow.SearchTerms = searchkey.SearchKey(str);
                        //取得搜索引擎名称
                        flow.VisitSE = searchkey.EngineName;
                    }
                    else
                    {
                        flow.SearchTerms = "其他";
                        flow.VisitSE     = vist.VisitSE;
                    }
                    flow.CurrentTime = DateTime.Now;
                    flow.WebInfo     = web;
                    lock (flow)
                    {
                        work.CreateRepository <FlowComputer>().Insert(flow);
                        work.Save();
                    }
                    #endregion

                    //web = work.CreateRepository<WebInfo>().GetFirst(m => m.WebKey == key);//获取pv
                    web.WebPv = web.WebPv + 1;
                    web.WebUv = work.CreateRepository <VisitorInfo>().GetCount(m => m.WebInfo.Id == web.Id);
                    int     rate       = work.CreateRepository <VisitorInfo>().GetCount(m => m.PageNumber == 0);
                    decimal rateResult = Math.Round((decimal)rate / web.WebPv, 4);
                    if (rateResult >= 1)
                    {
                        rateResult = 1;
                    }
                    web.BounceRate = (rateResult * 100).ToString().Length >= 5 ? (rateResult * 100).ToString().Substring(0, 4) + "%" : (rateResult * 100).ToString() + "%";
                    List <VisitorInfo> webuv = work.CreateRepository <VisitorInfo>().GetList().ToList();//获取uv
                    for (int i = 0; i < webuv.Count(); i++)
                    {
                        for (int j = webuv.Count() - 1; j > i; j--)
                        {
                            if (webuv[i].IpAddress == webuv[j].IpAddress)
                            {
                                webuv.RemoveAt(j);
                            }
                        }
                    }
                    web.IpCount = webuv.Count;//获取ip数 去重
                    lock (web)
                    {
                        work.CreateRepository <WebInfo>().Update(web);
                        work.Save();
                    }
                }
                else
                {
                    //var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                    //{
                    //    Content = new StringContent(string.Format("没有找到Key={0}的密钥", key)),
                    //    ReasonPhrase = "object is not found"
                    //};
                    //throw new HttpResponseException(resp);
                }
            }
            catch (Exception ex)
            {
                //var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                //{
                //    Content = new StringContent(string.Format("没有找到Key={0}的密钥", key)),
                //    ReasonPhrase = "object is not found"
                //};
                //throw new HttpResponseException(resp);
                return(new object[] { "" + ex });

                throw ex;
            }
            return(new object[] { "持行成功" });
        }
        /// <summary>
        /// 删除资讯分类
        /// </summary>
        /// <param name="p_id"></param>
        /// <param name="f_id"></param>
        /// <returns></returns>
        public static ReturnInfo DeleteClassById(int p_id, int f_id)//需考虑情况:可能操作者选择了大类,现在情况操作者选择了小类
        {
            ReturnInfo    rif   = new ReturnInfo();
            StringBuilder sb    = new StringBuilder();
            List <int>    arrId = new List <int>();

            using (WorkOfUnit work = new WorkOfUnit())
            {
                try
                {
                    //判断是否是父级 如果不是父级
                    if (f_id != 0)
                    {
                        //假如存在外键约束则先删除关联数据
                        var smQuery = work.CreateRepository <SmClass>().GetList(m => m.Id == p_id);
                        if (smQuery.Count() != 0)
                        {
                            foreach (var item in smQuery.ToList())
                            {
                                #region 获取资讯
                                //var mQuery = work.CreateRepository<Information>().GetList(m => m.Sm.Id == item.Id);
                                if (item.Information.Count() != 0)
                                {
                                    foreach (var mation in item.Information.ToList())
                                    {
                                        arrId.Add(mation.Id);
                                    }
                                }
                                #endregion
                            }
                            if (arrId.Count() != 0)
                            {
                                foreach (var item in arrId)
                                {
                                    //删除资讯
                                    var query = DeleteConsultById(item);
                                    rif.IsSuccess = query.IsSuccess;
                                    sb.Append(query.ErrorInfo);
                                }
                            }
                            foreach (var sm in smQuery.ToList())
                            {
                                try
                                {
                                    //删除资讯小类
                                    work.CreateRepository <SmClass>().Delete(sm);
                                    rif.IsSuccess = work.Save() >= 1;
                                }
                                catch (Exception ex)
                                {
                                    sm.Lock = 1;
                                    work.CreateRepository <SmClass>().Update(sm);
                                    work.Save();
                                    sb.Append("删除失败" + sm.Id + ":" + ex.Message);
                                    continue;
                                }
                            }
                        }
                    }
                    //如果是父级
                    if (f_id == 0)
                    {
                        var lgQuery = work.CreateRepository <LgClass>().GetEntityById(p_id);
                        //假如存在外键约束则先删除关联数据
                        var smQuery = work.CreateRepository <SmClass>().GetList(m => m.Lg.Id == lgQuery.Id);
                        if (smQuery.Count() != 0)
                        {
                            foreach (var item in smQuery.ToList())
                            {
                                #region 获取资讯
                                //var mQuery = work.CreateRepository<Information>().GetList(m => m.Sm.Id == item.Id);
                                if (item.Information.Count() != 0)
                                {
                                    foreach (var mation in item.Information.ToList())
                                    {
                                        arrId.Add(mation.Id);
                                    }
                                }
                                #endregion
                            }
                            if (arrId.Count() != 0)
                            {
                                foreach (var item in arrId)
                                {
                                    //删除资讯
                                    var query = DeleteConsultById(item);
                                    rif.IsSuccess = query.IsSuccess;
                                    sb.Append(query.ErrorInfo);
                                }
                            }
                            foreach (var sm in smQuery.ToList())
                            {
                                try
                                {
                                    //删除资讯小类
                                    work.CreateRepository <SmClass>().Delete(sm);
                                    rif.IsSuccess = work.Save() >= 1;
                                }
                                catch (Exception ex)
                                {
                                    sm.Lock = 1;
                                    work.CreateRepository <SmClass>().Update(sm);
                                    work.Save();
                                    sb.Append("删除失败" + sm.Id + ":" + ex.Message);
                                    continue;
                                }
                            }
                        }
                        try
                        {
                            //删除资讯大类
                            work.CreateRepository <LgClass>().Delete(lgQuery);
                            rif.IsSuccess = work.Save() >= 1;
                        }
                        catch (Exception ex)
                        {
                            rif.IsSuccess = false;
                            sb.Append(ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    rif.IsSuccess = false;
                    sb.Append(ex.Message);
                }
                rif.ErrorInfo = sb.ToString();
            }
            return(rif);
        }