187 lines
6.7 KiB
C#
187 lines
6.7 KiB
C#
using Microsoft.Extensions.Logging;
|
|
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 AnnounceService : IAnnounceService
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly ILogger _logger;
|
|
|
|
public AnnounceService(IUnitOfWork unitOfWork, ILogger<AnnounceService> logger)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_logger = logger;
|
|
}
|
|
|
|
public PageViewModel<IEnumerable<AnnounceViewModel>> GetAnnounces(SearchModel searchModel)
|
|
{
|
|
try
|
|
{
|
|
var announce = _unitOfWork.AnnounceRepository.GetAnnounces();
|
|
|
|
if (!string.IsNullOrEmpty(searchModel.Term))
|
|
{
|
|
announce = announce.Where(x => x.Title.Contains(searchModel.Term));
|
|
}
|
|
|
|
int count = announce.Count();
|
|
int pageCount = count % searchModel.PageSize != 0 ? (count / searchModel.PageSize) + 1 : count / searchModel.PageSize;
|
|
|
|
switch (searchModel.Order.ToLower())
|
|
{
|
|
case "id":
|
|
default:
|
|
announce = searchModel.IsAsc ? announce.OrderBy(x => x.Id) : announce.OrderByDescending(x => x.Id);
|
|
break;
|
|
case "starton":
|
|
announce = searchModel.IsAsc ? announce.OrderBy(x => x.StartOn).ThenBy(x => x.Id) : announce.OrderByDescending(x => x.StartOn).ThenByDescending(x => x.Id);
|
|
break;
|
|
case "status":
|
|
announce = searchModel.IsAsc ? announce.OrderBy(x => x.Status).ThenBy(x => x.Id) : announce.OrderByDescending(x => x.Status).ThenByDescending(x => x.Id);
|
|
break;
|
|
}
|
|
|
|
announce = announce.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize);
|
|
var data = announce.ToList().Select(x => new AnnounceViewModel
|
|
{
|
|
Id = x.Id,
|
|
Title = x.Title,
|
|
Content = x.Content,
|
|
StartOn = x.StartOn.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
Status = x.Status,
|
|
CreatorId = x.CreatorId,
|
|
CreatedOn = x.CreatedOn,
|
|
UpdatorId = x.UpdatorId,
|
|
UpdatedOn = x.UpdatedOn,
|
|
});
|
|
|
|
return new PageViewModel<IEnumerable<AnnounceViewModel>>
|
|
{
|
|
RowCount = count,
|
|
PageCount = pageCount,
|
|
CurrentPage = searchModel.Page,
|
|
Data = data,
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex.Message);
|
|
return new PageViewModel<IEnumerable<AnnounceViewModel>>();
|
|
}
|
|
}
|
|
|
|
public AnnounceViewModel GetAnnounceById(int id)
|
|
{
|
|
try
|
|
{
|
|
var data = _unitOfWork.AnnounceRepository.GetAnnounceById(id);
|
|
if (data != null)
|
|
{
|
|
return new AnnounceViewModel
|
|
{
|
|
Id = data.Id,
|
|
Title = data.Title,
|
|
Content = data.Content,
|
|
StartOn = data.StartOn.ToString("yyyy-MM-dd HH:mm:ss"),
|
|
Status = data.Status,
|
|
CreatorId = data.CreatorId,
|
|
CreatedOn = data.CreatedOn,
|
|
UpdatorId = data.UpdatorId,
|
|
UpdatedOn = data.UpdatedOn,
|
|
};
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex.Message);
|
|
return new AnnounceViewModel();
|
|
}
|
|
}
|
|
|
|
public TradeResultModel InsertAnnounce(int byWho, AnnounceInsertViewModel viewModel)
|
|
{
|
|
try
|
|
{
|
|
var data = new Announce
|
|
{
|
|
Title = viewModel.Title,
|
|
Content = viewModel.Content,
|
|
StartOn = Convert.ToDateTime(viewModel.StartOn),
|
|
Status = viewModel.Status,
|
|
CreatorId = byWho,
|
|
CreatedOn = DateTime.Now
|
|
};
|
|
|
|
var result = _unitOfWork.AnnounceRepository.InsertAnnounce(data);
|
|
_unitOfWork.Complete();
|
|
|
|
return new TradeResultModel { Success = result, Message = result ? "新增成功" : "新增失敗" };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_unitOfWork.Rollback();
|
|
_logger.LogError(ex.Message);
|
|
return new TradeResultModel() { Success = false, Message = ex.ToString() };
|
|
}
|
|
}
|
|
|
|
public TradeResultModel UpdatAnnounce(int id, AnnounceInsertViewModel viewModel, int byWho)
|
|
{
|
|
try
|
|
{
|
|
var data = new Announce
|
|
{
|
|
Id = id,
|
|
Title = viewModel.Title,
|
|
Content = viewModel.Content,
|
|
StartOn = Convert.ToDateTime(viewModel.StartOn),
|
|
Status = viewModel.Status,
|
|
UpdatorId = byWho,
|
|
UpdatedOn = DateTime.Now,
|
|
};
|
|
|
|
var result = _unitOfWork.AnnounceRepository.UpdateAnnounce(data);
|
|
_unitOfWork.Complete();
|
|
|
|
return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_unitOfWork.Rollback();
|
|
_logger.LogError(ex.Message);
|
|
return new TradeResultModel() { Success = false, Message = ex.ToString() };
|
|
}
|
|
}
|
|
|
|
public TradeResultModel DeleteAnnounce(int id)
|
|
{
|
|
try
|
|
{
|
|
var result = _unitOfWork.AnnounceRepository.DeleteAnnounce(id);
|
|
_unitOfWork.Complete();
|
|
|
|
return new TradeResultModel { Success = result, Message = result ? "刪除成功" : "刪除失敗" };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_unitOfWork.Rollback();
|
|
_logger.LogError(ex.Message);
|
|
return new TradeResultModel() { Success = false, Message = ex.ToString() };
|
|
}
|
|
}
|
|
}
|
|
}
|