RecipeService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using AutoMapper;
  2. using ProductionLineMonitor.Core.Dtos;
  3. using ProductionLineMonitor.Core.IRepositories;
  4. using ProductionLineMonitor.Core.Models;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. namespace ProductionLineMonitor.Core.Services
  10. {
  11. public class RecipeService : IRecipeService
  12. {
  13. private readonly IUnitOfWork _unitOfWork;
  14. private readonly IMapper _mapper;
  15. public RecipeService(IUnitOfWork unitOfWork, IMapper mapper)
  16. {
  17. _unitOfWork = unitOfWork;
  18. _mapper = mapper;
  19. }
  20. public ResultDto<RecipeDto> Create(RecipeCreateOrUpdateDto input)
  21. {
  22. if (_unitOfWork.RecipeRepository.Any(
  23. x => x.ModuleType == input.ModuleType &&
  24. x.ProductionLineId == input.ProductionLineId))
  25. {
  26. return ResultDto<RecipeDto>.Fail("该机种对应线别已存在,请检查!");
  27. }
  28. var recipe = _mapper.Map<Recipe>(input);
  29. recipe.CreateTime = DateTime.Now;
  30. _unitOfWork.RecipeRepository.Create(recipe);
  31. _unitOfWork.SaveChanges();
  32. var recipeDto = _mapper.Map<RecipeDto>(recipe);
  33. return ResultDto<RecipeDto>.Success(recipeDto);
  34. }
  35. public ResultDto Delete(string id)
  36. {
  37. var recipe = _unitOfWork.RecipeRepository.GetById(id);
  38. if (recipe == null)
  39. {
  40. return ResultDto.Fail("资源不存在!");
  41. }
  42. _unitOfWork.RecipeRepository.Delete(recipe);
  43. _unitOfWork.SaveChanges();
  44. return ResultDto.Success();
  45. }
  46. public ResultDto<RecipeDto> GetById(string id)
  47. {
  48. var recipe = _unitOfWork.RecipeRepository.GetById(id);
  49. if (recipe == null)
  50. {
  51. return ResultDto<RecipeDto>.Fail("资源不存在!");
  52. }
  53. var recipeDto = _mapper.Map<RecipeDto>(recipe);
  54. var line = _unitOfWork.ProductionLineRepository.GetById(recipe.ProductionLineId);
  55. if (line != null)
  56. {
  57. recipeDto.ProductionLineName = line.Name;
  58. }
  59. return ResultDto<RecipeDto>.Success(recipeDto);
  60. }
  61. public ResultDto<IEnumerable<RecipeDto>> GetList()
  62. {
  63. List<RecipeDto> list = new List<RecipeDto>();
  64. var recipes = _unitOfWork.RecipeRepository.GetList();
  65. if (recipes != null)
  66. {
  67. foreach (var recipe in recipes)
  68. {
  69. var recipeDto = _mapper.Map<RecipeDto>(recipe);
  70. var line = _unitOfWork.ProductionLineRepository.GetById(recipeDto.ProductionLineId);
  71. if (line != null)
  72. {
  73. recipeDto.ProductionLineName = line.Name;
  74. }
  75. list.Add(recipeDto);
  76. }
  77. }
  78. return ResultDto<IEnumerable<RecipeDto>>.Success(list);
  79. }
  80. public PageDto<IEnumerable<RecipeDto>> GetPageList(int pageIndex, int pageSize, string? keyword = null)
  81. {
  82. List<RecipeDto> recipeDtos = new List<RecipeDto>();
  83. var recipes = _unitOfWork.RecipeRepository.GetPageList(out int total, pageIndex, pageSize,
  84. o => o.OrderByDescending(o => o.CreateTime), x => x.ModuleType.Contains(keyword));
  85. if (recipes != null)
  86. {
  87. foreach (var recipe in recipes)
  88. {
  89. var recipeDto = _mapper.Map<RecipeDto>(recipe);
  90. var line = _unitOfWork.ProductionLineRepository.GetById(recipeDto.ProductionLineId);
  91. if (line != null)
  92. {
  93. recipeDto.ProductionLineName = line.Name;
  94. }
  95. recipeDtos.Add(recipeDto);
  96. }
  97. }
  98. return new PageDto<IEnumerable<RecipeDto>>(total, recipeDtos);
  99. }
  100. public ResultDto Update(string id, RecipeCreateOrUpdateDto input)
  101. {
  102. var recipe = _unitOfWork.RecipeRepository.GetById(id);
  103. if (recipe == null)
  104. {
  105. return ResultDto.Fail("资源不存在!");
  106. }
  107. _mapper.Map(input, recipe);
  108. recipe.UpdateTime = DateTime.Now;
  109. _unitOfWork.RecipeRepository.Update(recipe);
  110. _unitOfWork.SaveChanges();
  111. return ResultDto.Success();
  112. }
  113. }
  114. }