254 lines
11 KiB
C#
254 lines
11 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 NotPunishService : INotPunishService
|
|||
|
{
|
|||
|
private readonly IUnitOfWork _unitOfWork;
|
|||
|
private readonly ILogger _logger;
|
|||
|
|
|||
|
public NotPunishService(IUnitOfWork unitOfWork, ILogger<NotPunishService> logger)
|
|||
|
{
|
|||
|
_unitOfWork = unitOfWork;
|
|||
|
_logger = logger;
|
|||
|
}
|
|||
|
|
|||
|
public IEnumerable<NotPunishViewModel> GetNotPunishs()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogInformation($"GetNotPunishs Init");
|
|||
|
var data = _unitOfWork.NotPunishRepository.GetNotPunishs();
|
|||
|
|
|||
|
var eventTypes = _unitOfWork.EventTypeRepository.GetEventTypes();
|
|||
|
var result = data.ToList().Select(x => new NotPunishViewModel
|
|||
|
{
|
|||
|
Id = x.Id,
|
|||
|
EventTypeId = x.EventTypeId,
|
|||
|
EventType = eventTypes.Any(i => i.Id == x.EventTypeId) ? eventTypes.FirstOrDefault(i => i.Id == x.EventTypeId).EventType : string.Empty,
|
|||
|
EventName = eventTypes.Any(i => i.Id == x.EventTypeId) ? eventTypes.FirstOrDefault(i => i.Id == x.EventTypeId).EventName : string.Empty,
|
|||
|
NotPunishType = x.NotPunishType,
|
|||
|
NotPunishReason = x.NotPunishReason,
|
|||
|
});
|
|||
|
|
|||
|
_logger.LogInformation($"GetNotPunishs Finish");
|
|||
|
return result;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex.Message);
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public PageViewModel<IEnumerable<NotPunishViewModel>> GetNotPunishs(SearchModel searchModel)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogInformation($"GetNotPunishs Init");
|
|||
|
var data = _unitOfWork.NotPunishRepository.GetNotPunishs();
|
|||
|
|
|||
|
if (!string.IsNullOrEmpty(searchModel.Term))
|
|||
|
{
|
|||
|
_logger.LogInformation($"GetNotPunishs searchModel.Term ={searchModel.Term}");
|
|||
|
data = data.Where(x => x.NotPunishReason.Contains(searchModel.Term));
|
|||
|
}
|
|||
|
|
|||
|
int count = data.Count();
|
|||
|
int pageCount = count % searchModel.PageSize != 0 ? (count / searchModel.PageSize) + 1 : count / searchModel.PageSize;
|
|||
|
|
|||
|
switch (searchModel.Order.ToLower())
|
|||
|
{
|
|||
|
case "id":
|
|||
|
default:
|
|||
|
data = searchModel.IsAsc ? data.OrderBy(x => x.Id) : data.OrderByDescending(x => x.Id);
|
|||
|
break;
|
|||
|
case "eventtypeid":
|
|||
|
data = searchModel.IsAsc ? data.OrderBy(x => x.EventTypeId).ThenBy(x => x.Id) : data.OrderByDescending(x => x.EventTypeId).ThenByDescending(x => x.Id);
|
|||
|
break;
|
|||
|
case "notpunishtype":
|
|||
|
data = searchModel.IsAsc ? data.OrderBy(x => x.NotPunishType).ThenBy(x => x.Id) : data.OrderByDescending(x => x.NotPunishType).ThenByDescending(x => x.Id);
|
|||
|
break;
|
|||
|
case "notpunishreason":
|
|||
|
data = searchModel.IsAsc ? data.OrderBy(x => x.NotPunishReason).ThenBy(x => x.Id) : data.OrderByDescending(x => x.NotPunishReason).ThenByDescending(x => x.Id);
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
var eventTypes = _unitOfWork.EventTypeRepository.GetEventTypes();
|
|||
|
data = data.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize);
|
|||
|
var result = data.ToList().Select(x => new NotPunishViewModel
|
|||
|
{
|
|||
|
Id = x.Id,
|
|||
|
EventTypeId = x.EventTypeId,
|
|||
|
EventType = eventTypes.Any(i => i.Id == x.EventTypeId) ? eventTypes.FirstOrDefault(i => i.Id == x.EventTypeId).EventType : string.Empty,
|
|||
|
EventName = eventTypes.Any(i => i.Id == x.EventTypeId) ? eventTypes.FirstOrDefault(i => i.Id == x.EventTypeId).EventName : string.Empty,
|
|||
|
NotPunishType = x.NotPunishType,
|
|||
|
NotPunishReason = x.NotPunishReason,
|
|||
|
});
|
|||
|
|
|||
|
_logger.LogInformation($"GetNotPunishs Finish");
|
|||
|
return new PageViewModel<IEnumerable<NotPunishViewModel>>
|
|||
|
{
|
|||
|
RowCount = count,
|
|||
|
PageCount = pageCount,
|
|||
|
CurrentPage = searchModel.Page,
|
|||
|
Data = result,
|
|||
|
};
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex.Message);
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public NotPunishViewModel GetNotPunishById(int id)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogDebug($"GetNotPunishById Init");
|
|||
|
_logger.LogDebug($"GetNotPunishById id = {id}");
|
|||
|
|
|||
|
var data = _unitOfWork.NotPunishRepository.GetNotPunishById(id);
|
|||
|
if (data == null)
|
|||
|
{
|
|||
|
_logger.LogWarning($"GetNotPunishById id = {id} is Null");
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
_logger.LogDebug($"GetNotPunishById data.NotPunishType = {data.NotPunishType} and data.NotPunishReason = {data.NotPunishReason}");
|
|||
|
var eventType = _unitOfWork.EventTypeRepository.GetEventTypeById(data.EventTypeId);
|
|||
|
|
|||
|
_logger.LogDebug($"GetNotPunishById Finish");
|
|||
|
return new NotPunishViewModel
|
|||
|
{
|
|||
|
Id = data.Id,
|
|||
|
EventTypeId = data.EventTypeId,
|
|||
|
EventType = eventType.EventType,
|
|||
|
EventName = eventType.EventName,
|
|||
|
NotPunishType = data.NotPunishType,
|
|||
|
NotPunishReason = data.NotPunishReason,
|
|||
|
};
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex.Message);
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public TradeResultModel InsertNotPunish(NotPunishViewModel viewModel)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogDebug($"InsertNotPunish Init");
|
|||
|
var eventType = _unitOfWork.EventTypeRepository.GetEventTypeById(viewModel.EventTypeId);
|
|||
|
if (eventType == null)
|
|||
|
{
|
|||
|
_logger.LogWarning($"InsertNotPunish EventTypeId = {viewModel.EventTypeId} 無此事件類型");
|
|||
|
return new TradeResultModel { Success = false, Message = "查無此事件類型" };
|
|||
|
}
|
|||
|
|
|||
|
var data = new NotPunish
|
|||
|
{
|
|||
|
EventTypeId = viewModel.EventTypeId,
|
|||
|
NotPunishType = viewModel.NotPunishType,
|
|||
|
NotPunishReason = viewModel.NotPunishReason,
|
|||
|
};
|
|||
|
|
|||
|
_logger.LogDebug($"InsertNotPunish Insert EventTypeId = { viewModel.EventTypeId}");
|
|||
|
_logger.LogDebug($"InsertNotPunish Insert NotPunishType = {viewModel.NotPunishType}");
|
|||
|
_logger.LogDebug($"InsertNotPunish Insert EventTypeId= {viewModel.EventTypeId}");
|
|||
|
|
|||
|
var result = _unitOfWork.NotPunishRepository.InsertNotPunish(data);
|
|||
|
_unitOfWork.Complete();
|
|||
|
|
|||
|
_logger.LogDebug($"InsertNotPunish result is {result}");
|
|||
|
_logger.LogDebug($"InsertNotPunish Finish");
|
|||
|
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 UpdatNotPunish(int id, NotPunishViewModel viewModel)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogDebug($"UpdatNotPunish Init");
|
|||
|
var eventType = _unitOfWork.EventTypeRepository.GetEventTypeById(viewModel.EventTypeId);
|
|||
|
if (eventType == null)
|
|||
|
{
|
|||
|
_logger.LogWarning($"UpdatNotPunish EventTypeId = {viewModel.EventTypeId} 無此事件類型");
|
|||
|
return new TradeResultModel { Success = false, Message = "查無此事件類型" };
|
|||
|
}
|
|||
|
|
|||
|
var data = new NotPunish
|
|||
|
{
|
|||
|
Id = id,
|
|||
|
EventTypeId = viewModel.EventTypeId,
|
|||
|
NotPunishType = viewModel.NotPunishType,
|
|||
|
NotPunishReason = viewModel.NotPunishReason,
|
|||
|
};
|
|||
|
|
|||
|
_logger.LogDebug($"InsertNotPunish Insert EventTypeId = { viewModel.EventTypeId}");
|
|||
|
_logger.LogDebug($"InsertNotPunish Insert NotPunishType = {viewModel.NotPunishType}");
|
|||
|
_logger.LogDebug($"InsertNotPunish Insert EventTypeId= {viewModel.EventTypeId}");
|
|||
|
|
|||
|
var result = _unitOfWork.NotPunishRepository.UpdateNotPunish(data);
|
|||
|
_unitOfWork.Complete();
|
|||
|
|
|||
|
_logger.LogDebug($"UpdatNotPunish result is {result}");
|
|||
|
_logger.LogDebug($"UpdatNotPunish Finish");
|
|||
|
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 DeleteNotPunish(int id)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_logger.LogDebug($"DeleteNotPunish Init");
|
|||
|
var data = _unitOfWork.NotPunishRepository.GetNotPunishById(id);
|
|||
|
if (data == null)
|
|||
|
{
|
|||
|
_logger.LogWarning($"UpdatNotPunish id = {id} 無此不開單原因");
|
|||
|
return new TradeResultModel { Success = false, Message = "查無此不開單原因" };
|
|||
|
}
|
|||
|
|
|||
|
var result = _unitOfWork.NotPunishRepository.DeleteNotPunish(id);
|
|||
|
_unitOfWork.Complete();
|
|||
|
|
|||
|
_logger.LogDebug($"DeleteNotPunish result is {result}");
|
|||
|
_logger.LogDebug($"DeleteNotPunish Finish");
|
|||
|
return new TradeResultModel { Success = result, Message = result ? "刪除成功" : "刪除失敗" };
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_unitOfWork.Rollback();
|
|||
|
_logger.LogError(ex.Message);
|
|||
|
return new TradeResultModel() { Success = false, Message = ex.ToString() };
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|