public ResultInfo LoveShareReply(huangguan_sharereply dto)
        {
            ResultInfo         r = new ResultInfo();
            kuerhotelsEntities m = new kuerhotelsEntities();
            var q = m.huangguan_sharereply.Where(x => x.ShareId == dto.ShareId && x.Replyer == dto.Replyer && x.C_Love != null).Select(x => new ShareReplyDto
            {
                ID = x.ID,
            });

            if (dto.ID == Guid.Empty && q.ToList().Count == 0)
            {
                dto.CreatedAt = DateTime.Now;
                dto.ID        = Guid.NewGuid();
                if (dto.C_Love == null || dto.Replyer == Guid.Empty || dto.ShareId == Guid.Empty)
                {
                    r.Message = "参数空异常";
                    return(r);
                }
                m.huangguan_sharereply.Add(dto);
            }
            else
            {
                var s = m.huangguan_sharereply.FirstOrDefault(x => x.ShareId == dto.ShareId && x.Replyer == dto.Replyer && x.C_Love != null);
                if (s == null)
                {
                    r.Message = "分享评论为空异常";
                    return(r);
                }
                s.C_Love = dto.C_Love;
            }
            m.SaveChanges();
            r.IsSuccess = true;
            return(r);
        }
        public ResultInfo SetShareUserRead(Guid?id, Guid usrId)
        {
            ResultInfo r = new ResultInfo();

            kuerhotelsEntities m = new kuerhotelsEntities();

            if (id.HasValue && id != Guid.Empty)
            {
                var s = m.huangguan_shareusers.FirstOrDefault(x => x.ShareId == id && x.SharedUser == usrId);
                if (s == null)
                {
                    r.Message = "邀请用户信息为空异常";
                    return(r);
                }

                s.IsReaded = true;
                m.SaveChanges();
            }
            else
            {
                return(r);
            }

            r.IsSuccess = true;
            return(r);
        }
        public ResultInfo ShareToUsers(Guid shareId, string userIds)
        {
            ResultInfo r = new ResultInfo();

            kuerhotelsEntities m = new kuerhotelsEntities();

            if (shareId == Guid.Empty || string.IsNullOrEmpty(userIds))
            {
                r.Message = "参数空异常";
                return(r);
            }
            var userIdList = userIds.Split(';');

            foreach (var usrid in userIdList)
            {
                Guid dfdf = Guid.Parse(usrid);

                var s = m.huangguan_shareusers.FirstOrDefault(x => x.ShareId == shareId && x.SharedUser == dfdf);
                if (s == null)
                {
                    s            = new  huangguan_shareusers();
                    s.CreatedAt  = DateTime.Now;
                    s.ID         = Guid.NewGuid();
                    s.SharedUser = dfdf;
                    s.ShareId    = shareId;
                    m.huangguan_shareusers.Add(s);
                }
            }
            m.SaveChanges();
            r.IsSuccess = true;
            return(r);
        }
        public ResultInfo DelShare(Guid?id, bool isDel)
        {
            ResultInfo r = new ResultInfo();

            kuerhotelsEntities m = new kuerhotelsEntities();

            if (id.HasValue && id != Guid.Empty)
            {
                var s = m.huangguan_share.FirstOrDefault(x => x.ID == id);
                if (s == null)
                {
                    r.Message = "分享主题信息为空异常";
                    return(r);
                }

                s.IsDel = isDel;
                m.SaveChanges();
            }
            else
            {
                return(r);
            }

            r.IsSuccess = true;
            return(r);
        }
        public HttpResponseMessage download_ipfile(string md5)
        {
            var context = new kuerhotelsEntities();

            Guid id = new Guid(md5);
            var  f  = context.ipfiles.FirstOrDefault(x => x.Id == id);

            if (f != null && f.bytes.Length > 0)
            {
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                MemoryStream        stream   = new MemoryStream(f.bytes);
                response.Content = new StreamContent(stream);
                if (f.content_type == "png")
                {
                    response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
                }
                else
                {
                    response.Content.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
                    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        FileName = f.file_name
                    };
                }

                return(response);
            }
            return(ControllerContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, ""));
        }
        public bool upload_ipfile(string md5, string content_type)
        {
            try
            {
                Guid id = new Guid(md5);
                kuerhotelsEntities mm   = new kuerhotelsEntities();
                HttpPostedFile     file = HttpContext.Current.Request.Files[0];
                byte[]             bs   = new byte[file.ContentLength];
                file.InputStream.Read(bs, 0, bs.Length);

                //var f = mm.ipfiles.FirstOrDefault(x => x.Id == id);
                //if (f == null)
                //{
                ipfiles ff = new ipfiles();
                ff.bytes        = bs;
                ff.content_type = content_type;
                ff.file_name    = md5.ReplaceWith("-", "") + "." + content_type;
                ff.Id           = id;
                ff.size         = bs.Length;
                //mm.ipfiles.Add(ff);
                //mm.SaveChanges();

                string sql      = "insert into ipfiles(Id,bytes,content_type,size,file_name) values('{0}',@bytes,N'{1}','{2}',N'{3}')".FormatStr(ff.Id, ff.content_type, ff.size, ff.file_name);
                var    paraList = new List <MySqlParameter>
                {
                    new MySqlParameter("@bytes", ff.bytes)
                };
                MySqlDbHelper.ExecuteSql(connCommonsStr, sql, paraList);

                //string updatesql = "update ipfiles set bytes='{0}' where ID='{1}' ".FormatStr(ff.bytes, ff.Id);
                //MySqlDbHelper.ExecuteSql(connCommonsStr, updatesql);
                //}
                //else
                //{
                //    f.bytes = bs;
                //    f.size = bs.Length;
                //    mm.Entry(f).State = System.Data.EntityState.Modified;
                //    mm.SaveChanges();
                //}
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        public QueryResult <ShareDto> GetShareDetails(Guid id)
        {
            QueryResult <ShareDto> r = new QueryResult <ShareDto>();

            kuerhotelsEntities m = new kuerhotelsEntities();

            if (id != null)
            {
                var s = m.huangguan_share.Where(x => x.ID == id && x.IsDel == false).Select(x => new ShareDto
                {
                    CreatedAt = x.CreatedAt,
                    //Abstract = x.Abstract,
                    Description = x.Description,
                    ID          = x.ID,
                    Label       = x.Label,
                    Sender      = x.Sender,
                    Title       = x.Title,
                });
                r.Count  = s.Count();
                r.Result = s.OrderByDescending(x => x.CreatedAt).ToList();
                var usrId = r.Result.Select(x => x.Sender).Distinct().ToList();
                if (usrId.Count > 0)
                {
                    foreach (var item in r.Result)
                    {
                        if (item.Sender != Guid.Empty)
                        {
                            string touxing  = "select NickName,HeadIcon,Gender from huangguan_user where ID='{0}'".FormatStr(item.Sender);
                            var    dataicon = MySqlDbHelper.ExecuteQuery(connCommonsStr, touxing);
                            item.SenderName   = string.IsNullOrEmpty(dataicon.Rows[0]["NickName"].ToString()) ? "无昵称" : dataicon.Rows[0]["NickName"].ToString();
                            item.SenderHead   = dataicon.Rows[0]["HeadIcon"].ToString();
                            item.SenderGender = dataicon.Rows[0]["Gender"].ToString();
                        }
                    }
                }
                return(r);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// 获取分享的评论
        /// </summary>
        /// <param name="shareId"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public QueryResult <ShareReplyDto> GetShareReply(Guid?shareId, int page, int pagesize)
        {
            QueryResult <ShareReplyDto> r = new QueryResult <ShareReplyDto>();

            kuerhotelsEntities m = new kuerhotelsEntities();

            if (!shareId.HasValue || shareId == Guid.Empty)
            {
                return(r);
            }
            var q = m.huangguan_sharereply.Where(x => x.ShareId == shareId && x.IsDel == false).Select(x => new ShareReplyDto
            {
                CreatedAt   = x.CreatedAt,
                Description = x.Description,
                ID          = x.ID,
                Replyer     = x.Replyer,
                ShareId     = x.ShareId
            });

            r.Count  = q.Count();
            r.Result = q.OrderByDescending(x => x.CreatedAt).Skip(page * pagesize).Take(pagesize).ToList();
            var usrIds = r.Result.Select(x => x.Replyer).Distinct().ToList();

            if (usrIds.Count > 0)
            {
                foreach (var data in r.Result)
                {
                    if (data.Replyer != Guid.Empty && data.Replyer != null)
                    {
                        string touxing  = "select NickName,HeadIcon,Gender from huangguan_user where Id='{0}'".FormatStr(data.Replyer);
                        var    dataicon = MySqlDbHelper.ExecuteQuery(connCommonsStr, touxing);
                        data.ReplyerName   = string.IsNullOrEmpty(dataicon.Rows[0]["NickName"].ToString()) ? "无昵称" : dataicon.Rows[0]["NickName"].ToString();
                        data.ReplyerHead   = dataicon.Rows[0]["HeadIcon"].ToString();
                        data.ReplyerGender = dataicon.Rows[0]["Gender"].ToString();
                    }
                }
            }

            return(r);
        }
        public ResultInfo SaveShareReply(huangguan_sharereply dto)
        {
            ResultInfo r = new ResultInfo();

            kuerhotelsEntities m = new kuerhotelsEntities();

            if (dto.ID == Guid.Empty)
            {
                dto.CreatedAt = DateTime.Now;
                dto.ID        = Guid.NewGuid();
                dto.IsDel     = false;
                if (string.IsNullOrEmpty(dto.Description) || dto.Replyer == Guid.Empty || dto.ShareId == Guid.Empty)
                {
                    r.Message = "参数空异常";
                    return(r);
                }
                m.huangguan_sharereply.Add(dto);
            }
            else
            {
                var s = m.huangguan_sharereply.FirstOrDefault(x => x.ID == dto.ID);
                if (s == null)
                {
                    r.Message = "分享评论为空异常";
                    return(r);
                }
                s.Description = dto.Description;
                s.IsDel       = dto.IsDel;
                s.Replyer     = dto.Replyer;
                s.ShareId     = dto.ShareId;
                s.C_Love      = 0;
            }

            m.SaveChanges();
            r.IsSuccess = true;
            return(r);
        }
        public QueryResult <ShareDto> GetShare(Guid?usrId, string titlelabel, int?type, int page, int pagesize)
        {
            QueryResult <ShareDto> r = new QueryResult <ShareDto>();
            kuerhotelsEntities     m = new kuerhotelsEntities();
            var q = from s in m.huangguan_share
                    where s.IsDel == false
                    orderby s.CreatedAt descending
                    select new ShareDto
            {
                CreatedAt = s.CreatedAt,
                Abstract  = s.Abstract,
                //Description = s.Description,
                ID     = s.ID,
                Label  = s.Label,
                Sender = s.Sender,
                Title  = s.Title,
            };

            Dictionary <Guid?, bool?> shareIdDatas = new Dictionary <Guid?, bool?>();
            List <Guid?> shareIds = new List <Guid?>();

            if (type == 2)
            {
                string          sql1  = @"select s.Id, s.Title, s.Sender, s.Label, s.CreatedAt, s.IsDel, su.IsReaded, s.Abstract,su.ShareId from huangguan_share s join huangguan_shareusers su on s.Id = su.ShareId
                               where 1=1 and (s.IsDel = 0 and su.SharedUser = {0}) order by s.CreatedAt desc".FormatStr(usrId);
                DataTable       dt1   = MySqlDbHelper.ExecuteQuery(connCommonsStr, sql1);
                List <ShareDto> list1 = new List <ShareDto>();
                foreach (DataRow item in dt1.Rows)
                {
                    ShareDto sd = new ShareDto();
                    sd.CreatedAt = Convert.ToDateTime(item["CreatedAt"].ToString());
                    sd.Abstract  = item["Abstract"].ToString();
                    sd.ID        = Guid.Parse(item["Id"].ToString());
                    sd.Label     = item["Label"].ToString();
                    sd.Sender    = Guid.Parse(item["Sender"].ToString());
                    sd.Title     = item["Title"].ToString();
                    sd.IsReaded  = bool.Parse(item["IsReaded"].ToString());
                    sd.ShareId   = Guid.Parse(item["ShareId"].ToString());
                    list1.Add(sd);
                }
                q = list1.AsQueryable();
            }
            else if (type == 3)
            {
                string selsql = @"select s.Id, s.Title, s.Sender, s.Label, s.CreatedAt, s.IsDel,  s.Abstract from huangguan_share s 
                                where 1=1 and s.IsDel = 0 order by s.CreatedAt desc".FormatStr(usrId, usrId);

                DataTable       dt    = MySqlDbHelper.ExecuteQuery(connCommonsStr, selsql);
                List <ShareDto> list2 = new List <ShareDto>();
                foreach (DataRow item in dt.Rows)
                {
                    ShareDto sd = new ShareDto();
                    sd.CreatedAt = Convert.ToDateTime(item["CreatedAt"].ToString());
                    //Description = s.Description,
                    sd.Abstract = item["Abstract"].ToString();
                    sd.ID       = Guid.Parse(item["Id"].ToString());
                    sd.Label    = item["Label"].ToString();
                    sd.Sender   = Guid.Parse(item["Sender"].ToString());
                    sd.Title    = item["Title"].ToString();

                    list2.Add(sd);
                }

                q = list2.AsQueryable();
            }
            else if (type == 1)
            {
                if (usrId != Guid.Empty)
                {
                    q        = q.Where(x => x.Sender == usrId);
                    r.Count  = q.ToList().Count;
                    r.Result = q.ToList();
                    return(r);
                }
            }
            else if (!type.HasValue)
            {
                shareIdDatas = m.huangguan_shareusers.Where(x => x.SharedUser == usrId).ToDictionary(x => x.ShareId, y => y.IsReaded);
                shareIds     = shareIdDatas.Keys.ToList();
                if (shareIds.Count > 0)
                {
                    q = q.Where(x => shareIds.Contains(x.ID) || x.Sender == usrId);
                }
            }
            if (!string.IsNullOrEmpty(titlelabel))
            {
                q = q.Where(x => x.Title.Contains(titlelabel) || x.Label.Contains(titlelabel));
            }
            if (q.ToList().Count > 0)
            {
                var List = q.ToList();
                foreach (var item in List)
                {
                    if (item.Sender != Guid.Empty)
                    {
                        string touxing  = "select LoginName,NickName,HeadIcon,Gender from huangguan_user where ID='{0}'".FormatStr(item.Sender);
                        var    dataicon = MySqlDbHelper.ExecuteQuery(connCommonsStr, touxing);
                        if (dataicon.Rows.Count <= 0)
                        {
                            return(r);
                        }


                        item.SenderName   = string.IsNullOrEmpty(dataicon.Rows[0]["NickName"].ToString()) ? "无昵称" : dataicon.Rows[0]["NickName"].ToString();
                        item.SenderHead   = dataicon.Rows[0]["HeadIcon"].ToString();
                        item.SenderGender = dataicon.Rows[0]["Gender"].ToString();

                        string RCount = "select * from huangguan_sharereply where IsDel = 0 and ShareId = '{0}'".FormatStr(item.ID);
                        string LCount = "select * from huangguan_sharereply where  ` Love` = 1 and  ShareId = '{0}'".FormatStr(item.ID);
                        item.ReplyerCount = MySqlDbHelper.ExecuteQuery(connCommonsStr, RCount).Rows.Count;
                        item.LoveCount    = MySqlDbHelper.ExecuteQuery(connCommonsStr, LCount).Rows.Count;
                    }
                }
                r.Count  = List.Count();
                r.Result = List.Skip(page * pagesize).Take(pagesize).ToList();

                return(r);
            }
            else
            {
                r.Count  = 0;
                r.Result = q.ToList();
                return(r);
            }
        }