tycg_carviolation_BE/Traffic.Service/Implements/PageListService.cs

148 lines
4.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traffic.Data.Models;
using Traffic.Data.ViewModels;
using Traffic.Repository.Infrastructures;
using Traffic.Service.Interfaces;
namespace Traffic.Service.Implements
{
public class PageListService : IPageListService
{
private readonly IUnitOfWork _unitOfWork;
public PageListService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public TradeResultModel PostPageDataList(PageData pageData)
{
var result = new TradeResultModel { Success = false };
try
{
pageData.PageModule = pageData.PageModule;
pageData.PageName = pageData.PageName;
pageData.PageUrl = pageData.PageUrl;
_unitOfWork.RolePageRepository.InsertPageList(
new PageList
{
PageModule = pageData.PageModule,
PageName = pageData.PageName,
PageURL = pageData.PageUrl
});
_unitOfWork.Complete();
}
catch (Exception ex)
{
result.Message = ex.Message;
return result;
}
result.Success = true;
return result;
}
public TradeResultModel PutPageData(PageData pageData)
{
var result = new TradeResultModel { Success = false };
try
{
_unitOfWork.RolePageRepository.UpdatePageList(
new PageList
{
Id = pageData.Id,
PageModule = pageData.PageModule,
PageName = pageData.PageName,
PageURL = pageData.PageUrl
});
_unitOfWork.Complete();
}
catch (Exception ex)
{
result.Message = ex.Message;
return result;
}
result.Success = true;
return result;
}
public TradeResultModel DeletePageData(int id)
{
var result = new TradeResultModel { Success = false };
try
{
_unitOfWork.RolePageRepository.DeletePageList(id);
_unitOfWork.Complete();
}
catch (Exception ex)
{
result.Message = ex.Message;
return result;
}
result.Success = true;
return result;
}
public PageViewModel<IEnumerable<PageData>> GetPageDataList(SearchModel searchModel)
{
var sql = _unitOfWork.RolePageRepository.GetPageLists();
if (!string.IsNullOrEmpty(searchModel.Term))
{
sql = sql.Where(x => x.PageModule.Contains(searchModel.Term) || x.PageName.Contains(searchModel.Term));
}
int count = -1;
int pageCount = -1;
if (searchModel.Page == 1)
{
count = sql.Count();
if (count % 10 != 0)
{
pageCount = (count / searchModel.PageSize) + 1;
}
else
{
pageCount = count / searchModel.PageSize;
}
}
switch (searchModel.Order)
{
case "Id":
sql = searchModel.IsAsc ? sql.OrderBy(x => x.Id) : sql.OrderByDescending(x => x.Id);
break;
case "PageModule":
sql = searchModel.IsAsc ? sql.OrderBy(x => x.PageModule).ThenBy(x => x.Id) : sql.OrderByDescending(x => x.PageModule).ThenByDescending(x => x.Id);
break;
case "PageName":
sql = searchModel.IsAsc ? sql.OrderBy(x => x.PageName).ThenBy(x => x.Id) : sql.OrderByDescending(x => x.PageName).ThenByDescending(x => x.Id);
break;
case "PageURL":
sql = searchModel.IsAsc ? sql.OrderBy(x => x.PageURL).ThenBy(x => x.Id) : sql.OrderByDescending(x => x.PageURL).ThenByDescending(x => x.Id);
break;
default:
goto case "Id";
}
sql = sql.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize);
var data = sql.ToList().Select(x => new PageData
{
Id = x.Id,
PageModule = x.PageModule,
PageName = x.PageName,
PageUrl = x.PageURL
});
return new PageViewModel<IEnumerable<PageData>>
{
RowCount = count,
PageCount = pageCount,
CurrentPage = searchModel.Page,
Data = data,
};
}
}
}