using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Core.BaseServices; using WIDESEA_Core; using WIDESEA_DTO.MES; using WIDESEA_IBasicRepository; using WIDESEA_IBasicService; using WIDESEA_IOutboundRepository; using WIDESEA_IOutboundService; using WIDESEA_IRecordService; using WIDESEA_IStockService; using WIDESEA_Model.Models; using WIDESEA_DTO.ERP; using WIDESEA_Core.BaseRepository; using WIDESEA_Common.MaterielEnum; using WIDESEA_Common.WareHouseEnum; using WIDESEA_Core.Helper; using System.Reflection; namespace WIDESEA_OutboundService { public class OutSGOrderService : ServiceBase, IOutSGOrderService { public IOutSGOrderRepository Repository => BaseDal; private IBasicRepository _basicRepository; private IStockService _stockService; private IBasicService _basicService; private ILocationStatusChangeRecordService _locationStatusChangeRecordService; private readonly IMapper _mapper; private readonly IUnitOfWorkManage _unitOfWorkManage; public OutSGOrderService(IOutSGOrderRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IBasicService basicService, ILocationStatusChangeRecordService locationStatusChangeRecordService, IMapper mapper,IUnitOfWorkManage unitOfWorkManage) : base(BaseDal) { _basicRepository = basicRepository; _stockService = stockService; _basicService = basicService; _locationStatusChangeRecordService = locationStatusChangeRecordService; _mapper = mapper; _unitOfWorkManage = unitOfWorkManage; } public WebResponseContent AddOutSGOrder(List outOrderDTOs) { WebResponseContent content = new WebResponseContent(); try { //获取所有排程单 List outSGOrders = BaseDal.Db.Queryable().Includes(x => x.Details).ToList(); //判断单据 Dt_OutSGOrder? ExistAddOutOrder = outSGOrders.FirstOrDefault(x => outOrderDTOs.Select(x => x.OrderId).Distinct().Contains(x.OrderId)); if (ExistAddOutOrder != null) { return content.Error($"出库排程单号{nameof(SGOutOrderDTO.OrderId)}:{ExistAddOutOrder.OrderId}已存在"); } //获取所有物料 List materielInfos = _basicRepository.MaterielInfoRepository.QueryData(x => x.WarehouseId == WarehouseEnum.LLDYL.ObjToInt() && x.MaterielInvOrgId == MaterielInvOrgEnum.老厂.ObjToInt() && x.MaterialSourceId != 0); SGOutOrderDTO? sGOutOrderDTO = outOrderDTOs.FirstOrDefault(x => !materielInfos.Select(x => x.MaterielCode).Contains(x.MaterialNo)); if (sGOutOrderDTO != null) { return content.Error($"生产排程{sGOutOrderDTO.OrderId}物料:{sGOutOrderDTO.MaterialNo}不存在"); } List AddOutSGOrders = new List(); foreach (var item in outOrderDTOs) { //获取工单 Dt_OutSGOrder? ExistOutSGOrder = AddOutSGOrders.FirstOrDefault(x => x.OrderId == item.OrderId); //获取物料 Dt_MaterielInfo materielInfo = materielInfos.FirstOrDefault(x => x.MaterielCode == item.MaterialNo); //明细提前转换 Dt_OutSGOrderDetail outSGOrderDetail = _mapper.Map(item); outSGOrderDetail.MaterialName = materielInfo.MaterielName; //判断工单是否已经存在 if (ExistOutSGOrder != null) { ExistOutSGOrder.Details.Add(outSGOrderDetail); } else { Dt_OutSGOrder outSGOrder = _mapper.Map(item); outSGOrder.Details = new List() { outSGOrderDetail }; AddOutSGOrders.Add(outSGOrder); } } BaseDal.Db.InsertNav(AddOutSGOrders).Include(x => x.Details).ExecuteCommand(); content.OK("接收排程成功"); } catch (Exception ex) { content.Error(ex.Message); } return content; } } }