Beispiel #1
0
		public IQueryable<ChannelEntity> GetChannelsByCondition(ChannelSearchCondition condition)
		{
			var query = _channelRepository.Table;
			try
			{
				if (condition.Status.HasValue)
                {
                    query = query.Where(q => q.Status == condition.Status.Value);
                }
				if (!string.IsNullOrEmpty(condition.Name))
                {
                    query = query.Where(q => q.Name==condition.Name);
                }
				if (condition.Ids != null && condition.Ids.Any())
                {
                    query = query.Where(q => condition.Ids.Contains(q.Id));
                }
				if(condition.OrderBy.HasValue)
				{
					switch (condition.OrderBy.Value)
                    {
						case EnumChannelSearchOrderBy.OrderById:
							query = condition.IsDescending?query.OrderByDescending(q=>q.Id):query.OrderBy(q=>q.Id);
							break;
						case EnumChannelSearchOrderBy.OrderByName:
							query = condition.IsDescending?query.OrderByDescending(q=>q.Name):query.OrderBy(q=>q.Name);
							break;
						case EnumChannelSearchOrderBy.OrderByStatus:
							query = condition.IsDescending?query.OrderByDescending(q=>q.Status):query.OrderBy(q=>q.Status);
							break;
                    }
					
				}
				else
				{
					query = query.OrderBy(q=>q.Id);
				}

				if (condition.Page.HasValue && condition.PageCount.HasValue)
                {
                    query = query.Skip((condition.Page.Value - 1)*condition.PageCount.Value).Take(condition.PageCount.Value);
                }
				return query;
			}
			catch(Exception e)
			{
				_log.Error(e,"数据库操作出错");
                return null;
			}
		}
 public HttpResponseMessage Index(string name = null,int page = 1, int pageSize = 10)
 {
     var channelCon = new ChannelSearchCondition
     { 
         Name=name,
         Page=page,
         PageCount=pageSize
     };
     var channelList = _channelService.GetChannelsByCondition(channelCon).Select(a=> new ChannelModel
     { 
         Id=a.Id,
         Name=a.Name,
         Status=a.Status,
     }).ToList();
     var totalCount = _channelService.GetChannelCount(channelCon);
     return PageHelper.toJson(new{List=channelList,Condition=channelCon,TotalCount=totalCount});
 }
 public HttpResponseMessage Create(ChannelModel model)
 {
     Regex reg = new Regex(@"^[^ %@#!*~&',;=?$\x22]+$");
     var m = reg.IsMatch(model.Name);
     if (!m)
     {
         return PageHelper.toJson(PageHelper.ReturnValue(false, "存在非法字符!"));
     }
     else
     {
         var channelCon = new ChannelSearchCondition
         {
             Name = model.Name               
         };
         var totalCount = _channelService.GetChannelCount(channelCon);
         if (totalCount > 0)
         {
             return PageHelper.toJson(PageHelper.ReturnValue(false, "数据已存在!"));
         }
         else
         {
             var newParent = model.ParentId == 0 ? null : _channelService.GetChannelById(model.ParentId);
             var channel = new ChannelEntity
             {
                 Name = model.Name,
                 Status = model.Status,
                 Parent = newParent,
                 Adduser = _workContent.CurrentUser.Id,
                 Addtime = DateTime.Now,
                 UpdUser = _workContent.CurrentUser.Id,
                 UpdTime = DateTime.Now
             };
             if (_channelService.Create(channel) != null)
             {
                 return PageHelper.toJson(PageHelper.ReturnValue(true, "数据添加成功!"));
             }
             else
             {
                 return PageHelper.toJson(PageHelper.ReturnValue(false, "数据添加失败!"));
             }
         }
     }
 }
 public HttpResponseMessage Edit(ChannelModel model)
 {
     Regex reg = new Regex(@"^[^ %@#!*~&',;=?$\x22]+$");
     var m = reg.IsMatch(model.Name);
    if (!m)
    {
        return PageHelper.toJson(PageHelper.ReturnValue(false, "存在非法字符!"));
    }
    else
    {
        var channel = _channelService.GetChannelById(model.Id);
        if (channel.Name == model.Name)
        {
            var newParent = _channelService.GetChannelById(model.ParentId);
            channel.Name = model.Name;
            channel.Status = model.Status;
            channel.UpdUser = _workContent.CurrentUser.Id;
            channel.UpdTime = DateTime.Now;
            channel.Parent = newParent;
            if (_channelService.Update(channel) != null)
            {
                return PageHelper.toJson(PageHelper.ReturnValue(true, "数据更新成功!"));
            }
            else
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "数据更新失败!"));
            }
        }
        else
        {
              var channelCon = new ChannelSearchCondition
              {
                  Name = model.Name
              };
              var totalCount = _channelService.GetChannelCount(channelCon);
              if (totalCount > 0)
              {
                  return PageHelper.toJson(PageHelper.ReturnValue(false, "数据已存在!"));
              }
              else
              {
                  var newParent = _channelService.GetChannelById(model.ParentId);
                  channel.Name = model.Name;
                  channel.Status = model.Status;
                  channel.UpdUser = _workContent.CurrentUser.Id;
                  channel.UpdTime = DateTime.Now;
                  channel.Parent = newParent;
                  if (_channelService.Update(channel) != null)
                  {
                      return PageHelper.toJson(PageHelper.ReturnValue(true, "数据更新成功!"));
                  }
                  else
                  {
                      return PageHelper.toJson(PageHelper.ReturnValue(false, "数据更新失败!"));
                  }
              }
        }
    }          
 }
Beispiel #5
0
		public int GetChannelCount (ChannelSearchCondition condition)
		{
			var query = _channelRepository.Table;
			try
			{
				if (condition.Status.HasValue)
                {
                    query = query.Where(q => q.Status == condition.Status.Value);
                }
				if (!string.IsNullOrEmpty(condition.Name))
                {
                    query = query.Where(q => q.Name==condition.Name);
                }
				if (condition.Ids != null && condition.Ids.Any())
                {
                    query = query.Where(q => condition.Ids.Contains(q.Id));
                }
				return query.Count();
			}
			catch(Exception e)
			{
				_log.Error(e,"数据库操作出错");
                return -1;
			}
		}