123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using AutoMapper;
- using ProductionLineMonitor.Core.Dtos;
- using ProductionLineMonitor.Core.IRepositories;
- using ProductionLineMonitor.Core.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ProductionLineMonitor.Core.Services
- {
- public class RecipeService : IRecipeService
- {
- private readonly IUnitOfWork _unitOfWork;
- private readonly IMapper _mapper;
- public RecipeService(IUnitOfWork unitOfWork, IMapper mapper)
- {
- _unitOfWork = unitOfWork;
- _mapper = mapper;
- }
- public ResultDto<RecipeDto> Create(RecipeCreateOrUpdateDto input)
- {
- if (_unitOfWork.RecipeRepository.Any(
- x => x.ModuleType == input.ModuleType &&
- x.ProductionLineId == input.ProductionLineId))
- {
- return ResultDto<RecipeDto>.Fail("该机种对应线别已存在,请检查!");
- }
- var recipe = _mapper.Map<Recipe>(input);
- recipe.CreateTime = DateTime.Now;
- _unitOfWork.RecipeRepository.Create(recipe);
- _unitOfWork.SaveChanges();
- var recipeDto = _mapper.Map<RecipeDto>(recipe);
- return ResultDto<RecipeDto>.Success(recipeDto);
- }
- public ResultDto Delete(string id)
- {
- var recipe = _unitOfWork.RecipeRepository.GetById(id);
- if (recipe == null)
- {
- return ResultDto.Fail("资源不存在!");
- }
- _unitOfWork.RecipeRepository.Delete(recipe);
- _unitOfWork.SaveChanges();
- return ResultDto.Success();
- }
- public ResultDto<RecipeDto> GetById(string id)
- {
- var recipe = _unitOfWork.RecipeRepository.GetById(id);
- if (recipe == null)
- {
- return ResultDto<RecipeDto>.Fail("资源不存在!");
- }
- var recipeDto = _mapper.Map<RecipeDto>(recipe);
- var line = _unitOfWork.ProductionLineRepository.GetById(recipe.ProductionLineId);
- if (line != null)
- {
- recipeDto.ProductionLineName = line.Name;
- }
- return ResultDto<RecipeDto>.Success(recipeDto);
- }
- public ResultDto<IEnumerable<RecipeDto>> GetList()
- {
- List<RecipeDto> list = new List<RecipeDto>();
- var recipes = _unitOfWork.RecipeRepository.GetList();
- if (recipes != null)
- {
- foreach (var recipe in recipes)
- {
- var recipeDto = _mapper.Map<RecipeDto>(recipe);
- var line = _unitOfWork.ProductionLineRepository.GetById(recipeDto.ProductionLineId);
- if (line != null)
- {
- recipeDto.ProductionLineName = line.Name;
- }
- list.Add(recipeDto);
- }
- }
- return ResultDto<IEnumerable<RecipeDto>>.Success(list);
- }
- public PageDto<IEnumerable<RecipeDto>> GetPageList(int pageIndex, int pageSize, string? keyword = null)
- {
- List<RecipeDto> recipeDtos = new List<RecipeDto>();
- var recipes = _unitOfWork.RecipeRepository.GetPageList(out int total, pageIndex, pageSize,
- o => o.OrderByDescending(o => o.CreateTime), x => x.ModuleType.Contains(keyword));
- if (recipes != null)
- {
- foreach (var recipe in recipes)
- {
- var recipeDto = _mapper.Map<RecipeDto>(recipe);
- var line = _unitOfWork.ProductionLineRepository.GetById(recipeDto.ProductionLineId);
- if (line != null)
- {
- recipeDto.ProductionLineName = line.Name;
- }
- recipeDtos.Add(recipeDto);
- }
- }
- return new PageDto<IEnumerable<RecipeDto>>(total, recipeDtos);
- }
- public ResultDto Update(string id, RecipeCreateOrUpdateDto input)
- {
- var recipe = _unitOfWork.RecipeRepository.GetById(id);
- if (recipe == null)
- {
- return ResultDto.Fail("资源不存在!");
- }
- _mapper.Map(input, recipe);
- recipe.UpdateTime = DateTime.Now;
- _unitOfWork.RecipeRepository.Update(recipe);
- _unitOfWork.SaveChanges();
- return ResultDto.Success();
- }
- }
- }
|