wangxinhui
2025-09-26 9ae0890dd74771ba9edd44d4830e0de37f8d9938
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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;
 
namespace WIDESEA_OutboundService
{
    public class OutSGOrderService : ServiceBase<Dt_OutSGOrder, IOutSGOrderRepository>, 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<SGOutOrderDTO> outOrderDTOs)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                //获取所有排程单
                List<Dt_OutSGOrder> outSGOrders=BaseDal.Db.Queryable<Dt_OutSGOrder>().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<Dt_MaterielInfo> 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<Dt_OutSGOrder> AddOutSGOrders = new List<Dt_OutSGOrder>();
                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<Dt_OutSGOrderDetail>(item);
                    outSGOrderDetail.MaterialName = materielInfo.MaterielName;
                    //判断工单是否已经存在
                    if (ExistOutSGOrder!=null)
                    {
                        ExistOutSGOrder.Details.Add(outSGOrderDetail);
                    }
                    else
                    {
                        
                        Dt_OutSGOrder outSGOrder=_mapper.Map<Dt_OutSGOrder>(item);
                        outSGOrder.Details=new List<Dt_OutSGOrderDetail>() { outSGOrderDetail };
                        AddOutSGOrders.Add(outSGOrder);
                    }
                }
                BaseDal.Db.InsertNav(AddOutSGOrders).Include(x => x.Details).ExecuteCommand();
                content.OK("接收排程成功");
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}